This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
self.rank = [0] * n
def find(self, u):
if self.parent[u] != u:
self.parent[u] = self.find(self.parent[u])
return self.parent[u]
def union(self, u, v):
root_u = self.find(u)
root_v = self.find(v)
if root_u != root_v:
if self.rank[root_u] > self.rank[root_v]:
self.parent[root_v] = root_u
elif self.rank[root_u] < self.rank[root_v]:
self.parent[root_u] = root_v
else:
self.parent[root_v] = root_u
self.rank[root_u] += 1
n, m = map(int, input().split())
bridges = []
for i in range(m):
u, v, d = map(int, input().split())
bridges.append((d, u-1, v-1, i))
q = int(input())
queries = []
for i in range(q):
t, *params = map(int, input().split())
if t == 1:
b, r = params
bridges[b-1] = (r, bridges[b-1][1], bridges[b-1][2], b-1)
else:
s, w = params
queries.append((w, s-1, i))
bridges.sort(reverse=True, key=lambda x: x[0])
queries.sort(reverse=True, key=lambda x: x[0])
answers = [-1] * len(queries)
uf = UnionFind(n)
bridge_index = 0
for w, s, query_index in queries:
while bridge_index < len(bridges) and bridges[bridge_index][0] >= w:
_, u, v, _ = bridges[bridge_index]
uf.union(u, v)
bridge_index += 1
root_s = uf.find(s)
reachable = sum(1 for i in range(n) if uf.find(i) == root_s)
answers[query_index] = reachable
for answer in answers:
print(answer)
Compilation message (stderr)
File "plan.py", line 32
t, *params = map(int, input().split())
^
SyntaxError: invalid syntax
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |