-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabc277_c.py
More file actions
199 lines (159 loc) · 5.69 KB
/
abc277_c.py
File metadata and controls
199 lines (159 loc) · 5.69 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
[deepseek AC]
from collections import deque
import sys
def main():
input = sys.stdin.read
data = input().split()
n = int(data[0])
graph = {}
index = 1
for i in range(n):
a = int(data[index])
b = int(data[index+1])
index += 2
if a not in graph:
graph[a] = []
if b not in graph:
graph[b] = []
graph[a].append(b)
graph[b].append(a)
if 1 not in graph:
print(1)
return
visited = set()
queue = deque([1])
visited.add(1)
max_vertex = 1
while queue:
current = queue.popleft()
max_vertex = max(max_vertex, current)
for neighbor in graph.get(current, []):
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
print(max_vertex)
if __name__ == "__main__":
main()
###############################################
[deepseek AC]
入力の制約から、頂点番号が最大で10^9まで大きくなる可能性があることがわかりました。
そのため、単純に配列でグラフを表現することはメモリ的に不可能です。
代わりに、辞書を使ってグラフを表現し、BFSを行う必要があります。
```python
from collections import deque
def main():
import sys
input = sys.stdin.read
data = input().split()
n = int(data[0])
graph = {}
# グラフを構築
index = 1
for _ in range(n):
a = int(data[index])
b = int(data[index+1])
index += 2
if a not in graph:
graph[a] = []
if b not in graph:
graph[b] = []
graph[a].append(b)
graph[b].append(a)
# 頂点1が存在しない場合
if 1 not in graph:
print(1)
return
# BFS
visited = set()
queue = deque([1])
visited.add(1)
max_vertex = 1
while queue:
current = queue.popleft()
max_vertex = max(max_vertex, current)
if current in graph:
for neighbor in graph[current]:
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
print(max_vertex)
if __name__ == "__main__":
main()
```
### 修正点:
1. **辞書を使ったグラフ表現**: 頂点番号が非常に大きくなるため、配列の代わりに辞書を使用
2. **集合を使った訪問管理**: 訪問済み頂点の管理も集合を使用
3. **入力の効率的な読み込み**: 大量の入力に対応するため `sys.stdin.read` を使用
4. **頂点1の存在確認**: 頂点1がグラフに存在しない場合の処理
この実装では:
- メモリ使用量は辺の数に比例(O(N))
- 時間計算量もO(N)(各頂点と辺を1回ずつ処理)
- 頂点番号が非常に大きくても問題なく処理可能
これで制約を満たすことができます。
###############################################
[gemini AC]
import collections
# グラフの構築
N = int(input())
graph = collections.defaultdict(list)
for _ in range(N):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
# 1 を含む連結成分の探索(幅優先探索)
start_node = 1
q = collections.deque([start_node])
visited = {start_node}
while q:
current_node = q.popleft()
# 隣接するすべてのノードを探索
for neighbor in graph[current_node]:
if neighbor not in visited:
visited.add(neighbor)
q.append(neighbor)
# 探索したノードの最大値を求める
result = max(visited)
print(result)
###############################################
[my WA]
N=int(input())
AB=[]
for i in range(N):
a,b=map(int,input().split())
AB+=[(a,b)]
DA=[1]
DB=[1]
for a,b in AB:
if a in DA:
DB+=[b]
DA+=[b]
if b in DB:
DA+=[a]
DB+=[a]
print(max(max(DA),max(DB)))
###############################################
[my 問題点]
この問題は、グラフ理論の連結成分を求める問題として捉えることができます。入力の (a, b) は、a と b の間に辺があることを意味します。
1から始まる連結成分のサイズ(頂点数)を最大化することが目的です。
ご提示のコードでは、DA と DB という2つのリストを使って連結を探索していますが、以下の問題点があります。
探索が不完全: if a in DA: や if b in DB: という条件は、
すでに探索済みのノードをチェックしていますが、
この方法では、新しく追加されたノードからさらに連結するノードを再帰的にまたは反復的に探索できていません。######
非効率な探索: in 演算子はリスト全体を線形探索するため、計算量が大きくなります。
N が大きい場合、これがTLEの主な原因となります。
###############################################
###############################################
###############################################
###############################################
###############################################