-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabc277_c2.py
More file actions
75 lines (64 loc) · 1.94 KB
/
Copy pathabc277_c2.py
File metadata and controls
75 lines (64 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
[TLE6]
n=int(input())
from collections import defaultdict
cnt = defaultdict(list)
for _ in range(n):
a,b=map(int,input().split())
cnt[a].append(b)
cnt[b].append(a)
M=set()#到達範囲
M.add(1)
checked=set()#check済み
while len(M-checked)>0:
#print(M-checked)
M2=set()#拡大
adchecked=set()
for li in list(M-checked):
lst2=cnt[li]
adchecked.add(li)
M2|=set(lst2)
if M2==set():
break
else:
M|=M2
checked|=adchecked
print(max(list(M)))
###############################################
###############################################
###############################################
###############################################
[deepseek]
from collections import defaultdict, deque
def main():
n = int(input())
graph = defaultdict(list)
for _ in range(n):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
visited = set([1])
queue = deque([1])
while queue:
current = queue.popleft()
for neighbor in graph[current]:
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
print(max(visited))
if __name__ == "__main__":
main()
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################