ABC288 C - Don’t be cycle
https://atcoder.jp/contests/abc288/tasks/abc288_c
提出
code: python
from collections import deque
n, m = list(map(int, input().split()))
ab = list(map(int, input().split())) for _ in range(m)
g = [[] for _ in range(n+1)]
for a, b in ab:
ga.append(b)
gb.append(a)
# 1 <-> 2, 3 && 2<->3
# 4 <-> 5, 6 && 5<->6
# 閉路を持つ =>
# 消す =>
visited = False * (n+1)
q = deque()
q.append(g1)
visited1 = True
count = 0
while q:
next = q.popleft()
visit = 0
for i in next:
if visitedi:
visit += 1
else:
visitedi = True
q.append(gi)
if len(next) > 1 and visit == len(next):
count += 1
print(0) if count == 0 else print(count - 1)
解答
code: python
from collections import deque, defaultdict
n, m = map(int, input().split())
ab = list(map(int, input().split())) for _ in range(m)
d = defaultdict(list)
for a, b in ab:
da.append(b)
db.append(a)
visited = False * (n+1)
# グラフの連結成分(独立した部分)の個数
s = 0
# 最大で L 本の辺を残すことができるとすると、元の問題の答えは M−L
# L = N−S
for i in range(1, n+1):
if not visitedi:
s += 1
que = deque(i)
while que:
next = que.popleft()
for j in dnext:
if not visitedj:
visitedj = True
que.append(j)
print(m - n + s)
テーマ
#UnionFind
メモ
https://atcoder.jp/contests/abc288/editorial
https://scrapbox.io/files/63ed8478f0e9d5001bcf45e3.png
提出
code: python
from collections import defaultdict
from collections import deque
n, m = map(int, input().split())
ab = list(map(int, input().split())) for _ in range(m)
d = defaultdict(list)
for a, b in ab:
da.append(b)
db.append(a)
# print(d)
# {1: 2, 3, 2: 1, 3, 4, 3: 1, 2, 4: 2, 6, 5, 6: 5, 4, 5: 6, 4}
que = deque()
que.append(1)
visited = False * (n+1)
visited1 = True
close = False * (n+1)
while que:
next = que.popleft()
for i in dnext:
if visitedi:
closei = True
break
else:
visitedi = True
for j in di:
que.append(j)
print(sum(close))