Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

27-H0ngJu #240

Merged
merged 2 commits into from
Sep 20, 2024
Merged

27-H0ngJu #240

merged 2 commits into from
Sep 20, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Aug 27, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

2H

โœจ ์ˆ˜๋„ ์ฝ”๋“œ



๋ฌธ์ œ ์š”์•ฝ

์š”๋ ‡๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ตœ๋Œ€ ์ง€๋ฆ„ ๊ตฌํ•˜๊ธฐ@!



์ผ๋‹จ ์ €๋Š” ์ด ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ 

  1. ์ตœ๋Œ€ ์ง€๋ฆ„์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๋‹ˆ, ๋ ๋…ธ๋“œ๊ฐ€ ๋ฌด์กฐ๊ฑด ํฌํ•จ์ด๋‹ค.
  2. a, b ๋‘ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•˜๊ณ  ๊ฐ™์€ ๋ถ€๋ชจ์—์„œ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ฆฌ๋ฉด ๋˜์ง€ ์•Š์„๊นŒ?

ํ•˜๊ณ  ์ ‘๊ทผํ–ˆ์Šต๋‹ˆ๋‹ค.


image


๊ทผ๋ฐ ๋˜ ๋ณด๋‹ค๋ณด๋ฉด ํ•œ๊ฐ€์ง€ ํŠน์ง•์ด ์žˆ๋Š” ๊ฒƒ์ด,

๋‘ ๋…ธ๋“œ๋ฅผ ์žก์•˜์„ ๋•Œ, ๊ฒฐ์ •๋˜๋Š” ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์€
๋ฐ˜๋“œ์‹œ ๋‘ ๋…ธ๋“œ์˜ ์ฒซ ๊ณตํ†ต ๋ถ€๋ชจ๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค.



์œ„ ์‚ฌ์ง„์—์„œ, 9๋ฒˆ ๋…ธ๋“œ์™€ 12๋ฒˆ ๋…ธ๋“œ๋ฅผ ์žก์•˜์„ ๋•Œ,
๊ณตํ†ต ์กฐ์ƒ์€ 3๋ฒˆ ๋…ธ๋“œ์™€ 1๋ฒˆ ๋…ธ๋“œ์ง€๋งŒ,


์ฒซ ๋ฒˆ์งธ๋กœ ๋“ฑ์žฅํ•˜๋Š” ์กฐ์ƒ ๋…ธ๋“œ๋Š” 3๋ฒˆ ๋…ธ๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์—,


ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„์ด 3๋ฒˆ ๋…ธ๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•œ ๊ฐ๊ฐ์˜ ๊ฑฐ๋ฆฌ๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.


image



๊ทธ๋ž˜์„œ ์ €๋Š” ๊ฐ ๋…ธ๋“œ๋“ค์— ๋Œ€ํ•ด์„œ ์กฐ์ƒ์„ ๋‹ด์„ parent ๋ฆฌ์ŠคํŠธ์™€, ๊ฐ€์ค‘์น˜ ์ •๋ณด๋ฅผ ๋‹ด์„ tree๋ฅผ ๋”ฐ๋กœ ๋‘์–ด์„œ ์ €์žฅํ•˜๊ณ ,


๋‘ ๋…ธ๋“œ๋“ค์˜ ๊ณตํ†ต ์กฐ์ƒ ์ค‘์— ๊ฐ€์žฅ ํฐ ๊ฒƒ์„ ๋ฝ‘์•„
ํŠธ๋ฆฌ์˜ ์ตœ๋Œ€ ์ง€๋ฆ„์„ ๊ตฌํ•˜๋ฉด ๋˜๊ฒ ๊ตฌ๋‚˜! ํ•ด์„œ

์—ด์‹ฌํžˆ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๊ณ ,,,


import sys

def input() : return sys.stdin.readline().rstrip()

n = int(input())
tree = [[] for _ in range(n+1)]
parents = [[] for _ in range(n+1)]
max_dis = 0

for i in range(1, n):
    v, u, w = map(int, input().split())
    
    if v == 1:
        tree[u].append(list((v, w)))
    else:
        tree[u].append(list((v, w)))
        parents[u].append(v)

        for arr in tree[v]:
            tree[u].append(list((arr[0], w + arr[1])))
            parents[u].append(arr[0])

for i in range(1, n + 1):
    for j in range(1, n + 1):
        if i == j:
            continue

        min_common_value_i = 0
        min_common_value_j = 0
        check = 0

        for parent in parents[i]:
            if check:
                break
            if parent in parents[j]:
                check = 1
                min_common_value_i = next((x[1] for x in tree[i] if x[0] == parent), None)
                min_common_value_j = next((x[1] for x in tree[j] if x[0] == parent), None)
                max_dis = max(max_dis, min_common_value_i + min_common_value_j)

            min_common_value = min_common_value_i + min_common_value_j

print(max_dis)



ํ–ˆ๋Š”๋ฐ,,
์‹œ๊ฐ„์ดˆ๊ณผ ์ž…๋‹ˆ๋‹ค.

์™œ๋ƒ๋ฉด,, n์ด 10000์ดํ•˜๋ผ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ O^2๋˜๋Š” ์ˆœ๊ฐ„ ํƒˆ๋ฝ์ž…๋‹ˆ๋‹ค..



n์ด ์–ผ๋งˆ์ธ์ง€ ํ™•์ธํ•˜๊ณ  ๋“ค์–ด๊ฐ”์–ด์•ผ ํ•˜๋Š” ๊ฑด๋ฐ ,,,,
๊ฑ ๋“ค์ด๋ฐ€์—ˆ๋”๋‹ˆ 2์‹œ๊ฐ„์ด ๊ทธ๋ƒฅ ๋‚ ๋ผ๊ฐ”๋„ค์˜ˆ



๋”ฑํžˆ ๋ณ„๋‹ค๋ฅธ ํ’€์ด๊ฐ€ ๋” ์ด์ƒ ๋– ์˜ค๋ฅด์ง€ ์•Š์•„์„œ ๋‹ค๋ฅธ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ฐธ๊ณ ํ–ˆ๊ณ ,
DFS๋กœ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.


์œผ์œผ n์„ ๋จผ์ € ํ™•์ธํ–ˆ์–ด์•ผ ํ•˜๋Š”๊ฑด๋ฐ ...


DFS์˜ ์•„์ด๋””์–ด๋Š” ์•„๋ž˜๋ฅผ ๊ธฐ์ดˆ๋กœ ํ•ฉ๋‹ˆ๋‹ค!


๋๋…ธ๋“œ๊ฐ€ ๋ฐ˜๋“œ์‹œ ํŠธ๋ฆฌ์˜ ์ตœ๋Œ€ ์ง€๋ฆ„์„ ๊ฒฐ์ •ํ•œ๋‹ค.


๋ฃจํŠธ ๊ธฐ์ค€์œผ๋กœ ๊ฐ€์žฅ ๋จผ ๋…ธ๋“œ๋Š” ๋๋…ธ๋“œ์ผ ์ˆ˜ ๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค. ๋๋…ธ๋“œ๋ณด๋‹ค ๋” ๋จผ ๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•˜๋Š” ์ˆœ๊ฐ„, ๋ ๋…ธ๋“œ๊ฐ€ ๋ ๋…ธ๋“œ๊ฐ€ ์•„๋‹ˆ๊ฒŒ ๋˜๋Š”๊ฑฐ๋‹ˆ๊นŒ์š”.

๊ทธ๋ž˜์„œ ์ผ๋‹จ ๋ฃจํŠธ๋…ธ๋“œ์—์„œ ๊ฐ€์žฅ ๋จผ ๋ ๋…ธ๋“œ๋ฅผ dfs๋กœ ์ฐพ์•„์ค๋‹ˆ๋‹ค.


๊ทธ๋ ‡๋‹ค๋ฉด, ๋ฃจํŠธ์—์„œ ๊ฐ€์žฅ ๋จผ ๋ ๋…ธ๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ
dfs๋กœ ๊ฐ€์žฅ ๋จผ ๋…ธ๋“œ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ํŠธ๋ฆฌ์˜ ์ตœ๋Œ€ ์ง€๋ฆ„์ด ๋‚˜์˜ต๋‹ˆ๋‹ค!


import sys

sys.setrecursionlimit(10**9)
def input() : return sys.stdin.readline().rstrip()

n = int(input())
tree = [[] for _ in range(n+1)]
visited = [-1] * (n+1)
visited[1] = 0

for i in range(n-1):
    v, u, w = map(int, input().split())
    tree[v].append((u, w))
    tree[u].append((v, w))

def dfs(start, dis):
    for node, node_dis in tree[start]:
        if visited[node] == -1:
            visited[node] = dis + node_dis
            dfs(node, dis + node_dis)

dfs(1,0)

far_node = visited.index(max(visited))
visited = [-1] * (n+1)
visited[far_node] = 0
dfs(far_node, 0)

print(max(visited))



์‹œ๊ฐ„์€ ์˜ค๋ž˜ ๊ฑธ๋ ธ๋Š”๋ฐ
๊ธ€๋กœ ์˜ฎ๊ฒจ ์ ์œผ๋‹ˆ๊นŒ ์ข€ ๊ฐ„๋‹จํ•ด๋ณด์ด๋„ค์š” ...

๊ทธ๋ž˜๋„ ์–ด๋ ค์› ์Šต๋‹ˆ๋‹ค ๐Ÿคฏ

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ๋ฌธ์ œ ์—„์ฒญ ์žฌ๋ฐŒ๊ฒŒ ํ’€์—ˆ๋˜ ๊ธฐ์–ต์ด ์žˆ๋„ค์šฉ

์ €๋„ ์ด ๋ฌธ์ œ ๋„์ €ํžˆ ๋ชฐ๋ผ์„œ ๋ ˆํผ๋Ÿฐ์Šค ๋ณด๊ณ  ํ’€์—ˆ๋Š”๋ฐ, ๋ฌด๋ฆŽ์„ ํƒ ์ณค์Šต๋‹ˆ๋‹ค...

import sys
from collections import defaultdict

sys.setrecursionlimit(100000)

def input(): return sys.stdin.readline()

N = int(input())
graph = defaultdict(list)
table = [int(1e9) for _ in range(N+1)]
table[1] = 0

def dfs(now, visited, table):
    for next, cost in graph[now]:
        if next in visited: continue
        
        visited.add(next)
        table[next] = table[now] + cost
        dfs(next, visited, table)
    

for _ in range(N-1):
    start, destination, cost = map(int,input().split())
    graph[start].append([destination, cost])
    graph[destination].append([start, cost])

visited = {1,}
dfs(1, visited, table)

max_value = -1
max_idx = 0
for idx, value in enumerate(table):
    if idx == 0: continue

    if max_value < value:
        max_value = value
        max_idx = idx

visited = {max_idx,}
table = [int(1e9) for _ in range(N+1)]
table[max_idx] = 0
dfs(max_idx, visited, table)
print(max(table[1:]))

์ด ๋ฌธ์ œ๋ณด๋‹ค ์‚ด์ง ๋” ์–ด๋ ค์šด ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š”๋ฐ,

๋‘ ๋ฒˆ์งธ ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„ ๋ฌธ์ œ๋„ ํ•œ ๋ฒˆ ํ’€์–ด๋ณด์‹ค๋ž˜์š”?!

๋‹ค์Œ PR์— ์˜ฌ๋ฆฌ์…”๋„ ์ข‹์•„์š”.

@H0ngJu H0ngJu merged commit 825c2d2 into main Sep 20, 2024
@H0ngJu H0ngJu deleted the 27-H0ngJu branch September 20, 2024 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants