-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabc075_c.py
More file actions
179 lines (153 loc) · 3.52 KB
/
abc075_c.py
File metadata and controls
179 lines (153 loc) · 3.52 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
abc075_c.py
graph_circle_bridge
######################################################
from collections import deque
n,m=map(int, input().split())
c=[0]+[list(map(int, input().split())) for _ in range(m)]
d=[[] for _ in range(n+1)]
v=deque()
v.append(1)
s=[False]*(n+1)
s[0]=True
s[1]=True
counts=0
for i in range(1,m+1):
for j in range(1,m+1):
if i == j:
continue
else:
d[c[j][1]].append(c[j][0])
d[c[j][0]].append(c[j][1])
while v:
k=v.popleft()
for l in d[k]:
if not s[l]:
s[l]=True
v.append(l)
for p in range(n+1):
if s[p]==False:
counts+=1
break
d=[[] for _ in range(n+1)]
v.append(1)
s=[False]*(n+1)
s[0]=True
s[1]=True
print(counts)
######################################################
import sys
sys.setrecursionlimit(10**7)
n,m=map(int,input().split())
par=[i for i in range(n)]
def find(x):
if x==par[x]:
return x
else:
par[x]=find(par[x])
return par[x]
def same(x,y):
return find(x)==find(y)
def unite(x,y):
x=find(x)
y=find(y)
if x==y:
return
par[x]=y
edges=[]
for i in range(m):
a,b=map(int,input().split())
edges.append((a-1, b-1))
ans=0
for i in range(m):
par=[i for i in range(n)]
for j in range(m):
if i==j:
continue
a,b=edges[j]
unite(a,b)
for j in range(n-1):
if not same(j,j+1):
ans+=1
break
print(ans)
######################################################
import sys
sys.setrecursionlimit(10000)
N, M = map(int, input().split())
graphs = [[[] for _ in range(N)] for i in range(M)]
sides = []
for i in range(M):
a, b = map(int, input().split())
a -= 1
b -= 1
sides.append([a, b])
for m in range(M):
for m_idx in range(M):
if m_idx == m:
continue
a, b = sides[m_idx]
graphs[m][a].append(b)
graphs[m][b].append(a)
def dfs(graph, v):
seen[v] = True
for next_v in graph[v]:
if seen[next_v]:
continue
dfs(graph, next_v)
cnt = 0
for m in range(M):
is_ok = True
for i in range(N):
seen = [False] * N
dfs(graphs[m], i)
if not min(seen):
is_ok = False
break
if is_ok:
cnt += 1
print(M - cnt)
######################################################
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
N, M = map(int, input().split())
edges = []
for i in range(M):
a, b = map(int, input().split())
a -= 1
b -= 1
edges.append((a, b))
ans = 0
for i in range(M):
uf = UnionFind(N)
for j in range(M):
if i != j:
a, b = edges[j]
uf.union(a, b)
temp = 0
for n in uf.parents:
if n < 0:
temp += 1
if temp >= 2:
ans += 1
print(ans)
######################################################
######################################################
######################################################
######################################################