problem_id
stringlengths
6
6
user_id
stringlengths
10
10
time_limit
float64
1k
8k
memory_limit
float64
262k
1.05M
problem_description
stringlengths
48
1.55k
codes
stringlengths
35
98.9k
status
stringlengths
28
1.7k
submission_ids
stringlengths
28
1.41k
memories
stringlengths
13
808
cpu_times
stringlengths
11
610
code_sizes
stringlengths
7
505
p03108
u311379832
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['def find(x):\n if par[x] == x:\n return x\n else:\n par[x] = find(par[x])\n return par[x]\n\ndef unite(x, y):\n x = find(x)\n y = find(y)\n if x == y:\n return\n if rank[x] < rank[y]:\n par[x] = y\n else:\n par[y] = x\n if rank[x] == rank[y]:\n rank[x] += 1\n\nN, M = map(int, input().split())\npar = [i for i in range(N + 1)]\nrank = [0] * (N + 1)\nans = [0] * M\nans[M - 1] = (N * (N - 1) // 2)\nab = [list(map(int, input().split())) for i in range(M)]\nfor i in (range(M)):\n ans[i - 1] = ans[i]\n if find(ab[i][0]) != find(ab[i][1]):\n ans[i - 1] -= rank[ab[i][0]] * rank[ab[i][1]]\n unite(ab[i][0], ab[i][1])\n\nfor i in ans:\n print(i)', 'def find(x):\n if par[x] < 0:\n return x\n else:\n par[x] = find(par[x])\n return par[x]\n\ndef same(x, y):\n if find(x) == find(y):\n return True\n else:\n return False\n\ndef unite(x, y):\n x = find(x)\n y = find(y)\n if x == y:\n return\n if par[x] > par[y]:\n x, y = y, x\n par[x] += par[y]\n par[y] = x\n\n\ndef uCnt(x):\n return -par[find(x)]\n\nN, M = map(int, input().split())\npar = [-1 for i in range(N)]\ntmp = ((N * (N - 1)) // 2)\nab = [list(map(int, input().split())) for _ in range(M)]\nans = [0] * M\nans[M - 1] = tmp\nindex = M - 1\nfor i in range(M - 1, 0, -1):\n fa = find(ab[i][0] - 1)\n fb = find(ab[i][1] - 1)\n if fa != ab:\n ans[index - 1] = ans[index] - uCnt(fa) * uCnt(fb)\n unite(ab[i][0] - 1, ab[i][1] - 1)\n index -= 1\n\nfor i in ans:\n print(i)', 'def find(x):\n if par[x] < 0:\n return x\n else:\n par[x] = find(par[x])\n return par[x]\n\ndef same(x, y):\n if find(x) == find(y):\n return True\n else:\n return False\n\ndef unite(x, y):\n x = find(x)\n y = find(y)\n if x == y:\n return\n if par[x] > par[y]:\n x, y = y, x\n par[x] += par[y]\n par[y] = x\n\n\ndef uCnt(x):\n return -par[find(x)]\n\nN, M = map(int, input().split())\npar = [-1 for i in range(N)]\ntmp = ((N * (N - 1)) // 2)\nab = [list(map(int, input().split())) for _ in range(M)]\nans = [0] * M\nans[M - 1] = tmp\nfor i in range(M - 1, 0, -1):\n fa = find(ab[i][0] - 1)\n fb = find(ab[i][1] - 1)\n ans[i - 1] = ans[i]\n if fa != fb:\n ans[i - 1] -= uCnt(fa) * uCnt(fb)\n unite(ab[i][0] - 1, ab[i][1] - 1)\n\nfor i in ans:\n print(i)']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s030983281', 's690332653', 's803284651']
[32916.0, 34604.0, 34600.0]
[655.0, 690.0, 728.0]
[713, 900, 879]
p03108
u315485238
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['N, M = list(map(int, input().split()))\nA=[0] * M\nB=[0] * M\n\nimport math\n\nfor i in range(M):\n A[i], B[i] = list(map(int, input().split()))\n\n \nisland_dict={i+1:i+1 for i in range(N)} \ngroup_dict={i+1:1 for i in range(N)} \n\nanswer = [0] * M\nanswer[-1] = math.factorial(N) // (math.factorial(N-2)*2)\n\n \nfor i in range(M)[:0:-1]:\n if island_dict[A[i]] == island_dict[B[i]]:\n answer[i-1] = answer[i]\n else:\n groupA = island_dict[A[i]]\n groupB = island_dict[B[i]] \n NA = group_dict[groupA]\n NB = group_dict[groupB]\n \n for j in group_dict[groupB]:\n island_dict[j] = groupA\n \n group_dict[groupA] = group_dict[groupA] + group_dict[groupB]\n group_dict[groupB] = 0\n answer[i-1] = answer[i] - NA*NB\n\n#print(answer)\n\nfor i in range(M):\n print(answer[i])\n \n ', 'N, M = list(map(int, input().split()))\nA=[0] * M\nB=[0] * M\n\nimport math\n\nfor i in range(M):\n A[i], B[i] = list(map(int, input().split()))\n\n \nisland_dict = {i+1:i+1 for i in range(N)} \ngroup_dict = {i+1:[i+1] for i in range(N)} \n\nanswer = [0] * M\nanswer[-1] = math.factorial(N) // (math.factorial(N-2)*2)\n \nfor i in range(M)[:0:-1]:\n if island_dict[A[i]] == island_dict[B[i]]:\n answer[i-1] = answer[i]\n else:\n groupA = island_dict[A[i]]\n groupB = island_dict[B[i]] \n NA = group_dict[groupA]\n NB = group_dict[groupB]\n \n if NA>=NB:\n for j in group_dict[groupB]:\n island_dict[j] = groupA\n\n group_dict[groupA] = group_dict[groupA] + group_dict[groupB]\n group_dict[groupB] = []\n else:\n for j in group_dict[groupA]:\n island_dict[j] = groupB\n\n group_dict[groupB] = group_dict[groupA] + group_dict[groupB]\n group_dict[groupA] = []\n \n answer[i-1] = answer[i] - NA*NB\n\n#print(answer)\n\nfor i in range(M):\n print(answer[i])\n \n ', 'import sys\nsys.setrecursionlimit(int(1e7))\n\nN, M = list(map(int, input().split()))\nAB = [list(map(int, input().split())) for _ in range(M)]\n\nroots = [(i,1) for i in range(N+1)] # root, num_islands_in_group\n\ninconveniences = [0]*(M+1)\ninconveniences[-1] = N*(N-1)//2 \n\ndef find_root(a, roots=roots):\n if roots[a][0] == a:\n return roots[a]\n else:\n roots[a] = find_root(roots[a][0])\n return roots[a]\n\ndef reduce_inconvenience_with_unite(a, b, roots=roots):\n root_a, n_a = find_root(a)\n root_b, n_b = find_root(b)\n if root_a == root_b:\n return 0\n else:\n roots[root_a] = (root_b, n_a + n_b) \n roots[root_b] = (root_b, n_a + n_b)\n return n_a * n_b\n\nfor i, ab in enumerate(AB[:0:-1]): \n a, b = ab\n inconveniences[M-1-i] = inconveniences[M-i] - reduce_inconvenience_with_unite(a,b)\n\nfor inc in inconveniences[1:]:\n print(inc)\n']
['Runtime Error', 'Runtime Error', 'Accepted']
['s144196084', 's254619335', 's380642565']
[38500.0, 48364.0, 56628.0]
[776.0, 811.0, 688.0]
[887, 1089, 953]
p03108
u319818856
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['class UnionFind:\n def __init__(self, size: int):\n self.__root = [-1 for _ in range(size)]\n self.__size = [1 for _ in range(size)]\n\n def find(self, v: int) -> int:\n if self.__root[v] == -1:\n return v\n return self.find(self.__root[v])\n\n def size(self, v: int) -> int:\n root = self.find(v)\n return self.__size[root]\n\n def unite(self, u: int, v: int):\n ur, vr = self.find(u), self.find(v)\n if ur > vr:\n ur, vr = vr, ur\n\n self.__size[ur] += self.__size[vr]\n self.__root[vr] = ur\n\n def connected(self, u: int, v: int):\n return self.find(u) == self.find(v)\n\n def print(self):\n print(\'-\' * 5 + \' root \' + \'-\' * 5)\n print(self.__root)\n print(\'-\' * 5 + \' size \' + \'-\' * 5)\n print(self.__size)\n\n\ndef decayed_bridges(N: int, M: int, edges: list) -> list:\n ret = [0 for _ in range(M+1)]\n uf = UnionFind(N+1)\n\n m = M\n ret[m] = N * (N - 1) // 2\n m -= 1\n for u, v in reversed(edges):\n if uf.connected(u, v):\n ret[m] = ret[m+1]\n else:\n n1, n2 = uf.size(u), uf.size(v)\n ret[m] = ret[m+1] - n1 * n2\n uf.unite(u, v)\n\n m -= 1\n\n return ret\n\n\nif __name__ == "__main__":\n N, M = [int(s) for s in input().split()]\n edges = []\n for _ in range(M):\n a, b = [int(s) for s in input().split()]\n edges.append((a, b))\n\n answers = decayed_bridges(N, M, edges)\n\n for a in answers:\n print(a)\n', 'class UnionFind:\n def __init__(self, size: int):\n self.__root = [-1 for _ in range(size)]\n\n def find(self, v: int) -> int:\n if self.__root[v] < 0:\n return v\n self.__root[v] = self.find(self.__root[v])\n return self.__root[v]\n\n def size(self, v: int) -> int:\n r = self.find(v)\n return -self.__root[r]\n\n def unite(self, u: int, v: int):\n ur, vr = self.find(u), self.find(v)\n if ur == vr:\n return\n\n if self.size(ur) < self.size(vr):\n ur, vr = vr, ur\n\n self.__root[ur] += self.__root[vr]\n self.__root[vr] = ur\n\n def connected(self, u: int, v: int):\n return self.find(u) == self.find(v)\n\n\ndef decayed_bridges(N: int, M: int, edges: list) -> list:\n ret = [0 for _ in range(M)]\n uf = UnionFind(N)\n\n current = N*(N-1)//2\n for m in range(M-1, -1, -1):\n ret[m] = current\n\n u, v = edges[m]\n u, v = u-1, v-1\n\n if not uf.connected(u, v):\n n1, n2 = uf.size(u), uf.size(v)\n current -= n1 * n2\n uf.unite(u, v)\n\n return ret\n\n\nif __name__ == "__main__":\n N, M = [int(s) for s in input().split()]\n edges = []\n for _ in range(M):\n a, b = [int(s) for s in input().split()]\n edges.append((a, b))\n\n answers = decayed_bridges(N, M, edges)\n\n for a in answers:\n print(a)\n']
['Wrong Answer', 'Accepted']
['s330577245', 's715117608']
[22188.0, 22932.0]
[1627.0, 810.0]
[1523, 1388]
p03108
u325264482
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['class UnionFindTree():\n def __init__(self, N):\n self.parent = [-1]*(N+1)\n self.size = [1]*(N+1)\n self.hubensa = N*(N-1)//2\n \n def find(self, i):\n if self.parent[i] == -1:\n group = i\n else:\n group = self.find(self.parent[i]) \n self.parent[i] = group\n return group\n \n def unite(self, x, y):\n px = self.find(x)\n py = self.find(y)\n if px != py:\n if self.rank[px] == self.rank[py]: # rank is same\n self.rank[px] += 1\n elif self.rank[px] < self.rank[py]:\n px, py = py, px\n \n self.parent[py] = px\n self.hubensa -= self.size[px] * self.size[py]\n self.size[px] += self.size[py]\n\nN, M = list(map(int, input().split()))\nA = [0 for _ in range(M)]\nB = [0 for _ in range(M)]\n\nfor i in range(M):\n A[i], B[i] = list(map(int, input().split()))\n\nans = []\nUFT = UnionFindTree(N)\n\n\nfor i in range(M-1, -1, -1):\n ans.append(UFT.hubensa)\n UFT.unite(A[i], B[i])\n\nprint(*ans[::-1], sep="\\n")\n', 'class UnionFindTree():\n def __init__(self, N):\n self.parent = [-1]*(N+1)\n self.rank = [1]*(N+1)\n self.size = [1]*(N+1)\n self.hubensa = N*(N-1)//2\n \n def find(self, i):\n if self.parent[i] == -1:\n group = i\n else:\n group = self.find(self.parent[i]) \n self.parent[i] = group\n return group\n \n def unite(self, x, y):\n px = self.find(x)\n py = self.find(y)\n if px != py:\n if self.rank[px] == self.rank[py]: # rank is same\n self.rank[px] += 1\n elif self.rank[px] < self.rank[py]:\n px, py = py, px\n \n self.parent[py] = px\n self.hubensa -= self.size[px] * self.size[py]\n self.size[px] += self.size[py]\n\nN, M = list(map(int, input().split()))\nA = [0 for _ in range(M)]\nB = [0 for _ in range(M)]\n\nfor i in range(M):\n A[i], B[i] = list(map(int, input().split()))\n\nans = []\nUFT = UnionFindTree(N)\n\n\nfor i in range(M-1, -1, -1):\n ans.append(UFT.hubensa)\n UFT.unite(A[i], B[i])\n\nprint(*ans[::-1], sep="\\n")\n']
['Runtime Error', 'Accepted']
['s123791548', 's744272591']
[12592.0, 20000.0]
[339.0, 581.0]
[1108, 1138]
p03108
u332906195
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['# -*- coding: utf-8 -*-\n\nfrom scipy.misc import comb\nimport sys\nsys.setrecursionlimit(10000000000)\n\n\nclass UnionFind:\n\n def __init__(self, n):\n self.parent = [i for i in range(n + 1)]\n self.depth = [1] * (n + 1)\n self.count = [1] * (n + 1)\n\n def find(self, x):\n if self.parent[x] == x:\n if self.depth[x] > 2:\n self.depth[x] = 2\n return x\n else:\n self.parent[x] = self.find(self.parent[x])\n self.depth[x], self.count[x] = 0, 0\n return self.parent[x]\n\n def isSame(self, x, y):\n return self.find(x) == self.find(y)\n\n def union(self, x, y):\n x, y = self.find(x), self.find(y)\n\n if self.depth[x] < self.depth[y]:\n self.parent[x] = self.parent[y]\n self.count[y] += self.count[x]\n self.depth[x], self.count[x] = 0, 0\n else:\n self.parent[y] = self.parent[x]\n self.count[x] += self.count[y]\n self.depth[y], self.count[y] = 0, 0\n if self.depth[x] == self.depth[y]:\n self.depth[x] += 1\n\n\nN, M = map(int, input().split())\n\nA, B = [], []\nfor _ in range(M):\n a, b = map(int, input().split())\n A.append(a)\n B.append(b)\n\nA.reverse()\nB.reverse()\n\ngroups = UnionFind(N)\nans = [comb(N, 2, exact=True)]\nfor i in range(M):\n tmp = ans[-1]\n if not groups.isSame(A[i], B[i]):\n nA, nB = groups.count[groups.find(\n A[i])], groups.count[groups.find(B[i])]\n tmp -= nA * nB\n groups.union(A[i], B[i])\n ans.append(tmp)\n\nfor i in range(M):\n print(ans[-(i + 2)])\n', '# -*- coding: utf-8 -*-\n\nfrom scipy.misc import comb\nimport sys\nsys.setrecursionlimit(10000000000)\n\n\nclass UnionFind:\n\n def __init__(self, n):\n # parent[x] < 0 means x is root and abs(parent[x]) == size[x]\n self.parent, self.depth = [-1] * (n + 1), [0] * (n + 1)\n\n def find(self, x):\n if self.parent[x] < 0:\n return x\n else:\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def isSame(self, x, y):\n return self.find(x) == self.find(y)\n\n def unite(self, x, y):\n x, y = self.find(x), self.find(y)\n\n if self.depth[x] > self.depth[y]:\n self.parent[x] += self.parent[y]\n self.parent[y] = x\n else:\n self.parent[y] += self.parent[x]\n self.parent[x] = y\n if self.depth[x] == self.depth[y]:\n self.depth[y] += 1\n\n def count(self, x):\n return - self.parent[self.find(x)]\n\n\nN, M = map(int, input().split())\n\nAB = [tuple(map(int, input().split())) for _ in range(M)]\nAB.reverse()\n\ngroups = UnionFind(N)\nans = [comb(N, 2, exact=True)]\nfor i in range(M):\n if groups.isSame(AB[i][0], AB[i][1]):\n ans.append(ans)\n else:\n ans.append(ans[-1] - groups.count(AB[i][0]) * groups.count(AB[i][1]))\n groups.union(AB[i][0], AB[i][1])\n\nfor i in range(M):\n print(ans[-(i + 2)])\n', '# -*- coding: utf-8 -*-\n\nfrom scipy.misc import comb\nimport sys\nsys.setrecursionlimit(1000000000)\n\n\nclass UnionFind:\n\n def __init__(self, n):\n # parent[x] < 0 means x is root and abs(parent[x]) == size[x]\n self.parent, self.depth = [-1] * (n + 1), [0] * (n + 1)\n\n def find(self, x):\n if self.parent[x] < 0:\n return x\n else:\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def isSame(self, x, y):\n return self.find(x) == self.find(y)\n\n def unite(self, x, y):\n x, y = self.find(x), self.find(y)\n\n if self.depth[x] > self.depth[y]:\n self.parent[x] += self.parent[y]\n self.parent[y] = x\n else:\n self.parent[y] += self.parent[x]\n self.parent[x] = y\n if self.depth[x] == self.depth[y]:\n self.depth[y] += 1\n\n def count(self, x):\n return - self.parent[self.find(x)]\n\n\nN, M = map(int, input().split())\n\nAB = [tuple(map(int, input().split())) for _ in range(M)]\nAB.reverse()\n\ngroups = UnionFind(N)\nans = [comb(N, 2, exact=True)]\nfor i in range(M):\n if groups.isSame(AB[i][0], AB[i][1]):\n ans.append(ans)\n else:\n ans.append(ans[-1] - groups.count(AB[i][0]) * groups.count(AB[i][1]))\n groups.unite(AB[i][0], AB[i][1])\n\nfor i in range(M):\n print(ans[-(i + 2)])\n', '# -*- coding: utf-8 -*-\n\n\nfrom scipy.misc import comb\n\n\nclass UnionFind:\n\n def __init__(self, n):\n self.parent = [i for i in range(n + 1)]\n self.depth = [1] * (n + 1)\n self.count = [1] * (n + 1)\n\n def find(self, x):\n if self.parent[x] == x:\n if self.depth[x] > 2:\n self.depth[x] = 2\n return x\n else:\n self.parent[x] = self.find(self.parent[x])\n self.depth[x], self.count[x] = 0, 0\n return self.parent[x]\n\n def isSame(self, x, y):\n return self.find(x) == self.find(y)\n\n def union(self, x, y):\n x, y = self.find(x), self.find(y)\n\n if self.depth[x] < self.depth[y]:\n self.parent[x] = self.parent[y]\n self.count[y] += self.count[x]\n self.depth[x], self.count[x] = 0, 0\n else:\n self.parent[y] = self.parent[x]\n self.count[x] += self.count[y]\n self.depth[y], self.count[y] = 0, 0\n if self.depth[x] == self.depth[y]:\n self.depth[x] += 1\n\n\nN, M = map(int, input().split())\n\nA, B = [], []\nfor _ in range(M):\n a, b = map(int, input().split())\n A.append(a)\n B.append(b)\n\nA.reverse()\nB.reverse()\n\ngroups = UnionFind(N)\nans = [comb(N, 2, exact=True)]\nfor i in range(M):\n tmp = ans[-1]\n if not groups.isSame(A[i], B[i]):\n nA, nB = groups.count[groups.find(A[i])], groups.count[groups.find(B[i])]\n tmp -= nA * nB\n groups.union(A[i], B[i])\n ans.append(tmp)\n\nfor i in range(M):\n print(ans[-(i + 2)])\n', '# -*- coding: utf-8 -*-\n\nimport sys\nsys.setrecursionlimit(10 ** 7)\n\n\nclass UnionFind:\n\n def __init__(self, n):\n # parent[x] < 0 means x is root and abs(parent[x]) == size[x]\n self.parent, self.depth = [-1] * (n + 1), [0] * (n + 1)\n\n def find(self, x):\n if self.parent[x] < 0:\n return x\n else:\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def isSame(self, x, y):\n return self.find(x) == self.find(y)\n\n def unite(self, x, y):\n x, y = self.find(x), self.find(y)\n\n if self.depth[x] > self.depth[y]:\n self.parent[x] += self.parent[y]\n self.parent[y] = x\n else:\n self.parent[y] += self.parent[x]\n self.parent[x] = y\n if self.depth[x] == self.depth[y]:\n self.depth[y] += 1\n\n def count(self, x):\n return - self.parent[self.find(x)]\n\n\nN, M = map(int, input().split())\n\nAB = [tuple(map(int, input().split())) for _ in range(M)]\nAB.reverse()\n\ngroups = UnionFind(N)\nans = [N * (N - 1) // 2]\nfor i in range(M):\n if groups.isSame(AB[i][0], AB[i][1]):\n ans.append(ans[-1])\n else:\n ans.append(ans[-1] - groups.count(AB[i][0]) * groups.count(AB[i][1]))\n groups.unite(AB[i][0], AB[i][1])\n\nfor i in range(M):\n print(ans[M - 1 - i])\n']
['Runtime Error', 'Runtime Error', 'Runtime Error', 'Runtime Error', 'Accepted']
['s305348500', 's371989484', 's430341119', 's592816843', 's281014551']
[20264.0, 13232.0, 33688.0, 30652.0, 23284.0]
[321.0, 167.0, 990.0, 1106.0, 818.0]
[1620, 1381, 1380, 1562, 1348]
p03108
u333945892
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["from collections import defaultdict\nimport sys,heapq,bisect,math,itertools,string,queue,datetime\nsys.setrecursionlimit(10**8)\nINF = float('inf')\nmod = 10**9+7\neps = 10**-7\ndef inpl(): return list(map(int, input().split()))\ndef inpl_str(): return list(input().split())\n\n\ndef Find(x): \n global table\n\n if table[x] == x:\n return x\n else:\n table[x] = Find(table[x]) \n size[x] = size[table[x]]\n return table[x]\n\ndef Unite(x,y): \n global size\n global rank\n x = Find(x)\n y = Find(y)\n sx = Size(x)\n sy = Size(y)\n\n if x == y:\n return\n\n if rank[x] > rank[y]:\n table[y] = x\n size[x] = sx + sy\n else:\n table[x] = y\n size[y] = sx + sy\n if rank[x] == rank[y]:\n rank[y] += 1\n\ndef Check(x,y):\n if Find(x) == Find(y):\n return True\n else:\n return False\n\ndef Size(x):\n return size[Find(x)]\n\nN,M = inpl()\n\ntable = [i for i in range(N)] \nrank = [1 for i in range(N)] \nsize = [1 for i in range(N)]\n\nABs = [inpl() for i in range(M)]\nans = N*(N-1)//2\nans_list = [ans]\n\nfor A,B in reversed(ABs):\n A -= 1\n B -= 1\n \n if not Check(A,B):\n ans -= Size(A) * Size(B)\n Unite(A,B)\n ans_list.append(ans)\n\nfor i in reversed(range(N-1)):\n print(ans_list[i])\n", "from collections import defaultdict\nimport sys,heapq,bisect,math,itertools,string,queue,datetime\nsys.setrecursionlimit(10**8)\nINF = float('inf')\nmod = 10**9+7\neps = 10**-7\ndef inpl(): return list(map(int, input().split()))\ndef inpl_str(): return list(input().split())\n\n\ndef Find(x): \n global table\n\n if table[x] == x:\n return x\n else:\n table[x] = Find(table[x]) \n size[x] = size[table[x]]\n return table[x]\n\ndef Unite(x,y): \n global size\n global rank\n x = Find(x)\n y = Find(y)\n sx = Size(x)\n sy = Size(y)\n\n if x == y:\n return\n\n if rank[x] > rank[y]:\n table[y] = x\n size[x] = sx + sy\n else:\n table[x] = y\n size[y] = sx + sy\n if rank[x] == rank[y]:\n rank[y] += 1\n\ndef Check(x,y):\n if Find(x) == Find(y):\n return True\n else:\n return False\n\ndef Size(x):\n return size[Find(x)]\n\nN,M = inpl()\n\ntable = [i for i in range(N)] \nrank = [1 for i in range(N)] \nsize = [1 for i in range(N)]\n\nABs = [inpl() for i in range(M)]\nans = N*(N-1)//2\nans_list = [ans]\n\nfor A,B in reversed(ABs):\n A -= 1\n B -= 1\n \n if not Check(A,B):\n ans -= Size(A) * Size(B)\n Unite(A,B)\n ans_list.append(ans)\n\nfor i in reversed(range(M)):\n print(ans_list[i])\n"]
['Wrong Answer', 'Accepted']
['s552273538', 's168965798']
[39284.0, 39288.0]
[883.0, 873.0]
[1479, 1477]
p03108
u334712262
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["# -*- coding: utf-8 -*-\nimport bisect\nimport heapq\nimport math\nimport random\nimport sys\nfrom collections import Counter, defaultdict, deque\nfrom decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal\nfrom functools import lru_cache, reduce\nfrom itertools import combinations, combinations_with_replacement, product, permutations\nfrom operator import add, mul, sub\n\nsys.setrecursionlimit(10000)\n\n\ndef read_int():\n return int(input())\n\n\ndef read_int_n():\n return list(map(int, input().split()))\n\n\ndef read_float():\n return float(input())\n\n\ndef read_float_n():\n return list(map(float, input().split()))\n\n\ndef read_str():\n return input().strip()\n\n\ndef read_str_n():\n return list(map(str, input().split()))\n\n\ndef error_print(*args):\n print(*args, file=sys.stderr)\n\n\ndef mt(f):\n import time\n\n def wrap(*args, **kwargs):\n s = time.time()\n ret = f(*args, **kwargs)\n e = time.time()\n\n error_print(e - s, 'sec')\n return ret\n\n return wrap\n\n\nclass UnionFind():\n def __init__(self):\n self.__table = {}\n self.__size = defaultdict(lambda : 1)\n\n def __root(self, x):\n if x not in self.__table:\n self.__table[x] = x\n elif x != self.__table[x]:\n self.__table[x] = self.__root(self.__table[x])\n\n return self.__table[x]\n\n def same(self, x, y):\n return self.__root(x) == self.__root(y)\n\n def union(self, x, y):\n x = self.__root(x)\n y = self.__root(y)\n if x != y:\n self.__size[x] = self.__size[x] + self.__size[y]\n self.__size[y] = self.__size[x]\n self.__table[x] = y\n \n\n def size(self, x):\n return self.__size[self.__root(x)]\n\n@mt\ndef slv(N, M, AB):\n uf = UnionFind()\n\n ans = [N*(N-1)//2]\n for a, b in reversed(AB):\n if uf.same(a, b):\n ans.append(ans[-1])\n else:\n s1, s2 = uf.size(a), uf.size(b)\n uf.union(a, b)\n ans.append(ans[-1] - s1*s2)\n # ans.pop()\n for a in reversed(ans):\n print(a)\n \n\n # return ans\n\n\ndef main():\n # N = read_int()\n N, M = read_int_n()\n AB = [read_int_n() for _ in range(M)]\n slv(N, M, AB)\n\n\n\nif __name__ == '__main__':\n main()\n", "# -*- coding: utf-8 -*-\nimport bisect\nimport heapq\nimport math\nimport random\nimport sys\nfrom collections import Counter, defaultdict, deque\nfrom decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal\nfrom functools import lru_cache, reduce\nfrom itertools import combinations, combinations_with_replacement, product, permutations\nfrom operator import add, mul, sub\n\nsys.setrecursionlimit(100000)\n\n\ndef read_int():\n return int(input())\n\n\ndef read_int_n():\n return list(map(int, input().split()))\n\n\ndef read_float():\n return float(input())\n\n\ndef read_float_n():\n return list(map(float, input().split()))\n\n\ndef read_str():\n return input().strip()\n\n\ndef read_str_n():\n return list(map(str, input().split()))\n\n\ndef error_print(*args):\n print(*args, file=sys.stderr)\n\n\ndef mt(f):\n import time\n\n def wrap(*args, **kwargs):\n s = time.time()\n ret = f(*args, **kwargs)\n e = time.time()\n\n error_print(e - s, 'sec')\n return ret\n\n return wrap\n\n\nclass UnionFind():\n def __init__(self):\n self.__table = {}\n self.__size = defaultdict(lambda: 1)\n\n def __root(self, x):\n if x not in self.__table:\n self.__table[x] = x\n elif x != self.__table[x]:\n self.__table[x] = self.__root(self.__table[x])\n\n return self.__table[x]\n\n def same(self, x, y):\n return self.__root(x) == self.__root(y)\n\n def union(self, x, y):\n x = self.__root(x)\n y = self.__root(y)\n if x != y:\n # self.__size[x] += self.__size[y]\n self.__size[y] += self.__size[x]\n self.__table[x] = y\n \n\n def size(self, x):\n return self.__size[self.__root(x)]\n\n@mt\ndef slv(N, M, AB):\n uf = UnionFind()\n\n ans = [N*(N-1)//2]\n for a, b in reversed(AB):\n if uf.same(a, b):\n ans.append(ans[-1])\n else:\n s1, s2 = uf.size(a), uf.size(b)\n uf.union(a, b)\n ans.append(ans[-1] - s1*s2)\n \n ans.pop()\n\n for a in reversed(ans):\n print(a)\n \n\n # return ans\n\n\ndef main():\n # N = read_int()\n N, M = read_int_n()\n AB = [read_int_n() for _ in range(M)]\n slv(N, M, AB)\n\n\n\nif __name__ == '__main__':\n main()\n"]
['Runtime Error', 'Accepted']
['s678104245', 's823473864']
[51484.0, 51736.0]
[1054.0, 1050.0]
[2254, 2252]
p03108
u341087021
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['n,m = map(int, input().split())\n\nbridges = []\nfor i in range(m):\n bridges.append([int(x) for x in input().split()])\n\ndef root(par, a):\n while par[a] != -1:\n a = par[a]\n return a \n\ndef unite(par, npar, depth, ra, rb):\n if depth[ra] >= depth[rb]:\n npar[ra] = npar[ra] + npar[rb]\n par[rb] = ra\n if depth[ra] == depth[rb]:\n depth[ra] += 1\n else:\n npar[rb] = npar[rb] + npar[ra]\n par[ra] = rb\n\npar = [-1 for _ in range(n+1)]\nnpar = [1 for _ in range(n+1)]\ndepth = [1 for _ in range(n+1)]\n\nans = []\nunconf = n*(n-1)//2\nans.append(unconf)\n\nfor x in reversed(bridges):\n a,b = x\n ra = root(par, ifpar, a)\n rb = root(par, ifpar, b)\n if ra != rb:\n tmp = npar[ra] * npar[rb]\n unite(par, ifpar, npar, depth, ra, rb)\n unconf -= tmp\n ans.append(unconf)\n\nans.reverse()\nans.pop(0)\nfor x in ans:\n print(x)', 'n,m = map(int, input().split())\n\nbridges = []\nfor i in range(m):\n bridges.append([int(x) for x in input().split()])\n\ndef root(par, a):\n while par[a] != -1:\n a = par[a]\n return a \n\ndef unite(par, npar, depth, ra, rb):\n if depth[ra] >= depth[rb]:\n npar[ra] = npar[ra] + npar[rb]\n par[rb] = ra\n if depth[ra] == depth[rb]:\n depth[ra] += 1\n else:\n npar[rb] = npar[rb] + npar[ra]\n par[ra] = rb\n\npar = [-1 for _ in range(n+1)]\nnpar = [1 for _ in range(n+1)]\ndepth = [1 for _ in range(n+1)]\n\nans = []\nunconf = n*(n-1)//2\nans.append(unconf)\n\nfor x in reversed(bridges):\n a,b = x\n ra = root(par, a)\n rb = root(par, b)\n if ra != rb:\n tmp = npar[ra] * npar[rb]\n unite(par, npar, depth, ra, rb)\n unconf -= tmp\n ans.append(unconf)\n\nans.reverse()\nans.pop(0)\nfor x in ans:\n print(x)']
['Runtime Error', 'Accepted']
['s177731863', 's061282229']
[23656.0, 28848.0]
[361.0, 566.0]
[893, 872]
p03108
u342869120
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['import math\nfrom collections import defaultdict\n\n\nclass UnionFindTree:\n def __init__(self):\n self.p = {}\n\n def make(self, x):\n self.p[x] = x\n\n def find(self, x):\n if x in self.p:\n if self.p[x] != x:\n self.p[x] = self.find(self.p[x])\n return self.p[x]\n return None\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def unite(self, x, y):\n return self._unite(self.find(x), self.find(y))\n\n def _unite(self, x, y):\n if x >= y:\n self.p[x] = y\n return y\n else:\n self.p[y] = x\n return x\n\n\n_ncr = {}\n\n\ndef ncr(n, r):\n if n < r:\n return 0\n k = "{}:{}".format(n, r)\n if k not in _ncr:\n _ncr[k] = math.factorial(n) // (math.factorial(n - r) *\n math.factorial(r))\n return _ncr[k]\n\n\nN, M = map(int, input().split())\nab = [list(map(int, input().split())) for _ in range(M)]\nab.reverse()\n\nutf = UnionFindTree()\nfor n in range(1, N + 1):\n utf.make(n)\n\nh = {}\nans = []\ntotal = ncr(N, 2)\nnow = 0\nfor i in range(M):\n ans.append(total)\n\n a, b = ab[i]\n x, y = utf.find(a), utf.find(b)\n a = utf.unite(x, y)\n \n\nfor v in reversed(ans):\n print(v)', 'import math\nfrom collections import defaultdict\nfrom collections import deque\n\n\nclass UnionFindTree:\n \'\'\'\n Union Find Tree\n \'\'\'\n\n def __init__(self):\n self.p = {}\n self.rank = {}\n\n def make(self, x):\n self.p[x] = x\n self.rank[x] = 0\n\n def find(self, x):\n if x in self.p:\n if self.p[x] != x:\n \n self.p[x] = self.find(self.p[x])\n return self.p[x]\n return None\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def unite(self, x, y):\n return self._unite(self.find(x), self.find(y))\n\n def _unite(self, x, y):\n if self.rank[x] < self.rank[y]:\n self.p[x] = y\n return y\n else:\n self.p[y] = x\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n return x\n\n\n_ncr = {}\n\n\ndef ncr(n, r):\n if n < r:\n return 0\n k = "{}:{}".format(n, r)\n if k not in _ncr:\n _ncr[k] = math.factorial(n) // (math.factorial(n - r) *\n math.factorial(r))\n return _ncr[k]\n\n\nN, M = map(int, input().split())\nab = [list(map(int, input().split())) for _ in range(M)]\n\nutf = UnionFindTree()\nfor n in range(1, N + 1):\n utf.make(n)\n\nh = {}\nans = deque([])\ntotal = ncr(N, 2)\nnow = 0\nfor i in range(M):\n ans.appendleft(total)\n\n a, b = ab[M - i - 1]\n x, y = utf.find(a), utf.find(b)\n a = utf.unite(x, y)\n if x != y:\n nx, ny = h.get(x, 1), h.get(y, 1)\n n = nx + ny\n #total += ncr(nx, 2) + ncr(ny, 2)\n #total -= ncr(n, 2)\n\n h[x] = 1\n h[y] = 1\n h[a] = n\n\nfor v in ans:\n print(v)', 'import math\nfrom collections import defaultdict\nfrom collections import deque\n\n\nclass UnionFindTree:\n def __init__(self):\n self.p = {}\n\n def make(self, x):\n self.p[x] = x\n\n def find(self, x):\n if x in self.p:\n if self.p[x] != x:\n self.p[x] = self.find(self.p[x])\n return self.p[x]\n return None\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def unite(self, x, y):\n return self._unite(self.find(x), self.find(y))\n\n def _unite(self, x, y):\n if x >= y:\n self.p[x] = y\n return y\n else:\n self.p[y] = x\n return x\n\n\n_ncr = {}\n\n\ndef ncr(n, r):\n if n < r:\n return 0\n k = "{}:{}".format(n, r)\n if k not in _ncr:\n _ncr[k] = math.factorial(n) // (math.factorial(n - r) *\n math.factorial(r))\n return _ncr[k]\n\n\nN, M = map(int, input().split())\nab = [list(map(int, input().split())) for _ in range(M)]\n\nutf = UnionFindTree()\nfor n in range(1, N + 1):\n utf.make(n)\n\nh = {}\nans = deque([])\ntotal = ncr(N, 2)\nnow = 0\nfor i in range(M):\n ans.appendleft(total)\n\n a, b = ab[M - i - 1]\n x, y = utf.find(a), utf.find(b)\n a = utf.unite(x, y)\n \nfor v in ans:\n print(v)', 'from collections import defaultdict\n\n\nclass UnionFindTree:\n def __init__(self):\n self.p = {}\n self.rank = {}\n\n def make(self, x):\n self.p[x] = x\n self.rank[x] = 0\n\n def find(self, x):\n if x in self.p:\n if self.p[x] != x:\n \n self.p[x] = self.find(self.p[x])\n return self.p[x]\n return None\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def unite(self, x, y):\n return self._unite(self.find(x), self.find(y))\n\n def _unite(self, x, y):\n if self.rank[x] < self.rank[y]:\n self.p[x] = y\n return y\n else:\n self.p[y] = x\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n return x\n\n\ndef nc2(n):\n return (n * (n - 1)) >> 1\n\n\nN, M = map(int, input().split())\nab = [list(map(int, input().split())) for _ in range(M)]\n\nutf = UnionFindTree()\nfor n in range(1, N + 1):\n utf.make(n)\n\nh = {}\nans = []\ntotal = nc2(N)\nnow = 0\nfor i in range(M):\n ans.append(total)\n\n a, b = ab[M - i - 1]\n x, y = utf.find(a), utf.find(b)\n a = utf.unite(x, y)\n if x != y:\n nx, ny = h.get(x, 1), h.get(y, 1)\n n = nx + ny\n total += nc2(nx) + nc2(ny)\n total -= nc2(n)\n\n h[x] = 1\n h[y] = 1\n h[a] = n\n\nfor v in reversed(ans):\n print(v)']
['Wrong Answer', 'Wrong Answer', 'Wrong Answer', 'Accepted']
['s617585517', 's629293526', 's732513399', 's883286647']
[39820.0, 56676.0, 39816.0, 59696.0]
[1131.0, 1269.0, 1130.0, 917.0]
[1523, 1711, 1554, 1408]
p03108
u343523393
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['N,M=list(map(int,input().split()))\nL=[]\nclass UnionFind():\n def __init__(self, n):\n self.n = n\n self.root = [-1]*(n+1)\n self.rnk = [0]*(n+1)\n\n def find_root(self, x):\n if(self.root[x]<0):\n return x\n else:\n self.root[x] = self.find_root(self.root[x])\n return self.root[x]\n\n def Unite(self, x, y):\n x=self.find_root(x)\n y=self.find_root(y)\n\n if(x==y):\n return \n elif(self.rnk[x]>self.rnk[y]):\n self.root[x]+=self.root[y]\n self.root[y]=x\n else:\n self.root[y]+=self.root[x]\n self.root[x]=y\n if(self.rnk[x]==self.rnk[y]):\n self.rnk[y]+=1\n def isSameGroup(self,x,y):\n if(self.find_root(x)==self.find_root(y)):\n return True\n else:\n False\n\n def Size(self,x):\n return -self.root[self.find_root(x)]\n\nfor i in range(M):\n li = list(map(int,input().split()))\n L.append(li)\nL=L[::-1]\n\nans = [0]*(M+1)\nans[0] = N*(N-1)/2\nT = UnionFind(N)\nfor i in range(N):\n if(T.isSameGroup(L[i][0],L[i][1])):\n ans[i+1] = ans[i]\n continue\n else:\n ans[i+1] = max(0, ans[i]-T.Size(L[i][0])*T.Size(L[i][1]))\n T.Unite(L[i][0],L[i][1])\n\nfor i in range(N,0,-1):\n print(int(ans[i]))\n#print(len(ans))', 'N,M=list(map(int,input().split()))\nL=[]\nclass UnionFind():\n def __init__(self, n):\n self.n = n\n self.root = [-1]*(n+1)\n self.rnk = [0]*(n+1)\n\n def find_root(self, x):\n if(self.root[x]<0):\n return x\n else:\n self.root[x] = self.find_root(self.root[x])\n return self.root[x]\n\n def Unite(self, x, y):\n x=self.find_root(x)\n y=self.find_root(y)\n\n if(x==y):\n return \n elif(self.rnk[x]>self.rnk[y]):\n self.root[x]+=self.root[y]\n self.root[y]=x\n else:\n self.root[y]+=self.root[x]\n self.root[x]=y\n if(self.rnk[x]==self.rnk[y]):\n self.rnk[y]+=1\n def isSameGroup(self,x,y):\n if(self.find_root(x)==self.find_root(y)):\n return True\n else:\n False\n\n def Size(self,x):\n return -self.root[self.find_root(x)]\n\nfor i in range(M):\n li = list(map(int,input().split()))\n L.append(li)\nL=L[::-1]\n\nans = [0]*(M)\nans[0] = N*(N-1)/2\nT = UnionFind(N)\nfor i in range(M-1):\n if(T.isSameGroup(L[i][0],L[i][1])):\n ans[i+1] = ans[i]\n continue\n else:\n ans[i+1] = max(0, ans[i]-T.Size(L[i][0])*T.Size(L[i][1]))\n T.Unite(L[i][0],L[i][1])\n\nfor i in ans[::-1]:\n print(int(i))\n#print(len(ans))']
['Runtime Error', 'Accepted']
['s450438051', 's758392209']
[33328.0, 34300.0]
[964.0, 988.0]
[1346, 1337]
p03108
u368796742
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['from collections import deque\nn,m = map(int,input().split())\nl = [list(map(int,input().split())) for i in range(m)]\nans = n*(n-1)//2\ncheck = [-1]*m\ncheck[-1] = ans\n\ne = [[] for i in range(n)]\n\ndef search(x,y):\n q = deque([])\n dep = list(set(e[x]+e[y]+[x,y]))\n \n \n q.append((x,dep))\n q.append((y,dep))\n e[x] = dep\n e[y] = dep\n\n while q:\n bef,dep = q.popleft()\n\n for nex in e[bef]:\n if e[nex] != dep:\n e[nex] = dep\n q.append((nex,dep))\n \n\nfor i in range(m-1):\n \n search(l[-1-i][0]-1,l[-1-i][1]-1)\n \n check[-1-i] = ans\nprint(check)\n\n\n\n\n', 'from collections import deque\nn,m = map(int,input().split())\nl = [list(map(int,input().split())) for i in range(m)]\nans = n*(n-1)//2\ncheck = [ans]\nclass Unionfind:\n \n def __init__(self,n):\n self.uf = [-1]*n\n\n def find(self,x):\n if self.uf[x] < 0:\n return x\n else:\n self.uf[x] = self.find(self.uf[x])\n return self.uf[x]\n\n def same(self,x,y):\n return self.find(x) == self.find(y)\n\n def union(self,x,y):\n x = self.find(x)\n y = self.find(y)\n if x == y:\n return \n if self.uf[x] > self.uf[y]:\n x,y = y,x\n self.uf[x] += self.uf[y]\n self.uf[y] = x\n \n\n def size(self,x):\n x = self.find(x)\n return -self.uf[x]\n\n\nuf = Unionfind(n)\nfor i,j in l[::-1]:\n if uf.same(i-1,j-1) == False:\n ans -= uf.size(i-1)*uf.size(j-1)\n uf.union(i-1,j-1)\n check.append(ans)\n \nfor i in check[m-1::-1]:\n print(i)\n\n\n\n\n']
['Wrong Answer', 'Accepted']
['s887340383', 's800935808']
[39856.0, 35780.0]
[2106.0, 777.0]
[630, 978]
p03108
u374103100
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["\n\nimport itertools\nimport collections\nimport bisect\nimport sys\ninput = sys.stdin.readline\n\n# Union find\nclass UnionFind():\n \n \n def __init__(self,size):\n self.table = [-1 for _ in range(size)]\n self.size_table = [1 for _ in range(size)]\n\n \n def find(self, x):\n while self.table[x] >= 0:\n \n x = self.table[x]\n return x\n\n \n def union(self, x, y):\n s1 = self.find(x) \n s2 = self.find(y)\n if s1 != s2: \n if self.table[s1] != self.table[s2]: \n if self.table[s1] < self.table[s2]:\n self.table[s2] = s1\n self.size_table[s1] += self.size_table[s2]\n else:\n self.table[s1] = s2\n self.size_table[s2] += self.size_table[s1]\n else:\n \n \n self.table[s1] += -1\n self.table[s2] = s1\n self.size_table[s1] += self.size_table[s2]\n return\n\n def size(self, x):\n return self.size_table[self.find(x)]\n\n\ndef main():\n N, M = map(int, input().split())\n bridges = [list(map(int, input().split())) for _ in range(M)]\n bridges = list(map(lambda l: [i-1 for i in l], bridges))\n # print(bridges)\n\n fuben = N * (N-1) // 2\n answers = []\n uf = UnionFind(N)\n for j in range(len(bridges)-1, 0, -1):\n br = bridges[j]\n\n if uf.find(br[0]) != uf.find(br[1]): \n fuben -= (uf.size(br[0]) * uf.size(br[1]))\n\n answers.append(fuben)\n\n uf.union(br[0], br[1])\n\n # print(answers)\n\n for a in answers.reverse():\n print(a)\n\n \n print(N * (N-1) // 2)\n\n\nif __name__ == '__main__':\n main()\n\n", "\n\nimport itertools\nimport collections\nimport bisect\nimport sys\ninput = sys.stdin.readline\n\n# Union find\nclass UnionFind():\n \n \n def __init__(self,size):\n self.table = [-1 for _ in range(size)]\n self.size_table = [1 for _ in range(size)]\n\n \n def find(self, x):\n while self.table[x] >= 0:\n \n x = self.table[x]\n return x\n\n \n def union(self, x, y):\n s1 = self.find(x) \n s2 = self.find(y)\n if s1 != s2: \n if self.table[s1] != self.table[s2]: \n if self.table[s1] < self.table[s2]:\n self.table[s2] = s1\n self.size_table[s1] += self.size_table[s2]\n else:\n self.table[s1] = s2\n self.size_table[s2] += self.size_table[s1]\n else:\n \n \n self.table[s1] += -1\n self.table[s2] = s1\n self.size_table[s1] += self.size_table[s2]\n return\n\n def size(self, x):\n return self.size_table[self.find(x)]\n\n\ndef main():\n N, M = map(int, input().split())\n bridges = [list(map(int, input().split())) for _ in range(M)]\n bridges = list(map(lambda l: [i-1 for i in l], bridges))\n # print(bridges)\n\n fuben = N * (N-1) // 2\n answers = []\n uf = UnionFind(N)\n for j in range(len(bridges)-1, 0, -1):\n br = bridges[j]\n\n if uf.find(br[0]) != uf.find(br[1]): \n fuben -= (uf.size(br[0]) * uf.size(br[1]))\n\n answers.insert(0, fuben)\n\n uf.union(br[0], br[1])\n\n # print(answers)\n\n for a in reversed(answers):\n print(a)\n\n \n print(N * (N-1) // 2)\n\n\nif __name__ == '__main__':\n main()\n\n", "\n\nimport itertools\nimport collections\nimport bisect\n\n\n# Union find\nclass UnionFind():\n \n \n def __init__(self,size):\n self.table = [-1 for _ in range(size)]\n\n \n def find(self,x):\n while self.table[x] >= 0:\n \n x = self.table[x]\n return x\n\n \n def union(self,x,y):\n s1 = self.find(x)\n s2 = self.find(y)\n if s1 != s2:\n if self.table[s1] < self.table[s2]: \n self.table[s1] += self.table[s2]\n self.table[s2] = s1\n else:\n self.table[s2] += self.table[s1]\n self.table[s1] = s2\n return\n\n def size(self, x):\n return -self.table[self.find(x)]\n\n\ndef main():\n N, M = map(int, input().split())\n bridges = [list(map(int, input().split())) for _ in range(M)]\n bridges = list(map(lambda l: [i-1 for i in l], bridges))\n # print(bridges)\n\n fuben = N * (N-1) // 2\n answers = []\n uf = UnionFind(N)\n for j in range(len(bridges)-1, 0, -1):\n br = bridges[j]\n\n if uf.find(br[0]) == uf.find(br[1]): \n answers.insert(0, fuben)\n else: \n b1 = uf.size(br[0])\n b2 = uf.size(br[1])\n fuben = fuben - b1 * b2\n answers.insert(0, fuben)\n\n uf.union(br[0], br[1])\n\n print(answers)\n\n for a in answers:\n print(a)\n\n \n print(N * (N-1) // 2)\n\n\nif __name__ == '__main__':\n main()\n", "\n\nimport itertools\nimport collections\nimport bisect\nimport sys\ninput = sys.stdin.readline\n\n# Union find\nclass UnionFind():\n \n \n def __init__(self,size):\n self.table = [-1 for _ in range(size)]\n self.size_table = [1 for _ in range(size)]\n\n \n def find(self, x):\n while self.table[x] >= 0:\n \n x = self.table[x]\n return x\n\n \n def union(self, x, y):\n s1 = self.find(x) \n s2 = self.find(y)\n if s1 != s2: \n if self.table[s1] != self.table[s2]: \n if self.table[s1] < self.table[s2]:\n self.table[s2] = s1\n self.size_table[s1] += self.size_table[s2]\n else:\n self.table[s1] = s2\n self.size_table[s2] += self.size_table[s1]\n else:\n \n \n self.table[s1] += -1\n self.table[s2] = s1\n self.size_table[s1] += self.size_table[s2]\n return\n\n def size(self, x):\n return self.size_table[self.find(x)]\n\n\ndef main():\n N, M = map(int, input().split())\n bridges = [list(map(int, input().split())) for _ in range(M)]\n bridges = list(map(lambda l: [i-1 for i in l], bridges))\n # print(bridges)\n\n fuben = N * (N-1) // 2\n answers = []\n uf = UnionFind(N)\n for j in range(len(bridges)-1, 0, -1):\n br = bridges[j]\n\n if uf.find(br[0]) != uf.find(br[1]): \n fuben -= (uf.size(br[0]) * uf.size(br[1]))\n\n answers.append(fuben)\n\n uf.union(br[0], br[1])\n\n # print(answers)\n\n for a in reversed(answers):\n print(a)\n\n \n print(N * (N-1) // 2)\n\n\nif __name__ == '__main__':\n main()\n\n"]
['Runtime Error', 'Wrong Answer', 'Wrong Answer', 'Accepted']
['s112493224', 's360055524', 's372075556', 's123860750']
[41324.0, 41324.0, 41324.0, 41324.0]
[576.0, 2106.0, 2109.0, 594.0]
[2326, 2329, 1912, 2326]
p03108
u384261199
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['import numpy as np\nN, M = map(int, input().split())\nab_list = [list(map(int, input().split())) for _ in range(M)]\n\na_list = np.array(ab_list)[:, 0][-1::-1]\nb_list = np.array(ab_list)[:, 1][-1::-1]\n\nhuben = [int((N) * (N-1) * 0.5)]\n\nclass UnionFind():\n def __init__(self, size):\n # self.group holds a list of idx for parant node of i-th node\n # if self.group[i] = i, i-th node is a root.\n # if self.group[i] = j, j-th node is a paranet of i-th node. \n self.group = [i for i in range(size)]\n \n # self.height holds a list of trees\' heights\n self.height = [0] * (size)\n \n def find(self, i):\n """\n Search for i-th node\'s group.\n Args : \n i : index of target node.\n Return :\n index of root of target node\n """ \n \n if self.group[i] == i:\n # if root\n return i\n else:\n # if not root, search by the parent\'s node\n self.group[i] = self.find(self.group[i])\n return self.group[i] \n \n def unite(self, i, j):\n """\n Unite two groups\n Args : \n i, j : indices of target nodes.\n """ \n # search roots\n i = self.find(i)\n j = self.find(j)\n \n if self.height[i] < self.height[j]:\n self.group[i] = j\n else:\n self.group[j] = i\n if self.height[i] == self.height[j]:\n self.height[i] += 1\n \n def size(self, i):\n """\n Get num of nodes in same group\n Args : \n i : index of target node.\n """\n ans = 1\n root = self.find(i)\n for par in self.group:\n if self.find(par) == root:\n ans += 1\n ans -= 1\n return ans\n \n\nuni = UnionFind(size=N+1)\n\nfor a, b in zip(a_list, b_list):\n a_group = uni.find(a)\n b_group = uni.find(b)\n \n if a_group == b_group:\n huben.append(huben[-1])\n else:\n huben.append(huben[-1] - uni.size(a)*uni.size(b))\n uni.unite(a, b)\n \nprint(huben[-1::-1])', 'import numpy as np\nN, M = map(int, input().split())\nab_list = [list(map(int, input().split())) for _ in range(M)]\n\na_list = np.array(ab_list)[:, 0][-1::-1]\nb_list = np.array(ab_list)[:, 1][-1::-1]\n\nhuben = [int((N) * (N-1) * 0.5)]\n\nclass UnionFind():\n def __init__(self, size):\n # self.group holds a list of idx for parant node of i-th node\n # if self.group[i] = i, i-th node is a root.\n # if self.group[i] = j, j-th node is a paranet of i-th node. \n self.group = [i for i in range(size)]\n \n # self.height holds a list of trees\' heights\n self.height = [0] * (size)\n \n def find(self, i):\n """\n Search for i-th node\'s group.\n Args : \n i : index of target node.\n Return :\n index of root of target node\n """ \n \n if self.group[i] == i:\n # if root\n return i\n else:\n # if not root, search by the parent\'s node\n self.group[i] = self.find(self.group[i])\n return self.group[i] \n \n def unite(self, i, j):\n """\n Unite two groups\n Args : \n i, j : indices of target nodes.\n """ \n # search roots\n i = self.find(i)\n j = self.find(j)\n \n if self.height[i] < self.height[j]:\n self.group[i] = j\n else:\n self.group[j] = i\n if self.height[i] == self.height[j]:\n self.height[i] += 1\n \n def size(self, i):\n """\n Get num of nodes in same group\n Args : \n i : index of target node.\n """\n ans = 1\n root = self.find(i)\n for par in self.group:\n if self.find(par) == root:\n ans += 1\n ans -= 1\n return ans\n \n\nuni = UnionFind(size=N+1)\n\nfor a, b in zip(a_list, b_list):\n a_group = uni.find(a)\n b_group = uni.find(b)\n \n if a_group == b_group:\n huben.append(huben[-1])\n else:\n huben.append(huben[-1] - uni.size(a)*uni.size(b))\n uni.unite(a, b)\n \nfor ans in huben[-1::-1]:\n print(ans)', 'class UnionFind():\n def __init__(self, size):\n # self.parent holds a list of idx for parant node of i-th node\n # if i-th node is a root, self.parent[i] holds the - (number of nodes) in the parent.\n # if self.parent[i] = j, j-th node is a paranet of i-th node. \n self.parent = [-1 for _ in range(size)]\n \n # self.height holds a list of trees\' heights\n self.height = [0] * (size)\n \n def find(self, i):\n """\n Search for i-th node\'s parent.\n Args : \n i : index of target node.\n Return :\n index of root of target node\n """ \n \n if self.parent[i] < 0:\n # root node\n return i\n else:\n # if not root, search by the parent\'s node\n self.parent[i] = self.find(self.parent[i])\n return self.parent[i] \n \n def unite(self, i, j):\n """\n Unite two parents\n Args : \n i, j : indices of target nodes.\n """ \n # search roots\n i = self.find(i)\n j = self.find(j)\n \n if self.height[i] < self.height[j]:\n self.parent[j] += self.parent[i]\n self.parent[i] = j\n \n else:\n self.parent[i] += self.parent[j]\n self.parent[j] = i\n if self.height[i] == self.height[j]:\n self.height[i] += 1\n \n def size(self, i):\n """\n Get num of nodes in same parent\n Args : \n i : index of target node.\n """\n return - self.find(i)\n \n \n\nimport numpy as np\nN, M = map(int, input().split())\nab_list = [list(map(int, input().split())) for _ in range(M)]\n\na_list = np.array(ab_list)[:, 0][-1::-1]\nb_list = np.array(ab_list)[:, 1][-1::-1]\n\nhuben = [int((N) * (N-1) * 0.5)]\n \nuni = UnionFind(size=N+1)\n\nfor a, b in zip(a_list, b_list):\n a_group = uni.find(a)\n b_group = uni.find(b)\n \n if a_group == b_group:\n huben.append(huben[-1])\n else:\n huben.append(huben[-1] - uni.size(a)*uni.size(b))\n uni.unite(a, b)\n \nfor ans in huben[-1::-1][1:]:\n print(ans)', 'class UnionFind():\n def __init__(self, size):\n # self.parent holds a list of idx for parant node of i-th node\n # if i-th node is a root, self.parent[i] holds the - (number of nodes) in the parent.\n # if self.parent[i] = j, j-th node is a paranet of i-th node. \n self.parent = [-1 for _ in range(size)]\n \n # self.height holds a list of trees\' heights\n self.height = [0] * (size)\n \n def find(self, i):\n """\n Search for i-th node\'s parent.\n Args : \n i : index of target node.\n Return :\n index of root of target node\n """ \n \n if self.parent[i] < 0:\n # root node\n return i\n else:\n # if not root, search by the parent\'s node\n self.parent[i] = self.find(self.parent[i])\n return self.parent[i] \n \n def unite(self, i_, j_):\n """\n Unite two parents\n Args : \n i, j : indices of target nodes.\n """ \n # search roots\n i = self.find(i_)\n j = self.find(j_)\n \n if self.height[i] < self.height[j]:\n self.parent[j] += self.parent[i]\n self.parent[i] = j_\n \n else:\n self.parent[i] += self.parent[j]\n self.parent[j] = i_\n if self.height[i] == self.height[j]:\n self.height[i] += 1\n \n def size(self, i):\n """\n Get num of nodes in same parent\n Args : \n i : index of target node.\n """\n return - self.parent[self.find(i)]\n \n \n\nimport numpy as np\nN, M = map(int, input().split())\nab_list = [list(map(int, input().split())) for _ in range(M)]\n\na_list = np.array(ab_list)[:, 0][-1::-1]\nb_list = np.array(ab_list)[:, 1][-1::-1]\n\nhuben = [int((N) * (N-1) * 0.5)]\n \nuni = UnionFind(size=N+1)\n\nfor a, b in zip(a_list, b_list):\n a_group = uni.find(a)\n b_group = uni.find(b)\n \n if a_group == b_group:\n huben.append(huben[-1])\n else:\n huben.append(huben[-1] - uni.size(a)*uni.size(b))\n uni.unite(a, b)\n \nfor ans in huben[-1::-1][1:]:\n print(ans)']
['Wrong Answer', 'Wrong Answer', 'Wrong Answer', 'Accepted']
['s215863187', 's447294863', 's849145881', 's252416621']
[44548.0, 46468.0, 44528.0, 47196.0]
[2111.0, 2111.0, 1631.0, 1399.0]
[2128, 2148, 2182, 2201]
p03108
u385244248
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["import sys\nimport math\nimport string\nimport fractions\nimport random\nfrom operator import itemgetter\nimport itertools\nfrom collections import deque\nimport copy\nimport heapq\nimport bisect\nfrom scipy.misc import comb\n\nMOD = 10 ** 9 + 7\nINF = float('inf')\ninput = lambda: sys.stdin.readline().strip()\n\n\nclass UnionFind:\n def __init__(self, n):\n self.n = n\n self.parents = [-1] * n\n\n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return self.parents[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n\n if self.parents[x] > self.parents[y]:\n x, y = y, x\n\n self.parents[x] += self.parents[y]\n self.parents[y] = x\n\n def size(self, x):\n return -self.parents[self.find(x)]\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def members(self, x):\n root = self.find(x)\n return [i for i in range(self.n) if self.find(i) == root]\n\n def roots(self):\n return [i for i, x in enumerate(self.parents) if x < 0]\n\n def group_count(self):\n return len(self.roots())\n\n def all_group_members(self):\n return {r: self.members(r) for r in self.roots()}\n\n def __str__(self):\n return '\\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())\n\n\nN, M = map(int, input().split())\nAB = [list(map(int, input().split())) for _ in range(M)]\nuf = UnionFind(N)\nans = deque()\nans.appendleft(comb(N, 2))\nfor i in range(M - 1, 0, -1):\n size1 = uf.size(AB[i][0] - 1)\n size2 = uf.size(AB[i][1] - 1)\n if size1 == N:\n ans.appendleft(0)\n else:\n uf.union(AB[i][0] - 1, AB[i][1] - 1)\n if size1 + size2 == 2:\n ans.appendleft(ans[0] - 1)\n elif size1 == 1:\n ans.appendleft(ans[0] - (comb(size1 + size2, 2) - comb(size2, 2)))\n elif size2 == 1:\n ans.appendleft(ans[0] - (comb(size1 + size2, 2) - comb(size1, 2)))\n else:\n ans.appendleft(ans[0] - (comb(size1 + size2, 2) - comb(size1, 2) - comb(size2, 2)))\nfor i in ans:\n print(int(i))\n", "import sys\nimport math\nimport string\nimport fractions\nimport random\nfrom operator import itemgetter\nimport itertools\nfrom collections import deque\nimport copy\nimport heapq\nimport bisect\nfrom scipy.misc import comb\n\nMOD = 10 ** 9 + 7\nINF = float('inf')\ninput = lambda: sys.stdin.readline().strip()\n\n\nclass UnionFind:\n def __init__(self, n):\n self.n = n\n self.parents = [-1] * n\n\n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return self.parents[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n\n if self.parents[x] > self.parents[y]:\n x, y = y, x\n\n self.parents[x] += self.parents[y]\n self.parents[y] = x\n\n def size(self, x):\n return -self.parents[self.find(x)]\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def members(self, x):\n root = self.find(x)\n return [i for i in range(self.n) if self.find(i) == root]\n\n def roots(self):\n return [i for i, x in enumerate(self.parents) if x < 0]\n\n def group_count(self):\n return len(self.roots())\n\n def all_group_members(self):\n return {r: self.members(r) for r in self.roots()}\n\n def __str__(self):\n return '\\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())\n\n\nN, M = map(int, input().split())\nAB = [list(map(int, input().split())) for _ in range(M)]\nuf = UnionFind(N)\nans = deque()\nans.appendleft(comb(N, 2))\nfor i in range(M - 1, 0, -1):\n size1 = uf.size(AB[i][0] - 1)\n size2 = uf.size(AB[i][1] - 1)\n uf.union(AB[i][0] - 1, AB[i][1] - 1)\n if size1 + size2 == 2:\n ans.appendleft(ans[0] - 1)\n elif size1 == 1:\n ans.appendleft(ans[0] - (comb(size1 + size2, 2) - comb(size2, 2)))\n elif size2 == 1:\n ans.appendleft(ans[0] - (comb(size1 + size2, 2) - comb(size1, 2)))\n else:\n ans.appendleft(ans[0] - (comb(size1 + size2, 2) - comb(size1, 2) - comb(size2, 2)))\nfor i in ans:\n print(max(0, int(i)))\n", "import sys\nimport math\nimport string\nimport fractions\nimport random\nfrom operator import itemgetter\nimport itertools\nfrom collections import deque\nimport copy\nimport heapq\nimport bisect\nfrom scipy.misc import comb\n\nMOD = 10 ** 9 + 7\nINF = float('inf')\ninput = lambda: sys.stdin.readline().strip()\n\n\nclass UnionFind:\n def __init__(self, n):\n self.n = n\n self.parents = [-1] * n\n\n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return self.parents[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n\n if self.parents[x] > self.parents[y]:\n x, y = y, x\n\n self.parents[x] += self.parents[y]\n self.parents[y] = x\n\n def size(self, x):\n return -self.parents[self.find(x)]\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def members(self, x):\n root = self.find(x)\n return [i for i in range(self.n) if self.find(i) == root]\n\n def roots(self):\n return [i for i, x in enumerate(self.parents) if x < 0]\n\n def group_count(self):\n return len(self.roots())\n\n def all_group_members(self):\n return {r: self.members(r) for r in self.roots()}\n\n def __str__(self):\n return '\\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())\n\n\nN, M = map(int, input().split())\nAB = [list(map(int, input().split())) for _ in range(M)]\nuf = UnionFind(N)\nans = deque()\nans.appendleft(comb(N, 2))\nfor i in range(M - 1, 0, -1):\n size1 = uf.size(AB[i][0] - 1)\n size2 = uf.size(AB[i][1] - 1)\n if size1 == N:\n ans.appendleft(0)\n else:\n uf.union(AB[i][0] - 1, AB[i][1] - 1)\n size3 = uf.size(AB[i][0] - 1)\n if size1 + size2 == 2:\n ans.appendleft(ans[0] - 1)\n elif size1 == 1:\n ans.appendleft(ans[0] - (comb(size3, 2) - comb(size2, 2)))\n elif size2 == 1:\n ans.appendleft(ans[0] - (comb(size3, 2) - comb(size1, 2)))\n else:\n ans.appendleft(ans[0] - (comb(size3, 2) - comb(size1, 2) - comb(size2, 2)))\nfor i in ans:\n print(int(i))\n", "import sys\nimport math\nimport string\nimport fractions\nimport random\nfrom operator import itemgetter\nimport itertools\nfrom collections import deque\nimport copy\nimport heapq\nimport bisect\nfrom scipy.misc import comb\n\nMOD = 10 ** 9 + 7\nINF = float('inf')\ninput = lambda: sys.stdin.readline().strip()\n\n\nclass UnionFind:\n def __init__(self, n):\n self.n = n\n self.parents = [-1] * n\n\n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return self.parents[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n\n if self.parents[x] > self.parents[y]:\n x, y = y, x\n\n self.parents[x] += self.parents[y]\n self.parents[y] = x\n\n def size(self, x):\n return -self.parents[self.find(x)]\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def members(self, x):\n root = self.find(x)\n return [i for i in range(self.n) if self.find(i) == root]\n\n def roots(self):\n return [i for i, x in enumerate(self.parents) if x < 0]\n\n def group_count(self):\n return len(self.roots())\n\n def all_group_members(self):\n return {r: self.members(r) for r in self.roots()}\n\n def __str__(self):\n return '\\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())\n\n\nN, M = map(int, input().split())\nAB = [list(map(int, input().split())) for _ in range(M)]\nuf = UnionFind(N)\nans = deque()\nans.appendleft(comb(N, 2))\nfor i in range(M - 1, 0, -1):\n size1 = uf.size(AB[i][0] - 1)\n size2 = uf.size(AB[i][1] - 1)\n if size1 == N:\n ans.appendleft(0)\n elif uf.same(AB[i][0], AB[i][1]):\n ans.appendleft(ans[0])\n else:\n uf.union(AB[i][0] - 1, AB[i][1] - 1)\n size3 = uf.size(AB[i][0] - 1)\n if size1 + size2 == 2:\n ans.appendleft(ans[0] - 1)\n elif size1 == 1:\n ans.appendleft(ans[0] - (comb(size3, 2) - comb(size2, 2)))\n elif size2 == 1:\n ans.appendleft(ans[0] - (comb(size3, 2) - comb(size1, 2)))\n else:\n ans.appendleft(ans[0] - (comb(size3, 2) - comb(size1, 2) - comb(size2, 2)))\nfor i in ans:\n print(int(i))\n", "import sys\nimport math\nimport string\nimport fractions\nimport random\nfrom operator import itemgetter\nimport itertools\nfrom collections import deque\nimport copy\nimport heapq\nimport bisect\nfrom scipy.misc import comb\n\nMOD = 10 ** 9 + 7\nINF = float('inf')\ninput = lambda: sys.stdin.readline().strip()\n\n\nclass UnionFind:\n def __init__(self, n):\n self.n = n\n self.parents = [-1] * n\n\n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return self.parents[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n\n if self.parents[x] > self.parents[y]:\n x, y = y, x\n\n self.parents[x] += self.parents[y]\n self.parents[y] = x\n\n def size(self, x):\n return -self.parents[self.find(x)]\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def members(self, x):\n root = self.find(x)\n return [i for i in range(self.n) if self.find(i) == root]\n\n def roots(self):\n return [i for i, x in enumerate(self.parents) if x < 0]\n\n def group_count(self):\n return len(self.roots())\n\n def all_group_members(self):\n return {r: self.members(r) for r in self.roots()}\n\n def __str__(self):\n return '\\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())\n\n\nN, M = map(int, input().split())\nAB = [list(map(int, input().split())) for _ in range(M)]\nuf = UnionFind(N)\nans = deque()\nans.appendleft(int(comb(N, 2)))\nfor i in range(M - 1, 0, -1):\n size1 = uf.size(AB[i][0] - 1)\n size2 = uf.size(AB[i][1] - 1)\n if size1 == N:\n ans.appendleft(0)\n elif uf.same(AB[i][0]-1, AB[i][1]-1):\n ans.appendleft(ans[0])\n else:\n uf.union(AB[i][0] - 1, AB[i][1] - 1)\n size3 = uf.size(AB[i][0] - 1)\n if size1 + size2 == 2:\n ans.appendleft(ans[0] - 1)\n elif size1 == 1:\n ans.appendleft(ans[0] - (int(comb(size3, 2)) - int(comb(size2, 2))))\n elif size2 == 1:\n ans.appendleft(ans[0] - (int(comb(size3, 2)) - int(comb(size1, 2))))\n else:\n ans.appendleft(ans[0] - (int(comb(size3, 2)) - int(comb(size1, 2)) - int(comb(size2, 2))))\nfor i in ans:\n print(i)\n", "import sys\nimport math\nimport string\nimport fractions\nimport random\nfrom operator import itemgetter\nimport itertools\nfrom collections import deque\nimport copy\nimport heapq\nimport bisect\nfrom scipy.special import comb\n\nMOD = 10 ** 9 + 7\nINF = float('inf')\ninput = lambda: sys.stdin.readline().strip()\n\n\nclass UnionFind:\n def __init__(self, n):\n self.n = n\n self.parents = [-1] * n\n\n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return self.parents[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n\n if self.parents[x] > self.parents[y]:\n x, y = y, x\n\n self.parents[x] += self.parents[y]\n self.parents[y] = x\n\n def size(self, x):\n return -self.parents[self.find(x)]\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def members(self, x):\n root = self.find(x)\n return [i for i in range(self.n) if self.find(i) == root]\n\n def roots(self):\n return [i for i, x in enumerate(self.parents) if x < 0]\n\n def group_count(self):\n return len(self.roots())\n\n def all_group_members(self):\n return {r: self.members(r) for r in self.roots()}\n\n def __str__(self):\n return '\\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())\n\n\nN, M = map(int, input().split())\nAB = [list(map(int, input().split())) for _ in range(M)]\nuf = UnionFind(N)\nans = deque()\nans.appendleft(comb(N, 2))\nfor i in range(M - 1, 0, -1):\n size1 = uf.size(AB[i][0] - 1)\n size2 = uf.size(AB[i][1] - 1)\n if size1 == N:\n ans.appendleft(0)\n elif uf.same(AB[i][0], AB[i][1]):\n ans.appendleft(ans[0])\n else:\n uf.union(AB[i][0] - 1, AB[i][1] - 1)\n size3 = uf.size(AB[i][0] - 1)\n if size1 + size2 == 2:\n ans.appendleft(ans[0] - 1)\n elif size1 == 1:\n ans.appendleft(ans[0] - (comb(size3, 2) - comb(size2, 2)))\n elif size2 == 1:\n ans.appendleft(ans[0] - (comb(size3, 2) - comb(size1, 2)))\n else:\n ans.appendleft(ans[0] - (comb(size3, 2) - comb(size1, 2) - comb(size2, 2)))\nfor i in ans:\n print(int(i))\n", "import sys\nimport math\nimport string\nimport fractions\nimport random\nfrom operator import itemgetter\nimport itertools\nfrom collections import deque\nimport copy\nimport heapq\nimport bisect\nfrom scipy.misc import comb\n\nMOD = 10 ** 9 + 7\nINF = float('inf')\ninput = lambda: sys.stdin.readline().strip()\n\n\nclass UnionFind:\n def __init__(self, n):\n self.n = n\n self.parents = [-1] * n\n\n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return self.parents[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n\n if self.parents[x] > self.parents[y]:\n x, y = y, x\n\n self.parents[x] += self.parents[y]\n self.parents[y] = x\n\n def size(self, x):\n return -self.parents[self.find(x)]\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def members(self, x):\n root = self.find(x)\n return [i for i in range(self.n) if self.find(i) == root]\n\n def roots(self):\n return [i for i, x in enumerate(self.parents) if x < 0]\n\n def group_count(self):\n return len(self.roots())\n\n def all_group_members(self):\n return {r: self.members(r) for r in self.roots()}\n\n def __str__(self):\n return '\\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())\n\n\nN, M = map(int, input().split())\nAB = [list(map(int, input().split())) for _ in range(M)]\nuf = UnionFind(N)\nans = deque()\nans.appendleft(comb(N, 2))\nfor i in range(M - 1, 0, -1):\n size1 = uf.size(AB[i][0] - 1)\n size2 = uf.size(AB[i][1] - 1)\n if size1 == N:\n ans.appendleft(0)\n elif uf.same(AB[i][0]-1, AB[i][1]-1):\n ans.appendleft(ans[0])\n else:\n uf.union(AB[i][0] - 1, AB[i][1] - 1)\n size3 = uf.size(AB[i][0] - 1)\n if size1 + size2 == 2:\n ans.appendleft(ans[0] - 1)\n elif size1 == 1:\n ans.appendleft(ans[0] - (comb(size3, 2) - comb(size2, 2)))\n elif size2 == 1:\n ans.appendleft(ans[0] - (comb(size3, 2) - comb(size1, 2)))\n else:\n ans.appendleft(ans[0] - (comb(size3, 2) - comb(size1, 2) - comb(size2, 2)))\nfor i in ans:\n print(int(i))\n", "import sys\nimport math\nimport string\nimport fractions\nimport random\nfrom operator import itemgetter\nimport itertools\nfrom collections import deque\nimport copy\nimport heapq\nimport bisect\n\nMOD = 10 ** 9 + 7\nINF = float('inf')\ninput = lambda: sys.stdin.readline().strip()\n\n\nclass UnionFind:\n def __init__(self, n):\n self.n = n\n self.parents = [-1] * n\n\n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return self.parents[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n\n if self.parents[x] > self.parents[y]:\n x, y = y, x\n\n self.parents[x] += self.parents[y]\n self.parents[y] = x\n\n def size(self, x):\n return -self.parents[self.find(x)]\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def members(self, x):\n root = self.find(x)\n return [i for i in range(self.n) if self.find(i) == root]\n\n def roots(self):\n return [i for i, x in enumerate(self.parents) if x < 0]\n\n def group_count(self):\n return len(self.roots())\n\n def all_group_members(self):\n return {r: self.members(r) for r in self.roots()}\n\n def __str__(self):\n return '\\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())\n\n\ndef comb(n, k):\n if n % 2 == 0:\n return n / 2 * (n - 1)\n else:\n return (n - 1) / 2 * n\n\n\nN, M = map(int, input().split())\nAB = [list(map(int, input().split())) for _ in range(M)]\nuf = UnionFind(N)\nans = deque()\nans.appendleft(int(comb(N, 2)))\nfor i in range(M - 1, 0, -1):\n size1 = uf.size(AB[i][0] - 1)\n size2 = uf.size(AB[i][1] - 1)\n if size1 == N:\n ans.appendleft(0)\n elif uf.same(AB[i][0] - 1, AB[i][1] - 1):\n ans.appendleft(ans[0])\n else:\n uf.union(AB[i][0] - 1, AB[i][1] - 1)\n size3 = uf.size(AB[i][0] - 1)\n if size1 + size2 == 2:\n ans.appendleft(ans[0] - 1)\n elif size1 == 1:\n ans.appendleft(ans[0] - (int(comb(size3, 2)) - int(comb(size2, 2))))\n elif size2 == 1:\n ans.appendleft(ans[0] - (int(comb(size3, 2)) - int(comb(size1, 2))))\n else:\n ans.appendleft(ans[0] - (int(comb(size3, 2)) - int(comb(size1, 2)) - int(comb(size2, 2))))\nfor i in ans:\n print(i)\n"]
['Wrong Answer', 'Wrong Answer', 'Wrong Answer', 'Runtime Error', 'Wrong Answer', 'Runtime Error', 'Wrong Answer', 'Accepted']
['s013361610', 's191341340', 's258829073', 's313564470', 's458632560', 's718868344', 's842036944', 's160849549']
[41288.0, 41396.0, 40664.0, 41388.0, 42080.0, 15976.0, 40644.0, 37064.0]
[2111.0, 2115.0, 2111.0, 2110.0, 2112.0, 210.0, 2110.0, 930.0]
[2212, 2129, 2226, 2295, 2334, 2298, 2299, 2419]
p03108
u390694622
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['N,M=map(int,input().split())\nunionfindTree = [i for i in range(N)]\nsize = [1 for _ in range(N)]\ninconvenience = N*(N-1)//2\nanswers = [inconvenience] \n#answers[i] should be inconvenience after M-i bridges destroyed.\n\ndef root(x):\n p = unionfindTree[x]\n if x==p:\n return(p)\n else:\n unionfindTree[x] = root(p)\n # size[x] = size[p]\n return(unionfindTree[x])\n\ndef getsize(x):\n r = root(x)\n return(size[r])\n\ndef union(x,y):\n rx = root(x)\n ry = root(y)\n convenience = 0\n if rx != ry:\n unionfindTree[ry] = rx\n sx = size[rx]\n sy = size[ry]\n convenience = sx * sy\n size[rx] = sx+sy\n return(convenience)\n\nrevBridges = []\nfor _ in range(M):\n a,b = map(int,input().split())\n revBridges.append((a-1,b-1))\nrevBridges.reverse()\n\nfor i in range(M):\n a,b = revBridges[i]\n c = union(a,b)\n inconvenience -= c\n answers.append(inconvenience)\n # print("link",(a,b))\n # print(unionfindTree)\n \n\n', 'N,M=map(int,input().split())\nunionfindTree = [i for i in range(N)]\nsize = [1 for _ in range(N)]\ninconvenience = N*(N-1)//2\nanswers = [inconvenience] \n#answers[i] should be inconvenience after M-i bridges destroyed.\n\ndef root(x):\n p = unionfindTree[x]\n if x==p:\n return(p)\n else:\n r = root(p)\n unionfindTree[x] = r\n return(r)\n\ndef union(x,y):\n rx = root(x)\n ry = root(y)\n convenience = 0\n if rx != ry:\n # unionfindTree[ry] = rx\n sx = size[rx]\n sy = size[ry]\n convenience = sx * sy\n size[rx] = sx+sy\n return(convenience)\n\nrevBridges = []\nfor _ in range(M):\n a,b = map(int,input().split())\n revBridges.append((a-1,b-1))\nrevBridges.reverse()\n\nfor (a,b) in revBridges:\n c = union(a,b)\n inconvenience -= c\n answers.append(inconvenience)\nfor i in range(M):\n print(answers[M-1-i])', 'N,M=map(int,input().split())\nunionfindTree = [i for i in range(N)]\nsize = [1 for _ in range(N)]\ninconvenience = N*(N-1)//2\nanswers = [inconvenience] \n#answers[i] should be inconvenience after M-i bridges destroyed.\n\ndef root(x):\n p = unionfindTree[x]\n if x==p:\n return(p)\n else:\n root = root(p)\n unionfindTree[x] = root\n return(root)\n\ndef getsize(x):\n r = root(x)\n return(size[r])\n\ndef union(x,y):\n rx = root(x)\n ry = root(y)\n convenience = 0\n if rx != ry:\n unionfindTree[ry] = rx\n sx = size[rx]\n sy = size[ry]\n convenience = sx * sy\n size[rx] = sx+sy\n return(convenience)\n\nrevBridges = []\nfor _ in range(M):\n a,b = map(int,input().split())\n revBridges.append((a-1,b-1))\nrevBridges.reverse()\n\nfor (a,b) in revBridges:\n c = union(a,b)\n inconvenience -= c\n answers.append(inconvenience)\n\n # print(answers[M-1-i])', 'N,M=map(int,input().split())\nunionfindTree = [i for i in range(N)]\nsize = [1 for _ in range(N)]\ninconvenience = N*(N-1)//2\nanswers = [inconvenience] \n#answers[i] should be inconvenience after M-i bridges destroyed.\n\ndef root(x):\n p = unionfindTree[x]\n if x==p:\n return(p)\n else:\n r = root(p)\n unionfindTree[x] = r\n return(r)\n\ndef getsize(x):\n r = root(x)\n return(size[r])\n\ndef union(x,y):\n rx = root(x)\n ry = root(y)\n convenience = 0\n if rx != ry:\n unionfindTree[ry] = rx\n # sx = size[rx]\n # sy = size[ry]\n # convenience = sx * sy\n # size[rx] = sx+sy\n return(convenience)\n\nrevBridges = []\nfor _ in range(M):\n a,b = map(int,input().split())\n revBridges.append((a-1,b-1))\nrevBridges.reverse()\n\nfor (a,b) in revBridges:\n c = union(a,b)\n inconvenience -= c\n answers.append(inconvenience)\nfor i in range(M):\n print(answers[M-1-i])', 'N,M = map(int,input().split())\nedges = []\nfor _ in range(M):\n a,b = map(int,input().split())\n edges.append((a-1,b-1))\nedges.reverse()\nparents = list(range(N))\nsize = [1 for _ in range(N)]\nrank = [0 for _ in range(N)]\n\ndef root(x):\n px = parents[x]\n if px == x:\n return(x)\n else:\n r = root(px)\n parents[x] = r\n return(r)\n\ndef same(x,y):\n return(root(x) == root(y))\n\ndef union(x,y):\n rx = root(x)\n ry = root(y)\n if rx == ry:\n return(0)\n if rank[ry] > rank[rx]:\n parents[rx] = ry\n size[ry] += size[rx]\n else:\n parents[ry] = rx\n size[rx] += size[ry]\n if rank[rx] == rank[ry]:\n rank[rx] += 1\n return(1)\n\ninconvinience = N*(N-1)//2\nanswers=[inconvinience]\n\nfor edge in edges:\n x,y = edge\n rx = root(x)\n ry = root(y)\n sx = size[rx]\n sy = size[ry]\n res = union(rx,ry)\n if res == 1:\n inconvinience -= sx*sy\n answers.append(inconvinience)\n\nanswers.reverse()\n\nfor answer in answers[1:]:\n print(answer)']
['Runtime Error', 'Wrong Answer', 'Runtime Error', 'Runtime Error', 'Accepted']
['s205058922', 's356883815', 's736347082', 's922170507', 's317164559']
[25820.0, 28216.0, 21352.0, 25064.0, 24868.0]
[502.0, 520.0, 324.0, 558.0, 616.0]
[1049, 897, 961, 956, 1041]
p03108
u390958150
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['import numpy as np\n\nn,m = [int(i) for i in input().split()]\na = np.array([0]*m)\nb = np.array([0]*m)\nans = np.array([0]*m)\nans[m - 1] = n * (n - 1) / 2\n\nfor i in range(m):\n\ta[i],b[i] = [int(i) for i in input().split()]\n\nclass UnionFind:\n def __init__(self, n):\n \n \n self.par = [-1 for i in range(n+1)]\n self.rank = [0] * (n+1)\n\n \n def find(self, x):\n if self.par[x] < 0:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n \n def size(self, x):\n return -self.par[self.find(x)]\n\n \n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x == y:\n return \n if self.size(x) < self.size(y):\n x , y = y , x\n self.par[x] += self.par[y]\n self.par[y] = x\n def same_check(self, x, y):\n return self.find(x) == self.find(y)\n\nuni = UnionFind(n)\n\nfor i in range(m-1,0,-1):\n x,y = a[i]-1,b[i]-1\n if uni.same_check(x,y):\n ans[i-1] = ans[i]\n else:\n ans[i-1] -= uni.size(x)*uni.size(y)\n \n uni.union(x,y)\n\nfor i in ans:\n print(i)', 'class UnionFind:\n def __init__(self, n):\n self.par = [i for i in range(n+1)]\n self.rank = [0] * (n+1)\n self.size = [1 for _ in range(n+1)]\n\n \n def find(self, x):\n if self.par[x] == x:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n\n \n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if self.rank[x] < self.rank[y]:\n self.par[x] = y\n self.size[y] += self.size[x]\n else:\n self.par[y] = x\n self.size[x] += self.size[y]\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n \n def same_check(self, x, y):\n return self.find(x) == self.find(y)\n \n \ndef main():\n N, M = [int(_) for _ in input().split()]\n bridge = [tuple(map(int, input().split())) for _ in range(M)]\n\n last = []\n last.append(int(N * (N-1) * 0.5))\n\n ins = UnionFind(N)\n last_temp = 0\n\n for i in range(M-1):\n #print(ins.par)\n if ins.same_check(bridge[M-1-i][0],bridge[M-1-i][1]):\n last.append(last[-1])\n #ins.union(bridge[M-1-i][0],bridge[M-1-i][1])\n else:\n last_temp = last[-1] - (ins.size[ins.find(bridge[M-1-i][0])]) * (ins.size[ins.find(bridge[M-1-i][1])])\n #print(ins.size,i)\n last.append(last_temp)\n ins.union(bridge[M-1-i][0],bridge[M-1-i][1])\n \n [print(last[M-i-1]) for i in range(M)]\n exit()\n \nmain()']
['Wrong Answer', 'Accepted']
['s319771628', 's237892616']
[18180.0, 24956.0]
[1917.0, 822.0]
[1305, 1577]
p03108
u391731808
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['N,M = map(int,input().split())\nA = np.zeros(M,dtype=int)\nB = np.zeros(M,dtype=int)\nfor i in range(M):\n A[i],B[i] = map(lambda x:int(x)-1,input().split())\n\n\n%%timeit\ndef check_connect(A,B):\n a = cnt[A]\n b = cnt[B]\n if a==-1 and b==-1:\n connect.append([A,B])\n cnt[A] = len(connect)-1\n cnt[B] = cnt[A]\n return 1\n if a == b:\n return 0\n if a==-1 or b==-1:\n if a==-1:\n a = b\n A,B = B,A\n connect[a] += [B]\n cnt[B] = a\n return len(connect[a])-1\n \n tmp1 = len(connect[a])\n tmp2 = len(connect[b])\n if tmp2 > tmp1:\n a,b = b,a\n connect[a] = connect[a] + connect[b]\n for i in connect[b]:\n cnt[i] = a\n tmp3 = tmp1+tmp2\n connect[b] = []\n return (tmp3*(tmp3-1)-tmp1*(tmp1-1)-tmp2*(tmp2-1))//2\n\nconnect = []\ncnt = [-1]*N\ndfubensa = [0]*M\nfor i in range(M-1,-1,-1):\n dfubensa[i] = check_connect(A[i],B[i])\n\nfubensa = dfubensa[0]\nprint(fubensa)\nfor i in range(1,M):\n fubensa = fubensa+ dfubensa[i]\n print(fubensa)', 'N,M = map(int,input().split())\nA = [0]*M\nB = [0]*M\nfor i in range(M):\n A[i],B[i] = map(lambda x:int(x)-1,input().split())\n\ndef check_connect(A,B):\n a = cnt[A]\n b = cnt[B]\n if a==-1 and b==-1:\n connect.append([A,B])\n cnt[A] = len(connect)-1\n cnt[B] = cnt[A]\n return 1\n if a == b:\n return 0\n if a==-1:\n a,b = b,a\n A,B = B,A\n if b==-1:\n connect[a] += [B]\n cnt[B] = a\n return len(connect[a])-1\n \n tmp1 = len(connect[a])\n tmp2 = len(connect[b])\n if tmp2 > tmp1:\n a,b = b,a\n connect[a] = connect[a] + connect[b]\n for i in connect[b]:\n cnt[i] = a\n tmp3 = tmp1+tmp2\n connect[b] = []\n return (tmp3*(tmp3-1)-tmp1*(tmp1-1)-tmp2*(tmp2-1))//2\n\nconnect = []\ncnt = -np.ones(N,dtype=int)\ndfubensa = np.zeros(M,dtype=int)\nfor i in range(M-1,-1,-1):\n dfubensa[i] = check_connect(A[i],B[i])\n\nfubensa = dfubensa[0]\nprint(fubensa)\nfor i in range(1,M):\n fubensa = fubensa+ dfubensa[i]\n print(fubensa)', 'N,M = map(int,input().split())\nA = np.zeros(M,dtype=int)\nB = np.zeros(M,dtype=int)\nfor i in range(M):\n A[i],B[i] = map(lambda x:int(x)-1,input().split())\n\n\ndef check_connect(A,B):\n a = cnt[A]\n b = cnt[B]\n if a==-1 and b==-1:\n connect.append([A,B])\n cnt[A] = len(connect)-1\n cnt[B] = cnt[A]\n return 1\n if a == b:\n return 0\n if a==-1 or b==-1:\n if a==-1:\n a = b\n A,B = B,A\n connect[a] += [B]\n cnt[B] = a\n return len(connect[a])-1\n \n tmp1 = len(connect[a])\n tmp2 = len(connect[b])\n if tmp2 > tmp1:\n a,b = b,a\n connect[a] = connect[a] + connect[b]\n for i in connect[b]:\n cnt[i] = a\n tmp3 = tmp1+tmp2\n connect[b] = []\n return (tmp3*(tmp3-1)-tmp1*(tmp1-1)-tmp2*(tmp2-1))//2\n\nconnect = []\ncnt = [-1]*N\ndfubensa = [0]*M\nfor i in range(M-1,-1,-1):\n dfubensa[i] = check_connect(A[i],B[i])\n\nfubensa = dfubensa[0]\nprint(fubensa)\nfor i in range(1,M):\n fubensa = fubensa+ dfubensa[i]\n print(fubensa)\n', 'import sys\nsys.setrecursionlimit(10**7)\n\nN,M = map(int,input().split())\nAB = [list(map(int,input().split())) for _ in [0]*M]\n\nclass UnionFind:\n def __init__(self,N):\n self.Parent = list(range(N))\n self.size = [1]*N\n def get_Parent(self,n):\n if self.Parent[n] == n:return n\n p = self.get_Parent(self.Parent[n])\n self.Parent[n] = p\n return p\n def get_size(self,n):\n return self.size[self.get_Parent(n)]\n def merge(self,x,y):\n x = self.get_Parent(x)\n y = self.get_Parent(y)\n if x!=y:\n self.size[x] += self.size[y]\n self.Parent[y] = x\n return True\n return False\n def is_united(self,x,y):\n return self.get_Parent(x)==self.get_Parent(y)\n\nans = [0]\nT = UnionFind(N)\nfor a,b in AB[::-1]:\n a-=1;b-=1\n ans.append(ans[-1])\n A = T.get_size(a)\n B = T.get_size(b)\n if T.merge(a,b):\n C = T.get_size(a)\n ans[-1] += (C*(C-1)-A*(A-1)-B*(B-1))//2\n\na0 = ans[-1]\nfor o in ans[-2::-1]:print(a0-o)']
['Runtime Error', 'Runtime Error', 'Runtime Error', 'Accepted']
['s557617127', 's589282770', 's866257493', 's051948605']
[3064.0, 10996.0, 3188.0, 48800.0]
[18.0, 335.0, 18.0, 866.0]
[1051, 1023, 1043, 1035]
p03108
u392319141
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['class UnionFind :\n def __init__(self, size) :\n self.rootList = list(range(size))\n self.height = [0] * size\n\n def getRootList(self) :\n return self.rootList\n\n def root(self, index) :\n if self.rootList[index] == index : \n return index\n rootIndex = self.root(self.rootList[index]) \n self.rootList[index] = rootIndex \n return rootIndex\n\n def union(self, index1, index2) : \n root1 = self.root(index1)\n root2 = self.root(index2)\n\n if root1 == root2 :\n return\n\n if self.height[root1] < self.height[root2] :\n self.rootList[root1] = root2\n else :\n self.rootList[root2] = root1\n if self.height[root1] == self.height[root2] :\n self.height[root1] += 1\n return\n\n def isSameRoot(self, index1, index2) :\n return self.root(index1) == self.root(index2)\n\n def sizeOfSameRoot(self, index) :\n root = self.root(index)\n ret = 0\n for parent in self.rootList :\n if root == self.root(parent) :\n ret += 1\n return ret\n\nN , M = map(int,input().split())\n\ntree = UnionFind(N)\n\nedge = [0] * M\n\nfor i in range(M) :\n s , t = map(int,input().split())\n edge.append((s-1,t-1))\n\nans = [0] * (M + 1)\nans[-1] = (N * (N - 1)) // 2\n\nfor i in range(2,M+1) :\n s , t = edge[-i+1]\n\n count1 = tree.sizeOfSameRoot(s)\n count2 = tree.sizeOfSameRoot(t)\n\n ans[-i] = ans[-i+1] - count1 * count2\n tree.union(s,t)\n\nans.pop(0)\n\nfor a in ans :\n print(a)', "class UnionFind :\n def __init__(self, size) :\n self.parent = list(range(size))\n self.height = [0] * size\n self.size = [1] * size\n self.component = size\n\n def root(self, index) :\n if self.parent[index] == index : \n return index\n rootIndex = self.root(self.parent[index]) \n self.parent[index] = rootIndex \n return rootIndex\n\n def union(self, index1, index2) : \n root1 = self.root(index1)\n root2 = self.root(index2)\n\n if root1 == root2 : \n return\n\n self.component -= 1 \n\n if self.height[root1] < self.height[root2] :\n self.parent[root1] = root2 \n self.size[root2] += self.size[root1]\n else :\n self.parent[root2] = root1 \n self.size[root1] += self.size[root2]\n if self.height[root1] == self.height[root2] :\n self.height[root1] += 1\n return\n\n def isSameRoot(self, index1, index2) :\n return self.root(index1) == self.root(index2)\n\n def sizeOfSameRoot(self, index) :\n return self.size[self.root(index)]\n\n def getComponent(self) :\n return self.component\n\nN, M = map(int, input().split())\nAB = [tuple(map(lambda a: int(a) - 1, input().split())) for _ in range(M)]\n\ntree = UnionFind(N)\nnow = N * (N - 1) // 2\n\nans = [now]\nfor a, b in AB[::-1]:\n if not tree.isSameRoot(a, b):\n now -= tree.sizeOfSameRoot(a) * tree.sizeOfSameRoot(b)\n tree.union(a, b)\n ans.append(now)\nprint(*ans[::-1][1:], sep='\\n')"]
['Wrong Answer', 'Accepted']
['s696371012', 's640833669']
[24736.0, 26228.0]
[2105.0, 800.0]
[1638, 1708]
p03108
u405660020
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['import sys\nsys.setrecursionlimit(10**7)\n\nn, m = map(int, input().split())\n\npar = [i for i in range(n+1)]\nrank = [0]*(n+1)\nsize = [1]*(n+1)\n\ndef root(x):\n if par[x] == x:\n return x\n else:\n par[x] = root(par[x])\n return par[x]\n\ndef same(x, y):\n return root(x) == root(y)\n\ndef unite(x, y):\n xx = root(x)\n yy = root(y)\n if xx == yy:\n return\n if rank[xx] < rank[yy]:\n par[xx] = yy\n size[yy] += size[xx]\n else:\n par[yy] = xx\n size[xx] += size[yy]\n if rank[xx] == rank[yy]:\n rank[yy] += 1\n\nab=[list(map(int, input().split())) for _ in range(m)]\n\nans=[0]*m\nans[m-1]=n*(n-1)//2\n\nfor i in range(M-1, 0, -1):\n a = ab[i][0]\n b = ab[i][1]\n if root(a) != root(b):\n ans[i-1] = ans[i] - size[root(a)]*size[root(b)]\n else:\n ans[i-1] = ans[i]\n unite(a,b)\n\nfor ans_i in ans:\n print(ans_i)\n', "import sys\nsys.setrecursionlimit(10**7)\n\n\nclass UnionFind():\n def __init__(self, n):\n self.n = n\n self.parents = [-1]*n\n\n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return self.parents[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x == y:\n return\n if self.parents[x] > self.parents[y]:\n x, y = y, x\n self.parents[x] += self.parents[y]\n self.parents[y] = x\n\n def size(self, x):\n return -self.parents[self.find(x)]\n\n def is_same(self, x, y):\n return self.find(x) == self.find(y)\n\n def roots(self):\n return [i for i, x in enumerate(self.parents) if x < 0]\n\n def members(self, x):\n root = self.find(x)\n return [i for i in range(self.n) if self.find(i) == root]\n\n def group_count(self):\n return len(self.roots())\n\n def all_group_members(self):\n return {r: self.members(r) for r in self.roots()}\n\n def __str__(self):\n return '\\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())\n\n\nn, m = map(int, input().split())\nab=[list(map(int, input().split())) for _ in range(m)]\n\nuf = UnionFind(n)\ntmp_ans = n*(n-1)//2\nans = []\nfor i in range(m):\n ans.append(tmp_ans)\n a=ab[m-1-i][0]\n b=ab[m-1-i][1]\n a-=1\n b-=1\n if not uf.is_same(a,b):\n tmp_ans-=uf.size(a)*uf.size(b)\n uf.union(a,b)\n\nans.reverse()\nfor i in ans:\n print(i)\n"]
['Runtime Error', 'Accepted']
['s510455536', 's942264713']
[33456.0, 33316.0]
[233.0, 545.0]
[900, 1551]
p03108
u407160848
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['import sys\n\nsys.setrecursionlimit(2000)\n\ntmp=input().split(" ")\nn = int(tmp[0])\nm = int(tmp[1])\n\nnode_groups = [ i for i in range(n)]\ngroup_sizes = [ 1 for _ in range(n)]\n\nedges = []\nfor i in range(m):\n tmp=input().split(" ")\n edges.append([int(tmp[0])-1, int(tmp[1])-1])\n\nhubensa_list = [0] * (m + 1)\nhubensa_list[m] = n*(n-1)//2\n\ndef root(i):\n if node_groups[i] == i:\n return i\n else:\n node_groups[i] = root(node_groups[i])\n return node_groups[i]\n\ndef same(a, b):\n return root(a) == root(b)\n\ndef size(i):\n return group_sizes[root(i)]\n\ndef unite(a, b):\n a = root(a)\n b = root(b)\n\n if a == b:\n return\n \n group_sizes[a] += size(b)\n node_groups[b] = a\n\n\nfor i in range(m-1, 0, -1):\n edge = edges[i]\n print("node_groups: ", node_groups)\n node_1 = edge[0]\n node_2 = edge[1]\n\n root_1 = root(node_1)\n root_2 = root(node_2)\n if root_1 == root_2:\n hubensa_list[i] = hubensa_list[i+1]\n continue\n\n \n hubensa_list[i] = hubensa_list[i+1] - (size(root_1) * size(root_2))\n\n unite(root_1, root_2)\n\nprint("node_groups: ", node_groups)\n\n\nfor i in range(1, m+1):\n print(hubensa_list[i])', 'import sys\n\n\n\ntmp=input().split(" ")\nn = int(tmp[0])\nm = int(tmp[1])\n\nnode_groups = [ i for i in range(n)]\ngroup_sizes = [ 1 for _ in range(n)]\n\nedges = []\nfor i in range(m):\n tmp=input().split(" ")\n edges.append([int(tmp[0])-1, int(tmp[1])-1])\n\nhubensa_list = [0] * (m + 1)\nhubensa_list[m] = n*(n-1)//2\n\ndef root(i):\n if node_groups[i] == i:\n #print(i, "is root.")\n return i\n else:\n #_1 = node_groups[i]\n node_groups[i] = root(node_groups[i])\n #_2 = node_groups[i]\n #print ("root: ", _1, _2)\n return node_groups[i]\n\ndef same(a, b):\n return root(a) == root(b)\n\ndef size(i):\n return group_sizes[root(i)]\n\ndef unite(a, b):\n a = root(a)\n b = root(b)\n\n if a == b:\n return\n \n group_sizes[a] += size(b)\n node_groups[b] = a\n\n\nfor i in range(m-1, -1, -1):\n edge = edges[i]\n print("edge: ",edge)\n #print("node_groups: ", node_groups)\n node_1 = edge[0]\n node_2 = edge[1]\n\n root_1 = root(node_1)\n root_2 = root(node_2)\n if root_1 == root_2:\n hubensa_list[i] = hubensa_list[i+1]\n continue\n\n \n hubensa_list[i] = hubensa_list[i+1] - (size(root_1) * size(root_2))\n\n unite(root_1, root_2)\n\n#print("node_groups: ", node_groups)\n\n\nfor i in range(1, m+1):\n print(hubensa_list[i])', '# import sys\n\n\n\ntmp=input().split(" ")\nn = int(tmp[0])\nm = int(tmp[1])\n\nnode_groups = [ i for i in range(n)]\ngroup_sizes = [ 1 for _ in range(n)]\ngroup_ranks = [ 0 for _ in range(n)]\n\nedges = []\nfor i in range(m):\n tmp=input().split(" ")\n edges.append([int(tmp[0])-1, int(tmp[1])-1])\n\nhubensa_list = [0] * (m + 1)\nhubensa_list[m] = n*(n-1)//2\n\ndef root(i):\n if node_groups[i] == i:\n #print(i, "is root.")\n return i\n else:\n #_1 = node_groups[i]\n node_groups[i] = root(node_groups[i])\n #_2 = node_groups[i]\n #print ("root: ", _1, _2)\n return node_groups[i]\n\ndef same(a, b):\n return root(a) == root(b)\n\ndef size(i):\n return group_sizes[root(i)]\n\ndef unite(a, b):\n a = root(a)\n b = root(b)\n\n if a == b:\n return\n\n if group_ranks[a] < group_ranks[b]:\n group_sizes[b] += size(a)\n node_groups[a] = b\n else:\n group_sizes[a] += size(b)\n node_groups[b] = a\n if group_ranks[a] == group_ranks[b]:\n group_ranks[a] += 1\n\n\nfor i in range(m-1, -1, -1):\n edge = edges[i]\n # print("edge: ",edge)\n #print("node_groups: ", node_groups)\n node_1 = edge[0]\n node_2 = edge[1]\n\n root_1 = root(node_1)\n root_2 = root(node_2)\n if root_1 == root_2:\n hubensa_list[i] = hubensa_list[i+1]\n continue\n\n \n hubensa_list[i] = hubensa_list[i+1] - (size(root_1) * size(root_2))\n\n unite(root_1, root_2)\n\n#print("node_groups: ", node_groups)\n\n\nfor i in range(1, m+1):\n print(hubensa_list[i])']
['Runtime Error', 'Runtime Error', 'Accepted']
['s588681170', 's798772644', 's468431126']
[158212.0, 30320.0, 27204.0]
[2104.0, 859.0, 691.0]
[1199, 1351, 1584]
p03108
u413165887
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["n, m = map(int, input().split(' '))\ns = [list(map(int, input().split(' '))) for _ in range(m)]\n\ndic = [x for x in range(n)]\nresult = [int(n*(n-1)/2)]\nfor i in range(1,m):\n if dic[s[-1*i][0]-1] == dic[s[-1*i][1]-1]:\n result.append(result[-1])\n else:\n# p = [x for x in dic if x == dic[s[-1*i][0]-1]]\n# q = [x for x in dic if x == dic[s[-1*i][1]-1]]\n p = x.count(dic[s[-1*i][0]-1])\n p = x.count(dic[s[-1*i][1]-1])\n result.append(result[-1]-len(p)*len(q))\n if len(p) < len(q):\n dic = [dic[s[-1*i][1]-1] if x == dic[s[-1*i][0]-1] else x for x in dic]\n else:\n dic = [dic[s[-1*i][0]-1] if x == dic[s[-1*i][1]-1] else x for x in dic]\nfor i in range(1,m+1):\n print(result[-1*i])", "n, m = map(int, input().split(' '))\na = [0]*m\nb = [0]*m\nfor i in range(m):\n a[i], b[i] = list(map(int, input().split(' ')))\n\nc = [-1]*(m+2)\n\ndef find(num):\n if c[num] < 0:\n return num\n else:\n k = find(c[num])\n c[num] = k\n return k\n\nresult = [int(n*(n-1)/2)]\nfor i in range(m-1, 0,-1):\n x = find(a[i])\n y = find(b[i])\n\n if x != y:\n result.append(result[-1]-c[x]*c[y])\n x, y = sorted([x,y])\n c[x] += c[y]\n c[y] = x\n else:\n result.append(result[-1])\n\nfor i in result[::-1]:\n print(int(i))\n"]
['Runtime Error', 'Accepted']
['s548874775', 's776923088']
[31468.0, 17688.0]
[331.0, 594.0]
[756, 573]
p03108
u414425367
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['def find(tab, a):\n if tab[0][a]==-1:\n return a\n tab[0][a] = find(tab, tab[0][a])\n return tab[0][a]\n\ndef union(tab, a, b):\n p1, p2 = find(tab, a), find(tab, b)\n if p1 != p2:\n tab[0][p1] = p2\n tab[1][p2] += tab[1][p1]\n\ndef size(tab, a):\n return tab[1][a]\n\nN, M = [int(a) for a in input().split(" ")]\nABs = [[int(a) for a in input().split(" ")] for i in range(M)]\ntab = [[-1]*(N*10), [1]*(N*10ee)]\narr = []\n\nfor a, b in reversed(ABs):\n a, b = a-1, b-1\n pa, pb = find(tab, a), find(tab, b)\n na, nb = size(tab, pa), size(tab, pb)\n if pa!=pb:\n dn = ((na+nb)*(na+nb-1)//2)-(na*(na-1)//2)-(nb*(nb-1)//2)\n arr.append(dn)\n union(tab, a, b)\n else:\n arr.append(0)\n\nacc = 0\nfor cp in reversed(arr):\n acc += cp\n print(acc)\n', 'def find(tab, a):\n if tab[0][a]==-1:\n return a\n tab[0][a] = find(tab, tab[0][a])\n return tab[0][a]\n\ndef union(tab, a, b):\n p1, p2 = find(tab, a), find(tab, b)\n if p1 != p2:\n tab[0][p1] = p2\n tab[1][p2] += tab[1][p1]\n\ndef size(tab, a):\n return tab[1][a]\n\nfrom random import randint\nN = randint(2, 100)\nM = randint(1, 100)\nABs = [(randint(1,N), randint(1,N)) for i in range(M)]\n#N, M = [int(a) for a in input().split(" ")]\n#ABs = [[int(a) for a in input().split(" ")] for i in range(M)]\ntab = [[-1]*N, [1]*N]\narr = []\n\nfor a, b in reversed(ABs):\n a, b = a-1, b-1\n pa, pb = find(tab, a), find(tab, b)\n na, nb = size(tab, pa), size(tab, pb)\n if pa!=pb:\n dn = ((na+nb)*(na+nb-1)//2)-(na*(na-1)//2)-(nb*(nb-1)//2)\n arr.append(dn)\n try:\n union(tab, a, b)\n except:\n pass\n else:\n arr.append(0)\n\nacc = 0\nfor cp in reversed(arr):\n acc += cp\n print(acc)\n', 'import sys\nsys.setrecursionlimit(100000)\n\ndef find(tab, a):\n if tab[0][a]==a:\n return a\n tab[0][a] = find(tab, tab[0][a])\n return tab[0][a]\n\ndef union(tab, a, b):\n p1, p2 = (find(tab, a), find(tab, b))\n if p1 != p2:\n tab[0][p1] = p2\n tab[1][p2] += tab[1][p1]\n\ndef size(tab, a):\n return tab[1][a]\n\nN, M = [int(a) for a in input().split(" ")]\nABs = [[int(a) for a in input().split(" ")] for i in range(M)]\ntab = [[i for i in range(N)], [1 for i in range(N)]]\narr = []\n\nfor a, b in reversed(ABs):\n a, b = (a-1, b-1)\n pa, pb = (find(tab, a), find(tab, b))\n na, nb = (size(tab, pa), size(tab, pb))\n if pa!=pb:\n dn = ((na+nb)*(na+nb-1)//2)-(na*(na-1)//2)-(nb*(nb-1)//2)\n arr.append(dn)\n union(tab, a, b)\n else:\n arr.append(0)\n\nacc = 0\nfor cp in reversed(arr):\n acc += cp\n print(acc)\n']
['Runtime Error', 'Wrong Answer', 'Accepted']
['s237066059', 's996119840', 's315044434']
[3064.0, 4208.0, 42920.0]
[25.0, 31.0, 793.0]
[797, 959, 901]
p03108
u415905784
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["N, M = map(int, input().split())\nBridge= [[int(i) for i in input().split()] for _ in range(M)]\nParent = [int(i) for i in range(N)]\nRank = [1] * N\n \ndef find(x):\n if x == Parent[x]: return x\n else:\n Parent[x] = y = find(Parent[x])\n return y\n \ndef unite(x, y, Total):\n rx, ry = find(x), find(y)\n if rx == ry: return Total\n else:\n Total -= Rank[rx] * Rank[ry]\n if rx > ry:\n Parent[rx] = ry\n Rank[ry] += Rank[rx]\n else:\n Parent[ry] = rx\n Rank[rx] += Rank[ry]\n return Total\n \nTotal = N * (N - 1) // 2\nAns = [str(Total)] * M\nfor i in reversed(range(1, M)):\n A, B = Bridge[i]\n A -= 1\n B -= 1\n Total = unite(A, B, Total)\n Ans[i - 2] = str(Total)\n \nprint('\\n'.join(Ans))", "N, M = map(int, input().split())\nBridge= [[int(i) for i in input().split()] for _ in range(M)]\nParent = [int(i) for i in range(N)]\nRank = [1] * N\n \ndef find(x):\n if x == Parent[x]: return x\n else:\n Parent[x] = y = find(Parent[x])\n return y\n \ndef unite(x, y, Total):\n rx, ry = find(x), find(y)\n if rx == ry: return Total\n else:\n Total -= Rank[rx] * Rank[ry]\n if rx > ry:\n Parent[rx] = ry\n Rank[ry] += Rank[rx]\n else:\n Parent[ry] = rx\n Rank[rx] += Rank[ry]\n return Total\n \nTotal = N * (N - 1) // 2\nAns = [str(Total)] * M\nfor i in reversed(range(1, M)):\n A, B = Bridge[i]\n A -= 1\n B -= 1\n Total = unite(A, B, Total)\n Ans[i - 1] = str(Total)\n \nprint('\\n'.join(Ans))"]
['Wrong Answer', 'Accepted']
['s913514474', 's215350693']
[36316.0, 36312.0]
[516.0, 524.0]
[753, 753]
p03108
u427344224
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['class UnionFind:\n def __init__(self, n):\n self.parent = [i for i in range(n + 1)]\n self.rank = [0 for _ in range(n + 1)]\n self.size = [1] * (n + 1)\n self.group = [[i] for i in range(n + 1)]\n\n def find(self, x):\n # If x is root\n if self.parent[x] == x:\n return x\n # If x is not root, search again by using x\'s parent\n else:\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x == y:\n return\n # Make an edge from the root of lower tree to the root of higher tree\n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n self.size[y] += self.size[x]\n else:\n self.parent[y] = x\n self.size[x] += self.size[y]\n # If the height of tree the tree is the same, increase one of the heights by 1\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n def check_same(self, x, y):\n return self.find(x) == self.find(y)\n\n def get_size(self, x):\n return self.size[self.find(x)]\n\n def merge(self, x, y):\n \n x = self.find(x)\n y = self.find(y)\n if len(self.group[x]) < len(self.group[y]):\n x, y = y, x\n self.group[x].extend(self.group[y])\n self.group[y] = []\n self.parent[y] = x\n\n\ndef combination(n, r):\n """\n :param n: the count of different items\n :param r: the number of select\n :return: combination\n n! / (r! * (n - r)!)\n """\n r = min(n - r, r)\n result = 1\n for i in range(n, n - r, -1):\n result *= i\n for i in range(1, r + 1):\n result //= i\n return result\n\n\nN, M = map(int, input().split())\nitems = []\nfor i in range(M):\n a, b = map(int, input().split())\n items.append((a, b))\nitems = list(reversed(items))\n\nunion_find = UnionFind(N)\nans = combination(N, 2)\npre = 0\nresult = []\nfor i, ab in enumerate(items):\n a, b = ab\n\n if i == 0:\n pre = max(len(union_find.group[a]), len(union_find.group[b]))\n result.append(ans)\n else:\n tmp = max(union_find.group[a], union_find.group[b])\n if len(tmp) > pre:\n ans2 = ans\n ans2 -= combination(len(tmp), 2)\n result.append(max(ans2, 0))\n else:\n tmp = union_find.group[a]\n ans2 = ans\n ans2 -= (combination(len(tmp), 2) + combination(pre, 2))\n result.append(max(ans2, 0))\n if i != M - 1:\n prea = max(len(union_find.group[items[i + 1][0]]), len(union_find.group[items[i + 1][1]]))\n pre = max(len(union_find.group[a]), len(union_find.group[b]))\n union_find.merge(a, b)\n\nfor r in result[::-1]:\n print(r)\n', 'class UnionFind:\n def __init__(self, n):\n self.parent = [i for i in range(n + 1)]\n self.rank = [0 for _ in range(n + 1)]\n self.size = [1] * (n + 1)\n self.group = [[i] for i in range(n + 1)]\n\n def find(self, x):\n # If x is root\n if self.parent[x] == x:\n return x\n # If x is not root, search again by using x\'s parent\n else:\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x == y:\n return\n # Make an edge from the root of lower tree to the root of higher tree\n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n self.size[y] += self.size[x]\n else:\n self.parent[y] = x\n self.size[x] += self.size[y]\n # If the height of tree the tree is the same, increase one of the heights by 1\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n def check_same(self, x, y):\n return self.find(x) == self.find(y)\n\n def get_size(self, x):\n return self.size[self.find(x)]\n\n def merge(self, x, y):\n \n x = self.find(x)\n y = self.find(y)\n if len(self.group[x]) < len(self.group[y]):\n x, y = y, x\n self.group[x].extend(self.group[y])\n self.group[y] = []\n self.parent[y] = x\n\n\ndef combination(n, r):\n """\n :param n: the count of different items\n :param r: the number of select\n :return: combination\n n! / (r! * (n - r)!)\n """\n r = min(n - r, r)\n result = 1\n for i in range(n, n - r, -1):\n result *= i\n for i in range(1, r + 1):\n result //= i\n return result\n\n\nN, M = map(int, input().split())\nitems = []\nfor i in range(M):\n a, b = map(int, input().split())\n items.append((a, b))\nitems = list(reversed(items))\n\nunion_find = UnionFind(N)\nans = combination(N, 2)\nf = 0\npre = 0\nresult = []\nfor i, ab in enumerate(items):\n a, b = ab\n\n if i == 0:\n f = a\n pre = max(len(union_find.group[a]), len(union_find.group[b]))\n result.append(ans)\n else:\n tmp = union_find.group[f]\n if len(tmp) > pre:\n ans2 = ans\n ans2 -= combination(len(tmp), 2)\n result.append(max(ans2, 0))\n else:\n tmp = union_find.group[a]\n ans2 = ans\n ans2 -= (combination(len(tmp), 2) + combination(pre, 2))\n result.append(max(ans2, 0))\n if i != M - 1:\n prea = max(len(union_find.group[items[i + 1][0]]), len(union_find.group[items[i + 1][1]]))\n pre = max(len(union_find.group[a]), len(union_find.group[b]))\n union_find.merge(a, b)\n\nfor r in result[::-1]:\n print(r)\n', "class UnionFind:\n def __init__(self, n):\n self.parent = [i for i in range(n + 1)]\n self.rank = [0 for _ in range(n + 1)]\n self.size = [1] * (n + 1)\n self.group = [[i] for i in range(n + 1)]\n\n def find(self, x):\n # If x is root\n if self.parent[x] == x:\n return x\n # If x is not root, search again by using x's parent\n else:\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x == y:\n return\n # Make an edge from the root of lower tree to the root of higher tree\n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n self.size[y] += self.size[x]\n else:\n self.parent[y] = x\n self.size[x] += self.size[y]\n # If the height of tree the tree is the same, increase one of the heights by 1\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n def check_same(self, x, y):\n return self.find(x) == self.find(y)\n\n def get_size(self, x):\n return self.size[self.find(x)]\n\n def merge(self, x, y):\n \n x = self.find(x)\n y = self.find(y)\n if len(self.group[x]) < len(self.group[y]):\n x, y = y, x\n self.group[x].extend(self.group[y])\n self.group[y] = []\n self.parent[y] = x\n\nN, M = map(int, input().split())\nitems = []\nfor i in range(M):\n items.append(tuple(map(int, input().split())))\n\nitems = list(reversed(items))\nuf = UnionFind(N)\nans = N * (N - 1) // 2\nminus = 0\nres = []\n\nfor a, b in items:\n ans -= minus\n res.append(ans)\n if uf.check_same(a, b):\n minus = 0\n else:\n minus = uf.get_size(a) * uf.get_size(b)\n uf.union(a, b)\n\nfor r in res[::-1]:\n print(r)\n"]
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s833149904', 's969033750', 's594891001']
[40012.0, 39756.0, 38536.0]
[1184.0, 1109.0, 851.0]
[2881, 2876, 1949]
p03108
u451017206
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['\nclass DisjointSet:\n def __init__(self):\n self.rank = {}\n self.p = {}\n self._size = {}\n\n def makeSet(self, x):\n self.p[x] = x\n self.rank[x] = 0\n self._size[x] = 1\n\n def same(self, x, y):\n return self.findSet(x) == self.findSet(y)\n\n def unite(self, x, y):\n self.link(self.findSet(x), self.findSet(y))\n\n def link(self, x, y):\n if(self.rank[x] > self.rank[y]):\n self.p[y] = x\n self._size[x] += self._size[y]\n else:\n self.p[x] = y\n self._size[y] += self._size[x]\n if(self.rank[x] == self.rank[y]):\n self.rank[y] += 1\n\n \n def size(self, x):\n print(self._size)\n return self._size[self.findSet(x)]\n\n def findSet(self, x):\n if(x != self.p[x]):\n self.p[x] = self.findSet(self.p[x])\n return self.p[x]\n\n\nN, M = map(int, input().split())\nl = [[int(i) for i in input().split()] for i in range(M)][::-1]\n\nds = DisjointSet()\nfor i in range(N):\n ds.makeSet(i+1)\n\nans = [N*(N-1)//2]\n\nfor a, b in l[:-1]:\n if ds.same(a, b):\n ans.append(ans[-1])\n else:\n x = ds.size(a)\n y = ds.size(b)\n ans.append(ans[-1] - x * y)\n ds.unite(a, b)\n\nfor a in ans[::-1]:\n print(a)', '\nclass DisjointSet:\n def __init__(self):\n self.rank = {}\n self.p = {}\n self._size = {}\n\n def makeSet(self, x):\n self.p[x] = x\n self.rank[x] = 0\n self._size[x] = 1\n\n def same(self, x, y):\n return self.findSet(x) == self.findSet(y)\n\n def unite(self, x, y):\n self.link(self.findSet(x), self.findSet(y))\n\n def link(self, x, y):\n if x == y:\n return\n if(self.rank[x] > self.rank[y]):\n self.p[y] = x\n self._size[x] += self._size[y]\n else:\n self.p[x] = y\n self._size[y] += self._size[x]\n if(self.rank[x] == self.rank[y]):\n self.rank[y] += 1\n\n \n def size(self, x):\n return self._size[self.findSet(x)]\n\n def findSet(self, x):\n if(x != self.p[x]):\n self.p[x] = self.findSet(self.p[x])\n return self.p[x]\n\n\nN, M = map(int, input().split())\nl = [[int(i) for i in input().split()] for i in range(M)]\n\nds = DisjointSet()\nfor i in range(N):\n ds.makeSet(i+1)\n\nans = [0] * M\nans[M-1] = N * (N - 1) // 2\n\nfor i in range(M-1, 0, -1):\n a, b = l[i]\n if ds.same(a, b):\n ans[i-1] = ans[i]\n else:\n x = ds.size(a)\n y = ds.size(b)\n ans[i-1] = ans[i] - x * y\n ds.unite(a, b)\n\nfor a in ans:\n print(a)']
['Wrong Answer', 'Accepted']
['s998048077', 's609012386']
[157464.0, 47980.0]
[2106.0, 1071.0]
[1388, 1431]
p03108
u472065247
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["def find(x):\n if P[x] < 0:\n return x\n else:\n return P[x]\n\nN, M, *X = map(int,open(0).read().split())\nP = [-1] * (N + 1)\nS = N * (N - 1) // 2\nr = [S]\n\nwhile X:\n b, a = find(X.pop()), find(X.pop())\n if a != b:\n S -= P[a] * P[b]\n if P[a] > P[b]:\n a, b = b, a\n P[a] += P[b]\n P[b] = a\n r.append(S)\n\nr.pop()\nprint(*reversed(r), sep='\\n')", 'F=lambda x:x if P[x]<0else F(P[x]);N,M,*X=map(int,open(0).read().split());P=[-1]*-~N;S=N*~-N//2;r=[]\nwhile X:\n r+=S,;b,a=sorted(map(F,(X.pop(),X.pop())))\n if a!=b:S-=P[a]*P[b];P[a]+=P[b];P[b]=a\n*_,=map(print,r[::-1])']
['Runtime Error', 'Accepted']
['s027514959', 's930910096']
[25308.0, 25076.0]
[246.0, 565.0]
[359, 216]
p03108
u474423089
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["class UnionFind:\n def __init__(self,N):\n self.par =[-1 for i in range(N)]\n def find(self,x):\n if self.par[x] < 0:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n\n def unite(self,x,y):\n x = self.find(x)\n y = self.find(y)\n if x==y:\n return\n if self.par[x] > self.par[y]:\n x,y = y,x\n self.par[x] +=self.par[y]\n self.par[y] = x\n\n def size(self,x):\n return -self.par[self.find(x)]\n def same(self,x,y):\n return self.find(x)==self.find(y)\n\nN,M=map(int,input().split(' '))\nbridge = [list(map(int,input().split(' '))) for i in range(M)]\nans = N*(N-1)//2\nans_list = [ans]\nuf = UnionFind(N)\nfor a,b in bridge[::-1][:-1]:\n if not uf.same(a-1,b-1):\n A = uf.size(a-1)\n B = uf.size(b-1)\n ans += (A*(A-1)+B*(B-1)-(A+B)*(A+B-1))//2\n ans_list.append(ans)\n uf.unite(a-1,b-1)\nfor i in ans_list[::-1]:\n print(i)", "class UnionFind:\n def __init__(self,N):\n self.par =[-1 for i in range(N)]\n def find(self,x):\n if self.par[x] < 0:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n\n def unite(self,x,y):\n x = self.find(x)\n y = self.find(y)\n if x==y:\n return\n if self.par[x] > self.par[y]:\n x,y = y,x\n self.par[x] +=self.par[y]\n self.par[y] = x\n\n def size(self,x):\n return -self.par[self.find(x)]\n def same(self,x,y):\n return self.find(x)==self.find(y)\n\nN,M=map(int,input().split(' '))\nbridge = [list(map(int,input().split(' '))) for i in range(M)]\nans = N*(N-1)//2\nans_list = [ans]\nuf = UnionFind(N)\nfor a,b in bridge[::-1][:-1]:\n if not uf.same(a-1,b-1):\n A = uf.size(a-1)\n B = uf.size(b-1)\n ans += (A*(A-1)+B*(B-1)-(A+B)*(A+B-1))//2\n ans_list.append(ans)\n uf.unite(a-1,b-1)\nfor i in ans_list[::-1]:\n print(i)"]
['Wrong Answer', 'Accepted']
['s243842188', 's880323268']
[35688.0, 35604.0]
[896.0, 849.0]
[1002, 998]
p03108
u476604182
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['def cmb(n,r):\n r = min(r, n-r)\n if r==0: return 1\n if r<0: return 0\n over = reduce(mul, range(n, n-r, -1))\n under = reduce(mul, range(1,r+1))\n return over//under\n\nclass UnionFind:\n def __init__(self, n):\n self.par = [i for i in range(n)]\n self.size = [1]*n\n self.rank = [0]*n\n def find(self, x):\n if self.par[x]==x:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n def unit(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x==y:\n return\n elif self.rank[x]<self.rank[y]:\n self.par[x] = y\n self.size[y] += self.size[x]\n return\n elif self.rank[y]<self.rank[x]:\n self.par[y] = x\n self.size[x] += self.size[y]\n else:\n self.par[y] = x\n self.size[x] += self.size[y]\n self.rank[x] += 1\n \n def same(self, x, y):\n return self.find(x)==self.find(y)\n \n def count(self, x):\n return self.size[x]\n\nN, M = map(int, input().split())\nbridge = []\nu = UnionFind(N)\nm = cmb(N,2)\nans = [m]\n\nfor i in range(M):\n A, B = map(int, input().split())\n bridge += [(A,B)]\n \nfor i in range(M-1,0,-1):\n a, b = bridge[i]\n if u.same(a-1,b-1):\n ans += [m]\n continue\n pa = u.find(a-1)\n pb = u.find(b-1)\n m -= u.count(pa)*u.count(pb)\n ans += [m]\n u.unit(a-1,b-1)\n\nfor i in range(M-1,-1,-1):\n print(ans[i])\n ', "class UnionFind:\n def __init__(self, n):\n self.par = [i for i in range(n)]\n self.size = [1]*n\n self.rank = [0]*n\n def find(self, x):\n if self.par[x]==x:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n def unit(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x==y:\n return\n elif self.rank[x]<self.rank[y]:\n self.par[x] = y\n self.size[y] += self.size[x]\n return\n elif self.rank[y]<self.rank[x]:\n self.par[y] = x\n self.size[x] += self.size[y]\n else:\n self.par[y] = x\n self.size[x] += self.size[y]\n self.rank[x] += 1\n \n def same(self, x, y):\n return self.find(x)==self.find(y)\n \n def count(self, x):\n return self.size[x]\n\nN, M, *L = map(int, open(0).read().split())\nu = UnionFind(N+1)\nm = N*(N-1)//2\nans = []\nfor a, b in zip(*[iter(L[::-1])]*2):\n ans.append(m)\n A = u.find(a)\n B = u.find(b)\n if A!=B:\n na = u.count(A)\n nb = u.count(B)\n u.unit(a,b)\n m -= na*nb\n\nprint(*reversed(ans),sep='\\n')"]
['Runtime Error', 'Accepted']
['s673781601', 's897882354']
[8692.0, 25308.0]
[28.0, 440.0]
[1334, 1046]
p03108
u477320129
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['#!/usr/bin/env python3\nimport sys\n{ % if prediction_success % }\n{ % endif % }\n{ % if mod or yes_str or no_str % }\n\n{ % endif % }\n{ % if mod % }\nMOD = None # type: int\n{ % endif % }\n{ % if yes_str % }\nYES = "None" # type: str\n{ % endif % }\n{ % if no_str % }\nNO = "None" # type: str\n{ % endif % }\n{ % if prediction_success % }\n\n\ndef solve(N: int, M: int, A: "List[int]", B: "List[int]"):\n return\n{ % endif % }\n\n\n# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)\ndef main():\n { % if prediction_success % }\n\n def iterate_tokens():\n for line in sys.stdin:\n for word in line.split():\n yield word\n tokens = iterate_tokens()\n N = int(next(tokens)) # type: int\n M = int(next(tokens)) # type: int\n A = [int()] * (M) # type: "List[int]"\n B = [int()] * (M) # type: "List[int]"\n for i in range(M):\n A[i] = int(next(tokens))\n B[i] = int(next(tokens))\n solve(N, M, A, B)\n { % else % }\n # Failed to predict input format\n pass\n { % endif % }\n\n\nif __name__ == \'__main__\':\n main()\n', '#!/usr/bin/env python3\nimport sys\n\ndef solve_iter(N: int, M: int, A: "List[int]", B: "List[int]"):\n ic = N * (N - 1) // 2\n uf = UnionFind(N+1)\n for a, b in zip(A[::-1], B[::-1]):\n yield ic\n if uf.same(a, b):\n continue\n ic -= uf.size(a) * uf.size(b)\n uf.union(a, b)\n\ndef solve(N: int, M: int, A: "List[int]", B: "List[int]"):\n for a in reversed(list(solve_iter(N, M, A, B))):\n print(a)\n\n# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools\ndef main():\n def iterate_tokens():\n for line in sys.stdin:\n for word in line.split():\n yield word\n tokens = iterate_tokens()\n N = int(next(tokens)) # type: int\n M = int(next(tokens)) # type: int\n A = [int()] * (M) # type: "List[int]"\n B = [int()] * (M) # type: "List[int]"\n for i in range(M):\n A[i] = int(next(tokens))\n B[i] = int(next(tokens))\n solve(N, M, A, B)\n\n# https://note.nkmk.me/python-union-find/\nclass UnionFind():\n def __init__(self, n):\n self.n = n\n self.parents = [-1] * n\n \n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return self.parents[x]\n \n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n \n if x == y:\n return\n \n if self.parents[x] > self.parents[y]:\n x, y = y, x\n \n self.parents[x] += self.parents[y]\n self.parents[y] = x\n \n def size(self, x):\n return -self.parents[self.find(x)]\n \n def same(self, x, y):\n return self.find(x) == self.find(y)\n \n def members(self, x):\n root = self.find(x)\n return [i for i in range(self.n) if self.find(i) == root]\n \n def roots(self):\n return [i for i, x in enumerate(self.parents) if x < 0]\n \n def group_count(self):\n return len(self.roots())\n \n def all_group_members(self):\n return {r: self.members(r) for r in self.roots()}\n \n def __str__(self):\n return \'\\n\'.join(\'{}: {}\'.format(r, self.members(r)) for r in self.roots())\n\n\ndef test():\n import doctest\n doctest.testmod()\n\nif __name__ == \'__main__\':\n #test()\n main()\n']
['Runtime Error', 'Accepted']
['s924541914', 's358169676']
[2940.0, 17416.0]
[17.0, 515.0]
[1177, 2313]
p03108
u486448566
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["import sys\nsys.setrecursionlimit(10**9)\ninput = sys.stdin.readline\ninput_int = lambda:int(input())\ninput_ints = lambda:map(int,input().split())\ninput_ints_list = lambda:list(input_ints())\ninput_str = lambda:input()\ninput_strs = lambda:input().split()\ninput_lines = lambda n,input_func:[input_func() for _ in range(n)]\n\nimport importlib\nimport_module = lambda module_name:importlib.import_module(module_name)\n\ninit_array_1dim = lambda value,n:[value]*n \ninit_array_2dim = lambda value,n,m:[init_array_1dim(value,n) for _ in range(m)] \n\ngcd_base = lambda value1,value2:import_module('fractions').gcd(value1,value2) \ngcd = lambda lst:import_module('functools').reduce(gcd_base,lst) \nlcm_base = lambda value1,value2:(value1*value2)//gcd_base(value1,value2) \nlcm = lambda lst:import_module('functools').reduce(lcm_base,lst,1) \n\npermutations = lambda lst,n:import_module('itertools').permutations(lst,n) \ncombinations = lambda lst,n:import_module('itertools').combinations(lst,n) \nproduct = lambda lst1,lst2:import_module('itertools').product(lst1,lst2) \n\nround = lambda value:round(value) \nceil = lambda value:import_module('math').ceil(value) \nfloor = lambda value:import_module('math').floor(value) \n\n\ninit_q = lambda lst: import_module('collections').deque(lst)\nq_pop = lambda q: q.popleft() \nq_push = lambda q,value:q.appendleft(value) \nq_pushlist = lambda q,lst:q.extendleft(lst) \nq_append = lambda q,value:q.append(value) \nq_appendlist = lambda q,lst:q.extend(lst) \n\n\ninit_heap = lambda a:import_module('heapq').heapify(a)\nheap_push = lambda a,v:import_module('heapq').heappush(a,v)\nheap_pop = lambda a:import_module('heapq').heappop(a) \n\n\nbisect_left = lambda a,x:import_module('bisect').bisect_left(a,x)\nbisect_right = lambda a,x:import_module('bisect').bisect_left(a,x)\ninsert_left = lambda a,x:import_module('bisect').insert_left(a,bisect_left(a,x))\ninsert_right = lambda a,x:import_module('bisect').insert_right(a,bisect_right(a,x))\n\n# numpy\nnparray = lambda lst:import_module('numpy').array(lst)\nnpzeros = lambda n:import_module('numpy').zeros((n,n))\nnpones = lambda n:import_module('numpy').ones((n,n))\n\n\ncumsum = lambda a:import_module('numpy').cumsum(import_module('numpy').array(a)) \ncumprod = lambda a:import_module('numpy').cumprod(import_module('numpy').array(a)) \n\ndef two_pointers(lst,threshold,ope='sum'):\n l,r = 0,0\n base = 0 \n if ope == 'sum':base = 0\n if ope == 'prd':base = 1\n diff = 0\n while r < len(lst):\n val = 0\n if ope == 'sum':val = base + lst[r]\n if ope == 'prd':val = base * lst[r]\n if val <= threshold:\n if ope == 'sum':base += lst[r]\n if ope == 'prd':base *= lst[r]\n diff = max(diff, r-l+1)\n r += 1\n elif l == r:\n r += 1\n l += 1\n else:\n if ope == 'sum':base -= lst[l]\n if ope == 'prd':base //= lst[l]\n l += 1\n return diff\n\n\nto_csr_matrix = lambda graph:import_module('scipy.sparse').csr_matrix(graph)\nconnected_components = lambda graph:import_module('scipy.sparse.csgraph').connected_components(csgraph=to_csr_matrix(graph), directed=False, return_labels=True) \nshortest_path = lambda graph:import_module('scipy.sparse.csgraph').shortest_path(to_csr_matrix(graph), directed=False) \n\nを初期化(グラフの要素数、グラフの線の両端、有向無向)\ndef creat_graph(n,line_lst,directed=False):\n graph = npzeros(n)\n for line in line_lst:\n graph[line[0]][line[1]] = 1\n if not directed:graph[line[1]][line[0]] = 1 \n return graph\n\ndef solution():\n \n N,M = input_ints()\n graph = npzeros(N)\n AB = [[line[0]-1,line[1]-1] for line in input_lines(M,input_ints_list)]\n AB.reverse()\n\n ans = []\n count = N*(N-1)//2 \n \n _, labels = connected_components(graph)\n for ab in AB:\n ans.append(count)\n if labels[ab[0]] != labels[ab[1]]:\n count -= labels[labels == labels[ab[0]]].shape[0] * labels[labels == labels[ab[1]]].shape[0]\n \n graph[ab[0],ab[1]] = 1\n graph[ab[1],ab[0]] = 1\n _, labels = connected_components(graph)\n\n ans.reverse()\n for _ans in ans:\n print(_ans)\n\t\nif __name__ == '__main__':\n solution()", 'import sys\nsys.setrecursionlimit(10**9)\ninput = sys.stdin.readline\ninput_int = lambda:int(input())\ninput_ints = lambda:map(int,input().split())\ninput_ints_list = lambda:list(input_ints())\ninput_str = lambda:input()\ninput_strs = lambda:input().split()\ninput_lines = lambda n,input_func:[input_func() for _ in range(n)]\n\nimport importlib\nimport_module = lambda module_name:importlib.import_module(module_name)\n\ninit_array_1dim = lambda value,n:[value]*n \ninit_array_2dim = lambda value,n,m:[init_array_1dim(value,n) for _ in range(m)] \n\ngcd_base = lambda value1,value2:import_module(\'fractions\').gcd(value1,value2) \ngcd = lambda lst:import_module(\'functools\').reduce(gcd_base,lst) \nlcm_base = lambda value1,value2:(value1*value2)//gcd_base(value1,value2) \nlcm = lambda lst:import_module(\'functools\').reduce(lcm_base,lst,1) \n\npermutations = lambda lst,n:import_module(\'itertools\').permutations(lst,n) \ncombinations = lambda lst,n:import_module(\'itertools\').combinations(lst,n) \nproduct = lambda lst1,lst2:import_module(\'itertools\').product(lst1,lst2) \n\nround = lambda value:round(value) \nceil = lambda value:import_module(\'math\').ceil(value) \nfloor = lambda value:import_module(\'math\').floor(value) \n\n\ninit_q = lambda lst: import_module(\'collections\').deque(lst)\nq_pop = lambda q: q.popleft() \nq_push = lambda q,value:q.appendleft(value) \nq_pushlist = lambda q,lst:q.extendleft(lst) \nq_append = lambda q,value:q.append(value) \nq_appendlist = lambda q,lst:q.extend(lst) \n\n\ninit_heap = lambda a:import_module(\'heapq\').heapify(a)\nheap_push = lambda a,v:import_module(\'heapq\').heappush(a,v)\nheap_pop = lambda a:import_module(\'heapq\').heappop(a) \n\n\nbisect_left = lambda a,x:import_module(\'bisect\').bisect_left(a,x)\nbisect_right = lambda a,x:import_module(\'bisect\').bisect_left(a,x)\ninsert_left = lambda a,x:import_module(\'bisect\').insert_left(a,bisect_left(a,x))\ninsert_right = lambda a,x:import_module(\'bisect\').insert_right(a,bisect_right(a,x))\n\n# numpy\nnparray = lambda lst:import_module(\'numpy\').array(lst)\nnpzeros = lambda n:import_module(\'numpy\').zeros((n,n))\nnpones = lambda n:import_module(\'numpy\').ones((n,n))\n\n\ncumsum = lambda a:import_module(\'numpy\').cumsum(import_module(\'numpy\').array(a)) \ncumprod = lambda a:import_module(\'numpy\').cumprod(import_module(\'numpy\').array(a)) \n\ndef two_pointers(lst,threshold,ope=\'sum\'):\n l,r = 0,0\n base = 0 \n if ope == \'sum\':base = 0\n if ope == \'prd\':base = 1\n diff = 0\n while r < len(lst):\n val = 0\n if ope == \'sum\':val = base + lst[r]\n if ope == \'prd\':val = base * lst[r]\n if val <= threshold:\n if ope == \'sum\':base += lst[r]\n if ope == \'prd\':base *= lst[r]\n diff = max(diff, r-l+1)\n r += 1\n elif l == r:\n r += 1\n l += 1\n else:\n if ope == \'sum\':base -= lst[l]\n if ope == \'prd\':base //= lst[l]\n l += 1\n return diff\n\n\nto_csr_matrix = lambda graph:import_module(\'scipy.sparse\').csr_matrix(graph)\nconnected_components = lambda graph:import_module(\'scipy.sparse.csgraph\').connected_components(csgraph=to_csr_matrix(graph), directed=False, return_labels=True) \nshortest_path = lambda graph:import_module(\'scipy.sparse.csgraph\').shortest_path(to_csr_matrix(graph), directed=False) \n\nを初期化(グラフの要素数、グラフの線の両端、有向無向)\ndef init_graph(n,line_lst,directed=False):\n visited,graph = init_array_1dim(False,n),init_array_2dim(False,n,n)\n for line in line_lst:\n graph[line[0]][line[1]] = True\n if not directed:graph[line[1]][line[0]] = True \n return visited,graph\n\ndef solution():\n \n N,M = input_ints()\n graph = npzeros(N)\n AB = [[line[0]-1,line[1]-1] for line in input_lines(M,input_ints_list)]\n AB.reverse()\n\n ans = init_array_1dim(0,M)\n count = N*(N-1)//2 \n \n np = import_module("numpy")\n _, labels = connected_components(graph)\n for i in range(M):\n ans[M-i-1] = count\n if labels[AB[i][0]] != labels[AB[i][1]]:\n count -= np.where(labels == labels[AB[i][0]])[0].shape[0]*np.where(labels == labels[AB[i][1]])[0].shape[0]\n \n graph[AB[i][0],AB[i][1]] = 1\n graph[AB[i][1],AB[i][0]] = 1\n _, labels = connected_components(graph)\n\n for _ans in ans:\n print(_ans)\n\t\nif __name__ == \'__main__\':\n solution()', "import sys\nsys.setrecursionlimit(10**9)\ninput = sys.stdin.readline\ninput_int = lambda:int(input())\ninput_ints = lambda:map(int,input().split())\ninput_ints_list = lambda:list(input_ints())\ninput_str = lambda:input()\ninput_strs = lambda:input().split()\ninput_lines = lambda n,input_func:[input_func() for _ in range(n)]\n\nimport importlib\nimport_module = lambda module_name:importlib.import_module(module_name)\n\ninit_array_1dim = lambda value,n:[value]*n \ninit_array_2dim = lambda value,n,m:[init_array_1dim(value,n) for _ in range(m)] \n\ngcd_base = lambda value1,value2:import_module('fractions').gcd(value1,value2) \ngcd = lambda lst:import_module('functools').reduce(gcd_base,lst) \nlcm_base = lambda value1,value2:(value1*value2)//gcd_base(value1,value2) \nlcm = lambda lst:import_module('functools').reduce(lcm_base,lst,1) \n\npermutations = lambda lst,n:import_module('itertools').permutations(lst,n) \ncombinations = lambda lst,n:import_module('itertools').combinations(lst,n) \nproduct = lambda lst1,lst2:import_module('itertools').product(lst1,lst2) \n\nround = lambda value:round(value) \nceil = lambda value:import_module('math').ceil(value) \nfloor = lambda value:import_module('math').floor(value) \n\n\ninit_q = lambda lst: import_module('collections').deque(lst)\nq_pop = lambda q: q.popleft() \nq_push = lambda q,value:q.appendleft(value) \nq_pushlist = lambda q,lst:q.extendleft(lst) \nq_append = lambda q,value:q.append(value) \nq_appendlist = lambda q,lst:q.extend(lst) \n\n\ninit_heap = lambda a:import_module('heapq').heapify(a)\nheap_push = lambda a,v:import_module('heapq').heappush(a,v)\nheap_pop = lambda a:import_module('heapq').heappop(a) \n\n\nbisect_left = lambda a,x:import_module('bisect').bisect_left(a,x)\nbisect_right = lambda a,x:import_module('bisect').bisect_left(a,x)\ninsert_left = lambda a,x:import_module('bisect').insert_left(a,bisect_left(a,x))\ninsert_right = lambda a,x:import_module('bisect').insert_right(a,bisect_right(a,x))\n\n# numpy\nnparray = lambda lst:import_module('numpy').array(lst)\nnpzeros = lambda n:import_module('numpy').zeros((n,n))\nnpones = lambda n:import_module('numpy').ones((n,n))\n\n\ncumsum = lambda a:import_module('numpy').cumsum(import_module('numpy').array(a)) \ncumprod = lambda a:import_module('numpy').cumprod(import_module('numpy').array(a)) \n\ndef two_pointers(lst,threshold,ope='sum'):\n l,r = 0,0\n base = 0 \n if ope == 'sum':base = 0\n if ope == 'prd':base = 1\n diff = 0\n while r < len(lst):\n val = 0\n if ope == 'sum':val = base + lst[r]\n if ope == 'prd':val = base * lst[r]\n if val <= threshold:\n if ope == 'sum':base += lst[r]\n if ope == 'prd':base *= lst[r]\n diff = max(diff, r-l+1)\n r += 1\n elif l == r:\n r += 1\n l += 1\n else:\n if ope == 'sum':base -= lst[l]\n if ope == 'prd':base //= lst[l]\n l += 1\n return diff\n\n\nto_csr_matrix = lambda graph:import_module('scipy.sparse').csr_matrix(graph)\nconnected_components = lambda graph:import_module('scipy.sparse.csgraph').connected_components(csgraph=to_csr_matrix(graph), directed=False, return_labels=True) \nshortest_path = lambda graph:import_module('scipy.sparse.csgraph').shortest_path(to_csr_matrix(graph), directed=False) \n\nを初期化(グラフの要素数、グラフの線の両端、有向無向)\ndef creat_graph(n,line_lst,directed=False):\n graph = npzeros(n)\n for line in line_lst:\n graph[line[0]][line[1]] = 1\n if not directed:graph[line[1]][line[0]] = 1 \n return graph\n\ndef solution():\n \n N,M = input_ints()\n graph = init_array_2dim(0,N,N)\n AB = [[line[0]-1,line[1]-1] for line in input_lines(M,input_ints_list)]\n AB.reverse()\n\n ans = []\n count = N*(N-1)//2 \n \n _, labels = connected_components(graph)\n for ab in AB:\n ans.append(count)\n if labels[ab[0]] != labels[ab[1]]:\n count -= labels[labels == labels[ab[0]]].shape[0] * labels[labels == labels[ab[1]]].shape[0]\n \n graph[ab[0]][ab[1]] = 1\n graph[ab[1]][ab[0]] = 1\n _, labels = connected_components(graph)\n\n ans.reverse()\n for _ans in ans:\n print(_ans)\n\t\nif __name__ == '__main__':\n solution()", 'import sys\nsys.setrecursionlimit(10**9)\ninput = sys.stdin.readline\ninput_int = lambda:int(input())\ninput_ints = lambda:map(int,input().split())\ninput_ints_list = lambda:list(input_ints())\ninput_str = lambda:input()\ninput_strs = lambda:input().split()\ninput_lines = lambda n,input_func:[input_func() for _ in range(n)]\n\nimport importlib\nimport_module = lambda module_name:importlib.import_module(module_name)\n\ninit_array_1dim = lambda value,n:[value]*n \ninit_array_2dim = lambda value,n,m:[init_array_1dim(value,n) for _ in range(m)] \n\ngcd_base = lambda value1,value2:import_module(\'fractions\').gcd(value1,value2) \ngcd = lambda lst:import_module(\'functools\').reduce(gcd_base,lst) \nlcm_base = lambda value1,value2:(value1*value2)//gcd_base(value1,value2) \nlcm = lambda lst:import_module(\'functools\').reduce(lcm_base,lst,1) \n\npermutations = lambda lst,n:import_module(\'itertools\').permutations(lst,n) \ncombinations = lambda lst,n:import_module(\'itertools\').combinations(lst,n) \nproduct = lambda lst1,lst2:import_module(\'itertools\').product(lst1,lst2) \n\nround = lambda value:round(value) \nceil = lambda value:import_module(\'math\').ceil(value) \nfloor = lambda value:import_module(\'math\').floor(value) \n\n\ninit_q = lambda lst: import_module(\'collections\').deque(lst)\nq_pop = lambda q: q.popleft() \nq_push = lambda q,value:q.appendleft(value) \nq_pushlist = lambda q,lst:q.extendleft(lst) \nq_append = lambda q,value:q.append(value) \nq_appendlist = lambda q,lst:q.extend(lst) \n\n\ninit_heap = lambda a:import_module(\'heapq\').heapify(a)\nheap_push = lambda a,v:import_module(\'heapq\').heappush(a,v)\nheap_pop = lambda a:import_module(\'heapq\').heappop(a) \n\n\nbisect_left = lambda a,x:import_module(\'bisect\').bisect_left(a,x)\nbisect_right = lambda a,x:import_module(\'bisect\').bisect_left(a,x)\ninsert_left = lambda a,x:import_module(\'bisect\').insert_left(a,bisect_left(a,x))\ninsert_right = lambda a,x:import_module(\'bisect\').insert_right(a,bisect_right(a,x))\n\n# numpy\nnparray = lambda lst:import_module(\'numpy\').array(lst)\nnpzeros = lambda n:import_module(\'numpy\').zeros((n,n))\nnpones = lambda n:import_module(\'numpy\').ones((n,n))\n\n\ncumsum = lambda a:import_module(\'numpy\').cumsum(import_module(\'numpy\').array(a)) \ncumprod = lambda a:import_module(\'numpy\').cumprod(import_module(\'numpy\').array(a)) \n\ndef two_pointers(lst,threshold,ope=\'sum\'):\n l,r = 0,0\n base = 0 \n if ope == \'sum\':base = 0\n if ope == \'prd\':base = 1\n diff = 0\n while r < len(lst):\n val = 0\n if ope == \'sum\':val = base + lst[r]\n if ope == \'prd\':val = base * lst[r]\n if val <= threshold:\n if ope == \'sum\':base += lst[r]\n if ope == \'prd\':base *= lst[r]\n diff = max(diff, r-l+1)\n r += 1\n elif l == r:\n r += 1\n l += 1\n else:\n if ope == \'sum\':base -= lst[l]\n if ope == \'prd\':base //= lst[l]\n l += 1\n return diff\n\n\nto_csr_matrix = lambda graph:import_module(\'scipy.sparse\').csr_matrix(graph)\nconnected_components = lambda graph:import_module(\'scipy.sparse.csgraph\').connected_components(csgraph=to_csr_matrix(graph), directed=False, return_labels=True) \nshortest_path = lambda graph:import_module(\'scipy.sparse.csgraph\').shortest_path(to_csr_matrix(graph), directed=False) \n\nを初期化(グラフの要素数、グラフの線の両端、有向無向)\ndef init_graph(n,line_lst,directed=False):\n visited,graph = init_array_1dim(False,n),init_array_2dim(False,n,n)\n for line in line_lst:\n graph[line[0]][line[1]] = True\n if not directed:graph[line[1]][line[0]] = True \n return visited,graph\n\ndef solution():\n \n N,M = input_ints()\n graph = npzeros(N)\n AB = [[line[0]-1,line[1]-1] for line in input_lines(M,input_ints_list)]\n AB.reverse()\n\n ans = []\n count = N*(N-1)//2 \n \n np = import_module("numpy")\n _, labels = connected_components(graph)\n for ab in AB:\n ans.append(count)\n if labels[ab[0]] != labels[ab[1]]:\n count -= np.where(labels == labels[ab[0]])[0].shape[0]*np.where(labels == labels[ab[1]])[0].shape[0]\n \n graph[ab[0],ab[1]] = 1\n graph[ab[1],ab[0]] = 1\n _, labels = connected_components(graph)\n\n ans.reverse()\n for _ans in ans:\n print(_ans)\n\t\nif __name__ == \'__main__\':\n solution()', "import sys\nsys.setrecursionlimit(10**9)\ninput = sys.stdin.readline\ninput_int = lambda:int(input())\ninput_ints = lambda:map(int,input().split())\ninput_ints_list = lambda:list(input_ints())\ninput_str = lambda:input()\ninput_strs = lambda:input().split()\ninput_lines = lambda n,input_func:[input_func() for _ in range(n)]\n\nimport importlib\nimport_module = lambda module_name:importlib.import_module(module_name)\n\ninit_array_1dim = lambda value,n:[value]*n \ninit_array_2dim = lambda value,n,m:[init_array_1dim(value,n) for _ in range(m)] \n\ngcd_base = lambda value1,value2:import_module('fractions').gcd(value1,value2) \ngcd = lambda lst:import_module('functools').reduce(gcd_base,lst) \nlcm_base = lambda value1,value2:(value1*value2)//gcd_base(value1,value2) \nlcm = lambda lst:import_module('functools').reduce(lcm_base,lst,1) \n\npermutations = lambda lst,n:import_module('itertools').permutations(lst,n) \ncombinations = lambda lst,n:import_module('itertools').combinations(lst,n) \nproduct = lambda lst1,lst2:import_module('itertools').product(lst1,lst2) \n\nround = lambda value:round(value) \nceil = lambda value:import_module('math').ceil(value) \nfloor = lambda value:import_module('math').floor(value) \n\n\ninit_q = lambda lst: import_module('collections').deque(lst)\nq_pop = lambda q: q.popleft() \nq_push = lambda q,value:q.appendleft(value) \nq_pushlist = lambda q,lst:q.extendleft(lst) \nq_append = lambda q,value:q.append(value) \nq_appendlist = lambda q,lst:q.extend(lst) \n\n\ninit_heap = lambda a:import_module('heapq').heapify(a)\nheap_push = lambda a,v:import_module('heapq').heappush(a,v)\nheap_pop = lambda a:import_module('heapq').heappop(a) \n\n\nbisect_left = lambda a,x:import_module('bisect').bisect_left(a,x)\nbisect_right = lambda a,x:import_module('bisect').bisect_left(a,x)\ninsert_left = lambda a,x:import_module('bisect').insert_left(a,bisect_left(a,x))\ninsert_right = lambda a,x:import_module('bisect').insert_right(a,bisect_right(a,x))\n\n# numpy\nnparray = lambda lst:import_module('numpy').array(lst)\nnpzeros = lambda n:import_module('numpy').zeros((n,n))\nnpones = lambda n:import_module('numpy').ones((n,n))\n\n\ncumsum = lambda a:import_module('numpy').cumsum(import_module('numpy').array(a)) \ncumprod = lambda a:import_module('numpy').cumprod(import_module('numpy').array(a)) \n\ndef two_pointers(lst,threshold,ope='sum'):\n l,r = 0,0\n base = 0 \n if ope == 'sum':base = 0\n if ope == 'prd':base = 1\n diff = 0\n while r < len(lst):\n val = 0\n if ope == 'sum':val = base + lst[r]\n if ope == 'prd':val = base * lst[r]\n if val <= threshold:\n if ope == 'sum':base += lst[r]\n if ope == 'prd':base *= lst[r]\n diff = max(diff, r-l+1)\n r += 1\n elif l == r:\n r += 1\n l += 1\n else:\n if ope == 'sum':base -= lst[l]\n if ope == 'prd':base //= lst[l]\n l += 1\n return diff\n\n\nto_csr_matrix = lambda graph:import_module('scipy.sparse').csr_matrix(graph)\nconnected_components = lambda graph:import_module('scipy.sparse.csgraph').connected_components(csgraph=to_csr_matrix(graph), directed=False, return_labels=True) \nshortest_path = lambda graph:import_module('scipy.sparse.csgraph').shortest_path(to_csr_matrix(graph), directed=False) \n\nを初期化(グラフの要素数、グラフの線の両端、有向無向)\ndef creat_graph(n,line_lst,directed=False):\n graph = npzeros(n)\n for line in line_lst:\n graph[line[0]][line[1]] = 1\n if not directed:graph[line[1]][line[0]] = 1 \n return graph\n\ndef solution():\n \n N,M = input_ints()\n graph = init_array_2dim(0,N,N)\n AB = [[line[0]-1,line[1]-1] for line in input_lines(M,input_ints_list)]\n AB.reverse()\n\n ans = []\n count = N*(N-1)//2 \n \n _, labels = connected_components(graph)\n for ab in AB:\n ans.append(count)\n if labels[ab[0]] != labels[ab[1]]:\n count -= len(labels[labels == labels[ab[0]]]) * len(labels[labels == labels[ab[1]]])\n \n graph[ab[0]][ab[1]] = 1\n graph[ab[1]][ab[0]] = 1\n _, labels = connected_components(graph)\n\n ans.reverse()\n for _ans in ans:\n print(_ans)\n\t\nif __name__ == '__main__':\n solution()", "import sys\nsys.setrecursionlimit(10**9)\ninput = sys.stdin.readline\ninput_int = lambda:int(input())\ninput_ints = lambda:map(int,input().split())\ninput_ints_list = lambda:list(input_ints())\ninput_str = lambda:input()\ninput_strs = lambda:input().split()\ninput_lines = lambda n,input_func:[input_func() for _ in range(n)]\n\nimport importlib\nimport_module = lambda module_name:importlib.import_module(module_name)\n\ninit_array_1dim = lambda value,n:[value]*n \ninit_array_2dim = lambda value,n,m:[init_array_1dim(value,n) for _ in range(m)] \n\ngcd_base = lambda value1,value2:import_module('fractions').gcd(value1,value2) \ngcd = lambda lst:import_module('functools').reduce(gcd_base,lst) \nlcm_base = lambda value1,value2:(value1*value2)//gcd_base(value1,value2) \nlcm = lambda lst:import_module('functools').reduce(lcm_base,lst,1) \n\npermutations = lambda lst,n:import_module('itertools').permutations(lst,n) \ncombinations = lambda lst,n:import_module('itertools').combinations(lst,n) \nproduct = lambda lst1,lst2:import_module('itertools').product(lst1,lst2) \n\nround = lambda value:round(value) \nceil = lambda value:import_module('math').ceil(value) \nfloor = lambda value:import_module('math').floor(value) \n\n\ninit_q = lambda lst: import_module('collections').deque(lst)\nq_pop = lambda q: q.popleft() \nq_push = lambda q,value:q.appendleft(value) \nq_pushlist = lambda q,lst:q.extendleft(lst) \nq_append = lambda q,value:q.append(value) \nq_appendlist = lambda q,lst:q.extend(lst) \n\n\ninit_heap = lambda a:import_module('heapq').heapify(a)\nheap_push = lambda a,v:import_module('heapq').heappush(a,v)\nheap_pop = lambda a:import_module('heapq').heappop(a) \n\n\nbisect_left = lambda a,x:import_module('bisect').bisect_left(a,x)\nbisect_right = lambda a,x:import_module('bisect').bisect_left(a,x)\ninsert_left = lambda a,x:import_module('bisect').insert_left(a,bisect_left(a,x))\ninsert_right = lambda a,x:import_module('bisect').insert_right(a,bisect_right(a,x))\n\n# numpy\nnparray = lambda lst:import_module('numpy').array(lst)\nnpzeros = lambda n:import_module('numpy').zeros((n,n))\nnpones = lambda n:import_module('numpy').ones((n,n))\n\n\ncumsum = lambda a:import_module('numpy').cumsum(import_module('numpy').array(a)) \ncumprod = lambda a:import_module('numpy').cumprod(import_module('numpy').array(a)) \n\ndef two_pointers(lst,threshold,ope='sum'):\n l,r = 0,0\n base = 0 \n if ope == 'sum':base = 0\n if ope == 'prd':base = 1\n diff = 0\n while r < len(lst):\n val = 0\n if ope == 'sum':val = base + lst[r]\n if ope == 'prd':val = base * lst[r]\n if val <= threshold:\n if ope == 'sum':base += lst[r]\n if ope == 'prd':base *= lst[r]\n diff = max(diff, r-l+1)\n r += 1\n elif l == r:\n r += 1\n l += 1\n else:\n if ope == 'sum':base -= lst[l]\n if ope == 'prd':base //= lst[l]\n l += 1\n return diff\n\n\nto_csr_matrix = lambda graph:import_module('scipy.sparse').csr_matrix(graph)\nconnected_components = lambda graph:import_module('scipy.sparse.csgraph').connected_components(to_csr_matrix(graph), directed=False, return_labels=True) \nshortest_path = lambda graph:import_module('scipy.sparse.csgraph').shortest_path(to_csr_matrix(graph), directed=False) \n\nを初期化(グラフの要素数、グラフの線の両端、有向無向)\ndef creat_graph(n,line_lst,directed=False):\n graph = npzeros(n)\n for line in line_lst:\n graph[line[0]][line[1]] = 1\n if not directed:graph[line[1]][line[0]] = 1 \n return graph\n\ndef solution():\n \n N,M = input_ints()\n graph = npzeros(N)\n AB = [[line[0]-1,line[1]-1] for line in input_lines(M,input_ints_list)]\n AB.reverse()\n\n ans = []\n count = N*(N-1)//2 \n \n _, labels = connected_components(graph)\n for a,b in AB:\n ans.append(count)\n if labels[a] != labels[b]:\n count -= labels[labels == labels[a]].size * labels[labels == labels[b]].size\n \n graph[a,b] = 1\n graph[b,a] = 1\n _,labels = connected_components(graph)\n\n ans.reverse()\n for _ans in ans:\n print(_ans)\n\t\nif __name__ == '__main__':\n solution()", 'INF = float("inf")\n\nimport sys\nsys.setrecursionlimit(10**9)\ninput = sys.stdin.readline\ninput_int = lambda:int(input())\ninput_ints = lambda:map(int,input().split())\ninput_ints_list = lambda:list(input_ints())\ninput_str = lambda:input()\ninput_strs = lambda:input().split()\ninput_lines = lambda n,input_func:[input_func() for _ in range(n)]\n\nimport importlib\nimport_module = lambda module_name:importlib.import_module(module_name)\n\ninit_array_1dim = lambda value,n:[value]*n \ninit_array_2dim = lambda value,n,m:[init_array_1dim(value,n) for _ in range(m)] \n\ngcd_base = lambda value1,value2:import_module(\'fractions\').gcd(value1,value2) \ngcd = lambda lst:import_module(\'functools\').reduce(gcd_base,lst) \nlcm_base = lambda value1,value2:(value1*value2)//gcd_base(value1,value2) \nlcm = lambda lst:import_module(\'functools\').reduce(lcm_base,lst,1) \n\npermutations = lambda lst,n:import_module(\'itertools\').permutations(lst,n) \ncombinations = lambda lst,n:import_module(\'itertools\').combinations(lst,n) \nproduct = lambda lst1,lst2:import_module(\'itertools\').product(lst1,lst2) \n\nround = lambda value:round(value) \nceil = lambda value:import_module(\'math\').ceil(value) \nfloor = lambda value:import_module(\'math\').floor(value) \n\n\ninit_q = lambda lst: import_module(\'collections\').deque(lst)\nq_pop = lambda q: q.popleft() \nq_push = lambda q,value:q.appendleft(value) \nq_pushlist = lambda q,lst:q.extendleft(lst) \nq_append = lambda q,value:q.append(value) \nq_appendlist = lambda q,lst:q.extend(lst) \n\n\ninit_heap = lambda a:import_module(\'heapq\').heapify(a)\nheap_push = lambda a,v:import_module(\'heapq\').heappush(a,v)\nheap_pop = lambda a:import_module(\'heapq\').heappop(a) \n\n\nbisect_left = lambda a,x:import_module(\'bisect\').bisect_left(a,x)\nbisect_right = lambda a,x:import_module(\'bisect\').bisect_left(a,x)\ninsert_left = lambda a,x:import_module(\'bisect\').insert_left(a,bisect_left(a,x))\ninsert_right = lambda a,x:import_module(\'bisect\').insert_right(a,bisect_right(a,x))\n\n# numpy\nnparray = lambda lst:import_module(\'numpy\').array(lst)\nnpzeros = lambda n:import_module(\'numpy\').zeros((n,n))\nnpones = lambda n:import_module(\'numpy\').ones((n,n))\n\n\ncumsum = lambda a:import_module(\'numpy\').cumsum(import_module(\'numpy\').array(a)) \ncumprod = lambda a:import_module(\'numpy\').cumprod(import_module(\'numpy\').array(a)) \n\ndef two_pointers(lst,threshold,ope=\'sum\'):\n l,r = 0,0\n base = 0 \n if ope == \'sum\':base = 0\n if ope == \'prd\':base = 1\n diff = 0\n while r < len(lst):\n val = 0\n if ope == \'sum\':val = base + lst[r]\n if ope == \'prd\':val = base * lst[r]\n if val <= threshold:\n if ope == \'sum\':base += lst[r]\n if ope == \'prd\':base *= lst[r]\n diff = max(diff, r-l+1)\n r += 1\n elif l == r:\n r += 1\n l += 1\n else:\n if ope == \'sum\':base -= lst[l]\n if ope == \'prd\':base //= lst[l]\n l += 1\n return diff\n\n\nto_csr_matrix = lambda graph:import_module(\'scipy.sparse\').csr_matrix(graph)\nconnected_components = lambda graph,directed=False:import_module(\'scipy.sparse.csgraph\').connected_components(csgraph=to_csr_matrix(graph), return_labels=True) \nshortest_path = lambda graph:import_module(\'scipy.sparse.csgraph\').shortest_path(to_csr_matrix(graph), directed=False) \n\nを初期化(グラフの要素数、グラフの線の両端、有向無向)\ndef init_graph(n,line_lst,directed=False):\n visited,graph = init_array_1dim(False,n),init_array_2dim(False,n,n)\n for line in line_lst:\n graph[line[0]][line[1]] = True\n if not directed:graph[line[1]][line[0]] = True \n return visited,graph\n\ndef solution():\n np = import_module(\'numpy\')\n \n N,M = input_ints()\n graph = npzeros(N)\n AB = [[line[0]-1,line[1]-1] for line in input_lines(M,input_ints_list)]\n AB.reverse()\n\n ans = init_array_1dim(0,M)\n count = N*(N-1)//2 \n \n _, labels = connected_components(graph,True)\n for i in range(M):\n ans[M-i-1] = count\n graph[AB[i][0],AB[i][1]] = 1\n graph[AB[i][1],AB[i][0]] = 1\n \n if labels[AB[i][0]] != labels[AB[i][1]]:\n count -= np.where(labels == labels[AB[i][0]])[0].shape[0]*np.where(labels == labels[AB[i][1]])[0].shape[0]\n pred, labels = connected_components(graph,True)\n \n for _ans in ans:\n print(_ans)\n\t\nif __name__ == \'__main__\':\n solution()', "import sys\nsys.setrecursionlimit(10**9)\ninput = sys.stdin.readline\ninput_int = lambda:int(input())\ninput_ints = lambda:map(int,input().split())\ninput_ints_list = lambda:list(input_ints())\ninput_str = lambda:input()\ninput_strs = lambda:input().split()\ninput_lines = lambda n,input_func:[input_func() for _ in range(n)]\n\nimport importlib\nimport_module = lambda module_name:importlib.import_module(module_name)\n\ninit_array_1dim = lambda value,n:[value]*n \ninit_array_2dim = lambda value,n,m:[init_array_1dim(value,n) for _ in range(m)] \n\ngcd_base = lambda value1,value2:import_module('fractions').gcd(value1,value2) \ngcd = lambda lst:import_module('functools').reduce(gcd_base,lst) \nlcm_base = lambda value1,value2:(value1*value2)//gcd_base(value1,value2) \nlcm = lambda lst:import_module('functools').reduce(lcm_base,lst,1) \n\npermutations = lambda lst,n:import_module('itertools').permutations(lst,n) \ncombinations = lambda lst,n:import_module('itertools').combinations(lst,n) \nproduct = lambda lst1,lst2:import_module('itertools').product(lst1,lst2) \n\nround = lambda value:round(value) \nceil = lambda value:import_module('math').ceil(value) \nfloor = lambda value:import_module('math').floor(value) \n\n\ninit_q = lambda lst: import_module('collections').deque(lst)\nq_pop = lambda q: q.popleft() \nq_push = lambda q,value:q.appendleft(value) \nq_pushlist = lambda q,lst:q.extendleft(lst) \nq_append = lambda q,value:q.append(value) \nq_appendlist = lambda q,lst:q.extend(lst) \n\n\ninit_heap = lambda a:import_module('heapq').heapify(a)\nheap_push = lambda a,v:import_module('heapq').heappush(a,v)\nheap_pop = lambda a:import_module('heapq').heappop(a) \n\n\nbisect_left = lambda a,x:import_module('bisect').bisect_left(a,x)\nbisect_right = lambda a,x:import_module('bisect').bisect_left(a,x)\ninsert_left = lambda a,x:import_module('bisect').insert_left(a,bisect_left(a,x))\ninsert_right = lambda a,x:import_module('bisect').insert_right(a,bisect_right(a,x))\n\n# numpy\nnparray = lambda lst:import_module('numpy').array(lst)\nnpzeros = lambda n:import_module('numpy').zeros((n,n))\nnpones = lambda n:import_module('numpy').ones((n,n))\n\n\ncumsum = lambda a:import_module('numpy').cumsum(import_module('numpy').array(a)) \ncumprod = lambda a:import_module('numpy').cumprod(import_module('numpy').array(a)) \n\ndef two_pointers(lst,threshold,ope='sum'):\n l,r = 0,0\n base = 0 \n if ope == 'sum':base = 0\n if ope == 'prd':base = 1\n diff = 0\n while r < len(lst):\n val = 0\n if ope == 'sum':val = base + lst[r]\n if ope == 'prd':val = base * lst[r]\n if val <= threshold:\n if ope == 'sum':base += lst[r]\n if ope == 'prd':base *= lst[r]\n diff = max(diff, r-l+1)\n r += 1\n elif l == r:\n r += 1\n l += 1\n else:\n if ope == 'sum':base -= lst[l]\n if ope == 'prd':base //= lst[l]\n l += 1\n return diff\n\n\nto_csr_matrix = lambda graph:import_module('scipy.sparse').csr_matrix(graph)\nconnected_components = lambda graph:import_module('scipy.sparse.csgraph').connected_components(csgraph=to_csr_matrix(graph), directed=False, return_labels=True) \nshortest_path = lambda graph:import_module('scipy.sparse.csgraph').shortest_path(to_csr_matrix(graph), directed=False) \n\nを初期化(グラフの要素数、グラフの線の両端、有向無向)\ndef creat_graph(n,line_lst,directed=False):\n graph = npzeros(n)\n for line in line_lst:\n graph[line[0]][line[1]] = 1\n if not directed:graph[line[1]][line[0]] = 1 \n return graph\n\ndef solution():\n \n N,M = input_ints()\n graph = init_array_2dim(0,N,N)\n AB = [[line[0]-1,line[1]-1] for line in input_lines(M,input_ints_list)]\n AB.reverse()\n\n ans = []\n count = N*(N-1)//2 \n \n _, labels = connected_components(graph)\n for a,b in AB:\n ans.append(count)\n if labels[a] != labels[b]:\n count -= len(labels[labels == labels[a]]) * len(labels[labels == labels[b]])\n \n graph[a][b] = 1\n graph[a][b] = 1\n _, labels = connected_components(graph)\n\n ans.reverse()\n for _ans in ans:\n print(_ans)\n\t\nif __name__ == '__main__':\n solution()", 'INF = float("inf")\n\nimport sys\nsys.setrecursionlimit(10**9)\ninput = sys.stdin.readline\ninput_int = lambda:int(input())\ninput_ints = lambda:map(int,input().split())\ninput_ints_list = lambda:list(input_ints())\ninput_str = lambda:input()\ninput_strs = lambda:input().split()\ninput_lines = lambda n,input_func:[input_func() for _ in range(n)]\n\nimport importlib\nimport_module = lambda module_name:importlib.import_module(module_name)\n\ninit_array_1dim = lambda value,n:[value]*n \ninit_array_2dim = lambda value,n,m:[init_array_1dim(value,n) for _ in range(m)] \n\ngcd_base = lambda value1,value2:import_module(\'fractions\').gcd(value1,value2) \ngcd = lambda lst:import_module(\'functools\').reduce(gcd_base,lst) \nlcm_base = lambda value1,value2:(value1*value2)//gcd_base(value1,value2) \nlcm = lambda lst:import_module(\'functools\').reduce(lcm_base,lst,1) \n\npermutations = lambda lst,n:import_module(\'itertools\').permutations(lst,n) \ncombinations = lambda lst,n:import_module(\'itertools\').combinations(lst,n) \nproduct = lambda lst1,lst2:import_module(\'itertools\').product(lst1,lst2) \n\nround = lambda value:round(value) \nceil = lambda value:import_module(\'math\').ceil(value) \nfloor = lambda value:import_module(\'math\').floor(value) \n\n\ninit_q = lambda lst: import_module(\'collections\').deque(lst)\nq_pop = lambda q: q.popleft() \nq_push = lambda q,value:q.appendleft(value) \nq_pushlist = lambda q,lst:q.extendleft(lst) \nq_append = lambda q,value:q.append(value) \nq_appendlist = lambda q,lst:q.extend(lst) \n\n\ninit_heap = lambda a:import_module(\'heapq\').heapify(a)\nheap_push = lambda a,v:import_module(\'heapq\').heappush(a,v)\nheap_pop = lambda a:import_module(\'heapq\').heappop(a) \n\n\nbisect_left = lambda a,x:import_module(\'bisect\').bisect_left(a,x)\nbisect_right = lambda a,x:import_module(\'bisect\').bisect_left(a,x)\ninsert_left = lambda a,x:import_module(\'bisect\').insert_left(a,bisect_left(a,x))\ninsert_right = lambda a,x:import_module(\'bisect\').insert_right(a,bisect_right(a,x))\n\n# numpy\nnparray = lambda lst:import_module(\'numpy\').array(lst)\nnpzeros = lambda n:import_module(\'numpy\').zeros((n,n))\nnpones = lambda n:import_module(\'numpy\').ones((n,n))\n\n\ncumsum = lambda a:import_module(\'numpy\').cumsum(import_module(\'numpy\').array(a)) \ncumprod = lambda a:import_module(\'numpy\').cumprod(import_module(\'numpy\').array(a)) \n\ndef two_pointers(lst,threshold,ope=\'sum\'):\n l,r = 0,0\n base = 0 \n if ope == \'sum\':base = 0\n if ope == \'prd\':base = 1\n diff = 0\n while r < len(lst):\n val = 0\n if ope == \'sum\':val = base + lst[r]\n if ope == \'prd\':val = base * lst[r]\n if val <= threshold:\n if ope == \'sum\':base += lst[r]\n if ope == \'prd\':base *= lst[r]\n diff = max(diff, r-l+1)\n r += 1\n elif l == r:\n r += 1\n l += 1\n else:\n if ope == \'sum\':base -= lst[l]\n if ope == \'prd\':base //= lst[l]\n l += 1\n return diff\n\n\nto_csr_matrix = lambda graph:import_module(\'scipy.sparse\').csr_matrix(graph)\nconnected_components = lambda graph:import_module(\'scipy.sparse.csgraph\').connected_components(csgraph=to_csr_matrix(graph), directed=False, return_labels=True) \nshortest_path = lambda graph:import_module(\'scipy.sparse.csgraph\').shortest_path(to_csr_matrix(graph), directed=False) \n\nを初期化(グラフの要素数、グラフの線の両端、有向無向)\ndef init_graph(n,line_lst,directed=False):\n visited,graph = init_array_1dim(False,n),init_array_2dim(False,n,n)\n for line in line_lst:\n graph[line[0]][line[1]] = True\n if not directed:graph[line[1]][line[0]] = True \n return visited,graph\n\ndef solution():\n np = import_module(\'numpy\')\n \n N,M = input_ints()\n graph = npzeros(N)\n AB = [[line[0]-1,line[1]-1] for line in input_lines(M,input_ints_list)]\n AB.reverse()\n\n ans = init_array_1dim(0,M)\n count = N*(N-1)//2 \n \n _, labels = connected_components(graph)\n for i in range(M):\n ans[M-i-1] = count\n graph[AB[i][0],AB[i][1]] = 1\n graph[AB[i][1],AB[i][0]] = 1\n \n if labels[AB[i][0]] != labels[AB[i][1]]:\n count -= np.where(labels == labels[AB[i][0]])[0].shape[0]*np.where(labels == labels[AB[i][1]])[0].shape[0]\n _, labels = connected_components(graph)\n \n for _ans in ans:\n print(_ans)\n\t\nif __name__ == \'__main__\':\n solution()', 'import sys\n\ninput_int = lambda:int(input())\ninput_ints = lambda:map(int,input().split())\ninput_ints_list = lambda:list(input_ints())\ninput_str = lambda:input()\ninput_strs = lambda:input().split()\ninput_lines = lambda n,f:[f() for _ in range(n)]\n\nimport functools,fractions\ngcd = lambda a:functools.reduce(fractions.gcd,a) \nlcm_base = lambda a,b:(a*b)//fractions.gcd(a,b) \nlcm = lambda a:functools.reduce(lcm_base,a,1) \n\nimport itertools\npermutations = lambda a,n:itertools.permutations(a,n) \ncombinations = lambda a,n:itertools.combinations(a,n) \nproduct = lambda a,b:itertools.product(a,b) \n\ninit_array_1dim = lambda v,n:[v for _ in range(n)]\ninit_array_2dim = lambda v,n,m:[[v for _ in range(n)] for _ in range(m)]\n\nimport math\n\nceil = lambda a:math.ceil(a) \nfloor = lambda a:math.floor(a) \n\nclass UnionFind(object):\n def __init__(self, n):\n self.par = [i for i in range(n)]\n self.rank = [0 for _ in range(n)]\n self.size = [1 for _ in range(n)]\n\n def find(self, x):\n if self.par[x] == x:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x != y:\n if self.rank[x] < self.rank[y]:\n x, y = y, x\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n self.par[y] = x\n self.size[x] += self.size[y]\n\n def is_same(self, x, y):\n return self.find(x) == self.find(y)\n\n def get_size(self, x):\n x = self.find(x)\n return self.size[x]\n\n\ndef dfs_graph(v,n,graph,visited):\n\tif all(visited):return 1 \n\tans = 0\n\tfor i in range(n):\n\t\tif not graph[v][i]:continue \n\t\tif visited[i]:continue \n\t\tvisited[i] = True\n\t\tans += dfs_graph(i,n,graph,visited)\n\t\tvisited[i] = False\n\treturn ans\n\n\ndef warshall_floyd(n,graph):\n\td = init_array_2dim(float("inf"),n,n)\n\t\n\tfor i in range(n):\n\t\td[i][i] = 0\n\t\tfor j in range(n):\n\t\t\tif graph[i][j]:d[i][j] = 1\n\t\n\tfor k in range(n):\n\t\tfor i in range(n):\n\t\t\tfor j in range(n):\n\t\t\t\td[i][j] = min(d[i][j],d[i][k] + d[k][j])\n\treturn d\n\n\ndef init_graph(n,a,directed=False):\n visited = init_array_1dim(False,n)\n graph = init_array_2dim(False,n,n)\n for e in a:\n graph[e[0]-1][e[1]-1] = True\n if not directed:graph[e[1]-1][e[0]-1] = True \n return visited,graph\n\ndef solution():\n\t\n N,M = input_ints()\n AB = input_lines(M,input_ints_list)\n\n ans = []\n count = N*(N-1)//2 \n uf = UnionFind(N)\n \n while(AB):\n ans.append(count)\n a,b = AB.pop()\n if not uf.is_same(a-1,b-1):\n count -= uf.get_size(a-1)*uf.get_size(b-1)\n uf.union(a-1,b-1)\n \n ans.reverse()\n for _ans in ans:\n print(_ans)\n\t\nif __name__ == \'__main__\':\n solution()']
['Runtime Error', 'Runtime Error', 'Time Limit Exceeded', 'Runtime Error', 'Time Limit Exceeded', 'Runtime Error', 'Runtime Error', 'Time Limit Exceeded', 'Runtime Error', 'Accepted']
['s000335639', 's122838643', 's165406577', 's281872486', 's363171758', 's399100939', 's545288211', 's682532550', 's890314926', 's715798784']
[42900.0, 42848.0, 2078196.0, 42884.0, 2087796.0, 42836.0, 42844.0, 2080372.0, 42800.0, 35536.0]
[2110.0, 2110.0, 2233.0, 2111.0, 2233.0, 2110.0, 2110.0, 2240.0, 2110.0, 842.0]
[4962, 5094, 4976, 5064, 4968, 4914, 5134, 4937, 5122, 3526]
p03108
u487252913
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['class UnionFind:\n def __init__(self, size: int):\n self.parent = [-1 for _ in range(size)]\n self.size = [1 for _ in range(size)]\n\n def unite(self, x, y):\n px = self.find(x)\n py = self.find(y)\n if px == py:\n return\n if self.size[px] > self.size[py]:\n px, py = py, px\n self.parent[py] += self.parent[px]\n self.parent[px] = py\n return\n\n def find(self, x: int) -> int:\n if self.parent[x] < 0:\n return x\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def size(self, x):\n return -self.parent[self.find(x)]\n\n def is_same(self, x: int, y: int) -> bool:\n return self.find(x) == self.find(y)\n\n\ndef main():\n # input\n N, M = map(int, input().split())\n A, B = [], []\n\n for _ in range(M):\n a, b = map(int, input().split())\n A.append(a-1)\n B.append(b-1)\n\n # solve\n \n \n uf = UnionFind(N)\n ans = [N*(N-1)//2]\n for i in range(M-1, 0, -1):\n p1 = uf.find(A[i])\n p2 = uf.find(B[i])\n if p1 != p2:\n ans.append(ans[-1] - uf.size[p1]*uf.size[p2])\n else:\n ans.append(ans[-1])\n uf.unite(p1, p2)\n return ans\n\n\nprint(*list(reversed(main())), sep="\\n")\n', 'class UnionFind:\n def __init__(self, size: int):\n self.parent = [-1 for _ in range(size)]\n self.size = [1 for _ in range(size)]\n\n def unite(self, x, y):\n px = self.find(x)\n py = self.find(y)\n if px == py:\n return\n if self.parent[px] > self.parent[py]:\n px, py = py, px\n self.parent[px] = py\n self.parent[py] -= 1\n self.size[py] += self.size[px]\n self.size[px] = 0\n\n def find(self, x: int) -> int:\n if self.parent[x] < 0:\n return x\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def is_same(self, x: int, y: int) -> bool:\n return self.find(x) == self.find(y)\n\n\ndef main():\n # input\n N, M = map(int, input().split())\n A = list()\n B = list()\n\n for i in range(M):\n pair = list(map(int, input().split()))\n A.append(pair[0])\n B.append(pair[1])\n\n # solve\n \n \n uf = UnionFind(N)\n ans = [N*(N-1)//2]\n for i in range(M-1, 0, -1):\n p1 = uf.find(A[i])\n p2 = uf.find(B[i])\n if p1 != p2:\n ans.append(ans[-1] - uf.size[p1]*uf.size[p2])\n else:\n ans.append(ans[-1])\n uf.unite(p1, p2)\n return ans\n\n\nprint(*list(reversed(main())), sep="\\n")\n', 'class UnionFind:\n def __init__(self, size: int):\n self.parent = [-1 for _ in range(size)]\n\n def unite(self, x, y):\n px = self.find(x)\n py = self.find(y)\n if px == py:\n return\n if self.size(px) > self.size(py):\n px, py = py, px\n self.parent[py] += self.parent[px]\n self.parent[px] = py\n return\n\n def find(self, x: int) -> int:\n if self.parent[x] < 0:\n return x\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def size(self, x):\n return -self.parent[self.find(x)]\n\n def is_same(self, x: int, y: int) -> bool:\n return self.find(x) == self.find(y)\n\n\ndef main():\n # input\n N, M = map(int, input().split())\n A, B = [], []\n\n for _ in range(M):\n a, b = map(int, input().split())\n A.append(a-1)\n B.append(b-1)\n\n # solve\n \n \n uf = UnionFind(N)\n ans = [N*(N-1)//2]\n for i in range(M-1, 0, -1):\n p1 = uf.find(A[i])\n p2 = uf.find(B[i])\n if p1 != p2:\n ans.append(ans[-1] - uf.size(p1)*uf.size(p2))\n else:\n ans.append(ans[-1])\n uf.unite(p1, p2)\n return ans\n\n\nprint(*list(reversed(main())), sep="\\n")\n']
['Runtime Error', 'Runtime Error', 'Accepted']
['s002511465', 's249132322', 's048514460']
[16512.0, 16496.0, 15768.0]
[607.0, 596.0, 655.0]
[1424, 1424, 1379]
p03108
u504562455
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['import sys\nsys.setrecursionlimit(20000)\nN, M = [int(i) for i in input().split()]\nparent = [i for i in range(N+1)]\nnode_num = [1 for i in range(N+1)]\ndef find(x):\n if parent[x] == x:\n return x\n else:\n parent[x] = find(parent[x])\n return parent[x]\n\nbridge = []\nfor i in range(M):\n A, B = [int(i) for i in input().split()]\n bridge.append([A, B])\n\nbridge = bridge[::-1]\nans_rev = [N*(N-1)//2]\ncombi = 0\nfor i in range(M-1):\n A, B = bridge[i]\n r_A = find(A)\n r_B = find(B)\n if r_A != r_B:\n parent[r_B] = r_A\n combi += node_num[r_B] * node_num[r_A]\n node_num[r_A] += node_num[r_B]\n ans_rev.append(N*(N-1)//2-combi)\n\nans = ans_rev[::-1]\nprint(*ans, sep="\\n"', 'import sys\nsys.setrecursionlimit(10**6)\n\nN, M = [int(i) for i in input().split()]\nparent = [i for i in range(N+1)]\nnode_num = [1 for i in range(N+1)]\ndef find(x):\n if parent[x] == x:\n return x\n else:\n parent[x] = find(parent[x])\n return parent[x]\n\nbridge = []\nfor i in range(M):\n A, B = [int(i) for i in input().split()]\n bridge.append([A, B])\n\nbridge = bridge[::-1]\nans_rev = [N*(N-1)//2]\ncombi = 0\nfor i in range(M-1):\n A, B = bridge[i]\n r_A = find(A)\n r_B = find(B)\n if r_A != r_B:\n parent[r_B] = r_A\n combi += node_num[r_B] * node_num[r_A]\n node_num[r_A] += node_num[r_B]\n ans_rev.append(N*(N-1)//2-combi)\n\nans = ans_rev[::-1]\nprint(*ans, sep="\\n")']
['Runtime Error', 'Accepted']
['s149870660', 's842634682']
[3064.0, 39160.0]
[18.0, 615.0]
[719, 721]
p03108
u512007680
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['n, m = map(int, input().split())\nbridges_pre = []\nfor i in range(0,m):\n a, b = map(int,input().split())\n bridges_pre.append([a-1,b-1])\n\nbridges = list(reversed(bridges_pre))\nprint(bridges)\n\ndes = list(range(0,n))\nfor i in range(0,n):\n des[i] = list(range(0,n))\n for j in range(0,n):\n if i != j:\n des[i][j] = 0\n else:\n des[i][j] = 1\n \nresult = list(range(0,m))\n\nresult[m-1] = int(n * (n-1) / 2)\n\nfor k in range(0,m-1):\n bri = bridges[k]\n x = bri[0]\n y = bri[1]\n des[x][y] = 1\n des[y][x] = 1\n \n sum2 = 0\n for i in range(0,n):\n if des[i][x] == 1:\n des[i][y] = 1\n des[y][i] = 1\n if des[i][y] == 1:\n des[i][x] = 1\n des[x][i] = 1\n #print(i)\n #print(des)\n sum1 = 0\n for j in range(0,n):\n sum1 = sum1 + des[i][j]\n sum2 = sum2 + sum1\n \n result[m-k-2] = int(n * (n - 1)/2 -(sum2 - n)/2)\n \nfor l in range(0,m):\n print(result[l])\n \n\n', 'class UnionFind:\n def __init__(self, n):\n self.par = [i for i in range(n+1)]\n self.rank = [0] * (n+1)\n self.size = [1] * (n+1)\n self.con = [0] * (m+1)\n\n \n def find(self, x):\n if self.par[x] == x:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n\n \n def union(self, x, y, j):\n x = self.find(x)\n y = self.find(y)\n if x != y:\n self.con[j] = self.con[j+1] + self.size[x] * self.size[y]\n else:\n self.con[j] = self.con[j+1]\n #print([x,y])\n if self.rank[x] < self.rank[y]:\n self.par[x] = y\n self.size[y] = self.size[x] + self.size[y]\n self.size[x] = 0\n else:\n self.par[y] = x\n if x != y:\n self.size[x] = self.size[x] + self.size[y]\n self.size[y] = 0\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n \n def same_check(self, x, y):\n return self.find(x) == self.find(y)\n\nn, m = map(int, input().split())\n\nuf = UnionFind(n)\n\nx = [0] * (m+1)\ny = [0] * (m+1)\n\n\nfor i in range(1,m+1):\n x[i], y[i] = map(int, input().split())\n #print([x,y])\n \nfor j in range(m-1,0,-1):\n #print([x[j+1],y[j+1]])\n uf.union(x[j+1],y[j+1],j)\n \n \n \n\nN = int(n * (n-1) / 2)\n\nfor i in range(1,m+1):\n print(N-uf.con[i])\n ']
['Wrong Answer', 'Accepted']
['s423874578', 's693825390']
[112440.0, 20652.0]
[2111.0, 637.0]
[1027, 1523]
p03108
u518042385
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['n,m=map(int,input().split())\nl=[]\nfor i in range(m):\n l.append(list(map(int,input().split())))\nl=l[::-1]\nparent=[i for i in range(n+1)]\ndepth=[1 for i in range(n+1)]\ndef find(n):\n if parent[n]==n:\n return n\n else:\n parent[n]=find(parent[n])\n return parent[n]\ndef unite(n,m):\n n1=find(n)\n m1=find(m)\n if n1==m1:\n return None\n elif depth[n1]<depth[m1]:\n parent[n1]=m1\n depth[m1]+=depth[n1]\n else:\n parent[m1]=n1\n depth[n1]+=depth[m1]\nsum=n*(n-1)//2\nans=[]\nfor i in range(m):\n ans.append(sum)\n l1=l[i]\n a,b=l1[0],l1[1]\n a1=find(a)\n b1=find(b)\n sum-=depth[a1]*depth[b1]\n unite(a,b)\nfor i in range(1,m+1):\n print(ans[-i])', 'n,m=map(int,input().split())\nl=[]\nfor i in range(m):\n l.append(list(map(int,input().split())))\nl=l[::-1]\nparent=[i for i in range(n+1)]\ndepth=[1 for i in range(n+1)]\ndef find(n):\n if parent[n]==n:\n return n\n else:\n parent[n]=find(parent[n])\n return parent[n]\ndef unite(n,m):\n n1=find(n)\n m1=find(m)\n if n1==m1:\n return None\n elif depth[n1]<depth[m1]:\n parent[n1]=m1\n depth[m1]+=depth[n1]\n else:\n parent[m1]=n1\n depth[n1]+=depth[m1]\nsum=n*(n-1)//2\nans=[]\nfor i in range(m):\n ans.append(sum)\n l1=l[i]\n a,b=l1[0],l1[1]\n a1=find(a)\n b1=find(b)\n if a1==b1:\n pass\n else:\n sum-=depth[a1]*depth[b1]\n unite(a,b)\nfor i in range(1,m+1):\n print(ans[-i])']
['Wrong Answer', 'Accepted']
['s777427994', 's650100937']
[35452.0, 34688.0]
[684.0, 714.0]
[656, 690]
p03108
u522726434
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['class Union_Find():\n def __init__(self, n):\n self.par = [-1] * n\n def root(self, x):\n if self.par[x] < 0:\n return x\n else:\n self.par[x] = root(par[x])\n return self.par[x]\n def unite(self, x, y):\n root_x = self.root(x)\n root_y = self.root(y)\n if root_x == root_y:\n pass\n else:\n self.par[root_x] = root_y\n def same(self, x, y):\n root_x = self.root(x)\n root_y = self.root(y)\n return root_x == root_y\n def count_size(self, x):\n \n return self.par.count(self.root(x))\n \nN, M = list(map(int, input().split()))\nuf = Union_Find(N)\nbridge_list = []\ninconv_list = [0] * (M + 1)\ninconv_list[0] = N * (N - 1) //2\nfor _ in range(M):\n a, b = list(map(int, input().split()))\n bridge_list.append((a-1, b-1))\nbridge_list.reverse()\nfor i in range(M):\n x, y = bridge_list[i]\n if uf.same(x, y):\n inconv_list[i+1] = inconv_list[i]\n else:\n Nx = uf.count_size(x)\n Ny = uf.count_size(y)\n inconv_list[i+1] = inconv_list[i] - Nx * Ny\n uf.unite(x, y)\n \ninconv_list.sort() \n \nfor i in range(1, M+1):\n print(inconv_list[i])', 'class Union_Find():\n def __init__(self, n):\n self.par = list(range(n))\n self.size = [-1] * n\n self.rank = [0] * n\n def root(self, x):\n if self.par[x] == x:\n return x\n else:\n self.par[x] = self.root(self.par[x])\n return self.par[x]\n def unite(self, x, y):\n root_x = self.root(x)\n root_y = self.root(y)\n if root_x == root_y:\n pass\n else:\n if self.rank[root_x] > self.rank[root_y]:\n self.par[root_y] = root_x\n self.rank[root_x] += 1\n self.size[root_x] += self.size[root_y]\n else:\n self.par[root_x] = root_y\n self.rank[root_y] += 1\n self.size[root_y] += self.size[root_x]\n \n \n def same(self, x, y):\n root_x = self.root(x)\n root_y = self.root(y)\n return root_x == root_y\n def count_size(self, x):\n \n return self.size[self.root(x)]\n \nN, M = list(map(int, input().split()))\nuf = Union_Find(N)\nbridge_list = []\ninconv_list = [0] * (M + 1)\ninconv_list[0] = N * (N - 1) //2\nfor _ in range(M):\n a, b = list(map(int, input().split()))\n bridge_list.append((a-1, b-1))\nbridge_list.reverse()\nfor i in range(M):\n x, y = bridge_list[i]\n if uf.same(x, y):\n inconv_list[i+1] = inconv_list[i]\n else:\n Nx = uf.count_size(x)\n Ny = uf.count_size(y)\n inconv_list[i+1] = inconv_list[i] - Nx * Ny\n uf.unite(x, y)\n \ninconv_list.sort() \n \nfor i in range(1, M+1):\n print(inconv_list[i])']
['Runtime Error', 'Accepted']
['s752384882', 's051419388']
[18236.0, 24320.0]
[1738.0, 873.0]
[1134, 1465]
p03108
u529180657
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['n,m = map(int,input().split())\na = [0]*m\nb = [0]*m\nfor i in range(m):\n u,v = map(int, input().split())\n a[i] = u-1\n b[i] = v-1\npar = [i for i in range(n)]\nsize = [1]*n\nincomb = [n*(n-1)//2]*m\n\ndef root(x):\n child = x\n root = par[x]\n while True:\n if root == child: \n break\n else:\n child = root\n root = par[root]\n par[child] = root\n return root\n \ndef isSame(x,y):\n return par[x] == par[y]\n\ndef union(x,y):\n x = root(x)\n y = root(y)\n if x == y:\n return\n else:\n par[y] = x\n size[x] += size[y]\n \ndef size_count(x):\n return size[root(x)]\n\nfor i in reversed(range(1,m)):\n q_a = a[i]\n q_b = b[i]\n size_a = size_count(q_a)\n size_b = size_count(q_b)\n if not isSame(q_a,q_b):\n union(q_a,q_b)\n incomb[i-1] = incomb[i] - size_a*size_b\n else:\n incomb[i-1] = incomb[i]\n\nfor i in incomb:\n print(i)', ' import sys\n sys.setrecursionlimit(100000)\n n,m = map(int,input().split())\n a = [0]*m\n b = [0]*m\n for i in range(m):\n u,v = map(int, input().split())\n a[i] = u-1\n b[i] = v-1\n par = [i for i in range(n)]\n size = [1]*n\n incomb = [n*(n-1)//2]*m\n \n def root(x):\n if par[x] == x: \n return x\n else:\n par[x] = root(par[x])\n return par[x]\n \n def isSame(x,y):\n return par[x] == par[y]\n \n def union(x,y):\n x = root(x)\n y = root(y)\n if x == y:\n return\n else:\n par[y] = x\n size[x] += size[y]\n \n def size_count(x):\n return size[root(x)]\n \n for i in reversed(range(1,m)):\n q_a = a[i]\n q_b = b[i]\n size_a = size_count(q_a)\n size_b = size_count(q_b)\n if not isSame(q_a,q_b):\n union(q_a,q_b)\n incomb[i-1] = incomb[i] - size_a*size_b\n else:\n incomb[i-1] = incomb[i]\n \n for i in incomb:\n print(i)', ' import sys\n sys.setrecursionlimit(3000)\n n,m = map(int,input().split())\n a = [0]*m\n b = [0]*m\n for i in range(m):\n u,v = map(int, input().split())\n a[i] = u-1\n b[i] = v-1\n par = [i for i in range(n)]\n size = [1]*n\n incomb = [n*(n-1)//2]*m\n \n def root(x):\n if par[x] == x: \n return x\n else:\n par[x] = root(par[x])\n return par[x]\n \n def isSame(x,y):\n return par[x] == par[y]\n \n def union(x,y):\n x = root(x)\n y = root(y)\n if x == y:\n return\n else:\n par[y] = x\n size[x] += size[y]\n \n def size_count(x):\n return size[root(x)]\n \n for i in reversed(range(1,m)):\n q_a = a[i]\n q_b = b[i]\n size_a = size_count(q_a)\n size_b = size_count(q_b)\n if not isSame(q_a,q_b):\n union(q_a,q_b)\n incomb[i-1] = incomb[i] - size_a*size_b\n else:\n incomb[i-1] = incomb[i]\n \n for i in incomb:\n print(i)', 'import sys\nsys.setrecursionlimit(1000000)\nn,m = map(int,input().split())\na = [0]*m\nb = [0]*m\nfor i in range(m):\n u,v = map(int, input().split())\n a[i] = u-1\n b[i] = v-1\npar = [i for i in range(n)]\nsize = [1]*n\nincomb = [n*(n-1)//2]*m\n\ndef root(x):\n if par[x] == x: \n return x\n else:\n par[x] = root(par[x])\n return par[x]\n \ndef isSame(x,y):\n return par[x] == par[y]\n\ndef union(x,y):\n x = root(x)\n y = root(y)\n if x == y:\n return\n else:\n par[y] = x\n size[x] += size[y]\n \ndef size_count(x):\n return size[root(x)]\n\nfor i in reversed(range(1,m)):\n q_a = a[i]\n q_b = b[i]\n size_a = size_count(q_a)\n size_b = size_count(q_b)\n if not isSame(q_a,q_b):\n union(q_a,q_b)\n incomb[i-1] = incomb[i] - size_a*size_b\n else:\n incomb[i-1] = incomb[i]\n\nfor i in incomb:\n print(i)']
['Wrong Answer', 'Runtime Error', 'Runtime Error', 'Accepted']
['s512706878', 's603221712', 's897232710', 's623016581']
[20900.0, 2940.0, 2940.0, 32176.0]
[2104.0, 17.0, 20.0, 643.0]
[851, 1005, 1003, 810]
p03108
u532966492
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['N,M=map(int,input().split())\nAB=[list(map(int,input().split())) for _ in range(M)]\nparts=[i for i in range(N)]\nans=[N*(N-1)//2]\nparts_dict=dict([[i,[i]] for i in range(N)])\n\nfor i in range(M-1):\n ab=AB.pop()\n if parts[ab[0]-1]==parts[ab[1]-1]:\n ans.append(ans[-1])\n else:\n a,b=sorted(ab,key=lambda x:parts[x-1])\n len_a,len_b=len(parts_dict[parts[a-1]]),len(parts_dict[parts[b-1]])\n ans.append(ans[-1]-len_a*len_b)\n parts_dict[a-1]+=parts_dict[parts[b-1]]\n for i in parts_dict[parts[b-1]]:\n parts[i]=parts[a-1]\n\nfor i in range(M-2):\n if ans[i+1]==ans[i+2]:\n ans[i+1]=ans[i]\n\nfor i in reversed(ans):\n print(i)', 'N,M=map(int,input().split())\nAB=[list(map(int,input().split())) for _ in range(M)]\nparts=[i for i in range(N)]\nans=[N*(N-1)//2]\nparts_dict=dict([[i,[i]] for i in range(N)])\n\nfor i in range(M-1):\n ab=AB.pop()\n if parts[ab[0]-1]==parts[ab[1]-1]:\n ans.append(ans[-1])\n else:\n a,b=sorted(ab,key=lambda x:parts[x-1])\n len_a,len_b=len(parts_dict[parts[a-1]]),len(parts_dict[parts[b-1]])\n ans.append(ans[-1]-len_a*len_b)\n parts_dict[parts[a-1]]+=parts_dict[parts[b-1]]\n for i in parts_dict[parts[b-1]]:\n parts[i]=parts[a-1]\n print(parts)\n print(parts_dict)\n\nfor i in reversed(ans):\n print(i)', '\ndef find_root(n,parts):\n n=n-1\n \n if parts[n][0]==n:\n return tuple(parts[n])\n else:\n return find_root(parts[n][0]+1,parts)\n\n\nN,M=map(int,input().split())\nAB=[list(map(int,input().split())) for i in range(M)]\n\n\nparts=[[i,1,1] for i in range(N)]\nans=[N*(N-1)//2]\n\n\nfor i in range(M-1):\n \n ab=AB.pop()\n \n a=find_root(ab[0],parts)\n b=find_root(ab[1],parts)\n \n if a[1]>=b[1]:\n a,b=a,b\n else:\n b,a=a,b\n \n if a[0]==b[0]:\n ans.append(ans[-1])\n \n else:\n \n ans.append(ans[-1]-a[2]*b[2])\n \n parts[b[0]][0]=parts[a[0]][0]\n \n parts[a[0]][1]=max(parts[a[0]][1],parts[b[0]][1]+1)\n \n parts[a[0]][2]+=parts[b[0]][2]\n\nfor i in reversed(ans):\n print(i)']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s075395517', 's875817839', 's566481404']
[158604.0, 142760.0, 38180.0]
[2113.0, 2107.0, 772.0]
[682, 663, 1489]
p03108
u533885955
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['N,M=map(int,input().split())\nAB=[list(map(int,input().split())) for i in range(M)]\nABrev = AB[::-1]\nmaxn=int(N*(N-1)/2)\ncango=[]\nfor i in range(M):\n clen=len(cango)\n a=0\n san=0\n for a in range(clen):\n alen=len(cango[a])\n san+=int(alen*(alen-1)/2)\n print(maxn-san)\n j=0\n flag=[0,0]\n for j in range(clen):\n if ABrev[i][0] in cango[j]:\n flag=[1,j]\n break\n else:\n pass\n k=0\n for k in range(clen):\n if ABrev[i][1] in cango[k]:\n if flag[0] == 0:\n cango[k].append(ABrev[i][0])\n break\n elif flag[0] == 1:\n if k == flag[1]:\n pass\n else:\n cango[k]+=cango[flag[1]]\n del cango[flag[1]]\n flag[0]=2\n break\n else:\n pass\n if flag[0]==0:\n cango.append(ABrev[i])\n elif flag[0]==1:\n cango[flag[1]].append(ABrev[i][1])\n ', '\n#UnionFind(normal)\nclass UnionFind():\n \n \n def __init__(self, n):\n self.n = n\n \n \n self.root = [-1]*(n+1)\n \n self.rnk = [0]*(n+1)\n\n \n def Find_Root(self, x):\n if(self.root[x] < 0):\n return x\n else:\n \n self.root[x] = self.Find_Root(self.root[x])\n return self.root[x]\n \n def Unite(self, x, y):\n \n x = self.Find_Root(x)\n y = self.Find_Root(y)\n \n if(x == y):\n return \n \n elif(self.rnk[x] > self.rnk[y]):\n self.root[x] += self.root[y]\n self.root[y] = x\n\n else:\n self.root[y] += self.root[x]\n self.root[x] = y\n \n if(self.rnk[x] == self.rnk[y]):\n self.rnk[y] += 1\n \n def isSameGroup(self, x, y):\n return self.Find_Root(x) == self.Find_Root(y)\n\n \n def Count(self, x):\n return -self.root[self.Find_Root(x)]\n \n\n \n \nN,M=map(int,input().split())\nABre=[list(map(int,input().split())) for i in range(M)]\nAB=ABre[::-1]\ncl=UnionFind(N)\nmaxn=int(N*(N-1)/2)\nans=[maxn]\nfor i in range(M-1):\n a=AB[i][0]\n b=AB[i][1]\n if cl.isSameGroup(a,b):\n ans.append(maxn)\n else:\n asize=cl.Count(a)\n bsize=cl.Count(b)\n maxn-=asize*bsize\n ans.append(maxn)\n cl.Unite(AB[i][0],AB[i][1])\nfor i in range(M):\n print(ans[-1-i])']
['Wrong Answer', 'Accepted']
['s667171768', 's494290913']
[30276.0, 34980.0]
[2105.0, 886.0]
[1008, 2368]
p03108
u545368057
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['\nfrom scipy.misc import comb\n\n\nN, M = map(int,input().split())\npairs = []\nfor i in range(M):\n Ai, Bi = map(int,input().split())\n pairs.append([Ai,Bi])\nconnections = []\nans = [int(comb(N,2,exact=True))]\nfor Ai, Bi in pairs[::-1]:\n # connect_flg = False\n ind = []\n for i,connection in enumerate(connections):\n if Ai in connection and Bi in connection:\n ind = [0]\n continue\n elif Ai in connection:\n connections[i].append(Bi)\n # connect_flg = True\n ind.append(i)\n elif Bi in connection:\n connections[i].append(Ai)\n # connect_flg = True\n ind.append(i)\n \n if len(ind) == 2:\n connections[ind[0]].extend(connections[ind[1]])\n connections[ind[0]] = list(set(connections[ind[0]]))\n connections.remove(connections[ind[1]]) \n elif len(ind) == 0:\n connections.append([Ai,Bi])\n \n cnt = int(comb(N,2,exact=True))\n # print(connections,cnt)\n for connection in connections:\n cnt -= int(comb(len(connection),2,exact=True))\n ans.append(cnt)\nfor a in ans[::-1][1:]:\n print(a)', '\nfrom scipy.misc import comb\n\n\nN, M = map(int,input().split())\npairs = []\nfor i in range(M):\n Ai, Bi = map(int,input().split())\n pairs.append([Ai,Bi])\nconnections = []\nans = [int(comb(N,2),exact=True)]\nfor Ai, Bi in pairs[::-1]:\n # connect_flg = False\n ind = []\n for i,connection in enumerate(connections):\n if Ai in connection and Bi in connection:\n ind = [0]\n continue\n elif Ai in connection:\n connections[i].append(Bi)\n # connect_flg = True\n ind.append(i)\n elif Bi in connection:\n connections[i].append(Ai)\n # connect_flg = True\n ind.append(i)\n \n if len(ind) == 2:\n connections[ind[0]].extend(connections[ind[1]])\n connections[ind[0]] = list(set(connections[ind[0]]))\n connections.remove(connections[ind[1]]) \n elif len(ind) == 0:\n connections.append([Ai,Bi])\n \n cnt = int(comb(N,2,exact=True))\n # print(connections,cnt)\n for connection in connections:\n cnt -= int(comb(len(connection),2,exact=True))\n ans.append(cnt)\nfor a in ans[::-1][1:]:\n print(a)', '\nfrom scipy.misc import comb\n\n\nN, M = map(int,input().split())\npairs = []\nfor i in range(M):\n Ai, Bi = map(int,input().split())\n pairs.append([Ai,Bi])\nconnections = []\nans = [int(comb(N,2))]\nfor Ai, Bi in pairs[::-1]:\n # connect_flg = False\n ind = []\n for i,connection in enumerate(connections):\n if Ai in connection and Bi in connection:\n ind = [0]\n continue\n elif Ai in connection:\n connections[i].append(Bi)\n # connect_flg = True\n ind.append(i)\n elif Bi in connection:\n connections[i].append(Ai)\n # connect_flg = True\n ind.append(i)\n \n if len(ind) == 2:\n connections[ind[0]].extend(connections[ind[1]])\n connections[ind[0]] = list(set(connections[ind[0]]))\n connections.remove(connections[ind[1]]) \n elif len(ind) == 0:\n connections.append([Ai,Bi])\n \n cnt = int(comb(N,2,exact=True))\n # print(connections,cnt)\n for connection in connections:\n cnt -= int(comb(len(connection),2,exact=True))\n ans.append(cnt)\nfor a in ans[::-1][1:]:\n print(a)', 'n, m = map(int, input().split())\n\nfrom collections import defaultdict\nclass UnionFind:\n def __init__(self, n):\n class KeyDict(dict):\n \n def __missing__(self,key):\n self[key] = key\n return key\n self.parent = KeyDict()\n self.rank = defaultdict(int)\n self.weight = defaultdict(lambda:1)\n\n \n def find(self, x):\n if self.parent[x] == x:\n return x\n else:\n \n \n y = self.find(self.parent[x])\n self.parent[x] = y \n return self.parent[x]\n\n \n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n \n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n self.weight[y] += self.weight[x]\n self.weight[x] = 0\n else:\n self.parent[y] = x\n self.weight[x] += self.weight[y]\n self.weight[y] = 0\n \n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n\n \n def judge(self, x, y):\n return self.find(x) == self.find(y)\n\nps = []\nfor i in range(m):\n a,b = map(int, input().split())\n a -= 1\n b -= 1\n ps.append((a,b))\n\nuf = UnionFind(n)\nnow = n*(n-1)//2\nans = [now]\nfor a, b in ps[::-1]:\n if not uf.judge(a,b):\n n1 = uf.weight[uf.find(a)]\n n2 = uf.weight[uf.find(b)]\n now += n1*(n1-1)//2 + n2*(n2-1)//2 - (n1+n2)*(n1+n2-1)//2\n uf.union(a,b)\n ans.append(now)\n\nfor a in ans[::-1][1:]:\n print(a)']
['Time Limit Exceeded', 'Runtime Error', 'Wrong Answer', 'Accepted']
['s099547380', 's350038019', 's452699456', 's078531637']
[32668.0, 30152.0, 33052.0, 44684.0]
[2110.0, 468.0, 2110.0, 703.0]
[1224, 1224, 1213, 1807]
p03108
u548834738
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['N, M = map(int, input().split())\n\npar = list(range(0, N + 1))\nsize = [1] * (N + 1)\nrank = [1] * (N + 1)\n\ndef root(i):\n if par[i] == i:\n return i\n else:\n par[i] = root(par[i])\n return par[i]\n \ndef same(i, j):\n root(i) == root(j)\n \ndef findsize(i):\n size[i] = size[root(i)]\n \ndef unite(i, j):\n i = root(i)\n j = root(j)\n if i != j:\n if rank[i] > rank[j]:\n par[j] = i\n size[i] += size[j]\n else:\n par[i] = j\n size[j] += size[i]\n if rank[i] == rank[j]:\n rank[j] += 1\n\nbridge = [None] * M\nfor i in range(M):\n x, y = map(int, input().split())\n bridge[i] = [x, y]\n \ncnt = N * (N - 1) // 2\ninconv = [0] * M\nfor i in range(M - 1, -1, -1):\n inconv[i] = cnt\n a, b = bridge[i]\n if not same(a, b):\n cnt -= size[root(a)] * size[root(b)]\n unite(a, b)\n \nfor i in range(M):\n print(inconv[i])', 'N, M = map(int, input().split())\n\npar = list(range(0, N + 1))\nsize = [1] * (N + 1)\nrank = [1] * (N + 1)\n\ndef root(i):\n if par[i] == i:\n return i\n else:\n par[i] = root(par[i])\n return par[i]\n \ndef same(i, j):\n return root(i) == root(j)\n \ndef findsize(i):\n size[i] = size[root(i)]\n \ndef unite(i, j):\n i = root(i)\n j = root(j)\n if i != j:\n if rank[i] > rank[j]:\n par[j] = i\n size[i] += size[j]\n else:\n par[i] = j\n size[j] += size[i]\n if rank[i] == rank[j]:\n rank[j] += 1\n\nbridge = [None] * M\nfor i in range(M):\n x, y = map(int, input().split())\n bridge[i] = [x, y]\n \ncnt = N * (N - 1) // 2\ninconv = [0] * M\nfor i in range(M - 1, -1, -1):\n inconv[i] = cnt\n a, b = bridge[i]\n if not same(a, b):\n cnt -= size[root(a)] * size[root(b)]\n unite(a, b)\n \nfor i in range(M):\n print(inconv[i])']
['Wrong Answer', 'Accepted']
['s848241771', 's927827945']
[28236.0, 27176.0]
[704.0, 717.0]
[845, 852]
p03108
u550419600
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["def find_root(components, n):\n if components[n] == n:\n return n\n else:\n root = find_root(components, components[n])\n components[n] = root\n return root\n\nN, M = [int(s) for s in input().split(' ')]\nbridges = []\nfor i in range(M):\n bridges.append([int(s)-1 for s in input().split(' ')])\ncomponents = [i for i in range(N)]\ncomponentsLen = [1 for i in range(N)]\ninconvenience = []\ntotalNpair = N * (N-1) / 2\nfor i in range(M-1, -1, -1):\n convenience = 0\n for c in range(N):\n convenience += componentsLen[c] * (componentsLen[c]-1) / 2\n inconvenience.append(int(totalNpair - convenience))\n a, b = bridges[i]\n a_root = find_root(components, a)\n b_root = find_root(components, b)\n if a_root != b_root:\n components[b_root] = a_root\n componentsLen[a_root] += componentsLen[b_root]\n componentsLen[b_root] = 1\n print(components)\nfor i in range(M-1, -1, -1):\n print(inconvenience[i])\n", "def find_root(components, n):\n if components[n] == n:\n return n\n elif components[components[n]] == components[n]:\n return components[n]\n else:\n components[n] = find_root(components, components[n])\n return components[n]\n\nN, M = [int(s) for s in input().split(' ')]\nbridges = []\nfor i in range(M):\n bridges.append([int(s)-1 for s in input().split(' ')])\ncomponents = [i for i in range(N)]\ncomponentsLen = [1 for i in range(N)]\ninconvenience = [int(N * (N-1) / 2)]\nfor i in range(M-1, -1, -1):\n a, b = bridges[i]\n a_root = find_root(components, a)\n b_root = find_root(components, b)\n if a_root != b_root:\n inconvenience.append(inconvenience[-1] - componentsLen[a_root] * componentsLen[b_root])\n if componentsLen[a_root] < componentsLen[b_root]:\n a_root, b_root = b_root, a_root\n componentsLen[a_root] += componentsLen[b_root]\n components[b_root] = a_root\n else:\n inconvenience.append(inconvenience[-1])\nfor i in range(M-1, -1, -1):\n print(inconvenience[i])\n"]
['Wrong Answer', 'Accepted']
['s384451156', 's542237508']
[67508.0, 29892.0]
[2105.0, 619.0]
[963, 1058]
p03108
u556225812
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['def find(x):\n if par[x] == x:\n return x\n else:\n return find(par[x])\n\ndef unite(x, y):\n x = find(x)\n y = find(y)\n\n if x != y:\n if rank[x] < rank[y]:\n par[x] = y\n size[y] += size[x]\n else:\n par[y] = x\n size[x] += size[y]\n if rank[x] == rank[y]:\n rank[x] += 1\n\ndef same(x, y):\n return find(x) == find(y)\n\n\nn, m = map(int, input().split())\npar = [0]*n\nfor i in range(n):\n par[i] = i\u3000\nrank = [1]*n \nsize = [1]*n \n\nedge = [tuple(map(int, input().split())) for i in range(m)]\nedge = edge[::-1]\nfor i in range(m):\n edge[i] = (edge[i][0]-1, edge[i][1]-1)\n\nres = []\nfor i in range(m):\n fi = find(edge[i][0])\n se = find(edge[i][1])\n if fi == se:\n res.append(0)\n else:\n rs.append(size[fi]*size[se])\n unite(fi, se)\nass = 0\nfor i in range(m):\n ass += res[m-1-i]\n print(ass)\n', 'def find(x):\n if par[x] == x:\n return x\n else:\n return find(par[x])\n\ndef unite(x, y):\n x = find(x)\n y = find(y)\n\n if x != y:\n if rank[x] < rank[y]:\n par[x] = y\n size[y] += size[x]\n else:\n par[y] = x\n size[x] += size[y]\n if rank[x] == rank[y]:\n rank[x] += 1\n\ndef same(x, y):\n return find(x) == find(y)\n\n\nn, m = map(int, input().split())\npar = [0]*n\nfor i in range(n):\n par[i] = i \nrank = [1]*n \nsize = [1]*n \n\nedge = [tuple(map(int, input().split())) for i in range(m)]\nedge = edge[::-1]\nfor i in range(m):\n edge[i] = (edge[i][0]-1, edge[i][1]-1)\n\nres = []\nfor i in range(m):\n fi = find(edge[i][0])\n se = find(edge[i][1])\n if fi == se:\n res.append(0)\n else:\n res.append(size[fi]*size[se])\n unite(fi, se)\nass = 0\nfor i in range(m):\n ass += res[m-1-i]\n print(ass)\n\n']
['Runtime Error', 'Accepted']
['s959934336', 's918126806']
[3064.0, 24092.0]
[17.0, 668.0]
[1012, 1012]
p03108
u556326496
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["def factorial(n):\n 'n!を返す関数'\n ans = 1\n while(n > 0):\n ans *= n\n n -= 1\n return ans\n\ndef nCr(n, r):\n \n ans = factorial(n) / (factorial(r) * factorial(n-r))\n return int(ans)\n\nclass UnionFind:\n def __init__(self, N):\n \n self.N = N\n self.parent = [-1 for i in range(self.N)]\n\n def root(self, A):\n 'Aがどのグループに属しているか調べる。'\n if self.parent[A] < 0:\n return A\n else:\n self.parent[A] = self.root(self.parent[A])\n return self.parent[A]\n\n def size(self, A):\n '自分のいるグループの頂点数を調べる。'\n return -self.parent[self.root(A)]\n\n def connect(self, A, B):\n \n A = self.root(A)\n B = self.root(B)\n if A == B:\n \n return False\n elif self.size(A) < self.size(B):\n \n A, B = B, A\n else:\n pass\n \n self.parent[A] += self.parent[B]\n \n self.parent[B] = A\n return True\n\nislands, bridges = map(int, input().split())\nA, B = [], []\nans = [0 for i in range(bridges)]\n# ans[bridges-1] = nCr(islands, 2)\nans[bridges-1] = islands * (islands-1) / 2\n\nUF = UnionFind(islands)\n\nfor i in range(bridges):\n a, b = map(int, input().split())\n A.append(a-1)\n B.append(b-1)\n\nfor i in range(bridges-1, 0, -1):\n if UF.root(A[i]) != UF.root(B[i]):\n ans[i-1] = ans[i] - UF.size(A[i]) * UF.size(B[i])\n UF.connect(A[i], B[i])\n else:\n ans[i-1] = ans[i]\n\nfor i in range(bridges):\n print(ans[i])\n", "def factorial(n):\n 'n!を返す関数'\n ans = 1\n while(n > 0):\n ans *= n\n n -= 1\n return ans\n\ndef nCr(n, r):\n \n ans = factorial(n) / (factorial(r) * factorial(n-r))\n return int(ans)\n\nclass UnionFind:\n def __init__(self, N):\n \n self.N = N\n self.parent = [-1 for i in range(self.N)]\n\n def root(self, A):\n 'Aがどのグループに属しているか調べる。'\n if self.parent[A] < 0:\n return A\n else:\n self.parent[A] = self.root(self.parent[A])\n return self.parent[A]\n\n def size(self, A):\n '自分のいるグループの頂点数を調べる。'\n return -self.parent[self.root(A)]\n\n def connect(self, A, B):\n \n A = self.root(A)\n B = self.root(B)\n if A == B:\n \n return False\n elif self.size(A) < self.size(B):\n \n A, B = B, A\n else:\n pass\n \n self.parent[A] += self.parent[B]\n \n self.parent[B] = A\n return True\n\nislands, bridges = map(int, input().split())\nA, B = [], []\nans = [0 for i in range(bridges)]\n# ans[bridges-1] = nCr(islands, 2)\nans[bridges-1] = islands * (islands-1) / 2\n\nUF = UnionFind(islands)\n\nfor i in range(bridges):\n a, b = map(int, input().split())\n A.append(a-1)\n B.append(b-1)\n\nfor i in range(bridges-1, 0, -1):\n if UF.root(A[i]) != UF.root(B[i]):\n ans[i-1] = ans[i] - UF.size(A[i]) * UF.size(B[i])\n UF.connect(A[i], B[i])\n else:\n ans[i-1] = ans[i]\n\nfor i in range(bridges):\n print(ans[i])\n", "def factorial(n):\n 'n!を返す関数'\n ans = 1\n while(n > 0):\n ans *= n\n n -= 1\n return ans\n\ndef nCr(n, r):\n \n ans = factorial(n) / (factorial(r) * factorial(n-r))\n return int(ans)\n\nclass UnionFind:\n def __init__(self, N):\n \n self.N = N\n self.parent = [-1 for i in range(self.N)]\n\n def root(self, A):\n 'Aがどのグループに属しているか調べる。'\n if self.parent[A] < 0:\n return A\n else:\n self.parent[A] = self.root(self.parent[A])\n return self.parent[A]\n\n def size(self, A):\n '自分のいるグループの頂点数を調べる。'\n return -self.parent[self.root(A)]\n\n def connect(self, A, B):\n \n A = self.root(A)\n B = self.root(B)\n if A == B:\n \n return False\n elif self.size(A) < self.size(B):\n \n A, B = B, A\n else:\n pass\n \n self.parent[A] += self.parent[B]\n \n self.parent[B] = A\n return True\n\nislands, bridges = map(int, input().split())\nA, B = [], []\nans = [0 for i in range(bridges)]\n# ans[bridges-1] = nCr(islands, 2)\nans[bridges-1] = int(islands * (islands-1) / 2)\n\nUF = UnionFind(islands)\n\nfor i in range(bridges):\n a, b = map(int, input().split())\n A.append(a-1)\n B.append(b-1)\n\nfor i in range(bridges-1, 0, -1):\n if UF.root(A[i]) != UF.root(B[i]):\n ans[i-1] = ans[i] - UF.size(A[i]) * UF.size(B[i])\n UF.connect(A[i], B[i])\n else:\n ans[i-1] = ans[i]\n\nfor i in range(bridges):\n print(ans[i])\n"]
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s859818751', 's948985992', 's004713128']
[16448.0, 16472.0, 16948.0]
[846.0, 851.0, 813.0]
[2312, 2312, 2317]
p03108
u559823804
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['n,m=map(int,input().split())\npair=[]\nuf = [-1]*(n+1)\n\ndef root(x):\n if uf[x]<0: \n return x\n else: \n uf[x]=root(uf[x])\n return uf[x]\n\ndef issame(x,y):\n return root(x)==root(y)\n\ndef unite(x,y):\n x=root(x)\n y=root(y)\n if x==y:\n return False\n if uf[x]>uf[y]:\n x,y=y,x\n uf[x]+=uf[y]\n uf[y]=x\n return True\n\ndef size(x):\n return -uf[root(x)]\n\n\nans=[n*(n-1)//2]\n\nfor _ in range(m):\n a,b=map(int,input().split())\n pair.append((a,b))\n\nfor i,p in enumerate(reversed(pair)):\n if not issame(p[0],p[1]): \n ans.append(ans[i]-size(p[0])*size(p[1]))\n unite(p[0],p[1]) \n else:\n ans.append(ans[i])\nans.reverse()\nfor i in range(n-1):\n print(ans[i+1])', 'n,m=map(int,input().split())\npair=[]\nuf = [-1]*(n+1)\n\ndef root(x):\n if uf[x]<0: \n return x\n else: \n uf[x]=root(uf[x])\n return uf[x]\n\ndef issame(x,y):\n return root(x)==root(y)\n\ndef unite(x,y):\n x=root(x)\n y=root(y)\n if x==y:\n return False\n if uf[x]>uf[y]:\n x,y=y,x\n uf[x]+=uf[y]\n uf[y]=x\n return True\n\ndef size(x):\n return -uf[root(x)]\n\n\nans=[n*(n-1)//2]\n\nfor _ in range(m):\n a,b=map(int,input().split())\n pair.append((a,b))\n\nfor i,p in enumerate(reversed(pair)):\n if not issame(p[0],p[1]): \n ans.append(ans[i]-size(p[0])*size(p[1]))\n unite(p[0],p[1]) \n else:\n ans.append(ans[i])\n\nans.reverse()\nfor i in range(m):\n print(ans[i+1])\n ']
['Wrong Answer', 'Accepted']
['s626306282', 's764988322']
[22592.0, 22592.0]
[664.0, 685.0]
[879, 895]
p03108
u560889512
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['N,M=map(int,input().split())\nalist=[]\nblist=[]\nfor i in range(M):\n a,b=map(int,input().split())\n alist.append(a)\n blist.append(b)\nfather=[i for i in range(N+1)]\nrank=[0]*(N+1)\nsize=[1]*(N+1)\n\ndef find(x):\n if x!=father[x]:\n father[x]=find(father[x])\n return father[x]\n\ndef union(x,y):\n x,y=find(x),find(y)\n if x==y:\n return\n elif rank[x]<rank[y]:\n father[x]=y\n size[y]+=size(x)\n else:\n father[y]=x\n size[x]+=size[y]\n if rank[x]==rank[y]:\n rank[x]+=1\n \ndef samefather(x,y):\n return find(x)==find(y)\n\nans_list=[]\nans=N*(N-1)/2\nfor i in range(M):\n ans_list.append(ans)\n a=alist[M-1-i]\n b=alist[M-1-i]\n p,q=size[find(a)],size[size(b)]\n if samefather(a,b):\n continue\n else:\n ans-=p*q\n union(a,b)\nfor i in range(M):\n print(ans_list[M-1-i])', 'N,M=map(int,input().split())\nalist=[]\nblist=[]\nfor i in range(M):\n a,b=map(int,input().split())\n alist.append(a)\n blist.append(b)\nfather=[i for i in range(N+1)]\nrank=[0]*(N+1)\nsize=[1]*(N+1)\n\ndef find(x):\n if x!=father[x]:\n father[x]=find(father[x])\n return father[x]\n\ndef union(x,y):\n x,y=find(x),find(y)\n if x==y:\n return\n elif rank[x]<rank[y]:\n father[x]=y\n size[y]+=size[x]\n else:\n father[y]=x\n size[x]+=size[y]\n if rank[x]==rank[y]:\n rank[x]+=1\n \ndef samefather(x,y):\n return find(x)==find(y)\n\nans_list=[]\nans=N*(N-1)/2\nfor i in range(M):\n ans_list.append(ans)\n a=alist[M-1-i]\n b=alist[M-1-i]\n p,q=size[find(a)],size[find(b)]\n if samefather(a,b):\n continue\n else:\n ans-=p*q\n union(a,b)\nfor i in range(M):\n print(ans_list[M-1-i])\n', 'N,M = map(int,input().split())\nalist=[]\nblist=[]\nfor i in range(M):\n a,b=map(int,input().split())\n alist.append(a)\n blist.append(b)\nfather=[i for i in range(N+1)]\nrank=[0]*(N+1)\nsize=[1]*(N+1)\n\ndef find(x):\n if x!=father[x]:\n father[x]=find(father[x])\n return father[x]\n\ndef union(x,y):\n xfa,yfa=find(x),find(y)\n if xfa==yfa:\n return\n elif rank[xfa]<rank[yfa]:\n father[xfa]=yfa\n size[yfa]+=size[xfa]\n else:\n father[yfa]=xfa\n size[xfa]+=size[yfa]\n if rank[xfa]==rank[yfa]:\n rank[xfa]+=1\n \ndef samefather(x,y):\n return find(x)==find(y)\n\nans_list=[]\nans=int(N*(N-1)/2)\nfor i in range(M):\n ans_list.append(ans)\n a=alist[M-1-i]\n b=blist[M-1-i]\n p,q=size[find(a)],size[find(b)]\n if samefather(a,b):\n continue\n else:\n ans-=p*q\n union(a,b)\nfor i in range(M):\n print(ans_list[M-1-i])']
['Runtime Error', 'Wrong Answer', 'Accepted']
['s442291300', 's605192312', 's095862949']
[16552.0, 18660.0, 19384.0]
[311.0, 575.0, 709.0]
[790, 791, 831]
p03108
u561992253
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["import array\nimport time\n\nclass UnionFind :\n def __init__(self, n):\n self.par = array.array('i', range(n))\n self.sizes = array.array('i', [1]*n)\n\n\n def root(self, i):\n if i == self.par[i]:\n return i\n else:\n r = self.root(self.par[i])\n self.par[i] = r\n return r\n\n def unite(self, x, y):\n rx = self.root(x)\n ry = self.root(y)\n if(rx == ry):\n return\n self.par[rx] = ry\n rrx = self.root(x)\n self.sizes[rrx] = self.sizes[rx] + self.sizes[ry]\n\n def same(self, x, y) :\n return self.root(x) == self.root(y)\n\n def size(self, x):\n return self.sizes[self.root(x)]\n\n def debug(self):\n print(self.par)\n print(self.sizes)\n\ndef main():\n (n,m) = map(int, input().split())\n ab = []\n for i in range(m):\n (a,b) = map(int, input().split())\n ab.append((a-1,b-1))\n ab = ab[::-1]\n uf = UnionFind(n)\n res = []\n prev = 0\n time.sleep(30)\n for (a,b) in ab:\n \n res.append(prev)\n if uf.same(a,b):\n score = prev\n else:\n score = prev - calsc(uf.size(a)) - calsc(uf.size(b))\n uf.unite(a,b)\n score = score + calsc(uf.size(a))\n prev = score\n for i in range(len(res)):\n res[i] = calsc(n) - res[i]\n res = res[::-1]\n for r in res:\n print(r)\n\ndef calsc(x):\n return (x * (x-1)) // 2\n\nif __name__ == '__main__':\n main()\n", "import array\n\nclass UnionFind :\n def __init__(self, n):\n self.par = array.array('i', range(n))\n\n\n def root(self, i):\n if i == self.par[i]:\n return i\n else:\n r = self.root(self.par[i])\n self.par[i] = r\n return r\n\n\n def unite(self, x, y):\n rx = self.root(x)\n ry = self.root(y)\n if(rx == ry):\n return\n self.par[rx] = ry\n self.root(x)\n\n def same(self, x, y) :\n return self.root(x) == self.root(y)\n\n def debug(self):\n print(self.par)\n\nif __name__ == '__main__':\n main()\n\ndef main():\n (n,m) = map(int, input().split())\n ab = []\n for i in range(m):\n (a,b) = map(int, input().split())\n ab.append((a-1,b-1))\n ab = ab[::-1]\n uf = UnionFind(n)\n res = []\n for (a,b) in ab:\n res.append(calcScore(uf, n))\n uf.unite(a,b)\n for i in range(m):\n res[i] = ((n*(n-1)) // 2) - res[i]\n res = res[::-1]\n for r in res:\n print(r)\n\ndef calcScore(uf, n):\n nroots = array.array('i', [0] * n)\n for i in range(n):\n root = uf.root(i)\n nroots[root] = nroots[root] + 1\n res = 0\n for r in nroots:\n res = res + (r * (r-1)) // 2\n return res\n", "import array\nimport time\n\nclass UnionFind :\n def __init__(self, n):\n self.par = array.array('i', range(n))\n self.sizes = array.array('i', [1]*n)\n\n\n def root(self, i):\n if i == self.par[i]:\n return i\n else:\n r = self.root(self.par[i])\n self.par[i] = r\n return r\n\n def unite(self, x, y):\n rx = self.root(x)\n ry = self.root(y)\n if(rx == ry):\n return\n self.par[rx] = ry\n rrx = self.root(x)\n self.sizes[rrx] = self.sizes[rx] + self.sizes[ry]\n\n def same(self, x, y) :\n return self.root(x) == self.root(y)\n\n def size(self, x):\n return self.sizes[self.root(x)]\n\n def debug(self):\n print(self.par)\n print(self.sizes)\n\ndef main():\n (n,m) = map(int, input().split())\n ab = []\n for i in range(m):\n (a,b) = map(int, input().split())\n ab.append((a-1,b-1))\n ab = ab[::-1]\n uf = UnionFind(n)\n res = []\n prev = 0\n for (a,b) in ab:\n \n res.append(prev)\n if uf.same(a,b):\n score = prev\n else:\n score = prev - calsc(uf.size(a)) - calsc(uf.size(b))\n uf.unite(a,b)\n score = score + calsc(uf.size(a))\n prev = score\n time.sleep(30)\n for i in range(len(res)):\n res[i] = calsc(n) - res[i]\n res = res[::-1]\n for r in res:\n print(r)\n\ndef calsc(x):\n return (x * (x-1)) // 2\n\nif __name__ == '__main__':\n main()\n", "import array\nimport time\n\nclass UnionFind :\n def __init__(self, n):\n self.par = array.array('i', range(n))\n self.sizes = array.array('i', [1]*n)\n\n\n def root(self, i):\n if i == self.par[i]:\n return i\n else:\n r = self.root(self.par[i])\n self.par[i] = r\n return r\n\n def unite(self, x, y):\n rx = self.root(x)\n ry = self.root(y)\n if(rx == ry):\n return\n self.par[rx] = ry\n rrx = self.root(x)\n self.sizes[rrx] = self.sizes[rx] + self.sizes[ry]\n\n def same(self, x, y) :\n return self.root(x) == self.root(y)\n\n def sized(self, x):\n return self.sizes[self.root(x)]\n\n def debug(self):\n print(self.par)\n print(self.sizes)\n\ndef calsc(x):\n return (x * (x-1L)) // 2L\n\ndef main():\n (n,m) = map(int, input().split())\n ab = []\n for i in range(m):\n (a,b) = map(int, input().split())\n ab.append((a-1,b-1))\n uf = UnionFind(n)\n res = []\n prev = 0L\n while len(ab) > 0:\n (a,b) = ab.pop()\n \n res.append(prev)\n score = prev\n if not uf.same(a,b):\n score = score - calsc(uf.sized(a)) - calsc(uf.sized(b))\n uf.unite(a,b)\n score = score + calsc(uf.sized(a))\n prev = score\n for i in range(len(res)):\n res[i] = calsc(n) - res[i]\n res = res[::-1]\n for r in res:\n print(r)\n\nif __name__ == '__main__':\n main()\n", "import array\nimport time\n\nclass UnionFind :\n def __init__(self, n):\n self.par = array.array('i', range(n))\n self.sizes = array.array('i', [1]*n)\n self.rank = array.array('i', [0]*n)\n\n\n def root(self, i):\n if i == self.par[i]:\n return i\n else:\n r = self.root(self.par[i])\n self.par[i] = r\n return r\n\n def unite(self, x, y):\n rx = self.root(x)\n ry = self.root(y)\n srx = self.sized(x)\n sry = self.sized(y)\n if rx != ry:\n if self.rank[rx] < self.rank[ry]:\n self.par[rx] = ry\n else:\n self.par[ry] = rx\n if self.rank[rx] == self.rank[ry]:\n self.rank[rx] = self.rank[rx] + 1\n self.sizes[self.root(rx)] = srx + sry\n\n def same(self, x, y) :\n return self.root(x) == self.root(y)\n\n def sized(self, x):\n return self.sizes[self.root(x)]\n\n def debug(self):\n print(self.par)\n print(self.sizes)\n\ndef calsc(x):\n return (x * (x-1)) // 2\n\ndef main():\n (n,m) = map(int, input().split())\n ab = []\n for _ in range(m):\n (p,q) = map(int, input().split())\n ab.append((p-1,q-1))\n uf = UnionFind(n)\n res = []\n prev = 0\n while len(ab) > 0:\n (a,b) = ab.pop()\n \n res.append(prev)\n score = prev\n if not uf.same(a,b):\n score = score - calsc(uf.sized(a)) - calsc(uf.sized(b))\n uf.unite(a,b)\n score = score + calsc(uf.sized(a))\n prev = score\n for i in range(len(res)):\n res[i] = calsc(n) - res[i]\n res = res[::-1]\n for r in res:\n print(r)\n\nif __name__ == '__main__':\n main()\n"]
['Time Limit Exceeded', 'Runtime Error', 'Runtime Error', 'Runtime Error', 'Accepted']
['s382496114', 's606517019', 's943507577', 's985279615', 's594739203']
[18100.0, 3064.0, 22472.0, 3064.0, 18568.0]
[2105.0, 18.0, 2105.0, 19.0, 1070.0]
[1511, 1255, 1511, 1495, 1748]
p03108
u562935282
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['class UnionFind:\n def __init__(self, n):\n self.v = [-1 for _ in range(n)] \n\n def find(self, x): \n if self.v[x] < 0: \n return x\n else: \n self.v[x] = self.find(self.v[x]) \n return self.v[x]\n\n def unite(self, x, y): \n x = self.find(x)\n y = self.find(y)\n if x == y:\n return\n if -self.v[x] < -self.v[y]: \n x, y = y, x \n self.v[x] += self.v[y] \n self.v[y] = x \n\n def root(self, x):\n return self.v[x] < 0 \n\n def same(self, x, y):\n return self.find(x) == self.find(y) \n\n def size(self, x):\n return -self.v[self.find(x)] \n\n\nN, M = map(int, input().split())\ne = []\nfor _ in range(M):\n a, b = map(int, input().split())\n a -= 1\n b -= 1\n e.append((a, b))\n\ninit = N * (N - 1) // 2\nto_del = [0 for _ in range(M)]\nuf = UnionFind(N)\nfor i in sorted(range(M), reverse=True):\n a, b = e[i]\n sa, sb = uf.size(a), uf.size(b)\n if sa == 1:\n if sb == 1:\n cc = 1\n else: #sb > 1\n cc = sb * (sb + 1) // 2\n else:\n if sb == 1:\n cc = sa * (sa + 1) // 2\n else:\n # sa > 1 and sb > 1\n cc = (sa + sb) * (sa + sb - 1) // 2\n to_del[i] = cc\n uf.unite(a, b)\n\nfor i in range(M):\n print(init - to_del[i])\n', "class UnionFind:\n def __init__(self, n):\n self.v = [-1] * n\n\n def find(self, x):\n if self.v[x] < 0:\n return x\n else:\n self.v[x] = self.find(self.v[x])\n return self.v[x]\n\n def unite(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return False\n else:\n if self.v[x] > self.v[y]:\n x, y = y, x\n self.v[x] += self.v[y]\n self.v[y] = x\n return True\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def size(self, x):\n return -self.v[self.find(x)]\n\n\ndef main():\n import sys\n input = sys.stdin.readline\n\n N, M = map(int, input().split())\n\n es = []\n for _ in range(M):\n a, b = (int(x) - 1 for x in input().split())\n es.append((a, b))\n es.reverse()\n es.pop()\n\n uf = UnionFind(N)\n ans = [N * (N - 1) // 2]\n t = ans[-1]\n for a, b in es:\n if not uf.same(a, b):\n t -= uf.size(a) * uf.size(b)\n ans.append(t)\n uf.unite(a, b)\n ans.reverse()\n\n print(*ans, sep='\\n')\n\n\nif __name__ == '__main__':\n main()\n"]
['Wrong Answer', 'Accepted']
['s267421261', 's350716867']
[25268.0, 24088.0]
[723.0, 560.0]
[2047, 1181]
p03108
u577170763
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["import math\n\n\nclass Solution:\n\n def solve(self, N: int, M: int, bridges):\n\n class UnionFind:\n\n def __init__(self, __N: int):\n self.__N = __N\n self.__par = [i for i in range(__N)]\n self.__num_map = {i: 1 for i in range(__N)}\n\n def root(self, x: int) -> int:\n if self.__par[x] == x:\n return x\n else:\n self.__par[x] = self.root(self.__par[x])\n return self.__par[x]\n\n def unite(self, x: int, y: int):\n rx = self.root(x)\n ry = self.root(y)\n if rx != ry:\n self.__par[rx] = ry\n __num = self.__num_map[rx] + self.__num_map[ry]\n self.__num_map[rx] = __num\n self.__num_map[ry] = __num\n\n def same(self, x: int, y: int) -> bool:\n rx = self.root(x)\n ry = self.root(y)\n return rx == ry\n\n def num(self, x: int) -> int:\n return self.__num_map[self.root[x]]\n\n # solve\n\n uf = UnionFind(N+1)\n\n ans = N * (N-1) // 2\n answer_reversed = []\n\n for i in range(M-1, -1, -1):\n answer_reversed.append(ans)\n x = bridges[i][0]\n y = bridges[i][1]\n if not uf.same(x, y):\n ans -= uf.num(x) * uf.num(y)\n uf.unite(x, y)\n\n return reversed(answer_reversed)\n\n\nif __name__ == '__main__':\n\n # standard input\n N, M = map(int, input().split())\n bridges = []\n for i in range(M):\n bridges.append(tuple(map(int, input().split())))\n\n # solve\n solution = Solution()\n sol = solution.solve(N, M, bridges)\n for s in sol:\n print(s)\n", 'import math\n\n\nclass UnionFind:\n\n def __init__(self, N: int):\n # d[i] = <parent id> if i is a child,\n # - <size of the group> if i is a root\n self.d = [-1 for _ in range(N)]\n\n def root(self, x: int) -> int:\n if self.d[x] < 0:\n return x\n self.d[x] = self.root(self.d[x])\n return self.d[x]\n\n def unite(self, x: int, y: int) -> bool:\n x, y = self.root(x), self.root(y)\n if x == y:\n return False\n if self.d[x] > self.d[y]:\n x, y = y, x\n self.d[x] += self.d[y]\n self.d[y] = x\n return True\n\n def same(self, x: int, y: int) -> bool:\n return self.root(x) == self.root(y)\n\n def size(self, x: int):\n return -self.d[self.root(x)]\n\n def show(self):\n m = {}\n for n in range(len(self.d)):\n r = self.root(n)\n\n if r not in m:\n m[r] = [n]\n else:\n m[r].append(n)\n\n print("root -> childs")\n print("---------------------")\n for key in m:\n print("{} -> {}".format(key, m[key]))\n\n\nclass Solution:\n\n def solve(self, N: int, M: int, bridges):\n\n # solve\n uf = UnionFind(N+1)\n\n ans = N * (N-1) // 2\n answer = []\n\n for a, b in reversed(bridges):\n answer.append(ans)\n\n if not uf.same(a, b):\n ans -= uf.size(a) * uf.size(b)\n uf.unite(a, b)\n\n answer.reverse()\n\n return answer\n\n\nif __name__ == \'__main__\':\n\n # standard input\n N, M = map(int, input().split())\n bridges = []\n for i in range(M):\n bridges.append(tuple(map(int, input().split())))\n\n # solve\n solution = Solution()\n sol = solution.solve(N, M, bridges)\n for s in sol:\n print(s)\n']
['Runtime Error', 'Accepted']
['s770389373', 's396832114']
[32612.0, 21744.0]
[348.0, 726.0]
[1813, 1812]
p03108
u578489732
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["# -*- coding: utf-8 -*-\nimport sys\n# ----------------------------------------------------------------\n# Use Solve Function\n\nclass UnionFind:\n def __init__(self, n):\n \n self.par = [i for i in range(n+1)]\n self.size = [1 for i in range(n+1)]\n self.rank = [0] * (n+1)\n\n def __str__(self):\n return ' '.join(map(str,self.par))\n\n def tree_size(self, x):\n x = self.find(x)\n return self.size[x]\n\n def find(self, x):\n if self.par[x] == x:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n\n def same_check(self, x, y):\n return self.find(x) == self.find(y)\n\n def unite(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x == y: return\n\n if self.rank[x] < self.rank[y]:\n self.par[x] = y\n self.size[y] += self.size[x]\n else:\n self.par[y] = x\n self.size[x] += self.size[y]\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n\ndef solve(lines):\n N,M = map(int, lines.pop(0).split(' '))\n NLIST = []\n for i in range(M):\n A,B = map(int, lines.pop(0).split(' '))\n NLIST.append((A,B))\n\n res = N * (N-1) // 2\n R = str(res) + '\\n'\n uf = UnionFind(N)\n\n for i in range(M-1, 0, -1):\n x = NLIST[i]\n n1 = x[0]\n n2 = x[1]\n\n if not uf.same_check(n1,n2):\n res -= uf.tree_size(n1) * uf.tree_size(n2)\n uf.unite(n1,n2)\n R = str(res) + '\\n' + R\n\n print(R)\n #R.reverse()\n \n # print(R[i])\n \n # print(R[i])\n\n\nlines = [x.strip() for x in sys.stdin.readlines()]\nsolve(lines)", "# -*- coding: utf-8 -*-\nimport sys\n# ----------------------------------------------------------------\n# Use Solve Function\n\ndef solve(lines):\n N,M = map(int, lines.pop(0).split(' '))\n NLIST = []\n for i in range(M):\n A,B = map(int, lines.pop(0).split(' '))\n NLIST.append((A,B))\n\n res = N * (N-1) // 2\n R = [res]\n sets = []\n\n for i in range(M-1, 0, -1):\n x = NLIST[i]\n print(x)\n n1 = {x[0]}\n n2 = {x[1]}\n\n delete_idx = []\n\n for j,y in enumerate(sets):\n if x[0] in y:\n n1 = y\n delete_idx.append(j)\n if x[1] in y:\n n2 = y\n if not j in delete_idx:\n delete_idx.append(j)\n\n delete_idx.sort(reverse=True)\n for k in delete_idx:\n sets.pop(k)\n\n if n1 != n2:\n res -= (len(n1) * len(n2))\n n1 = n1.union(n2)\n\n sets.append(n1)\n R.append(res)\n\n print(R)\n\nlines = [x.strip() for x in sys.stdin.readlines()]\n\n#\n# solve !!\n#\nsolve(lines)", "# -*- coding: utf-8 -*-\nimport sys\n# ----------------------------------------------------------------\n# Use Solve Function\n\nclass UnionFind:\n def __init__(self, n):\n \n self.par = [i for i in range(n+1)]\n self.size = [1 for i in range(n+1)]\n self.rank = [0] * (n+1)\n\n def __str__(self):\n return ' '.join(map(str,self.par))\n\n def tree_size(self, x):\n x = self.find(x)\n return self.size[x]\n\n def find(self, x):\n if self.par[x] == x:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n\n def same_check(self, x, y):\n return self.find(x) == self.find(y)\n\n def unite(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x == y: return\n\n if self.rank[x] < self.rank[y]:\n self.par[x] = y\n self.size[y] += self.size[x]\n else:\n self.par[y] = x\n self.size[x] += self.size[y]\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\ndef solve(lines):\n N,M = map(int, lines.pop(0).split(' '))\n NLIST = []\n for i in range(M):\n A,B = map(int, lines.pop(0).split(' '))\n NLIST.append((A,B))\n\n res = N * (N-1) // 2\n R = str(res) + '\\n'\n uf = UnionFind(N)\n\n for i in range(M-1, 0, -1):\n x = NLIST[i]\n n1 = x[0]\n n2 = x[1]\n\n if not uf.same_check(n1,n2):\n res -= uf.tree_size(n1) * uf.tree_size(n2)\n uf.unite(n1,n2)\n R = str(res) + '\\n' + res\n\n print(R)\n #R.reverse()\n \n # print(R[i])\n \n # print(R[i])\n\n\nlines = [x.strip() for x in sys.stdin.readlines()]\nsolve(lines)", "# -*- coding: utf-8 -*-\nimport sys\n# ----------------------------------------------------------------\n# Use Solve Function\n\nclass UnionFind:\n def __init__(self, n):\n \n self.par = [i for i in range(n+1)]\n self.size = [1 for i in range(n+1)]\n self.rank = [0] * (n+1)\n\n def __str__(self):\n return ' '.join(map(str,self.par))\n\n def tree_size(self, x):\n x = self.find(x)\n return self.size[x]\n\n def find(self, x):\n if self.par[x] == x:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n\n def same_check(self, x, y):\n return self.find(x) == self.find(y)\n\n def unite(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x == y: return\n\n if self.rank[x] < self.rank[y]:\n self.par[x] = y\n self.size[y] += self.size[x]\n else:\n self.par[y] = x\n self.size[x] += self.size[y]\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n# A,B = map(int, input().split(' '))\nN,M = map(int, input().split(' '))\nNLIST = []\nfor i in range(M):\n A,B = map(int, input().split(' '))\n NLIST.append((A,B))\n\nres = N * (N-1) // 2\nR = [res]\nuf = UnionFind(N)\n\nNLIST.reverse()\nfor i in range(M-1):\n x = NLIST[i]\n n1 = x[0]\n n2 = x[1]\n\n if not uf.same_check(n1,n2):\n res -= uf.tree_size(n1) * uf.tree_size(n2)\n uf.unite(n1,n2)\n R.append(res)\n\nR.reverse()\nfor i in range(M):\n print(R[i])\n\n# print(R[i])"]
['Time Limit Exceeded', 'Wrong Answer', 'Runtime Error', 'Accepted']
['s169653995', 's581666360', 's818587707', 's370465569']
[22928.0, 17416.0, 22676.0, 24088.0]
[2105.0, 2104.0, 1683.0, 815.0]
[1858, 1067, 1859, 1688]
p03108
u593761424
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['class UnionFind(object):\n def __init__(self, n):\n self.parent = {i: i for i in range(n)}\n self.rank = {i: 0 for i in range(n)}\n self.group = {i: [i] for i in range(n)}\n \n def find(self, x):\n if self.parent[x] != x:\n self.parent[x] = self.find(self.parent[x])\n\n return self.parent[x]\n \n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n \n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n \n else:\n self.parent[y] = x\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n # update group\n self.group[y] += self.group[x]\n self.group[x] = self.group[y]\n\n def size(self, x):\n return len(self.group[x])\n\n\n# N, M\nn_lands, n_bridges = map(int, input().split())\n# A, B\nedges = list()\nfor i in range(n_bridges):\n edge = list(map(int, input().split()))\n edges.append(edge)\n\nans_list = list()\nans_list.append(n_lands * (n_lands - 1) // 2)\n\ngraph = UnionFind(n_lands)\nfor edge in edges[::-1]:\n print(ans_list[-1])\n\n beg, end = edge\n beg -= 1\n end -= 1\n\n # beg -> end was impossible\n if graph.find(beg) != graph.find(end):\n ans = ans_list[-1] - graph.size(beg) * graph.size(end)\n\n # unite group\n graph.union(beg, end)\n\n else:\n ans = ans_list[-1]\n ans_list.append(ans)\n \n\n# at the initial stage\nassert ans_list[-1] == 0\nans_list.pop()\n\nfor ans in ans_list[::-1]:\n print(ans)\n', 'class UnionFind(object):\n def __init__(self, n):\n self.parent = {i: i for i in range(n)}\n self.rank = {i: 0 for i in range(n)}\n self.group = {i: [i] for i in range(n)}\n \n def find(self, x):\n if self.parent[x] != x:\n self.parent[x] = self.find(self.parent[x])\n\n return self.parent[x]\n \n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n \n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n \n else:\n self.parent[y] = x\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n # update group\n self.group[y] += self.group[x]\n self.group[x] = self.group[y]\n\n def size(self, x):\n return len(self.group[x])\n\n\n# N, M\nn_lands, n_bridges = map(int, input().split())\n# A, B\nedges = list()\nfor i in range(n_bridges):\n edge = list(map(int, input().split()))\n edges.append(edge)\n\nans_list = list()\nans_list.append(n_lands * (n_lands - 1) // 2)\n\ngraph = UnionFind(n_lands)\nfor edge in edges[::-1]:\n beg, end = edge\n beg -= 1\n end -= 1\n\n # beg -> end was impossible\n if graph.find(beg) != graph.find(end):\n ans = ans_list[-1] - graph.size(beg) * graph.size(end)\n\n # unite group\n graph.union(beg, end)\n\n else:\n ans = ans_list[-1]\n ans_list.append(ans)\n \n\n# at the initial stage\n\n# ans_list.pop()\n\nfor ans in ans_list[::-1]:\n print(ans)\n', 'class UnionFind(object):\n def __init__(self, n):\n self.parent = {i: i for i in range(n)}\n self.rank = {i: 0 for i in range(n)}\n self.group = {i: [i] for i in range(n)}\n \n def find(self, x):\n if self.parent[x] != x:\n self.parent[x] = self.find(self.parent[x])\n\n return self.parent[x]\n \n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n \n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n \n else:\n self.parent[y] = x\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n # update group\n self.group[y] += self.group[x]\n self.group[x] = self.group[y]\n\n def size(self, x):\n return len(self.group[x])\n\ndef main_naive():\n # N, M\n n_lands, n_bridges = map(int, input().split())\n # A, B\n edges = list()\n for i in range(n_bridges):\n edge = list(map(int, input().split()))\n edges.append(edge)\n\n ans_list = list()\n ans_list.append(n_lands * (n_lands - 1) // 2)\n\n graph = Graph(n_lands)\n for edge in edges[::-1]:\n beg, end = edge\n # beg -> end was impossible\n if graph.indicators[beg] != graph.indicators[end]:\n ans = ans_list[-1] - len(graph[beg]) * len(graph[end])\n\n # unite group\n graph.union(beg, end)\n\n else:\n ans = ans_list[-1]\n ans_list.append(ans)\n \n\n # at the initial stage\n assert ans_list[-1] == 0\n ans_list.pop()\n\n for ans in ans_list[::-1]:\n print(ans)\n\n\ndef main():\n from union_find import UnionFind\n\n # N, M\n n_lands, n_bridges = map(int, input().split())\n # A, B\n edges = list()\n for i in range(n_bridges):\n edge = list(map(int, input().split()))\n edges.append(edge)\n\n ans_list = list()\n ans_list.append(n_lands * (n_lands - 1) // 2)\n\n graph = UnionFind(n_lands)\n for edge in edges[::-1]:\n print(ans_list[-1])\n\n beg, end = edge\n beg -= 1\n end -= 1\n\n # beg -> end was impossible\n if graph.find(beg) != graph.find(end):\n ans = ans_list[-1] - graph.size(beg) * graph.size(end)\n\n # unite group\n graph.union(beg, end)\n\n else:\n ans = ans_list[-1]\n ans_list.append(ans)\n \n\n # at the initial stage\n assert ans_list[-1] == 0\n ans_list.pop()\n\n for ans in ans_list[::-1]:\n print(ans)\n\n\nmain()\n', "class Graph(object):\n def __init__(self, size):\n self.size = size\n # node_i belong to group_i\n self.groups = {i: {i} for i in range(1, size+1)}\n self.indicators = {i: i for i in range(1, size+1)}\n\n def __getitem__(self, i):\n # i: index of node\n ind = self.indicators[i]\n return self.groups[ind]\n \n def union(self, i, j):\n # i, j: index of node\n ind_i = self.indicators[i]\n ind_j = self.indicators[j]\n\n # update group [N]\n group_j = self.groups.pop(ind_j)\n self.groups[ind_i] = self.groups[ind_i].union(group_j)\n\n \n for j in group_j:\n self.indicators[j] = ind_i\n\n\nclass UnionFind(object):\n def __init__(self, n):\n self.parent = {i: i for i in range(n)}\n self.rank = {i: 0 for i in range(n)}\n \n self.size = {i: 1 for i in range(n)}\n \n def find(self, x):\n if self.parent[x] != x:\n self.parent[x] = self.find(self.parent[x])\n\n return self.parent[x]\n \n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n \n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n\n else:\n self.parent[y] = x\n\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n self.size[x] += self.size[y]\n self.size[y] = self.size[x]\n\n\n def get_size(self, x):\n return self.size[self.find(x)]\n \n\n\ndef main_naive():\n # N, M\n n_lands, n_bridges = map(int, input().split())\n # A, B\n edges = list()\n for i in range(n_bridges):\n edge = list(map(int, input().split()))\n edges.append(edge)\n\n ans_list = list()\n ans_list.append(n_lands * (n_lands - 1) // 2)\n\n graph = Graph(n_lands)\n for edge in edges[::-1]:\n beg, end = edge\n # beg -> end was impossible\n if graph.indicators[beg] != graph.indicators[end]:\n ans = ans_list[-1] - len(graph[beg]) * len(graph[end])\n\n # unite group\n graph.union(beg, end)\n\n else:\n ans = ans_list[-1]\n ans_list.append(ans)\n \n\n # at the initial stage\n assert ans_list[-1] == 0\n ans_list.pop()\n\n for ans in ans_list[::-1]:\n print(ans)\n\n\ndef main():\n # N, M\n n_lands, n_bridges = map(int, input().split())\n # A, B\n edges = list()\n for i in range(n_bridges):\n edge = list(map(int, input().split()))\n edges.append(edge)\n\n ans_list = list()\n ans_list.append(n_lands * (n_lands - 1) // 2)\n\n graph = UnionFind(n_lands)\n for edge in edges[::-1]:\n # print(graph.size)\n beg, end = edge\n beg -= 1\n end -= 1\n\n # beg -> end was impossible\n if graph.find(beg) != graph.find(end):\n ans = ans_list[-1] - graph.get_size(beg) * graph.get_size(end)\n\n # unite group\n graph.union(beg, end)\n\n else:\n ans = ans_list[-1]\n ans_list.append(ans)\n \n\n # at the initial stage\n assert ans_list[-1] == 0\n ans_list.pop()\n\n for ans in ans_list[::-1]:\n print(ans)\n\n\nif __name__ == '__main__':\n main()\n # main_naive()"]
['Runtime Error', 'Wrong Answer', 'Runtime Error', 'Accepted']
['s302196190', 's741494858', 's861139514', 's535038569']
[1710848.0, 1745688.0, 3192.0, 64532.0]
[2212.0, 2217.0, 19.0, 964.0]
[1558, 1537, 2537, 3329]
p03108
u597455618
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['import sys\n\n\nclass UnionFind:\n def __init__(self, n):\n self.table = [-1] * n\n\n def _root(self, x):\n stack = []\n tbl = self.table\n while tbl[x] >= 0:\n stack.append(x)\n x = tbl[x]\n for y in stack:\n tbl[y] = x\n return x\n\n def find(self, x, y):\n return self._root(x) == self._root(y)\n\n def union(self, x, y):\n r1 = self._root(x)\n r2 = self._root(y)\n if r1 == r2:\n return\n d1 = self.table[r1]\n d2 = self.table[r2]\n if d1 <= d2:\n self.table[r2] = r1\n self.table[r1] += d2\n else:\n self.table[r1] = r2\n self.table[r2] += d1\n\n\ndef main():\n n, m, k = map(int, sys.stdin.buffer.readline().split())\n uf = UnionFind(n)\n fb = [0]*n\n i = 0\n for x in sys.stdin.buffer.readlines():\n if i < m:\n a, b = map(int, x.split())\n fb[a-1] += 1\n fb[b-1] += 1\n uf.union(a-1, b-1)\n else:\n c, d = map(int, x.split())\n fb[c-1] += 1\n fb[d-1] += 1\n i += 1\n\n ans = [-uf.table[uf._root(i)]-1-fb[i] for i in range(n)]\n print(*ans)\n\n\nif __name__ == "__main__":\n main()\n', 'import sys\n\n\nclass UnionFind:\n def __init__(self, n):\n self.table = [-1] * n\n\n def _root(self, x):\n stack = []\n tbl = self.table\n while tbl[x] >= 0:\n stack.append(x)\n x = tbl[x]\n for y in stack:\n tbl[y] = x\n return x\n\n def find(self, x, y):\n return self._root(x) == self._root(y)\n\n def union(self, x, y):\n r1 = self._root(x)\n r2 = self._root(y)\n if r1 == r2:\n return\n d1 = self.table[r1]\n d2 = self.table[r2]\n if d1 <= d2:\n self.table[r2] = r1\n self.table[r1] += d2\n else:\n self.table[r1] = r2\n self.table[r2] += d1\n\n\ndef main():\n n, m = map(int, sys.stdin.buffer.readline().split())\n uf = UnionFind(n)\n ans = [n*(n-1)//2]\n for x in reversed(sys.stdin.buffer.readlines()):\n a, b = map(int, x.split())\n a -= 1\n b -= 1\n if uf.find(a, b):\n ans.append(ans[-1])\n continue\n ba, bb = uf.table[uf._root(a)], uf.table[uf._root(b)]\n ans.append(ans[-1] - ba*bb)\n uf.union(a, b)\n ans = reversed(ans)\n print("\\n".join(map(str, ans)))\n\n\nif __name__ == "__main__":\n main()\n', 'import sys\n\n\nclass UnionFind:\n def __init__(self, n):\n self.table = [-1] * n\n\n def _root(self, x):\n stack = []\n tbl = self.table\n while tbl[x] >= 0:\n stack.append(x)\n x = tbl[x]\n for y in stack:\n tbl[y] = x\n return x\n\n def find(self, x, y):\n return self._root(x) == self._root(y)\n\n def union(self, x, y):\n r1 = self._root(x)\n r2 = self._root(y)\n if r1 == r2:\n return\n d1 = self.table[r1]\n d2 = self.table[r2]\n if d1 <= d2:\n self.table[r2] = r1\n self.table[r1] += d2\n else:\n self.table[r1] = r2\n self.table[r2] += d1\n\n\ndef main():\n n, m = map(int, sys.stdin.buffer.readline().split())\n uf = UnionFind(n)\n ans = [n*(n-1)//2]\n for x in reversed(sys.stdin.buffer.readlines()):\n a, b = map(int, x.split())\n a -= 1\n b -= 1\n if uf.find(a, b):\n ans.append(ans[-1])\n continue\n ba, bb = uf.table[uf._root(a)], uf.table[uf._root(b)]\n ans.append(ans[-1] - ba*bb)\n uf.union(a, b)\n ans.reverse()\n print("\\n".join(map(str, ans[1:])))\n\n\nif __name__ == "__main__":\n main()\n']
['Runtime Error', 'Wrong Answer', 'Accepted']
['s002284373', 's945353150', 's357785332']
[9388.0, 22460.0, 24040.0]
[24.0, 307.0, 306.0]
[1252, 1248, 1246]
p03108
u599547273
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['N, M = map(int, input().split(" "))\nAB = [list(map(int, input().split(" "))) for _ in range(M)]\n\n\nclass UnionFind:\n def __init__(self, n):\n self.n = n\n self.per = list(range(n))\n self.point = n*(n-1) // 2\n self.counts = [1]*n\n\n def root(self, x):\n if self.per[x] == x:\n return x\n return self.root(self.per[x])\n\n def union(self, x, y):\n rx = self.root(x)\n ry = self.root(y)\n if rx == ry:\n return False\n self.per[rx] = ry\n self.point -= self.counts[ry] * self.counts[rx]\n self.counts[ry] += self.counts[rx]\n self.counts[rx] = 0\n\n def is_same(self, x, y):\n rx = self.root(x)\n ry = self.root(y)\n return rx == ry\n\n\nu = UnionFind(N)\nresults = []\nfor a, b in AB[::-1]:\n x, y = a-1, b-1\n u.union(x, y)\n results.append(u.point)\n\nprint(*results[::-1])\n', 'N, M = map(int, input().split(" "))\nAB = [list(map(int, input().split(" "))) for _ in range(M)]\n\n\nclass UnionFind:\n def __init__(self, n):\n self.n = n\n self.per = list(range(n))\n self.point = n*(n-1) // 2\n self.counts = [1]*n\n\n def root(self, x):\n if self.per[x] == x:\n return x\n return self.root(self.per[x])\n\n def union(self, x, y):\n rx = self.root(x)\n ry = self.root(y)\n if rx == ry:\n return False\n self.per[rx] = ry\n self.point -= self.counts[ry] * self.counts[rx]\n self.counts[ry] += self.counts[rx]\n self.counts[rx] = 0\n\n def is_same(self, x, y):\n rx = self.root(x)\n ry = self.root(y)\n return rx == ry\n\n\nu = UnionFind(N)\nresults = []\nfor a, b in AB[::-1]:\n results.append(u.point)\n x, y = a-1, b-1\n u.union(x, y)\n\nprint(*results[::-1])\n# for r in results[::-1]:\n# print(r)\n', 'N, M = map(int, input().split(" "))\nAB = [list(map(int, input().split(" "))) for _ in range(M)]\n\n\nclass UnionFind:\n def __init__(self, n):\n self.n = n\n self.per = list(range(n))\n self.point = n*(n-1) // 2\n self.counts = [1]*n\n\n def root(self, x):\n if self.per[x] == x:\n return x\n return self.root(self.per[x])\n\n def union(self, x, y):\n rx = self.root(x)\n ry = self.root(y)\n if rx == ry:\n return False\n self.per[rx] = ry\n self.point -= self.counts[ry] * self.counts[rx]\n self.counts[ry] += self.counts[rx]\n self.counts[rx] = 0\n\n def is_same(self, x, y):\n rx = self.root(x)\n ry = self.root(y)\n return rx == ry\n\n\nu = UnionFind(N)\nresults = []\nfor a, b in AB[::-1]:\n x, y = a-1, b-1\n u.union(x, y)\n results.append(u.point)\n\nfor r in results[::-1]:\n print(r)\n', 'from sys\nsys.setrecursionlimit(10**8)\n\nN, M = map(int, input().split(" "))\nAB = [list(map(int, input().split(" "))) for _ in range(M)]\n\n\nclass UnionFind:\n def __init__(self, n):\n self.n = n\n self.per = list(range(n))\n self.point = n*(n-1) // 2\n self.counts = [1]*n\n\n def root(self, x):\n if self.per[x] == x:\n return x\n return self.root(self.per[x])\n\n def union(self, x, y):\n rx = self.root(x)\n ry = self.root(y)\n if rx == ry:\n return False\n self.per[rx] = ry\n self.point -= self.counts[ry] * self.counts[rx]\n self.counts[ry] += self.counts[rx]\n self.counts[rx] = 0\n\n def is_same(self, x, y):\n rx = self.root(x)\n ry = self.root(y)\n return rx == ry\n\n\nu = UnionFind(N)\nresults = []\nfor a, b in AB[::-1]:\n results.append(u.point)\n x, y = a-1, b-1\n u.union(x, y)\n\n# print(*results[::-1])\nfor r in results[::-1]:\n print(r)\n', 'N, M = map(int, input().split())\nAB = [list(map(int, input().split())) for _ in range(M)]\n\n\nclass UnionFind:\n def __init__(self, n):\n self.par = [-1]*n\n self.size = [1]*n\n self.point = n*(n-1) // 2\n\n def root(self, x):\n if self.par[x] < 0:\n return x\n self.par[x] = self.root(self.par[x])\n return self.par[x]\n\n def unite(self, x, y):\n rx = self.root(x)\n ry = self.root(y)\n if rx == ry:\n return False\n if self.size[rx] < self.size[ry]:\n rx, ry = ry, rx\n self.point -= self.size[rx]*self.size[ry]\n self.size[rx] += self.size[ry]\n self.par[ry] = rx\n return True\n\n def is_same(self, x, y):\n rx, ry = self.root(x), self.root(y)\n return rx == ry\n\n def get_size(self, x):\n return self.size[self.root(x)]\n\n\nuf = UnionFind(N)\nresults = []\nfor a, b in AB[::-1]:\n results.append(uf.point)\n x, y = a-1, b-1\n uf.unite(x, y)\n\nprint(*results[::-1], sep="\\n")']
['Runtime Error', 'Runtime Error', 'Runtime Error', 'Runtime Error', 'Accepted']
['s332524564', 's384294781', 's441256804', 's613101477', 's166423804']
[38984.0, 38988.0, 37576.0, 2940.0, 37600.0]
[2105.0, 2105.0, 2105.0, 17.0, 601.0]
[897, 938, 912, 975, 1018]
p03108
u602860891
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["import math\n\n\ndef get_group_num(ele):\n for i in range(len(group_list)):\n if ele in group_list[i]:\n return i\n raise Exception('could not find ele in any groups')\n\n\nN, M = list(map(int, input().split()))\nbridge_list = list()\nfor _ in range(M):\n bridge_list.append(list(map(int, input().split())))\n\nanswer_list = [math.factorial(N) // math.factorial(2) // math.factorial(N - 2)]\ngroup_list = [[i] for i in range(1, N + 1)]\n\nfor bridge_list in bridge_list[::-1]:\n a = bridge_list[0]\n b = bridge_list[1]\n a_gid = get_group_num(a)\n b_gid = get_group_num(b)\n if a_gid != b_gid:\n answer_list.append(answer_list[-1] - len(group_list[a_gid]) * len(group_list[b_gid]))\n next_gid = min(a_gid, b_gid)\n old_gid = max(a_gid, b_gid)\n group_list[next_gid] += group_list[old_gid]\n group_list[old_gid] = []\n else:\n answer_list.append(answer_list[-1])\n\nfor answer in answer_list[::-1]:\n print(answer)\n", "import math\n\n\ndef find_root(ele):\n root = node_list[ele]['root']\n if root is not None:\n return find_root(root)\n return ele\n\n\nN, M = list(map(int, input().split()))\nbridge_list = list()\nfor _ in range(M):\n bridge_list.append(list(map(int, input().split())))\n\nanswer_list = [math.factorial(N) // math.factorial(2) // math.factorial(N - 2)]\nnode_list = [{'root': None, 'child_count': 0} for i in range(N)]\n\nfor bridge_list in bridge_list[:0:-1]:\n a = bridge_list[0] - 1\n b = bridge_list[1] - 1\n a_root = find_root(a)\n b_root = find_root(b)\n if a_root != b_root:\n answer_list.append(answer_list[-1] - (node_list[a_root]['child_count'] + 1) * (node_list[b_root]['child_count'] + 1))\n next_root = min(a_root, b_root)\n old_root = max(a_root, b_root)\n node_list[next_root]['child_count'] += node_list[old_root]['child_count'] + 1\n node_list[old_root]['root'] = next_root\n else:\n answer_list.append(answer_list[-1])\n\nfor answer in answer_list[::-1]:\n print(answer)\n"]
['Wrong Answer', 'Accepted']
['s600414547', 's347418654']
[38796.0, 64460.0]
[2106.0, 1278.0]
[971, 1038]
p03108
u603253967
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["def comb(n):\n return (n * (n - 1)) // 2\n\n\ndef main():\n n, m = [int(a) for a in input().split()]\n\n AB = [\n [int(a) for a in input().split()]\n for _ in range(m)\n ]\n\n table = {i: (1, set([i])) for i in range(n)} \n\n current_benrisa = 0\n max_hubensa = comb(n)\n\n reversed_benrisa = []\n for ai, bi in reversed(AB):\n reversed_benrisa.append(current_benrisa)\n s1_benrisa, s1_set = table[ai - 1]\n s2_benrisa, s2_set = table[bi - 1]\n if s1_set is s2_set: \n continue\n\n new_benri = s1_benrisa + s2_benrisa\n if len(s1_set) > len(s2_set):\n s1_set.update(s2_set)\n for t in s2_set:\n table[t] = (new_benri, s1_set)\n else:\n s2_set.update(s1_set)\n for t in s1_set:\n table[t] = (new_benri, s2_set)\n\n # new_set = s1_set.union(s2_set)\n # for t in new_set:\n \n\n inc = s1_benrisa * s2_benrisa\n current_benrisa += inc\n continue\n\n for a in reversed(reversed_benrisa):\n print(max_hubensa - a)\n\n\nif __name__ == '__main__':\n main()\n", "def comb(n):\n return (n * (n - 1)) // 2\n\n\ndef main():\n n, m = [int(a) for a in input().split()]\n\n AB = [\n [int(a) for a in input().split()]\n for _ in range(m)\n ]\n\n table = {i: {i} for i in range(n)} \n\n current_benrisa = 0\n max_hubensa = comb(n)\n\n reversed_benrisa = []\n for ai, bi in reversed(AB):\n reversed_benrisa.append(current_benrisa)\n s1_set = table[ai - 1]\n s2_set = table[bi - 1]\n if s1_set is s2_set: \n continue\n\n s1_benrisa = len(s1_set)\n s2_benrisa = len(s2_set)\n \n if len(s1_set) > len(s2_set):\n s1_set.update(s2_set)\n for t in s2_set:\n table[t] = s1_set\n else:\n s2_set.update(s1_set)\n for t in s1_set:\n table[t] = s2_set\n\n # new_set = s1_set.union(s2_set)\n # for t in new_set:\n \n\n inc = s1_benrisa * s2_benrisa\n current_benrisa += inc\n continue\n\n for a in reversed(reversed_benrisa):\n print(max_hubensa - a)\n\n\nif __name__ == '__main__':\n main()\n"]
['Wrong Answer', 'Accepted']
['s817267602', 's010567260']
[75204.0, 68800.0]
[877.0, 668.0]
[1216, 1224]
p03108
u606045429
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['def comb(n):\n return n * (n - 1) // 2\n\nclass UnionFind():\n def __init__(self, size):\n self.table = [-1 for _ in range(size)]\n\n def find(self, x):\n if self.table[x] < 0:\n return x\n else:\n self.table[x] = self.find(self.table[x])\n return self.table[x]\n\n def union(self, x, y):\n s1 = self.find(x)\n s2 = self.find(y)\n if s1 != s2:\n if self.table[s1] <= self.table[s2]:\n self.table[s1] += self.table[s2]\n self.table[s2] = s1\n else:\n self.table[s2] += self.table[s1]\n self.table[s1] = s2\n return True\n return False\n\n def size(self, x):\n r =self.find(x)\n return -self.table[r]\n\nN, M = [int(i) for i in input().split()]\n\nA = [0] * M\nB = [0] * M\nfor i in range(M):\n A[M - i - 1], B[M - i - 1] = [int(i) - 1 for i in input().split()]\n\nuf = UnionFind(N)\n\nresult = comb(N)\nlst = [result]\nfor i in range(M - 1):\n result -= uf.size(A[i]) * uf.size(B[i])\n lst.append(result)\n uf.union(A[i], B[i])\n\nlst.reverse()\n\nfor li in lst:\n print(li)\n', 'class UnionFind:\n def __init__(self, size):\n self.data = [-1 for _ in range(size)]\n def find(self, x):\n if self.data[x] < 0:\n return x\n else:\n self.data[x] = self.find(self.data[x])\n return self.data[x]\n def union(self, x, y):\n x, y = self.find(x), self.find(y)\n if x != y:\n if self.data[y] < self.data[x]:\n x, y = y, x\n self.data[x] += self.data[y]\n self.data[y] = x\n return (x != y)\n def same(self, x, y):\n return (self.find(x) == self.find(y))\n def size(self, x):\n return -self.data[self.find(x)]\n\nN, M = [int(i) for i in input().split()]\n\nA = [0] * M\nB = [0] * M\nfor i in range(M):\n A[M - i - 1], B[M - i - 1] = [int(i) - 1 for i in input().split()]\n\nuf = UnionFind(N)\n\nresult = N * (N - 1) // 2\nlst = [result]\nfor i in range(M - 1):\n if not uf.same(A[i], B[i]):\n result -= uf.size(A[i]) * uf.size(B[i])\n lst.append(result)\n uf.union(A[i], B[i])\n\nfor i in range(M - 1, -1, -1):\n print(lst[i])\n']
['Wrong Answer', 'Accepted']
['s082296675', 's568737587']
[17232.0, 16900.0]
[685.0, 773.0]
[1143, 1071]
p03108
u607563136
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['class UnionFind():\n def __init__(self, n):\n self.n = n\n self.parents = [-1]*n\n \n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return self.parents[x]\n \n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n \n if x == y:\n return\n \n if self.parents[x] > self.parents[y]:\n x,y = y,x\n \n self.parents[x] += self.parents[y]\n self.parents[y] = x\n \n def size(self, x):\n return -self.parents[self.find(x)]\ndef main():\n n, m = map(int,input().split())\n ab = [tuple(map(int,input().split())) for _ in range(m)]\n\n uf = UnionFind(n)\n\n ans = [n*(n-1)//2]\n\n for i in range(m):\n a,b = ab[-(i+1)]\n sa,sb=uf.size(a-1),uf.size(b-1)\n if ans[i]-sa*sb <=0:\n ans.append(0)\n else:\n ans.append(ans[i]-sa*sb)\n uf.union(a-1,b-1)\n \n for i in range(1,m):\n print(ans[-(i+1)])\nmain()', 'class UnionFind():\n def __init__(self, n):\n self.n = n\n self.parents = [-1]*n\n \n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return self.parents[x]\n \n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n \n if x == y:\n return\n \n if self.parents[x] > self.parents[y]:\n x,y = y,x\n \n self.parents[x] += self.parents[y]\n self.parents[y] = x\n \n def size(self, x):\n return -self.parents[self.find(x)]\n \n def same(self, x, y):\n return self.find(x) == self.find(y)\n\ndef main():\n n, m = map(int,input().split())\n ab = [tuple(map(int,input().split())) for _ in range(m)]\n\n uf = UnionFind(n)\n\n ans = [n*(n-1)//2]\n\n for a,b in ab[::-1]:\n sa,sb=uf.size(a-1),uf.size(b-1)\n if uf.same(a-1,b-1):\n ans.append(ans[-1])\n else:\n ans.append(ans[-1]-sa*sb)\n uf.union(a-1,b-1)\n \n print(*ans[::-1][1:], sep="\\n")\n\nmain()']
['Wrong Answer', 'Accepted']
['s999646859', 's520806958']
[28404.0, 30568.0]
[434.0, 488.0]
[1099, 1144]
p03108
u608088992
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['N, M = map(int, input().split())\nBridge= [[int(i) for i in input().split()] for _ in range(M)]\nParent = [int(i) for i in range(N)]\nRank = [1] * N\n \ndef find(x):\n if x == Parent[x]: return x\n else:\n Parent[x] = y = find(Parent[x])\n return y\n \ndef unite(x, y, Total):\n rx, ry = find(x), find(y)\n if rx == ry: return Total\n else:\n \tTotal -= Rank[rx] * Rank[ry]\n if rx > ry:\n \t\tParent[rx] = ry\n \t\tRank[ry] += Rank[rx]\n else:\n Parent[ry] = rx\n Rank[rx] += Rank[ry]\n \treturn Total\n \nTotal = N * (N - 1) // 2\nAns = str(Total)\nfor i in reversed(range(1, M)):\n A, B = Bridge[i]\n A -= 1\n B -= 1\n Total = unite(A, B, Total)\n Ans = str(Total) + "\\n" + Ans \n \nprint(Ans)', 'import sys\n\nclass UFT: #Union-find tree class\n def __init__(self, N): \n self.tree = [int(i) for i in range(N)] \n self.rank = [0 for i in range(N)]\n self.size = [1] * N\n\n def find(self, a):\n if self.tree[a] == a: return a\n else:\n self.tree[a] = self.find(self.tree[a])\n return self.tree[a]\n\n def findSize(self, a):\n self.tree[a] = self.find(a)\n self.size[a] = self.size[self.tree[a]]\n return self.size[a]\n\n def unite(self, a, b):\n a = self.find(a)\n b = self.find(b)\n asize = self.size[a]\n bsize = self.size[b]\n if a == b: return\n if self.rank[a] < self.rank[b]: \n self.tree[a] = b\n self.size[b] += asize\n else:\n self.tree[b] = a\n self.size[a] += bsize\n if self.rank[a] == self.rank[b]: self.rank[a] += 1\n\n\n\ndef solve():\n input = sys.stdin.readline\n N, M = map(int, input().split())\n B = [[int(a) - 1 for a in input().split()] for _ in range(M)]\n parent = UFT(N)\n\n Ans = [N * (N - 1) // 2] * M\n for i in reversed(range(1, M)):\n x, y = B[i]\n if parent.find(x) != parent.find(y):\n xsize = parent.findSize(x)\n ysize = parent.findSize(y)\n parent.unite(x, y)\n Ans[i - 1] = Ans[i] - (xsize * ysize)\n else:\n Ans[i-1] = Ans[i]\n print("\\n".join(map(str, Ans)))\n\n return 0\n\nif __name__ == "__main__":\n solve()']
['Runtime Error', 'Accepted']
['s175983906', 's044869347']
[3064.0, 38988.0]
[17.0, 598.0]
[746, 1494]
p03108
u610473220
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["import sys\ninput = sys.stdin.readline\n\n\nclass UnionFindTree:\n def __init__(self, n: int):\n self.n = n\n self.root = [-1] * n\n self.rank = [0] * n\n def findRoot(self, x: int) -> int:\n if self.root[x] < 0:\n return x\n else:\n self.root[x] = self.findRoot(self.root[x])\n return self.root[x]\n def unite(self, x: int, y: int):\n x = self.findRoot(x)\n y = self.findRoot(y)\n if x == y:\n return\n elif self.rank[x] > self.rank[y]:\n self.root[x] += self.root[y]\n self.root[y] = x\n else:\n self.root[y] += self.root[x]\n self.root[x] = y\n if self.rank[x] == self.rank[y]:\n self.rank[y] += 1\n def same(self, x: int, y: int) -> bool:\n if self.findRoot(x) == self.findRoot(y):\n return True\n else:\n return False\n def count(self, x: int) -> int:\n return -self.root[self.findRoot(x)]\n def treeCount(self) -> int:\n cou = 0\n for i in self.root:\n if i < 0:\n cou += 1\n return cou\n\n\ndef main():\n N, M = map(int, input().split())\n UFT = UnionFindTree(N)\n ans = [0] * M\n bridges = []\n for _ in range(M):\n A, B = map(int, input().split())\n bridges.append((A, B))\n ans[M-1] = N * (N-1) // 2\n for i in range(M-1, -1, 0):\n A = bridges[i][0]\n B = bridges[i][1]\n if UFT.same(A, B):\n ans[i-1] = ans[i] - UFT.count(A) * UFT.count(B)\n UFT.unite(A, B)\n else:\n ans[i-1] = ans[i]\n for i in ans:\n print(i)\n\n\nif __name__ == '__main__': main()", "import sys\ninput = sys.stdin.readline\n\n\nclass UnionFindTree:\n def __init__(self, n: int):\n self.n = n\n self.root = [-1] * n\n self.rank = [0] * n\n def findRoot(self, x: int) -> int:\n if self.root[x] < 0:\n return x\n else:\n self.root[x] = self.findRoot(self.root[x])\n return self.root[x]\n def unite(self, x: int, y: int):\n x = self.findRoot(x)\n y = self.findRoot(y)\n if x == y:\n return\n elif self.rank[x] > self.rank[y]:\n self.root[x] += self.root[y]\n self.root[y] = x\n else:\n self.root[y] += self.root[x]\n self.root[x] = y\n if self.rank[x] == self.rank[y]:\n self.rank[y] += 1\n def same(self, x: int, y: int) -> bool:\n if self.findRoot(x) == self.findRoot(y):\n return True\n else:\n return False\n def count(self, x: int) -> int:\n return -self.root[self.findRoot(x)]\n def treeCount(self) -> int:\n cou = 0\n for i in self.root:\n if i < 0:\n cou += 1\n return cou\n\n\ndef main():\n N, M = map(int, input().split())\n UFT = UnionFindTree(N)\n ans = [0] * M\n bridges = []\n for _ in range(M):\n A, B = map(int, input().split())\n bridges.append((A-1, B-1))\n ans[M-1] = N * (N-1) // 2\n for i in range(M-1, 0, -1):\n A = bridges[i][0]\n B = bridges[i][1]\n if UFT.same(A, B):\n ans[i-1] = ans[i]\n else:\n ans[i-1] = ans[i] - UFT.count(A) * UFT.count(B)\n UFT.unite(A, B)\n for i in ans:\n print(i)\n\n\nif __name__ == '__main__': main()"]
['Runtime Error', 'Accepted']
['s801737751', 's262012240']
[18996.0, 23288.0]
[146.0, 580.0]
[1696, 1700]
p03108
u614314290
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['\nINF = float("inf")\nMOD = int(1e9 + 7)\ndef int1(n):\n return int(n) - 1\ndef parse(*args):\n return tuple(p(v) for p, v in zip(args, input().split()))\n\n\nclass uft:\n def __init__(self, n):\n self.height = [1] * n\n self.group = [-1] * n\n def root(self, v):\n if self.group[v] < 0:\n return v\n self.group[v] = self.root(self.group[v])\n return self.group[v]\n def size(self, v):\n return - self.group[self.root(v)]\n def equal(self, v1, v2):\n v1, v2 = self.root(v1), self.root(v2)\n return v1 == v2\n def merge(self, v1, v2):\n if self.equal(v1, v2):\n return False\n if self.height[v1] < self.height[v2]:\n self.group[v2] += self.group[v1]\n self.group[v1] = v2\n self.height[v2] = max(self.height[v1] + 1, self.height[v2])\n else:\n self.group[v1] += self.group[v2]\n self.group[v2] = v1\n self.height[v1] = max(self.height[v1], self.height[v2] + 1)\n return True\n\n\ndef main():\n N, M = parse(int, int)\n AB = [parse(int1, int1) for _ in range(M)]\n\n fuben = N * (N - 1) // 2\n g, rs = uft(N), [fuben]\n for a, b in reversed(AB):\n if not g.equal(a, b):\n fuben -= g.size(a) * g.size(b)\n g.merge(a, b)\n rs += [fuben]\n for r in reversed(rs):\n print(r)\n\nmain()\n', '\nINF = float("inf")\nMOD = int(1e9 + 7)\ndef int1(n):\n return int(n) - 1\ndef parse(*args):\n # (tuple(map(int, input().split())) for _ in range(N))\n return tuple(p(v) for p, v in zip(args, input().split()))\n\n\nclass uft:\n def __init__(self, n):\n self.height = [1] * n\n self.group = [-1] * n\n def root(self, v):\n if self.group[v] < 0:\n return v\n self.group[v] = self.root(self.group[v])\n return self.group[v]\n def size(self, v):\n return - self.group[self.root(v)]\n def equal(self, v1, v2):\n v1, v2 = self.root(v1), self.root(v2)\n return v1 == v2\n def merge(self, v1, v2):\n v1, v2 = self.root(v1), self.root(v2)\n if self.equal(v1, v2):\n return False\n if self.height[v1] < self.height[v2]:\n self.group[v2] += self.group[v1]\n self.group[v1] = v2\n self.height[v2] = max(self.height[v1] + 1, self.height[v2])\n else:\n self.group[v1] += self.group[v2]\n self.group[v2] = v1\n self.height[v1] = max(self.height[v1], self.height[v2] + 1)\n return True\n\n\ndef main():\n N, M = parse(int, int)\n AB = [tuple(map(int, input().split())) for _ in range(M)]\n\n fuben = N * (N - 1) // 2\n g, rs = uft(N), [fuben]\n for a, b in reversed(AB):\n a, b = a - 1, b - 1\n if not g.equal(a, b):\n fuben -= g.size(a) * g.size(b)\n g.merge(a, b)\n rs += [fuben]\n for r in reversed(rs[:-1]):\n print(r)\n\nmain()\n']
['Runtime Error', 'Accepted']
['s964175512', 's646817130']
[22176.0, 25352.0]
[661.0, 866.0]
[1437, 1590]
p03108
u619819312
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['n,m=map(int,input().split())\na=[list(map(int,input().split()))for i in range(m)][::-1]\nw=[i for i in range(n+1)]\ns=[1]*(n+1)\ndef root(x):\n if w[x]==x:\n return x\n else:\n w[x]=root(w[x])\n return root(w[x])\ndef unite(x,y):\n x=root(x)\n y=root(y)\n if x!=y:\n if s[x]<s[y]:\n w[x]=y\n s[y]+=s[x]\n else:\n w[y]=x\n s[x]+=s[y]\nk=[]\nans=n*(n-1)//2\nfor i in a:\n k.append(ans)\n g=root(i[0])\n h=root(i[1])\n if g!=h:\n c=s[g]\n d=s[h]\n unite(g,h)\n ans-=(c+d)*(c+d-1)//2-c*(c-1)//2-d*(d-1)//2\n unite(g,h)\nfor i in k[::-1]:\n print(i)', 'n,m=map(int,input().split())\na=[list(map(int,input().split()))for i in range(m)][::-1]\nw=[i for i in range(n+1)]\ns=[1]*(n+1)\ndef root(x):\n if w[x]==x:\n return x\n x=w[x]\n return root(w[x])\ndef unite(x,y):\n x=root(x)\n y=root(y)\n if x!=y:\n if s[x]<s[y]:\n w[x]=y\n s[y]+=s[x]\n else:\n w[y]=x\n s[x]+=s[y]\nk=[]\nans=n*(n-1)//2\nfor i in a:\n k.append(ans)\n g=root(i[0])\n h=root(i[1])\n if g!=h:\n c=s[g]\n d=s[h]\n unite(g,h)\n ans-=(c+d)*(c+d-1)//2-c*(c-1)//2-d*(d-1)//2\n unite(g,h)\nfor i in k[::-1]:\n print(i)']
['Wrong Answer', 'Accepted']
['s004451072', 's414634626']
[35436.0, 35884.0]
[764.0, 705.0]
[643, 628]
p03108
u623819879
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["sys.setrecursionlimit(100000)\n\nn, m=map(int, input().split())\na=[0 for i in range(m)]\nb=[0 for i in range(m)]\ng=[list() for i in range(1+n)]\n#print(g)\n\npar=[i for i in range(n)]\ncnt_con=[1 for i in range(n)]\n\ndef root(x):\n if x==par[x]:\n return x\n else:\n par[x]=root(par[x])\n return par[x]\ndef connect(x,y):\n #par[x]=root(x)\n #par[y]=root(y)\n if root(x)==root(y):\n 0\n else:\n cnt_con[root(y)]+=cnt_con[root(x)]\n par[root(x)]=root(y) \n return\n\nfor i in range(m):\n a[i], b[i]=map(int, input().split())\n a[i], b[i]=a[i]-1, b[i]-1\n\n\ntmp=int(n*(n-1)/2)\nans=[0]*m\nans[-1]=tmp\n#ans[0]=0\n#print('a,b=',a,b)\nfor i in range(m-1,-1,-1):\n ans[i]=tmp\n if root(a[i])==root(b[i]):\n 0\n else:\n tmp-=cnt_con[root(a[i])]*cnt_con[root(b[i])]\n connect(a[i],b[i])\n #print('i,tmp',i,(a[i],b[i]),ans[i],tmp,par,cnt_con)\n \n\n \nfor i in range(m):\n print(ans[i])\n", "n, m=map(int, input().split())\na=[0 for i in range(m)]\nb=[0 for i in range(m)]\ng=[list() for i in range(1+n)]\n#print(g)\n\ndef Count_n_d(neighbor, start, search):\n visited = []\n stack = []\n stack.append(start)\n result = []\n while len(stack) > 0:\n next_city = stack.pop()\n if next_city in visited:\n continue\n result.append(next_city)\n visited.append(next_city)\n for nei in neighbor[next_city]:\n if nei==search:\n return [[]] \n stack.append(nei)\n if search in result:\n return [[]]\n else:\n return [result]\n\ndef Count_n(neighbor, start, search):\n visited = []\n queue = []\n queue.append(start)\n result = []\n while len(queue) > 0:\n next_city = queue.pop(0)\n if next_city in visited:\n continue\n result.append (next_city)\n visited.append(next_city)\n for nei in neighbor[next_city]:\n queue.append(nei)\n if search in result:\n return [[]]\n else:\n return [result]\n'''\nprint(g)\n#print(Count(1,4,0))\nfor l in range(1,n+1):\n print('l,Count_n(g,l,0),len(Count_n(g,l,0))',l,Count_n(g,l,0),len(Count_n(g,l,0)[0]))\n'''\nfor i in range(m):\n a[i], b[i]=map(int, input().split())\n g[a[i]].append(b[i])\n g[b[i]].append(a[i])\n #print(g)\ntmp=int(m*(m-1)/2)\nans=[0]*m\nfor i in range(m,1,-1):\n \n cnt_a=len(Count_n(g,a[i],b[i])[0])\n if cnt_a==0:\n ans[i]=tmp\n else:\n tmp-=cnt_a*len(Count_n(g,b[i],0)[0])\n ans[i]=tmp\n \n g[a[i]].append(b[i])\n g[b[i]].append(a[i])\n \nfor i in range(m):\n print(ans[i])", "sys.setrecursionlimit(100000)\n\nn, m=map(int, input().split())\na=[0 for i in range(m)]\nb=[0 for i in range(m)]\n\npar=[i for i in range(n)]\ncnt_con=[1 for i in range(n)]\n\ndef root(x):\n if x==par[x]:\n return x\n else:\n par[x]=root(par[x])\n return par[x]\ndef connect(x,y):\n if root(x)==root(y):\n 0\n else:\n cnt_con[root(y)]+=cnt_con[root(x)]\n par[root(x)]=root(y) \n return\n\nfor i in range(m):\n a[i], b[i]=map(int, input().split())\n a[i], b[i]=a[i]-1, b[i]-1\n\n\ntmp=int(n*(n-1)/2)\nans=[0]*m\nans[-1]=tmp\n#ans[0]=0\n#print('a,b=',a,b)\na=a[::-1]\nb=b[::-1]\nfor i in range(m):\n ans[i]=tmp\n if root(a[i])==root(b[i]):\n 0\n else:\n tmp-=cnt_con[root(a[i])]*cnt_con[root(b[i])]\n connect(a[i],b[i])\n #print('i,tmp',i,(a[i],b[i]),ans[i],tmp,par,cnt_con)\nfor i in range(m):\n print(ans[i])\n", "n, m=map(int, input().split())\na=[0 for i in range(m)]\nb=[0 for i in range(m)]\ng=[list() for i in range(1+n)]\n#print(g)\n\nroot=[i for i in range(m)]\ncnt_con=[1 for i in range(m)]\n\ndef parent(x):\n return root[x]\ndef connect(x,y):\n if root[x]==root[y]:\n 0\n else:\n root[x]=root[y]\n cnt_con[root[y]]+=cnt_con[root[x]]\n return\n\nfor i in range(m):\n a[i], b[i]=map(int, input().split())\n a[i], b[i]=a[i]-1, b[i]-1\n\ntmp=int(n*(n-1)/2)\nans=[0]*m\nans[-1]=tmp\nans[0]=0\n#print('a,b=',a,b)\nfor i in range(m-1,0,-1):\n ans[i]=tmp\n if root[a[i]]==root[b[i]]:\n 0\n else:\n tmp-=cnt_con[a[i]]*cnt_con[b[i]]\n connect(a[i],b[i])\n #print('i,tmp',i,tmp,root,cnt_con)\n \n \nfor i in range(m):\n print(ans[i])", "n, m=map(int, input().split())\na=[0 for i in range(m)]\nb=[0 for i in range(m)]\ng=[list() for i in range(1+n)]\n#print(g)\n\ndef Count_n_d(neighbor, start, search):\n visited = []\n stack = []\n stack.append(start)\n result = []\n while len(stack) > 0:\n next_city = stack.pop()\n if next_city in visited:\n continue\n result.append(next_city)\n visited.append(next_city)\n for nei in neighbor[next_city]:\n if nei==search:\n return [[]] \n stack.append(nei)\n if search in result:\n return [[]]\n else:\n return [result]\n\ndef Count_n(neighbor, start, search):\n visited = []\n queue = []\n queue.append(start)\n result = []\n while len(queue) > 0:\n next_city = queue.pop(0)\n if next_city in visited:\n continue\n result.append (next_city)\n visited.append(next_city)\n for nei in neighbor[next_city]:\n queue.append(nei)\n if search in result:\n return [[]]\n else:\n return [result]\n'''\nprint(g)\n#print(Count(1,4,0))\nfor l in range(1,n+1):\n print('l,Count_n(g,l,0),len(Count_n(g,l,0))',l,Count_n(g,l,0),len(Count_n(g,l,0)[0]))\n'''\nfor i in range(m):\n a[i], b[i]=map(int, input().split())\n g[a[i]].append(b[i])\n g[b[i]].append(a[i])\n #print(g)\ntmp=int(m*(m-1)/2)\nans=[0]*m\nfor i in range(m-1,0,-1):\n \n cnt_a=len(Count_n(g,a[i],b[i])[0])\n if cnt_a==0:\n ans[i]=tmp\n else:\n tmp-=cnt_a*len(Count_n(g,b[i],0)[0])\n ans[i]=tmp\n \n g[a[i]].append(b[i])\n g[b[i]].append(a[i])\n \nfor i in range(m):\n print(ans[i])", "import sys\nsys.setrecursionlimit(100000)\n\nn, m=map(int, input().split())\na=[0 for i in range(m)]\nb=[0 for i in range(m)]\ng=[list() for i in range(1+n)]\n#print(g)\n\npar=[i for i in range(n)]\ncnt_con=[1 for i in range(n)]\n\ndef root(x):\n if x==par[x]:\n return x\n else:\n par[x]=root(par[x])\n return par[x]\ndef connect(x,y):\n #par[x]=root(x)\n #par[y]=root(y)\n if root(x)==root(y):\n 0\n else:\n cnt_con[root(y)]+=cnt_con[root(x)]\n par[root(x)]=root(y) \n return\n\nfor i in range(m):\n a[i], b[i]=map(int, input().split())\n a[i], b[i]=a[i]-1, b[i]-1\n\n\ntmp=int(n*(n-1)/2)\nans=[0]*m\nans[-1]=tmp\n#ans[0]=0\n#print('a,b=',a,b)\nfor i in range(m-1,-1,-1):\n ans[i]=tmp\n if root(a[i])==root(b[i]):\n 0\n else:\n tmp-=cnt_con[root(a[i])]*cnt_con[root(b[i])]\n connect(a[i],b[i])\n #print('i,tmp',i,(a[i],b[i]),ans[i],tmp,par,cnt_con)\n \n\n \nfor i in range(m):\n print(ans[i])\n\n"]
['Runtime Error', 'Runtime Error', 'Runtime Error', 'Runtime Error', 'Wrong Answer', 'Accepted']
['s223988624', 's253969231', 's295200044', 's608818776', 's769655383', 's364826077']
[3064.0, 24220.0, 3064.0, 98592.0, 25928.0, 38080.0]
[19.0, 454.0, 18.0, 2109.0, 2105.0, 867.0]
[946, 1652, 866, 760, 1654, 958]
p03108
u623954643
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['N,M = map(int,input().split())\n\na = [0]*M\nb = [0]*M\nans = [0]*M\nfor i in range(M):\n\tA,B = map(int,input().split())\n\ta[i] = A\n\tb[i] = B\n\n\nclass UnionFind():\n \n \n def __init__(self,size):\n self.table = [-1 for _ in range(size)]\n\n \n def find(self,x):\n while self.table[x] >= 0:\n \n x = self.table[x]\n return x\n def is_same_set(self,x,y):\n \treturn self.find(x)==self.find(y)\n \n def size(self,x):\n \ty = self.find(x)\n \treturn -self.table[y]\n def union(self,x,y):\n s1 = self.find(x)\n s2 = self.find(y)\n if s1 != s2:\n if self.table[s1] != self.table[s2]:\n if self.table[s1] < self.table[s2]:\n self.table[s1] -= self.size(s2)\t\n self.table[s2] = s1\n else:\n self.table[s2] -= self.size(s1)\n self.table[s1] = s2\n else:\n \n \n self.table[s1] -= self.size(s2)\n self.table[s2] = s1\n return\nuf = UnionFind(N+1)\nnow = N*(N-1)//2\n\nfor i in range(M):\n\tj = M-i-1\n\tans[j] = now\n\tsa = uf.size(a[j])\n\tsb = uf.size(b[j])\n\tprint(sa,sb)\n\tif not uf.is_same_set(a[j],b[j]):\n\t\tnow -= sa*sb\n\tuf.union(a[j],b[j])\n\nfor i in range(M):\n\tprint(ans[i])', 'N,M = map(int,input().split())\n\na = [0]*M\nb = [0]*M\nans = [0]*M\nfor i in range(M):\n\tA,B = map(int,input().split())\n\ta[i] = A\n\tb[i] = B\n\n\nclass UnionFind():\n \n \n def __init__(self,size):\n self.table = [-1 for _ in range(size)]\n\n \n def find(self,x):\n while self.table[x] >= 0:\n \n x = self.table[x]\n return x\n def is_same_set(self,x,y):\n \treturn self.find(x)==self.find(y)\n \n def size(self,x):\n \ty = self.find(x)\n \treturn -self.table[y]\n def union(self,x,y):\n s1 = self.find(x)\n s2 = self.find(y)\n if s1 != s2:\n if self.table[s1] != self.table[s2]:\n if self.table[s1] < self.table[s2]:\n self.table[s1] -= self.size(s2)\t\n self.table[s2] = s1\n else:\n self.table[s2] -= self.size(s1)\n self.table[s1] = s2\n else:\n \n \n self.table[s1] -= self.size(s2)\n self.table[s2] = s1\n return\nuf = UnionFind(N+1)\nnow = N*(N-1)//2\n\nfor i in range(M):\n\tj = M-i-1\n\tans[j] = now\n\tsa = uf.size(a[j])\n\tsb = uf.size(b[j])\n\tif not uf.is_same_set(a[j],b[j]):\n\t\tnow -= sa*sb\n\tuf.union(a[j],b[j])\n\nfor i in range(M):\n\tprint(ans[i])']
['Wrong Answer', 'Accepted']
['s359897209', 's546971813']
[17984.0, 16936.0]
[1024.0, 881.0]
[1754, 1740]
p03108
u650622056
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["N, M = [int(_) for _ in input().split()]\nbridge = [[int(_) for _ in input().split()] for _ in range(M)]\n\nbridge.reverse()\nbridge = bridge[:-1]\n\ninconvenience = N * (N - 1) // 2\ninconvenience_history = [inconvenience]\n\n\nclass UnionFind:\n def __init__(self, N):\n self.par = [i for i in range(N + 1)]\n self._number = [1] * (N + 1)\n\n def root(self, i):\n if self.par[i] == i:\n return i\n self.par[i] = self.root(self.par[i])\n return self.par[i]\n\n def number(self, i):\n return self._number[self.root(i)]\n\n def union(self, i, j):\n _root_i = self.root(i)\n _root_j = self.root(j)\n if _root_i == _root_j:\n return\n else:\n\t\t\tself.par[_root_j] = _root_i\n self._number[_root_i] += self._number[_root_j]\n\n\nislands = UnionFind(N)\n\nfor A_i, B_i in bridge:\n\n root_A = islands.root(A_i)\n root_B = islands.root(B_i)\n\n if root_A == root_B:\n inconvenience_history.append(inconvenience)\n continue\n else:\n improvement = islands.number(A_i) * islands.number(B_i)\n islands.union(A_i, B_i)\n\n inconvenience -= improvement\n if inconvenience == 0:\n inconvenience_history += [0] * (M - len(inconvenience_history))\n break\n inconvenience_history.append(inconvenience)\n\nprint(*inconvenience_history[::-1], sep='\\n')\n", "N, M = [int(_) for _ in input().split()]\nbridge = [[int(_) for _ in input().split()] for _ in range(M)]\n\nbridge.reverse()\nbridge = bridge[:-1]\n\ninconvenience = N * (N - 1) // 2\ninconvenience_history = [inconvenience]\n\n\nclass UnionFind:\n def __init__(self, N):\n self.par = [i for i in range(N + 1)]\n self.rank = [0] * (N + 1)\n self._number = [1] * (N + 1)\n\n def root(self, i):\n if self.par[i] == i:\n return i\n self.par[i] = self.root(self.par[i])\n return self.par[i]\n\n def tree_rank(self, i):\n return self.rank[self.root(i)]\n\n def number(self, i):\n return self._number[self.root(i)]\n\n def union(self, i, j):\n _root_i = self.root(i)\n _root_j = self.root(j)\n if _root_i == _root_j:\n return\n else:\n if self.tree_rank(i) < self.tree_rank(j):\n self.par[_root_i] = _root_j\n self._number[_root_j] += self._number[_root_i]\n else:\n self.par[_root_j] = _root_i\n self._number[_root_i] += self._number[_root_j]\n if self.tree_rank(i) == self.tree_rank(j):\n self.rank[_root_i] += 1\n\n\nislands = UnionFind(N)\n\nfor A_i, B_i in bridge:\n\n islands.pri()\n\n root_A = islands.root(A_i)\n root_B = islands.root(B_i)\n\n if root_A == root_B:\n inconvenience_history.append(inconvenience)\n continue\n else:\n improvement = islands.number(A_i) * islands.number(B_i)\n islands.union(A_i, B_i)\n\n inconvenience -= improvement\n if inconvenience == 0:\n inconvenience_history += [0] * (M - len(inconvenience_history))\n break\n inconvenience_history.append(inconvenience)\n\nprint(*inconvenience_history[::-1], sep='\\n')\n", "N, M = [int(_) for _ in input().split()]\nbridge = [[int(_) for _ in input().split()] for _ in range(M)]\n\nbridge.reverse()\nbridge = bridge[:-1]\n\nislands_group = []\ninconvenience = N * (N - 1) // 2\ninconvenience_history = [inconvenience]\n\nfor A_i, B_i in bridge:\n\n islands_of_A = set()\n islands_of_B = set()\n\n for j, islands in enumerate(islands_group):\n if A_i in islands: islands_of_A = islands_group.pop(j)\n if B_i in islands: islands_of_B = islands_group.pop(j)\n\n if islands_of_A and islands_of_B:\n if islands_of_A == islands_of_B:\n improvement = 0\n else:\n improvement = len(islands_of_A) * len(islands_of_B)\n islands_group.append(islands_of_A | islands_of_B)\n break\n else:\n if not islands_of_A and not islands_of_B:\n improvement = 1\n islands_group.append({A_i, B_i})\n else:\n if islands_of_A:\n improvement = len(islands_of_A)\n islands_group.append(islands_of_A | {B_i})\n else:\n improvement = len(islands_of_B)\n islands_group.append(islands_of_B | {A_i})\n\n inconvenience -= improvement\n if inconvenience == 0:\n inconvenience_history += [0] * (M - len(inconvenience_history))\n break\n inconvenience_history.append(inconvenience)\n\nprint(*inconvenience_history[::-1], sep='\\n')\n", "N, M = [int(_) for _ in input().split()]\nbridge = [[int(_) for _ in input().split()] for _ in range(M)]\n\nbridge.reverse()\nbridge = bridge[:-1]\n\ninconvenience = N * (N - 1) // 2\ninconvenience_history = [inconvenience]\n\n\nclass UnionFind:\n def __init__(self, N):\n self.par = [i for i in range(N + 1)]\n self.rank = [0] * (N + 1)\n self._number = [1] * (N + 1)\n\n def root(self, i):\n if self.par[i] == i:\n return i\n self.par[i] = self.root(self.par[i])\n return self.par[i]\n\n def tree_rank(self, i):\n return self.rank[self.root(i)]\n\n def number(self, i):\n return self._number[self.root(i)]\n\n def union(self, i, j):\n _root_i = self.root(i)\n _root_j = self.root(j)\n if _root_i == _root_j:\n return\n else:\n if self.tree_rank(i) < self.tree_rank(j):\n self.par[_root_i] = _root_j\n self._number[_root_j] += self._number[_root_i]\n else:\n self.par[_root_j] = _root_i\n self._number[_root_i] += self._number[_root_j]\n if self.tree_rank(i) == self.tree_rank(j):\n self.rank[_root_i] += 1\n\n\nislands = UnionFind(N)\n\nfor A_i, B_i in bridge:\n\n root_A = islands.root(A_i)\n root_B = islands.root(B_i)\n\n if root_A == root_B:\n inconvenience_history.append(inconvenience)\n continue\n else:\n improvement = islands.number(A_i) * islands.number(B_i)\n islands.union(A_i, B_i)\n\n inconvenience -= improvement\n if inconvenience == 0:\n inconvenience_history += [0] * (M - len(inconvenience_history))\n break\n inconvenience_history.append(inconvenience)\n\nprint(*inconvenience_history[::-1], sep='\\n')\n"]
['Runtime Error', 'Runtime Error', 'Runtime Error', 'Accepted']
['s175170215', 's568431545', 's790777927', 's933690907']
[3064.0, 27300.0, 24540.0, 31528.0]
[17.0, 326.0, 2105.0, 973.0]
[1379, 1799, 1427, 1780]
p03108
u655975843
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['import itertools\n\nn, m = map(int, input().split())\npair = []\nfor i in range(m):\n\tpair.append(list(map(int, input().split())))\npair = pair[::-1]\nans = []\nans.append(int(n*(n-1)/2))\n\nclass UnionFind:\n def __init__(self, n):\n self.par = [-1] * (n+1)\n self.rank = [0] * (n+1)\n\n def find(self, x):\n if self.par[x] < 0:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n\n def size(self, x):\n return -self.par[self.find(x)]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x == y:\n return False\n elif self.size(x) < self.size(y):\n x, y = y, x\n self.par[x] += self.par[y]\n self.par[y] = x\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n return True\n\n def same_check(self, x, y):\n return self.find(x) == self.find(y)\n\nu = UnionFind(n)\nfor i in range(len(pair) - 1):\n ans.append(int(ans[i] - u.size(pair[i][0]) * u.size(pair[i][1])))\n u.union(pair[i][0], pair[i][1])\nfor i in range(len(ans)):\n print(ans[- i - 1])', 'n, m = map(int, input().split())\npair = []\nfor i in range(m):\n\tpair.append(list(map(int, input().split())))\npair = pair[::-1]\nans = []\nans.append(int(n*(n-1)/2))\n\nclass UnionFind:\n def __init__(self, n):\n self.par = [-1] * (n+1)\n self.rank = [0] * (n+1)\n\n def find(self, x):\n if self.par[x] < 0:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n\n def size(self, x):\n return -self.par[self.find(x)]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x == y:\n return False\n elif self.size(x) < self.size(y):\n x, y = y, x\n self.par[x] += self.par[y]\n self.par[y] = x\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n return True\n\n def same_check(self, x, y):\n return self.find(x) == self.find(y)\n\nu = UnionFind(n)\nfor i in range(len(pair) - 1):\n ans.append(int(ans[i] - u.size(pair[i][0]) * u.size(pair[i][1])))\n u.union(pair[i][0], pair[i][1])\nans = ans[::-1]\nfor i in range(len(ans)):\n print(ans[i])', 'n, m = map(int, input().split())\npair = []\nfor i in range(m):\n\tpair.append(list(map(int, input().split())))\npair = pair[::-1]\nans = []\nans.append(int(n*(n-1)/2))\n\nclass UnionFind:\n def __init__(self, n):\n self.par = [-1] * (n+1)\n self.rank = [0] * (n+1)\n\n def find(self, x):\n if self.par[x] < 0:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n\n def size(self, x):\n return -self.par[self.find(x)]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x == y:\n return False\n elif self.size(x) < self.size(y):\n x, y = y, x\n self.par[x] += self.par[y]\n self.par[y] = x\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n return True\n\n def same_check(self, x, y):\n return self.find(x) == self.find(y)\n\nu = UnionFind(n)\nfor i in range(len(pair) - 1):\n if u.union(pair[i][0], pair[i][1]) == True:\n ans.append(int(ans[i] - u.size(pair[i][0]) * u.size(pair[i][1])))\n u.union(pair[i][0], pair[i][1])\nans = ans[::-1]\nfor i in range(len(ans)):\n print(ans[i])', 'n, m = map(int, input().split())\npair = []\nfor i in range(m):\n\tpair.append(list(map(int, input().split())))\npair = pair[::-1]\nans = []\nans.append(int(n*(n-1)/2))\n\nclass UnionFind:\n def __init__(self, n):\n self.par = [-1] * (n+1)\n self.rank = [0] * (n+1)\n\n def find(self, x):\n if self.par[x] < 0:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n\n def size(self, x):\n return -self.par[self.find(x)]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x == y:\n return\n elif self.size(x) < self.size(y):\n x, y = y, x\n self.par[x] += self.par[y]\n self.par[y] = x\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n return\n\n def same_check(self, x, y):\n return self.find(x) == self.find(y)\n\nu = UnionFind(n)\nfor i in range(len(pair) - 1):\n if u.find(pair[i][0]) != u.find(pair[i][1]):\n ans.append(max(0, int(ans[i] - u.size(pair[i][0]) * u.size(pair[i][1]))))\n else:\n ans.append(ans[i])\n u.union(pair[i][0], pair[i][1])\nans = ans[::-1]\nfor i in range(len(ans)):\n print(ans[i])']
['Wrong Answer', 'Wrong Answer', 'Runtime Error', 'Accepted']
['s211257732', 's723913493', 's956949455', 's503233076']
[34688.0, 35504.0, 37508.0, 35468.0]
[894.0, 862.0, 979.0, 942.0]
[1138, 1130, 1182, 1217]
p03108
u663230781
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['def top(L, node):\n current = node\n while L[current] != current:\n current = L[current]\n return current\n\n\ndef main():\n n, m = map(int, input().rstrip("\\n").split(" "))\n\n L = [0]\n counts = {}\n for i in range(1, n + 1):\n L.append(i)\n counts[i] = 1\n\n edges = []\n for j in range(m):\n a_b = map(int, input().rstrip("\\n").split(" "))\n edges.append(a_b)\n\n total = n * (n - 1) // 2\n\n answers = [total]\n for a, b in reversed(edges):\n a_top = top(L, a)\n current = a\n while L[current] != current:\n current = L[current]\n a_top = current\n b_top = top(L, b)\n current = a\n while L[current] != current:\n current = L[current]\n b_top = current\n if a_top == b_top:\n answers.append(total)\n else:\n total -= counts[a_top] * counts[b_top]\n answers.append(total)\n L[b_top] = a_top\n L[b] = a_top\n L[a] = a_top\n counts[a_top] += counts[b_top]\n\n for answer in reversed(answers[:-1]):\n print(answer)\n\nif __name__ == \'__main__\':\n main()\n', 'def main():\n n, m = map(int, input().rstrip("\\n").split(" "))\n\n L = [0]\n counts = {}\n for i in range(1, n + 1):\n L.append(i)\n counts[i] = 1\n\n edges = []\n for j in range(m):\n a_b = map(int, input().rstrip("\\n").split(" "))\n edges.append(a_b)\n\n total = n * (n - 1) // 2\n\n answers = [total]\n for a, b in reversed(edges):\n current = a\n while L[current] != current:\n current = L[current]\n a_top = current\n current = b\n while L[current] != current:\n current = L[current]\n b_top = current\n if a_top == b_top:\n answers.append(total)\n else:\n total -= counts[a_top] * counts[b_top]\n answers.append(total)\n L[b_top] = a_top\n L[b] = a_top\n L[a] = a_top\n counts[a_top] += counts[b_top]\n\n for answer in reversed(answers[:-1]):\n print(answer)\n\nif __name__ == \'__main__\':\n main()\n']
['Wrong Answer', 'Accepted']
['s743202419', 's048277669']
[60192.0, 60580.0]
[558.0, 1791.0]
[1159, 987]
p03108
u684120680
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['import numpy as np\n\nn,m = [int(i) for i in input().split()]\n\nkeiro = np.eye(n)\nab = []\nfor i in range(m):\n a,b = [int(i)-1 for i in input().split()]\n ab.append([a, b])\n keiro[a,b] = 1\n keiro[b,a] = 1\n\nfor i in range(m):\n cnt = 0\n a,b = ab[i]\n keiro[a,b] = 0\n keiro[b,a] = 0\n \n eig, p = np.linalg.eig(keiro)\n p_inv = np.linalg.inv(p)\n eig = eig**(m-i-1)\n d = np.diag(eig)\n keiro_tmp = np.dot(p,d)\n keiro_tmp = np.dot(keiro_tmp,p_inv)\n cnt = np.sum(keiro_tmp == 0) / 2\n print(cnt)', 'n,m = [int(i) for i in input().split()]\nab = []\nfor i in range(m):\n ab.append([int(i)-1 for i in input().split()])\n\ndef find_index(c,eld):\n if c in eld[c]:\n return(c,len(eld[c]))\n else:\n return(eld[c][0],len(eld[eld[c][0]]))\n \neld = []\nfor i in range(n):\n eld.append([i])\nstat = eld\nans = [int(len(eld)*(len(eld)-1)/2)]\n\nfor a,b in ab[1:][::-1]:\n a_index,a_size = find_index(a,eld)\n b_index,b_size = find_index(b,eld)\n if a_index == b_index:\n ans.append(ans[-1])\n continue\n if a_size >= b_size:\n eld[a_index].extend(eld[b_index])\n eld[b_index] = [a_index]\n else:\n eld[b_index].extend(eld[a_index])\n eld[a_index] = [b_index]\n\n next = a_size * b_size\n ans.append(ans[-1] - next)\n\nfor i in ans[::-1]:\n print(i)\n\n\n ', 'n,m = [int(i) for i in input().split()]\nab = []\nfor i in range(m):\n ab.append([int(i)-1 for i in input().split()])\n\ndef find_index(c,eld):\n if c in eld[c]:\n return(c,len(eld[c]))\n else:\n return(eld[c][0],len(eld[eld[c][0]]))\n \neld = []\nfor i in range(n):\n eld.append([i])\nstat = eld\nans = [int(len(eld)*(len(eld)-1)/2)]\n\nfor a,b in ab[1:][::-1]:\n# print(eld)\n a_index,a_size = find_index(a,eld)\n b_index,b_size = find_index(b,eld)\n# print(a_index,b_index)\n if a_index == b_index:\n ans.append(ans[-1])\n continue\n else:\n next = a_size * b_size\n ans.append(ans[-1] - next)\n\n if a_size >= b_size:\n eld[a_index].extend(eld[b_index])\n for i in eld[b_index]:\n eld[i] = [a_index]\n else:\n eld[b_index].extend(eld[a_index])\n for i in eld[a_index]:\n eld[i] = [b_index]\n\nfor i in ans[::-1]:\n print(i)']
['Runtime Error', 'Wrong Answer', 'Accepted']
['s385210934', 's544706110', 's246720203']
[58888.0, 42820.0, 43844.0]
[2119.0, 770.0, 831.0]
[495, 758, 851]
p03108
u687044304
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['# -*- coding:utf-8 -*-\n\nclass UnionFind():\n def __init__(self, N):\n \n \n self.parent = [-1] * N\n\n def root(self, A):\n \n if self.parent[A] < 0:\n return A\n self.parent[A] = self.root(self.parent[A])\n return self.parent[A]\n\n def size(self, A):\n \n return -self.parent[self.root(A)] \n \n def connect(self, A, B):\n \n A = self.root(A)\n B = self.root(B)\n if A == B:\n \n return False\n \n \n \n if self.size(A) < self.size(B):\n A, B = B, A\n \n \n self.parent[A] += self.parent[B]\n \n self.parent[B] = A\n\n return True\n\n\ndef solve():\n \n N, M = list(map(int, input().split(" ")))\n A = []\n B = []\n\n for i in range(M):\n a, b = list(map(int, input().split()))\n A.append(a-1)\n B.append(b-1)\n \n ans = [0]*M\n ans[M-1] = N*(N-1)/2 \n ufind = UnionFind(N)\n\n for i in range(M-1, -1, -1):\n if ufind.root(A[i]) != ufind.root(B[i]):\n \n ans[i-1] = ans[i] - ufind.size(A[i]) * ufind.size(B[i])\n ufind.connect(A[i], B[i])\n else:\n ans[i-1] = ans[i]\n \n for i in range(0, M):\n print(ans[i])\n\n\n\nif __name__ == "__main__":\n solve()', '# -*- coding:utf-8 -*-\n\n\nclass UnionFind():\n def __init__(self, N):\n \n \n self.parent = [-1] * N\n\n def root(self, A):\n \n if self.parent[A] < 0:\n return A\n self.parent[A] = self.root(self.parent[A])\n return self.parent[A]\n\n def size(self, A):\n \n return -self.parent[self.root(A)] \n \n def connect(self, A, B):\n \n A = self.root(A)\n B = self.root(B)\n if A == B:\n \n return False\n \n \n \n if self.size(A) < self.size(B):\n A, B = B, A\n \n \n self.parent[A] += self.parent[B]\n \n self.parent[B] = A\n\n return True\n\n\ndef solve():\n N, M = list(map(int, input().split(" ")))\n A = []\n B = []\n\n for i in range(M):\n a, b = list(map(int, input().split()))\n A.append(a-1)\n B.append(b-1)\n \n ans = [0]*M\n ans[M-1] = N*(N-1)/2 \n ufind = UnionFind(N)\n\n for i in range(M-1, 0, -1):\n if ufind.root(A[i]) != ufind.root(B[i]):\n \n ans[i-1] = ans[i] - ufind.size(A[i]) * ufind.size(B[i])\n ufind.connect(A[i], B[i])\n else:\n ans[i-1] = ans[i]\n \n for i in range(0, M):\n print(ans[i])\n\n\n\nif __name__ == "__main__":\n solve()', '# -*- coding:utf-8 -*-\n\nclass UnionFind():\n def __init__(self, N):\n self.parent = [-1 for i in range(N)] \n \n def find(self, x):\n \n if self.parent[x] < 0: return x\n \n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def unite(self, x, y):\n \n gx = self.find(x)\n gy = self.find(y)\n\n if gx == gy: return\n \n if self.size(gx) < self.size(gy):\n self.parent[gy] -= self.size(gx)\n self.parent[gx] = gy\n else:\n self.parent[gx] -= self.size(gy)\n self.parent[gy] = gx\n \n def size(self, x):\n \n gx = self.find(x)\n return -self.parent[gx]\n\n def is_same_group(self, x, y):\n \n return self.find(x) == self.find(y)\n\n\ndef solve():\n \n N, M = list(map(int, input().split()))\n A, B = [], []\n for m in range(M):\n a, b = list(map(int, input().split()))\n A.append(a-1)\n B.append(b-1)\n\n uf = UnionFind(N)\n ans = [(N-1)*N/2] * (M) \n\n for i in range(M-1, 0, -1):\n if not uf.is_same_group(A[i], B[i]):\n \n \n ans[i-1] = ans[i] - uf.size(A[i]) * uf.size(B[i])\n uf.unite(A[i], B[i])\n else:\n ans[i-1] = ans[i]\n \n for i in range(M):\n print(int(ans[i]))\n\n\nif __name__ == "__main__":\n solve()']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s264635082', 's471790722', 's769808278']
[16508.0, 16456.0, 16268.0]
[818.0, 791.0, 843.0]
[2769, 2650, 2210]
p03108
u690781906
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['def find_root(x):\n if parent[x] == x:\n return x\n else:\n return find_root(parent[x])\n\n\n\ndef unite(x, y):\n x = find_root(x)\n y = find_root(y)\n\n if x != y:\n if rank[x] < rank[y]:\n parent[x] = y\n size[y] += size[x]\n else:\n parent[y] = x\n size[x] += size[y]\n if rank[x] == rank[y]:\n rank[x] += 1\n\n\n\ndef same(x, y):\n return find_root(x) == find_root(y)\n\n\nN, M = map(int, input().split())\nab = [list(map(int, input().split())) for _ in range(M)]\nprint(ab)\n\nparent = [i for i in range(N)] \nrank = [1] * N \nsize = [1] * N \n\n\nedge = [[ab[M - 1 - i][0] - 1, ab[M - 1 - i][1] - 1] for i in range(M)]\nprint(edge)\n\nres = []\nfor i in range(M):\n a = find_root(edge[i][0])\n b = find_root(edge[i][1])\n if a == b:\n res.append(0)\n else:\n res.append(size[a] * size[b])\n unite(a, b)\nprint(res)\nans = 0\nfor i in range(M):\n ans += res[M - 1 - i]\n print(ans)\n', 'def find_root(x):\n if parent[x] == x:\n return x\n else:\n return find_root(parent[x])\n\n\n\ndef unite(x, y):\n x = find_root(x)\n y = find_root(y)\n\n if x != y:\n if rank[x] < rank[y]:\n parent[x] = y\n size[y] += size[x]\n else:\n parent[y] = x\n size[x] += size[y]\n if rank[x] == rank[y]:\n rank[x] += 1\n\n\n\ndef same(x, y):\n return find_root(x) == find_root(y)\n\n\nN, M = map(int, input().split())\nab = [list(map(int, input().split())) for _ in range(M)]\n\nparent = [i for i in range(N)] \nrank = [1] * N \nsize = [1] * N \n\n\nedge = [[ab[M - 1 - i][0] - 1, ab[M - 1 - i][1] - 1] for i in range(M)]\n\nres = []\nfor i in range(M):\n a = find_root(edge[i][0])\n b = find_root(edge[i][1])\n if a == b:\n res.append(0)\n else:\n res.append(size[a] * size[b])\n unite(a, b)\nans = 0\nfor i in range(M):\n ans += res[M - 1 - i]\n print(ans)']
['Wrong Answer', 'Accepted']
['s650461600', 's246353145']
[54760.0, 48436.0]
[827.0, 731.0]
[1136, 1102]
p03108
u691896522
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['n, m = map(int, input().split())\ndata = []\nfor i in range(m):\n data.append(list(map(int, input().split())))\ndata.reverse()\nclass UnionFind:\n def __init__(self, n):\n \n self.par = [i for i in range(n+1)]\n self.rank = [0] * (n+1)\n self.size = [1] * (n+1)\n def find(self, x):\n \n if self.par[x] == x:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n def same_check(self, x, y):\n \n return self.find(x) == self.find(y)\n def union(self, x, y):\n \n x = self.find(x)\n y = self.find(y)\n if self.rank[x] < self.rank[y]:\n self.par[x] = y\n self.size[y] += self.size[x]\n self.size[x] = 0\n elif self.rank[x] > self.rank[y]:\n self.par[y] = x\n self.size[x] += self.size[y]\n self.size[y] = 0\n else:\n self.rank[x] += 1\n self.par[y] = x\n self.size[x] += self.size[y]\n self.size[y] = 0\n def getsize(self, x):\n p = self.find(x)\n return self.size[p]\n def printsize(self):\n print(self.size)\n def printparents(self):\n print(self.par)\nans = []\nans.append(n*(n-1)//2)\ngraph = UnionFind(n)\nfor i in range(1, len(data)):\n a, b = data[i - 1]\n #print("{} {}".format(a, b))\n if graph.same_check(a,b):\n ans.append(ans[i-1])\n else:\n print(ans[i-1] - (graph.getsize(a) * graph.getsize(b)))\n ans.append(ans[i-1] - (graph.getsize(a) * graph.getsize(b)))\n graph.union(a,b)\nfor i in ans:\n print(i)\n\n\n', 'n, m = map(int, input().split())\ndata = []\nfor i in range(m):\n data.append(list(map(int, input().split())))\ndata.reverse()\nclass UnionFind:\n def __init__(self, n):\n \n self.par = [i for i in range(n+1)]\n self.rank = [0] * (n+1)\n self.size = [1] * (n+1)\n def find(self, x):\n \n if self.par[x] == x:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n def same_check(self, x, y):\n \n return self.find(x) == self.find(y)\n def union(self, x, y):\n \n x = self.find(x)\n y = self.find(y)\n if self.rank[x] < self.rank[y]:\n self.par[x] = y\n self.size[y] += self.size[x]\n self.size[x] = 0\n elif self.rank[x] > self.rank[y]:\n self.par[y] = x\n self.size[x] += self.size[y]\n self.size[y] = 0\n else:\n self.rank[x] += 1\n self.par[y] = x\n self.size[x] += self.size[y]\n self.size[y] = 0\n def getsize(self, x):\n p = self.find(x)\n return self.size[p]\n def printsize(self):\n print(self.size)\n def printparents(self):\n print(self.par)\nans = []\nans.append(n*(n-1)//2)\ngraph = UnionFind(n)\nfor i in range(1, len(data)):\n a, b = data[i - 1]\n #print("{} {}".format(a, b))\n if graph.same_check(a,b):\n ans.append(ans[i-1])\n else:\n print(ans[i-1] - (graph.getsize(a) * graph.getsize(b)))\n ans.append(ans[i-1] - (graph.getsize(a) * graph.getsize(b)))\n graph.union(a,b)\nfor i in ans:\n print(i)\n\n\n', 'n, m = map(int, input().split())\ndata = []\nfor i in range(m):\n data.append(list(map(int, input().split())))\ndata.reverse()\nclass UnionFind:\n def __init__(self, n):\n \n self.par = [i for i in range(n+1)]\n self.rank = [0] * (n+1)\n self.size = [1] * (n+1)\n def find(self, x):\n \n if self.par[x] == x:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n def same_check(self, x, y):\n \n return self.find(x) == self.find(y)\n def union(self, x, y):\n \n x = self.find(x)\n y = self.find(y)\n if self.rank[x] < self.rank[y]:\n self.par[x] = y\n self.size[y] += self.size[x]\n self.size[x] = 0\n elif self.rank[x] > self.rank[y]:\n self.par[y] = x\n self.size[x] += self.size[y]\n self.size[y] = 0\n else:\n self.rank[x] += 1\n self.par[y] = x\n self.size[x] += self.size[y]\n self.size[y] = 0\n def getsize(self, x):\n p = self.find(x)\n return self.size[p]\n def printsize(self):\n print(self.size)\n def printparents(self):\n print(self.par)\nans = []\nans.append(n*(n-1)//2)\ngraph = UnionFind(n)\nfor i in range(1, len(data)):\n a, b = data[i - 1]\n #print("{} {}".format(a, b))\n if graph.same_check(a,b):\n ans.append(ans[i-1])\n else:\n ans.append(ans[i-1] - (graph.getsize(a) * graph.getsize(b)))\n graph.union(a,b)\nfor i in ans:\n print(i)\n\n\n', 'n, m = map(int, input().split())\ndata = []\nfor i in range(m):\n data.append(list(map(int, input().split())))\ndata.reverse()\nclass UnionFind:\n def __init__(self, n):\n \n self.par = [i for i in range(n+1)]\n self.rank = [0] * (n+1)\n self.size = [1] * (n+1)\n def find(self, x):\n \n if self.par[x] == x:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n def same_check(self, x, y):\n \n return self.find(x) == self.find(y)\n def union(self, x, y):\n \n x = self.find(x)\n y = self.find(y)\n if self.rank[x] < self.rank[y]:\n self.par[x] = y\n self.size[y] += self.size[x]\n self.size[x] = 0\n elif self.rank[x] > self.rank[y]:\n self.par[y] = x\n self.size[x] += self.size[y]\n self.size[y] = 0\n else:\n self.rank[x] += 1\n self.par[y] = x\n self.size[x] += self.size[y]\n self.size[y] = 0\n def getsize(self, x):\n p = self.find(x)\n return self.size[p]\n def printsize(self):\n print(self.size)\n def printparents(self):\n print(self.par)\nans = []\nans.append(n*(n-1)//2)\ngraph = UnionFind(n)\nfor i in range(1, len(data)):\n a, b = data[i - 1]\n #print("{} {}".format(a, b))\n if graph.same_check(a,b):\n ans.append(ans[i-1])\n else:\n ans.append(ans[i-1] - (graph.getsize(a) * graph.getsize(b)))\n graph.union(a,b)\nans.reverse()\nfor i in ans:\n print(i)\n\n\n']
['Wrong Answer', 'Wrong Answer', 'Wrong Answer', 'Accepted']
['s442661026', 's724759340', 's743818767', 's747448253']
[36772.0, 36076.0, 35056.0, 34980.0]
[1070.0, 1110.0, 894.0, 858.0]
[1732, 1732, 1668, 1682]
p03108
u692746605
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['n,m=map(int,input().split())\nb=[[int(x) for x in input().split()] for y in range(m)]\nu=n*(n-1)//2\n\ns=[]\na=[u]\n\nfor i in range(m-1,0,-1):\n x=y=-1\n for j in range(len(s)):\n if b[i][0] in s[j]:\n x = j\n if b[i][1] in s[j]:\n y = j\n if x!=-1 and y!=-1:\n break\n\n if x==-1 and y==-1:\n s.append(set((b[i][0],b[i][1])))\n elif y==-1:\n s[x].add(b[i][1])\n elif x==-1:\n s[y].add(b[i][0])\n elif x!=y:\n for k in range(len(s[y])):\n s[x].add(s[y].pop())\n s.pop(y)\n print(s)\n\n t=0\n for j in range(len(s)):\n t += (len(s[j])*(len(s[j])-1))//2\n a.append(u-t)\n \na.reverse()\nfor i in range(len(a)):\n print(a[i])\n', 'n,m=map(int,input().split())\nb=[[int(x) for x in input().split()] for y in range(m)]\nu=n*(n-1)//2\n\ns=set()\na=[u]\n\nfor i in range(m-1,0,-1):\n x=y=-1\n for j in range(len(s)):\n if b[i][0] in s[j]:\n x = j\n if b[i][1] in s[j]:\n y = j\n if x!=-1 and y!=-1:\n break\n\n if x==-1 and y==-1:\n s.append(set((b[i][0],b[i][1])))\n elif y==-1:\n s[x].add(b[i][1])\n elif x==-1:\n s[y].add(b[i][0])\n elif x!=y:\n for k in range(len(s[y])):\n s[x].add(s[y].pop())\n s.pop(y)\n\n t=0\n for j in range(len(s)):\n t += (len(s[j])*(len(s[j])-1))//2\n a.append(u-t)\n \na.reverse()\nfor i in range(len(a)):\n print(a[i])\n', 'n, m = map(int, input().split())\ne = [[int(x) for x in input().split()] for _ in range(m)]\n\nu = [-1]*(n+1)\n\ndef find(x):\n return x if u[x] < 0 else find(u[x])\n\nt = n*(n-1)// 2\ns = [t]\n\nfor i in range(m-1,0,-1):\n a = find(e[i][0])\n b = find(e[i][1])\n if a > b:\n a, b = b, a\n if a != b:\n t -= u[a]*u[b]\n u[a] += u[b]\n u[b] = a\n s.append(t)\n\nfor i in reversed(s):\n print(i)\n']
['Wrong Answer', 'Runtime Error', 'Accepted']
['s843286127', 's863356857', 's584661206']
[137968.0, 21116.0, 27172.0]
[2105.0, 302.0, 721.0]
[646, 638, 390]
p03108
u708615801
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['from collections import deque\nimport sys\n\nstdin = sys.stdin\ndef li(): return map(int, stdin.readline().split())\n\nN, M = tuple(li())\nstack = []\nfor i in range(M):\n stack.append( tuple(li()) )\n\np_map = {}\nfor i in range(1, N + 1):\n # key: val\n # node_id: parent_id, union_size\n p_map[i] = [i, 1]\n\ncombs = deque()\ndef parent(i):\n if p_map[i][0] == i:\n return i\n else:\n return parent(p_map[i][0])\nwhile stack:\n l, r = stack.pop()\n # parent should be greater\n if parent(l) < parent(r):\n l, r = r, l\n if parent(l) == parent(r):\n if combs:\n combs.appendleft(combs[0])\n else:\n p_map[r][0] = l\n incr = p_map[parent(l)][1] * p_map[parent(r)][1]\n p_map[l][1] += p_map[r][1]\n if combs:\n combs.appendleft(combs[0] + incr)\n else:\n combs.appendleft(incr)\n\ncnt = 0\ncur = combs[0]\ncombs.append(0)\nwhile combs:\n cnt += cur - combs[0]\n print(cnt)\n cur = combs.popleft()\n', 'from collections import deque\nimport sys\n\nstdin = sys.stdin\ndef li(): return map(int, stdin.readline().split())\n\nN, M = tuple(li())\nstack = []\nfor i in range(M):\n stack.append( tuple(li()) )\n\np_map = {}\nfor i in range(1, N + 1):\n # key: val\n # node_id: parent_id, union_size\n p_map[i] = [i, 1]\n\ncombs = deque()\ndef parent(i):\n if p_map[i][0] == i:\n return i\n else:\n return parent(p_map[i][0])\nwhile stack:\n\n l, r = stack.pop()\n p_l = parent(l)\n p_r = parent(r)\n # parent should be greater\n if p_l == p_r:\n if combs:\n combs.appendleft(combs[0])\n else:\n if p_l < p_r:\n p_l, p_r = p_r, p_l\n p_map[p_r][0] = p_l\n incr = p_map[p_l][1] * p_map[p_r][1]\n p_map[p_l][1] += p_map[p_r][1]\n if combs:\n combs.appendleft(combs[0] + incr)\n else:\n combs.appendleft(incr)\ncnt = 0\ncombs.append(0)\nwhile combs:\n cur = combs.popleft()\n if combs:\n cnt += cur - combs[0]\n print(cnt)\n']
['Runtime Error', 'Accepted']
['s680546172', 's462601833']
[37648.0, 37732.0]
[1446.0, 802.0]
[989, 1024]
p03108
u709304134
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['N, M = map(int, input().split())\nab = [tuple(map(lambda x:int(x)-1, input().split())) for _ in range(M)]\n \n \nclass UnionFind:\n def __init__(self, n):\n self.parents = [i for i in range(n)]\n self.sizes = [1] * n\n self.rank = [0] * 1\n def find(self, i):\n if self.parents[i] == i:\n return i\n else:\n self.parents[i] = self.find(self.parents[i])\n return self.parents[i]\n def unite(self, i, j):\n pi = self.find(i)\n pj = self.find(j)\n if self.rank[pi] < self.rank[pj]:\n self.sizes[pj] += self.sizes[pi]\n self.parents[pi] = pj \n else: \n self.sizes[pi] += self.sizes[pj]\n self.parents[pj] = pi \n if self.rank[pi] == self.rank[pj]:\n self.rank[pi] += 1\ndef main():\n uf = UnionFind(N)\n p = N * (N - 1) // 2\n ans = [p]\n for x, y in reversed(ab):\n px = uf.find(x)\n py = uf.find(y)\n if px != py:\n p -= uf.sizes[px] * uf.sizes[py]\n uf.unite(px, py)\n ans.append(p)\n \n for a in reversed(ans[:-1]):\n print(a)\n \n \nmain()\n', 'N, M = map(int, input().split())\nab = [tuple(map(lambda x:int(x)-1, input().split())) for _ in range(M)]\n \n \nclass UnionFind:\n def __init__(self, n):\n self.parents = [i for i in range(n)]\n self.sizes = [1] * n\n def find(self, i):\n parent = self.parents[i]\n if parent == i:\n return i\n else:\n self.parents[i] = self.find(self.parents[i])\n return self.parent[i]\n def unite(self, i, j):\n pi = self.find(i)\n pj = self.find(j)\n self.sizes[pj] += self.sizes[pi]\n self.parents[pi] = pj \n \n \ndef main():\n uf = UnionFind(N)\n p = N * (N - 1) // 2\n ans = [p]\n for x, y in reversed(ab):\n px = uf.find(x)\n py = uf.find(y)\n if px != py:\n p -= uf.sizes[px] * uf.sizes[py]\n uf.unite(px, py)\n ans.append(p)\n \n for a in reversed(ans[:-1]):\n print(a)\n \n \nmain()\n', 'N, M = map(int, input().split())\nab = [tuple(map(lambda x:int(x)-1, input().split())) for _ in range(M)]\n \n \nclass UnionFind:\n def __init__(self, n):\n self.parents = [i for i in range(n)]\n self.sizes = [1] * n\n def find(self, i):\n parent = self.parents[i]\n if parent == i:\n return i\n else:\n self.parents[i] = self.find(self.parents[i])\n return self.parent[i]\n def unite(self, i, j):\n pi = self.find(i)\n pj = self.find(j)\n self.sizes[pj] += self.sizes[pi]\n self.parents[pi] = pj \n \n \ndef main():\n uf = UnionFind(N)\n p = N * (N - 1) // 2\n ans = [p]\n for x, y in reversed(ab):\n px = uf.find(x)\n py = uf.find(y)\n if px != py:\n p -= uf.sizes[px] * uf.sizes[py]\n uf.unite(px, py)\n ans.append(p)\n \n for a in reversed(ans[:-1]):\n print(a)\n \n \nmain()\n', 'N, M = map(int, input().split())\nab = [tuple(map(lambda x:int(x)-1, input().split())) for _ in range(M)]\n \n \nclass UnionFind:\n def __init__(self, n):\n self.parents = [i for i in range(n)]\n self.sizes = [1] * n\n self.rank = [0] * n\n def find(self, i):\n if self.parents[i] == i:\n return i\n else:\n self.parents[i] = self.find(self.parents[i])\n return self.parents[i]\n def unite(self, i, j):\n pi = self.find(i)\n pj = self.find(j)\n if pi != pj\n if self.rank[pi] < self.rank[pj]:\n self.sizes[pj] += self.sizes[pi]\n self.parents[pi] = pj \n else: \n self.sizes[pi] += self.sizes[pj]\n self.parents[pj] = pi \n if self.rank[pi] == self.rank[pj]:\n self.rank[pi] += 1\n \ndef main():\n uf = UnionFind(N)\n p = N * (N - 1) // 2\n ans = [p]\n for x, y in reversed(ab):\n px = uf.find(x)\n py = uf.find(y)\n if px != py:\n p -= uf.sizes[px] * uf.sizes[py]\n uf.unite(px, py)\n ans.append(p)\n \n for a in reversed(ans[:-1]):\n print(a)\n \n \nmain()\n', 'class UnionFind:\n def __init__(self, n):\n self.nodes = n\n self.parents = [i for i in range(n)]\n self.sizes = [1] * n\n self.rank = [0] * n\n\n def find(self, i): \n if self.parents[i] == i:\n return i\n else:\n self.parents[i] = self.find(self.parents[i]) \n return self.parents[i]\n\n def unite(self, i, j): \n pi = self.find(i)\n pj = self.find(j)\n if pi != pj:\n if self.rank[pi] < self.rank[pj]:\n self.sizes[pj] += self.sizes[pi]\n self.parents[pi] = pj\n else:\n self.sizes[pi] += self.sizes[pj]\n self.parents[pj] = pi\n if self.rank[pi] == self.rank[pj]:\n self.rank[pi] += 1\n def same(self, i, j): \n return self.find(i)==self.find(j)\n\n def get_parents(self): \n for n in range(self.nodes): \n self.find(n)\n return self.parents\n\n def size(self, i):\n p = self.find(i)\n return self.sizes[p]\n\n\nN, M = map(int, input().split())\nAB = []\nB = []\nfor m in range(M):\n a, b = map(int, input().split())\n AB.append((a-1,b-1))\n\nans = []\nscore = N * (N-1) // 2\nuf = UnionFind(N)\nfor a, b in AB[::-1]:\n ans.append(score)\n if not uf.same(a,b):\n score -= uf.size(a) * uf.sizes(b)\n uf.unite(a,b) \n\nfor score in ans[::-1]:\n print(score)\n \n\n\n', 'class UnionFind:\n def __init__(self, n):\n self.nodes = n\n self.parents = [i for i in range(n)]\n self.sizes = [1] * n\n self.rank = [0] * n\n\n def find(self, i): \n if self.parents[i] == i:\n return i\n else:\n self.parents[i] = self.find(self.parents[i]) \n return self.parents[i]\n\n def unite(self, i, j): \n pi = self.find(i)\n pj = self.find(j)\n if pi != pj:\n if self.rank[pi] < self.rank[pj]:\n self.sizes[pj] += self.sizes[pi]\n self.parents[pi] = pj\n else:\n self.sizes[pi] += self.sizes[pj]\n self.parents[pj] = pi\n if self.rank[pi] == self.rank[pj]:\n self.rank[pi] += 1\n def same(self, i, j): \n return self.find(i)==self.find(j)\n\n def get_parents(self): \n for n in range(self.nodes): \n self.find(n)\n return self.parents\n\n def size(self, i):\n p = self.find(i)\n return self.sizes[p]\n\n\nN, M = map(int, input().split())\nAB = []\nB = []\nfor m in range(M):\n a, b = map(int, input().split())\n AB.append((a-1,b-1))\n\nans = []\nscore = N * (N-1) // 2\nuf = UnionFind(N)\nfor a, b in AB[::-1]:\n ans.append(score)\n if not uf.same(a,b):\n score -= uf.size(a) * uf.size(b)\n uf.unite(a,b) \n\nfor score in ans[::-1]:\n print(score)\n \n\n\n']
['Runtime Error', 'Runtime Error', 'Runtime Error', 'Runtime Error', 'Runtime Error', 'Accepted']
['s170747073', 's530408134', 's681562648', 's766381860', 's980935266', 's542103963']
[21368.0, 21352.0, 21352.0, 3064.0, 22968.0, 24876.0]
[372.0, 366.0, 364.0, 20.0, 330.0, 832.0]
[1147, 926, 926, 1208, 1618, 1617]
p03108
u711238850
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['import sys\nsys.setrecursionlimit(10**9)\nclass UnionFind:\n def __init__(self,n):\n super().__init__()\n self.par = [-1]*n\n self.rank = [0]*n\n self.tsize = [1]*n\n\n \n def root(self,x):\n if self.par[x] == -1:\n return x \n else:\n self.par[x] = root(par[x])\n return self.par[x]\n\n def unite(self, x, y):\n x_r = self.root(x)\n y_r = self.root(y)\n \n if self.rank[x_r]>self.rank[y_r]:\n self.par[y_r] = x_r\n\n elif self.rank[x_r]<self.rank[y_r]:\n self.par[x_r] = y_r\n \n elif x_r != y_r:\n self.par[y_r] = x_r\n self.rank[x_r] += 1\n\n def isSame(self,x,y):\n return self.root(x) == self.root(y)\n\n def size(self,x):\n return self.tsize[self.root(x)]\n\n\n\ndef main():\n n,m = tuple([int(t)for t in input().split()])\n\n bridge = [tuple([int(t)-1 for t in input().split()])for _ in [0]*m]\n bridge = bridge[::-1]\n\n uf = UnionFind(n)\n ans = [n*(n-1)//2]\n for a_i,b_i in bridge:\n if not uf.isSame(a_i,b_i):\n ans.append(ans[-1]-uf.size(a_i)*uf.size(b_i))\n uf.unite(a_i,b_i)\n else:\n ans.append(ans[-1])\n ans.pop()\n for a in ans[::-1]:\n print(a)\n\n\n\nif __name__ == "__main__":\n main()', 'import sys\nsys.setrecursionlimit(10**9)\nclass UnionFind:\n def __init__(self,n):\n super().__init__()\n self.par = [-1]*n\n self.rank = [0]*n\n self.tsize = [1]*n\n\n \n def root(self,x):\n if self.par[x] == -1:\n return x \n else:\n self.par[x] = self.root(self.par[x])\n return self.par[x]\n\n def unite(self, x, y):\n x_r = self.root(x)\n y_r = self.root(y)\n \n if self.rank[x_r]>self.rank[y_r]:\n self.par[y_r] = x_r\n\n elif self.rank[x_r]<self.rank[y_r]:\n self.par[x_r] = y_r\n\n elif x_r != y_r:\n self.par[y_r] = x_r\n self.rank[x_r] += 1\n\n if x_r != y_r:\n size = self.tsize[x_r]+self.tsize[y_r]\n self.tsize[x_r] = size\n self.tsize[y_r] = size\n\n def isSame(self,x,y):\n return self.root(x) == self.root(y)\n\n def size(self,x):\n return self.tsize[self.root(x)]\n\n\ndef main():\n n,m = tuple([int(t)for t in input().split()])\n\n bridge = [tuple([int(t)-1 for t in input().split()])for _ in [0]*m]\n bridge = bridge[::-1]\n\n uf = UnionFind(n)\n ans = [n*(n-1)//2]\n for a_i,b_i in bridge:\n if not uf.isSame(a_i,b_i):\n ans.append(ans[-1]-uf.size(a_i)*uf.size(b_i))\n uf.unite(a_i,b_i)\n else:\n ans.append(ans[-1])\n ans.pop()\n for a in ans[::-1]:\n print(a)\n\n\n\nif __name__ == "__main__":\n main()']
['Runtime Error', 'Accepted']
['s506308576', 's731952503']
[18820.0, 28036.0]
[329.0, 751.0]
[1332, 1474]
p03108
u720636500
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['N, M = map(int, input().split())\nlis = [[x, -1] for x in range(1, N + 1)]\nBlis = [list(map(int, input().split())) for _ in range(M)]\ninc = int(N*(N - 1)/2)\nans = [x for x in range(M)]\nans[M - 1] = inc\ndef root(a):\n if lis[a - 1][1] < 0:\n return lis[a - 1][0]\n else:\n k = root(lis[a - 1][1])\n lis[a - 1][1] = k\n return k\ndef size(a):\n return -lis[root(a) - 1][1]\ndef connect(a, b):\n A = root(a)\n B = root(b)\n if A == B:\n return None\n else:\n if size(A) < size(B):\n A, B = B, A\n lis[A - 1][1] += lis[B - 1][1]\n lis[B - 1][1] = A\nfor i in reversed(range(0, M)):\n if root(Blis[i][0]) != root(Blis[i][1]):\n ans[i - 1] = ans[i] - size(Blis[i][0])*size(Blis[i][1])\n connect(Blis[i][0], Blis[i][1])\n else:\n ans[i - 1] = ans[i] \nfor i in range(M):\n print(ans[i])', 'N, M = map(int, input().split())\nParent = [-1 for x in range(N + 5)]\nlis = [list(map(int, input().split())) for _ in range(M)]\nini = int(N*(N - 1)/2)\nans = [x for x in range(M)]\nans[M - 1] = ini\ndef root(a):\n if Parent[a] < 0:\n return a\n else:\n k = root(Parent[a])\n Parent[a] = k\n return k\ndef size(a):\n return -Parent[root(a)]\ndef connect(a, b):\n A = root(a)\n B = root(b)\n if A == B:\n return None\n else:\n if size(A) < size(B):\n A, B = B, A\n Parent[A] += Parent[B]\n Parent[B] = A\nfor i in reversed(range(1, M)):\n ans[i - 1] = ans[i]\n if root(lis[i][0]) != root(lis[i][1]):\n ans[i - 1] -= size(lis[i][0])*size(lis[i][1])\n connect(lis[i][0], lis[i][1]) \nfor i in range(M):\n print(ans[i])']
['Wrong Answer', 'Accepted']
['s990057835', 's610853067']
[46236.0, 33604.0]
[993.0, 758.0]
[869, 797]
p03108
u729938879
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['\ndef union(group, new_mem):\n if (group[new_mem[0]-1] * group[new_mem[1]-1]) < 0: \n choice = max(group[new_mem[0]-1], group[new_mem[1]-1])\n \n for i in range(len(new_mem)):\n group[new_mem[i]-1] = choice\n \n elif group[new_mem[0]-1] <0 and group[new_mem[1]-1] < 0: \n for i in range(len(new_mem)):\n group[new_mem[i]-1] = min(new_mem[0], new_mem[1])\n \n elif (group[new_mem[0]-1] != group[new_mem[1]-1]) and (group[new_mem[0]-1] * group[new_mem[1]-1]) > 0: \n old = max(group[new_mem[0]-1], group[new_mem[1]-1])\n new = min(group[new_mem[0]-1], group[new_mem[1]-1])\n for i in range(len(group)):\n if group[i] == old:\n group[i] = new\n \n return group\n\ndef find(group, v):\n return group[v-1]\n\ndef size(group, v):\n input_v = find(group, v)\n return group.count(input_v)\n \nn,m = map(int, input().split())\nbridge = []\nfor i in range(m):\n a, b= map(int, input().split())\n bridge.append((a,b))\n\nnon_route = []\ntotal = int(n*(n-1)/2)\nnon_route.append(total)\n\ngroup = [-i for i in range(1, n+1)] \ntmp = total\ngroup_before = group.copy()\nfor i in range(m-1,0,-1):\n group = union(group, bridge[i]) \n print("group_before: {}".format(group_before))\n print("group: {}".format(group))\n \n if find(group_before,bridge[i][0]) == find(group_before, bridge[i][1]):\n non_route.append(tmp)\n else:\n n_1 = size(group_before, bridge[i][0])\n n_2 = size(group_before, bridge[i][1])\n tmp -= n_1 * n_2\n non_route.append(tmp)\n group_before = group.copy()\n\nfinal = sorted(non_route)\nfor i in range(len(final)):\n print(final[i])', 'class UnionFind:\n def __init__(self, n):\n self.p = [i for i in range(n)]\n self.s = [1 for i in range(n)]\n \n def find(self, x):\n if x == self.p[x]:\n return x\n else:\n self.p[x] = self.find(self.p[x])\n return self.p[x]\n\n def union(self, a, b):\n ap = self.find(a)\n bp = self.find(b)\n if ap != bp:\n mini = min(ap ,bp)\n maxi = max(ap, bp)\n self.p[maxi] = mini\n size = self.s[bp] + self.s[ap]\n self.s[bp] = size\n self.s[ap] = size\n return True\n else:\n return False\n \n def size(self, n):\n np = self.find(n)\n return self.s[np]\n \nN, M =map(int, input().split())\na, b = [], []\nfor i in range(M):\n a_i, b_i = map(int, input().split())\n a.append(a_i -1)\n b.append(b_i - 1)\n \ns = UnionFind(N)\n \nans = (N*(N-1)) // 2\nanss = [ans]\n \nfor i in range(M):\n j = M - i - 1\n if s.find(a[j]) != s.find(b[j]):\n ans -= s.size(a[j]) * s.size(b[j])\n s.union(a[j], b[j])\n if i != M-1:\n anss.append(ans)\n \nsort_ans = sorted(anss)\nfor i in range(len(sort_ans)):\n print(sort_ans[i])']
['Wrong Answer', 'Accepted']
['s092681077', 's593681687']
[154592.0, 21692.0]
[2105.0, 830.0]
[1720, 1233]
p03108
u737455052
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['from collections import Counter\n\n\nclass UnionFind:\n def __init__(self, node_size):\n self.node_size = node_size\n self.parent = [i for i in range(self.node_size)] # parent\'s number\n self.rank = [0 for i in range(self.node_size)]\n self.size = [1 for i in range(self.node_size)]\n\n # find root of x\n def root(self, x):\n if self.parent[x] == x: # x is a root\n return x\n else:\n self.parent[x] = self.root(self.parent[x]) # path compression\n return self.parent[x]\n\n # judge whether x is connected to y. returns boolian.\n def is_connected(self, x, y):\n return self.root(x) == self.root(y)\n\n # connects x to y\n def unite(self, x, y):\n x = self.root(x)\n y = self.root(y)\n if x != y:\n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n else:\n self.parent[y] = x\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n self.size[x] += self.size[y]\n self.size[y] = self.size[x]\n\n def get_size(self, x):\n x = self.root(x)\n return self.size[x]\n\n\ndef main():\n N, M = map(int, input().split()) # N islands, M bridges\n bridge_list_a = [0 for i in range(M)]\n bridge_list_b = [0 for i in range(M)]\n for i in range(M):\n bridge_list_a[i], bridge_list_b[i] = map(int, input().split())\n bridge_list_a.reverse()\n bridge_list_a = [a - 1 for a in bridge_list_a]\n bridge_list_b.reverse()\n bridge_list_b = [b - 1 for b in bridge_list_b]\n\n Islands = UnionFind(N)\n\n inconvenience = N * (N - 1) // 2\n ans = [inconvenience]\n\n for ai, bi in zip(bridge_list_a, bridge_list_b):\n if not Islands.is_connected(ai, bi):\n inconvenience -= Islands.size(ai) * Islands.size(bi)\n\n ans.append(inconvenience)\n Islands.unite(ai, bi)\n\n ans.reverse()\n for inconvenience in ans[1:]:\n print(inconvenience)\n\n\nif __name__ == "__main__":\n main()\n', 'from collections import Counter\n\n\nclass UnionFind:\n def __init__(self, node_size):\n self.node_size = node_size\n self.parent = [i for i in range(self.node_size)] # parent\'s number\n self.rank = [0 for i in range(self.node_size)]\n self.size = [1 for i in range(self.node_size)]\n\n # find root of x\n def root(self, x):\n if self.parent[x] == x: # x is a root\n return x\n else:\n self.parent[x] = self.root(self.parent[x]) # path compression\n return self.parent[x]\n\n # judge whether x is connected to y. returns boolian.\n def is_connected(self, x, y):\n return self.root(x) == self.root(y)\n\n # connects x to y\n def unite(self, x, y):\n x = self.root(x)\n y = self.root(y)\n if x != y:\n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n else:\n self.parent[y] = x\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n self.size[x] += self.size[y]\n self.size[y] = self.size[x]\n\n def get_size(self, x):\n x = self.root(x)\n return self.size[x]\n\n\ndef main():\n N, M = map(int, input().split()) # N islands, M bridges\n bridge_list_a = [0 for i in range(M)]\n bridge_list_b = [0 for i in range(M)]\n for i in range(M):\n bridge_list_a[i], bridge_list_b[i] = map(int, input().split())\n bridge_list_a.reverse()\n bridge_list_a = [a - 1 for a in bridge_list_a]\n bridge_list_b.reverse()\n bridge_list_b = [b - 1 for b in bridge_list_b]\n\n Islands = UnionFind(N)\n\n inconvenience = N * (N - 1) // 2\n ans = [inconvenience]\n\n for ai, bi in zip(bridge_list_a, bridge_list_b):\n if not Islands.is_connected(ai, bi):\n inconvenience -= Islands.get_size(ai) * Islands.get_size(bi)\n\n ans.append(inconvenience)\n Islands.unite(ai, bi)\n\n ans.reverse()\n for inconvenience in ans[1:]:\n print(inconvenience)\n\n\nif __name__ == "__main__":\n main()\n']
['Runtime Error', 'Accepted']
['s335762370', 's976096240']
[16776.0, 22768.0]
[308.0, 817.0]
[2042, 2050]
p03108
u740284863
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["from math import *\ndef nCk(n,k):\n if n < k:\n return 0\n return factorial(n)//(factorial(n-k)*factorial(k))\ndef root(i):\n if par[i] < 0:\n return i\n else:\n return root(par[i])\n\ndef size(a):\n return -par[root(a)]\n\ndef union(a,b):\n a = root(a)\n b = root(b)\n if a == b:\n return False\n if size(a) < size(b):\n a,b = b,a\n par[a] += par[b]\n par[b] = a\n return True\n\nn,m = map(int,input().split())\nbridge = []\nfor i in range(m):\n bridge.append([int(j)-1 for j in input().split()])\nans = 0\nfor i in range(m):\n #print('-------')\n par = [-1 for _ in range(n)]\n for a,b in bridge[i+1:m]:\n #print(a+1,b+1)\n union(a,b)\n roots = [ - k for k in par if k < 0]\n oth = [ i for i in roots if i != 1]\n ans = nCk(roots.count(1),2)\n tmp = 1\n for i in oth:\n tmp *= i\n if roots.count(1) > 1:\n tmp *= roots.count(1)\n if len(oth) >= 1:\n ans += tmp \n print(ans)", 'def root(i):\n if par[i] < 0:\n return i\n else:\n return root(par[i])\n\ndef size(a):\n return -par[root(a)]\n\ndef union(a,b):\n a = root(a)\n b = root(b)\n if a == b:\n return False\n if size(a) < size(b):\n a,b = b,a\n par[a] += par[b]\n par[b] = a\n return True\n\nn,m = map(int,input().split())\nbridge = []\nfor i in range(m):\n bridge.append([int(j)-1 for j in input().split()])\nans = [n*(n-1)//2]\npar = [-1 for _ in range(n)]\n\nfor a,b in bridge[::-1]:\n if root(a) == root(b):\n ans.append(ans[-1])\n else:\n ans.append(ans[-1] - size(a) * size(b))\n union(a,b)\n\nfor i in ans[::-1]:\n print(i)', 'def root(i):\n if par[i] < 0:\n return i\n else:\n return root(par[i])\n\ndef size(a):\n return -par[root(a)]\n\ndef union(a,b):\n a = root(a)\n b = root(b)\n if a == b:\n return False\n if size(a) < size(b):\n a,b = b,a\n par[a] += par[b]\n par[b] = a\n return True\n\nn,m = map(int,input().split())\nbridge = []\nfor i in range(m):\n bridge.append([int(j)-1 for j in input().split()])\nans = [n*(n-1)//2]\npar = [-1 for _ in range(n)]\n\nfor a,b in bridge[::-1]:\n if root(a) == root(b):\n ans.append(ans[-1])\n else:\n ans.append(ans[-1] - size(a) * size(b))\n union(a,b)\nans = ans[::-1]\nfor i in ans[1:]:\n print(i)']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s083108453', 's794411908', 's199199497']
[27292.0, 31100.0, 31164.0]
[2206.0, 461.0, 475.0]
[1025, 717, 730]
p03108
u758738963
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["#import numpy as np\n\n\nclass UnionFind():\n def __init__(self, n):\n self.n = n\n self.parents = [-1] * n\n\n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return self.parents[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n\n if self.parents[x] > self.parents[y]:\n x, y = y, x\n\n self.parents[x] += self.parents[y]\n self.parents[y] = x\n\n def size(self, x):\n return -self.parents[self.find(x)]\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def members(self, x):\n root = self.find(x)\n return [i for i in range(self.n) if self.find(i) == root]\n\n def roots(self):\n return [i for i, x in enumerate(self.parents) if x < 0]\n\n def group_count(self):\n return len(self.roots())\n\n def all_group_members(self):\n return {r: self.members(r) for r in self.roots()}\n\n def __str__(self):\n return '\\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())\n\nN,M = list( map(int, input().split()) )\n\nuf = UnionFind(N)\nA = [0]*M\nB = [0]*M\nfor i in range(M):\n a,b = list( map(int, input().split()) )\n A[i]=a-1\n B[i]=b-1\n\n#print(A)\n\n\ncmax = int(N*(N-1)/2)\nFD = [cmax]*(M+1)\n#print(FD)\nfor i in range(M):\n \n if same(A[M-i-1])*uf.size(B[M-i-1]):\n FD[M-i-1] = FD[M-i]\n else:\n cless = uf.size(A[M-i-1])*uf.size(B[M-i-1])\n FD[M-i-1] = FD[M-i] - cless\n if FD[M-i-1]<0: FD[M-i-1]=0\n uf.union(A[M-1-i],B[M-1-i])\n\n\n#print(FD[1:])\nfor fd in FD[1:]:\n print(fd)\n\n\n", "import numpy as np\n\n\nclass UnionFind():\n def __init__(self, n):\n self.n = n\n self.parents = [-1] * n\n\n def find(self, x):\n if self.parents[x] < 0:\n return x\n else:\n self.parents[x] = self.find(self.parents[x])\n return self.parents[x]\n\n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n\n if self.parents[x] > self.parents[y]:\n x, y = y, x\n\n self.parents[x] += self.parents[y]\n self.parents[y] = x\n\n def size(self, x):\n return -self.parents[self.find(x)]\n\n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n def members(self, x):\n root = self.find(x)\n return [i for i in range(self.n) if self.find(i) == root]\n\n def roots(self):\n return [i for i, x in enumerate(self.parents) if x < 0]\n\n def group_count(self):\n return len(self.roots())\n\n def all_group_members(self):\n return {r: self.members(r) for r in self.roots()}\n\n def __str__(self):\n return '\\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())\n\nN,M = list( map(int, input().split()) )\n\nuf = UnionFind(N)\nA = [0]*M\nB = [0]*M\nfor i in range(M):\n a,b = list( map(int, input().split()) )\n A[i]=a-1\n B[i]=b-1\n\n#print(A)\n\n\ncmax = int(N*(N-1)/2)\nFD = [cmax]*(M+1)\n#print(FD)\nfor i in range(M):\n \n if uf.same(A[M-i-1],B[M-i-1]):\n FD[M-i-1] = FD[M-i]\n else:\n cless = uf.size(A[M-i-1])*uf.size(B[M-i-1])\n FD[M-i-1] = FD[M-i] - cless\n if FD[M-i-1]<0: FD[M-i-1]=0\n uf.union(A[M-1-i],B[M-1-i])\n\n\n#print(FD[1:])\nfor fd in FD[1:]:\n print(fd)\n\n\n"]
['Runtime Error', 'Accepted']
['s291271089', 's567889899']
[18388.0, 40028.0]
[219.0, 645.0]
[1740, 1733]
p03108
u767664985
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["class UnionFind:\n def __init__(self, n):\n self.n = n\n self.parent = [i for i in range(n)] \n self.rank = [1] * n \n self.size = [1] * n \n\n def find(self, x): \n if self.parent[x] == x:\n return x\n else:\n self.parent[x] = self.find(self.parent[x]) \n return self.parent[x]\n\n def unite(self, x, y): \n x = self.find(x)\n y = self.find(y)\n if x != y:\n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n self.size[y] += self.size[x]\n else:\n self.parent[y] = x\n self.size[x] += self.size[y]\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n def is_same(self, x, y): \n return self.find(x) == self.find(y)\n\n def group_size(self, x): \n return self.size[self.find(x)]\n\n def __str__(self): \n return '\\n'.join('{}: {}'.format(r, self.group_members(r)) for r in self.roots())\n\n\nN, M = map(int, input().split())\nbridge = [list(map(int, input().split())) for _ in range(M)]\nans = [0]*M\n\nuf = UnionFind(N)\n\n\nans[M-1] = N*(N-1)//2\nfor i in range(1, M):\n A, B = bridge[M-i-1]\n\n if uf.is_same(A-1, B-1):\n ans[M-i-1] = ans[M-i]\n else:\n ans[M-i-1] = ans[M-i] - uf.group_size(A-1) * uf.group_size(B-1)\n\n uf.unite(A-1, B-1)\n\n\nfor a in ans:\n print(a)\n", "class UnionFind:\n def __init__(self, n):\n self.n = n\n self.parent = [i for i in range(n)] \n self.rank = [1] * n \n self.size = [1] * n \n\n def find(self, x): \n if self.parent[x] == x:\n return x\n else:\n self.parent[x] = self.find(self.parent[x]) \n return self.parent[x]\n\n def unite(self, x, y): \n x = self.find(x)\n y = self.find(y)\n if x != y:\n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n self.size[y] += self.size[x]\n else:\n self.parent[y] = x\n self.size[x] += self.size[y]\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n def is_same(self, x, y): \n return self.find(x) == self.find(y)\n\n def group_size(self, x): \n return self.size[self.find(x)]\n\n def __str__(self): \n return '\\n'.join('{}: {}'.format(r, self.group_members(r)) for r in self.roots())\n\n\nN, M = map(int, input().split())\nbridge = [list(map(int, input().split())) for _ in range(M)]\nans = [0]*M\nuf = UnionFind(N)\n\n\nans[M-1] = N*(N-1)//2\nfor i in range(M-2, -1, -1):\n A, B = bridge[i+1]\n if uf.is_same(A-1, B-1):\n ans[i] = ans[i+1]\n else:\n ans[i] = ans[i+1] - uf.group_size(A-1) * uf.group_size(B-1)\n uf.unite(A-1, B-1)\n\nfor a in ans:\n print(a)\n"]
['Wrong Answer', 'Accepted']
['s142962265', 's207867435']
[37284.0, 36428.0]
[906.0, 847.0]
[1708, 1701]
p03108
u777207626
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['import numpy as np\nn,m = map(int,input().split())\nab = [[int(i) for i in input().split()]for j in range(m)][1:]\n\nans = [int(n*(n-1)/2)]\nl = np.array([i for i in range(1,n+1)])\ns = {i:1 for i in range(1,n+1)}\nfor i in ab[::-1]:\n \n if l[i[0]-1] == l[i[1]-1]:\n ans.append(ans[-1])\n else:\n \n l_a,l_b,s_a,s_b = l[i[0]-1],l[i[1]-1],s[l[i[0]-1]],s[l[i[1]-1]]\n \n if l_a <= l_b:\n c = s_a*s_b\n s[l_b]+=s_a\n s[l_a]=0\n l[l==l_a]=l_b\n ans.append(ans[-1]-c)\n else:\n c = s_a*s_b\n s[l_a]+=s_b\n s[l_b]=0\n l[l==l_b]=l_a\n ans.append(ans[-1]-c) \n if ans[-1]==0:\n ans+= [0 for _ in range(m-1-len(ans))]\n break\nfor i in ans[::-1]:\n print(i)', 'n,m = map(int,input().split())\nab = [[int(i) for i in input().split()]for j in range(m)][1:]\nans = [0]*(m-1)+[int(n*(n-1)/2)]\nl = [i for i in range(1,n+1)]\ns = {i:1 for i in range(1,n+1)}\n \ndef serch_l(num):\n if l[num]==num+1:\n return l[num]\n else:\n leader = serch_l(l[num]-1)\n l[num]=leader\n return leader\n \nfor k,i in enumerate(ab[::-1]):\n k = m-1-k\n l_a,l_b = serch_l(i[0]-1),serch_l(i[1]-1)\n if l_a==l_b:\n ans[k-1]=ans[k]\n else:\n \n s_a,s_b = s[l_a],s[l_b]\n \n if s_a > s_b:\n (s_a,s_b) = (s_b,s_a)\n (l_a,l_b) = (l_b,l_a)\n c = s_a*s_b\n s[l_b]+=s_a\n s[l_a]=0\n l[l_a-1]=l_b\n ans[k-1]=ans[k]-c\nfor i in ans:\n print(i)']
['Wrong Answer', 'Accepted']
['s581391924', 's580023559']
[44408.0, 39788.0]
[2110.0, 662.0]
[697, 677]
p03108
u782685137
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['class UnionFindTree:\n def __init__(self, n):\n self._tree = [i for i in range(n)]\n# self._rank = [1] * n\n self._size = [1] * n\n def root(self, a):\n if self._tree[a] == a:return a\n self._tree[a] = self.root(self._tree[a])\n return self._tree[a]\n def is_same_set(self, a, b):\n return self.root(a)==self.root(b)\n def unite(self, a, b):\n ra = self.root(a)\n rb = self.root(b)\n if ra == rb: return\n if self._size[ra] < self._size[rb]:\n self._tree[ra] = rb\n self._size[rb] += self._size[ra]\n else:\n self._tree[rb] = ra\n self._size[ra] += self._size[rb]\n def size(self, a):\n return self._size[self.root(a)]\n\nN,M=map(int,input().split())\nAB=[list(map(int,input().split())) for _ in range(M)]\n\nuft=UnionFindTree(N)\ns=[0]\nfor a,b in AB[::-1]:\n if not(uft.is_same_set(a-1,b-1)):\n sa = uft.size(a-1)\n sb = uft.size(b-1)\n s += [s[-1] +(sa+sb)*(sa+sb-1)//2 -sa*~-sa//2 -sb*~-sb//2]\n uft.unite(a-1,b-1)\n else:s += [s[-1]]\nfor r in s[:N-1][::-1]:print(N*~-N//2-r)', 'class UnionFindTree:\n def __init__(self, n):\n self._tree = [i for i in range(n)]\n# self._rank = [1] * n\n self._size = [1] * n\n def root(self, a):\n if self._tree[a] == a:return a\n self._tree[a] = self.root(self._tree[a])\n return self._tree[a]\n def is_same_set(self, a, b):\n return self.root(a)==self.root(b)\n def unite(self, a, b):\n ra = self.root(a)\n rb = self.root(b)\n if ra == rb: return\n if self._size[ra] < self._size[rb]:\n self._tree[ra] = rb\n self._size[rb] += self._size[ra]\n else:\n self._tree[rb] = ra\n self._size[ra] += self._size[rb]\n def size(self, a):\n return self._size[self.root(a)]\n\nN,M=map(int,input().split())\nAB=[list(map(int,input().split())) for _ in range(M)]\n \nuft=UnionFindTree(N)\ns=[0]\nfor a,b in AB[::-1]:\n if not(uft.is_same_set(a-1,b-1)):\n sa = uft.size(a-1)\n sb = uft.size(b-1)\n s += [s[-1] +(sa+sb)*(sa+sb-1)//2 -sa*(sa-1)//2 -sb*(sb-1)//2]\n uft.unite(a-1,b-1)\n else:s += [s[-1]]\n#print(s)\nfor r in s[:M][::-1]:print(N*~-N//2-r)']
['Wrong Answer', 'Accepted']
['s446100794', 's324571017']
[36908.0, 36908.0]
[938.0, 914.0]
[1128, 1141]
p03108
u782748646
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['data = input().split()\nn = int(data[0])\nm = int(data[1])\nedge=[]\nfor i in range(m):\n tmp = []\n data = input().split()\n tmp.append(int(data[0]))\n tmp.append(int(data[1]))\n edge.append(tmp)\n\nfor i in range(m):\n ans = list(range(1, n+1))\n for j in edge:\n ans = [ans[j[0]-1] if s == ans[j[1]-1] else s for s in ans]\n edge.pop(0)\n\n res=int(n*(n-1)/2)\n for j in range(1, n+1):\n c = ans.count(j)\n res -= int(c*(c-1)/2)\n print(res)\n\nprint(int(n*(n-1)/2))', 'data = input().split()\nn = int(data[0])\nm = int(data[1])\nedge=[]\nfor i in range(m):\n tmp = []\n data = input().split()\n tmp.append(int(data[0]))\n tmp.append(int(data[1]))\n edge.append(tmp)\n\nres=[]\nans = list(range(1, n+1))\nben = int(n*(n-1)/2)\nedge.reverse()\nfor e in edge:\n res.append(ben)\n ben -= ans.count(ans[e[0]-1])*ans.count(ans[e[1]-1])\n ans = [ans[e[0]-1] if s == ans[e[1]-1] else s for s in ans]\n print(e, ans, ben)\n\nres.reverse()\nfor r in res:\n print(r)\n\n#print(int(n*(n-1)/2))', 'import sys\nsys.setrecursionlimit(100000)\n\ndata = input().split()\nn = int(data[0])\nm = int(data[1])\nedge=[]\nfor i in range(m):\n tmp = []\n data = input().split()\n tmp.append(int(data[0])-1)\n tmp.append(int(data[1])-1)\n edge.append(tmp)\n\ngrp=list(range(0, n))\ndef group(num):\n if grp[num] == num:\n return num\n return group(grp[num])\n\ndef merge(p_num, c_num):\n grp[c_num]=grp[p_num]\n\nres=[]\nans = [1]*n\nben = int(n*(n-1)/2)\nedge.reverse()\nfor e in edge:\n \n res.append(ben)\n g0 = group(e[0])\n g1 = group(e[1])\n if g0 != g1:\n ben -= ans[g0]*ans[g1]\n sum = ans[g0]+ans[g1]\n if ans[g0] > ans[g1]:\n merge(g0, g1)\n else:\n merge(g1, g0)\n ans[g0]=sum\n ans[g1]=sum\n\nres.reverse()\nfor r in res:\n print(r)\n\n#print(int(n*(n-1)/2))']
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s161467900', 's293481357', 's811707910']
[27836.0, 84336.0, 31092.0]
[2105.0, 2105.0, 570.0]
[500, 517, 851]
p03108
u785578220
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["\nclass UnionFind:\n def __init__(self, n):\n self.par = [i for i in range(n)]\n self.rank = [0] * n\n self.size = [1] * n\n\n \n def find(self, x):\n if self.par[x] == x:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n\n \n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n\n if self.rank[x] < self.rank[y]:\n self.par[x] = y\n self.size[y] += self.size[x]\n self.size[x] = 0\n else:\n self.par[y] = x\n self.size[x] += self.size[y]\n self.size[y] = 0\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n \n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n \n def all_find(self):\n for n in range(len(self.par)):\n self.find(n)\n\n\nuf = UnionFind(N)\nScore = N * (N - 1) // 2\nans = []\nwhile Edge:\n ans.append(Score)\n a, b = Edge.pop()\n pa, pb = uf.find(a), uf.find(b)\n if not uf.same(pa, pb):\n Score -= (uf.size[pa] * uf.size[pb])\n uf.union(a, b)\n\nprint(*ans[::-1], sep='\\n')\n\ndef main():\n N, M = map(int, input().split())\n Edge = []\n for i in range(M):\n a, b = map(int, input().split())\n a, b = a - 1, b - 1\n Edge.append([a, b])\n\n uf = UnionFind(N)\n Score = N * (N - 1) // 2\n ans = []\n while Edge:\n ans.append(Score)\n a, b = Edge.pop()\n pa, pb = uf.find(a), uf.find(b)\n if not uf.same(pa, pb):\n Score -= (uf.size[pa] * uf.size[pb])\n uf.union(a, b)\n\nprint(*ans[::-1], sep='\\n')\n\nif __name__ == '__main__':\n main()\n ", "class UnionFind:\n def __init__(self, n):\n self.par = [i for i in range(n)]\n self.rank = [0] * n\n self.size = [1] * n\n\n \n def find(self, x):\n if self.par[x] == x:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n\n \n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n\n if x == y:\n return\n\n if self.rank[x] < self.rank[y]:\n self.par[x] = y\n self.size[y] += self.size[x]\n self.size[x] = 0\n else:\n self.par[y] = x\n self.size[x] += self.size[y]\n self.size[y] = 0\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n \n def same(self, x, y):\n return self.find(x) == self.find(y)\n\n \n def all_find(self):\n for n in range(len(self.par)):\n self.find(n)\n\n\ndef main():\n N, M = map(int, input().split())\n Edge = []\n for i in range(M):\n a, b = map(int, input().split())\n a, b = a - 1, b - 1\n Edge.append([a, b])\n\n uf = UnionFind(N)\n Score = N * (N - 1) // 2\n ans = []\n while Edge:\n ans.append(Score)\n a, b = Edge.pop()\n pa, pb = uf.find(a), uf.find(b)\n if not uf.same(pa, pb):\n Score -= (uf.size[pa] * uf.size[pb])\n uf.union(a, b)\n\n print(*ans[::-1], sep='\\n')\n\nif __name__ == '__main__':\n main()\n "]
['Runtime Error', 'Accepted']
['s763425298', 's580495575']
[3192.0, 26352.0]
[18.0, 686.0]
[1835, 1572]
p03108
u788137651
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["from math import factorial\n\n\ndef combination(n, r): \n if n < r:\n return 0\n else:\n return factorial(n)//(factorial(r) * factorial(n-r))\n\n\nclass UnionFind():\n def __init__(self, n):\n self.nodes = [-1] * n \n\n def get_root(self, x):\n \n if self.nodes[x] < 0:\n return x\n \n else:\n self.nodes[x] = self.get_root(self.nodes[x])\n return self.nodes[x]\n\n def unite(self, x, y):\n root_x = self.get_root(x)\n root_y = self.get_root(y)\n \n # if root_x == root_y:\n \n if root_x != root_y:\n \n if self.nodes[root_x] < self.nodes[root_y]:\n big_root = root_x\n small_root = root_y\n else:\n small_root = root_x\n big_root = root_y\n self.nodes[big_root] += self.nodes[small_root]\n self.nodes[small_root] = big_root\n\n\ndef main():\n N, M = map(int, input().split())\n A, B = [], []\n for i in range(M):\n a, b = map(int, input().split())\n A.append(a)\n B.append(b)\n uf = UnionFind(N+1)\n get_root, unite, nodes = uf.get_root, uf.unite, uf.nodes\n uncomfort = (combination(N, 2))\n ans = [uncomfort]\n for i in reversed(range(M)):\n uncomfort -= abs(nodes[get_root(A[i])] * nodes[get_root(B[i])])\n ans.append(uncomfort)\n unite(A[i], B[i])\n print('\\n'.join(map(str, ans[-2::-1])))\n\n\nif __name__ == '__main__':\n main()", "from math import factorial\n\n\ndef combination(n, r): \n if n < r:\n return 0\n else:\n return factorial(n)//(factorial(r) * factorial(n-r))\n\n\nclass UnionFind():\n def __init__(self, n):\n self.nodes = [-1] * n \n\n def get_root(self, x):\n \n if self.nodes[x] < 0:\n return x\n \n else:\n self.nodes[x] = self.get_root(self.nodes[x])\n return self.nodes[x]\n\n def unite(self, x, y):\n root_x = self.get_root(x)\n root_y = self.get_root(y)\n \n # if root_x == root_y:\n \n if root_x != root_y:\n \n if self.nodes[root_x] < self.nodes[root_y]:\n big_root = root_x\n small_root = root_y\n else:\n small_root = root_x\n big_root = root_y\n self.nodes[big_root] += self.nodes[small_root]\n self.nodes[small_root] = big_root\n\n def solve_uncomfort(self, combi_n):\n sum = 0\n for n in self.nodes:\n if n < 0:\n n = abs(n)\n sum += combination(abs(n), 2)\n return combination(N, 2)-sum\n\n\ndef main():\n N, M = map(int, input().split())\n A, B = [], []\n for i in range(M):\n a, b = map(int, input().split())\n A.append(a)\n B.append(b)\n uf = UnionFind(N+1)\n get_root, unite, nodes = uf.get_root, uf.unite, uf.nodes\n ans = []\n combi_n = combination(N, 2)\n for i in reversed(range(M)):\n ans.append(uf.solve_uncomfort(combi_n))\n unite(A[i], B[i])\n print('\\n'.join(map(str, ans[::-1])))\n", "from math import factorial\n\n\ndef combination(n, r): \n if n < r:\n return 0\n else:\n return factorial(n)//(factorial(r) * factorial(n-r))\n\n\nclass UnionFind():\n def __init__(self, n):\n self.nodes = [-1] * n \n\n def get_root(self, x):\n \n if self.nodes[x] < 0:\n return x\n \n else:\n self.nodes[x] = self.get_root(self.nodes[x])\n return self.nodes[x]\n\n def unite(self, x, y):\n root_x = self.get_root(x)\n root_y = self.get_root(y)\n \n # if root_x == root_y:\n \n if root_x != root_y:\n \n if self.nodes[root_x] < self.nodes[root_y]:\n big_root = root_x\n small_root = root_y\n else:\n small_root = root_x\n big_root = root_y\n self.nodes[big_root] += self.nodes[small_root]\n self.nodes[small_root] = big_root\n\n\ndef main():\n N, M = map(int, input().split())\n A, B = [], []\n for i in range(M):\n a, b = map(int, input().split())\n A.append(a)\n B.append(b)\n uf = UnionFind(N+1)\n get_root, unite, nodes = uf.get_root, uf.unite, uf.nodes\n uncomfort = (combination(N, 2))\n ans = [uncomfort]\n for i in reversed(range(M)):\n if get_root(A[i]) != get_root(B[i]):\n uncomfort -= abs(nodes[get_root(A[i])] * nodes[get_root(B[i])])\n ans.append(uncomfort)\n unite(A[i], B[i])\n # print(ans)\n print('\\n'.join(map(str, ans[-2::-1])))\n\n\nif __name__ == '__main__':\n main()\n"]
['Wrong Answer', 'Wrong Answer', 'Accepted']
['s283934189', 's513420320', 's817472968']
[24252.0, 3064.0, 24236.0]
[933.0, 17.0, 1010.0]
[1782, 1892, 1849]
p03108
u794543767
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['\nclass UnionFind:\n def __init__(self, length):\n self.par = [-1 for _ in range(length)]\n\n def root(self,x):\n if (self.par[x] < 0 ):\n return x\n else:\n #change connection\n root = self.root(self.par[x])\n self.par[x] = root\n return root\n \n def unite(self,x,y):\n root_x = self.root(x)\n root_y = self.root(y)\n if (root_x==root_y):\n return \n else:\n self.par[root_y] += self.par[root_x]\n self.par[root_x] = root_y\n \n def same(self,x,y):\n root_x = self.root(x)\n root_y = self.root(y)\n return root_x == root_y \n \n def size(self,x):\n return -self.par[self.root(x)]\n\n\ndef solve(edges,N,M):\n\n score= N*(N-1)//2\n outputs = [score]\n uf = UnionFind(N+3)\n for edge in edges[::-1]:\n if not uf.same(*edge):\n score -= uf.size(edge[0]-1) * uf.size(edge[1]-1)\n uf.unite(*edge)\n outputs.append(score)\n for i in range(M):\n print(int(outputs[M-i-1]))\n\ndef get_input():\n N,M = map(int, input().split())\n edges = [tuple(map(int, input().split())) for _ in range(M)]\n return N,M,edges\n\nget_input()\n#N = 10**5\n#M=10**4\n\nsolve(edges,N,M)\n\n', '\nclass UnionFind:\n def __init__(self, length):\n self.par = [-1 for _ in range(length)]\n\n def root(self,x):\n if (self.par[x] < 0 ):\n return x\n else:\n #change connection\n root = self.root(self.par[x])\n self.par[x] = root\n return root\n \n def unite(self,x,y):\n root_x = self.root(x)\n root_y = self.root(y)\n if (root_x==root_y):\n return \n else:\n if self.size(root_x) < size(root_y):\n root_x,root_y = root_y, root_x\n self.par[root_y] += self.par[root_x]\n self.par[root_x] = root_y\n \n def same(self,x,y):\n root_x = self.root(x)\n root_y = self.root(y)\n return root_x == root_y \n \n def size(self,x):\n return -self.par[self.root(x)]\n\n\ndef solve(edges,N,M):\n\n score= N*(N-1)//2\n outputs = [score]\n uf = UnionFind(N+1)\n for edge in edges[::-1]:\n if not uf.same(*edge):\n score -= uf.size(edge[0]) * uf.size(edge[1])\n uf.unite(*edge)\n outputs.append(score)\n for i in range(M):\n print(int(outputs[M-i-1]))\n\ndef get_input():\n N,M = map(int, input().split())\n edges = [tuple(map(lambda x: int(x)-1, input().split())) for _ in range(M)]\n return N,M,edges\n\nN,M,edges = get_input()\n#N = 10**5\n#M=10**4\n\nsolve(edges,N,M)\n\n', 'from typing import List\n\nclass UnionFind:\n def __init__(self, length):\n self.par:List[int] = [_ for _ in range(length)]\n self.size_dict:dict = dict([(i,1) for i in range(length)])\n\n def root(self,x:int):\n if (self.par[x] == x):\n return x\n else:\n #change connection \n self.par[x] =self.root(self.par[x])\n return self.par[x]\n \n def unite(self,x:int,y:int):\n root_x:int = self.root(x)\n root_y:int = self.root(y)\n if (root_x==root_y):\n pass\n else:\n self.par[root_x] = root_y\n self.size_dict[root_y] += self.size_dict[root_x]\n self.size_dict[root_x] = -1\n \n def same(self,x:int,y:int)->bool:\n root_x:int = self.root(x)\n root_y:int = self.root(y)\n return root_x == root_y \n \n def size(self,x:int)->int:\n root_x = self.root(x)\n return self.size_dict[root_x]\n\n\n\n\nN,M = [ int(i) for i in input().split(" ")]\nedges = [ tuple([ int(num)-1 for num in input().split(" ")]) for _ in range(M) ]\nscore= N*(N-1)/2\noutputs = [score]\nuf = UnionFind(N)\nfor edge in edges[::-1]:\n if not uf.same(*edge):\n score -= uf.size(edge[0]) * uf.size(edge[1])\n uf.unite(*edge)\n outputs.append(score)\n\nfor i in outputs[::-1]:\n print(int(i))\n', 'class UnionFind:\n def __init__(self, length):\n self.par = [-1 for _ in range(length)]\n self.rank = [0 for _ in range(length)]\n \n def root(self,x):\n if (self.par[x] < 0 ):\n return x\n else:\n #change connection\n root = self.root(self.par[x])\n self.par[x] = root\n return root\n \n def unite(self,x,y):\n root_x = self.root(x)\n root_y = self.root(y)\n if (root_x==root_y):\n return \n else:\n if self.rank[root_x] < self.rank[root_y]:\n self.par[root_y] += self.par[root_x]\n self.par[root_x] = root_y\n\n else:\n self.par[root_x] += self.par[root_y]\n self.par[root_y] = root_x\n if self.rank[root_x] == self.rank[root_y]:\n self.rank[root_x]+=1\n \n def same(self,x,y):\n root_x = self.root(x)\n root_y = self.root(y)\n return root_x == root_y \n \n def size(self,x):\n return -self.par[self.root(x)]\n\n\ndef solve(edges,N,M):\n \n score= N*(N-1)//2\n outputs = [score]\n uf = UnionFind(N)\n for edge in edges[::-1]:\n if not uf.same(*edge):\n score -= uf.size(edge[0]) * uf.size(edge[1])\n uf.unite(*edge)\n outputs.append(score)\n for i in range(M):\n print(int(outputs[M-i-1]))\n\ndef get_input():\n N,M = map(int, input().split())\n edges = [tuple(map(lambda x: int(x)-1, input().split())) for _ in range(M)]\n return N,M,edges\n\nN,M,edges = get_input()\n# = 10**6\n#M=10**5\n#import numpy as np\n\nsolve(edges,N,M)']
['Runtime Error', 'Runtime Error', 'Runtime Error', 'Accepted']
['s269066791', 's580693998', 's785132697', 's294838041']
[16636.0, 18200.0, 3064.0, 23284.0]
[310.0, 356.0, 17.0, 799.0]
[1400, 1528, 1428, 1792]
p03108
u797740860
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['n, m = [int(i) for i in input().split(" ")]\ne = []\npar = [i for i in range(0, n + 1)]\ncount = [1] * (n + 1)\nunu = [0] * n\n\nfor i in range(0, m):\n e.append([int(j) for j in input().split(" ")])\n\ndef root(x):\n if par[x] == x:\n return x\n else:\n r = root(par[x])\n par[x] = r\n return r\n\ndef unite(x, y, e):\n x = root(x)\n y = root(y)\n if x == y:\n return\n else:\n unu[e] = count[x] * count[y]\n count[y] += count[x]\n par[x] = y\n\nfor i in reversed(range(0, m)):\n unite(e[i][0], e[i][1], i)\n\ns = 0\nfor i in range(0, m):\n s += unu[i]\n print(s)', 'import sys\n\nn, m = [int(i) for i in input().split(" ")]\ne = []\npar = [i for i in range(0, n + 1)]\ncount = [1] * (n + 1)\nunu = [0] * m\n\nsys.setrecursionlimit(10**6)\n\nfor i in range(0, m):\n e.append([int(j) for j in input().split(" ")])\n\ndef root(x):\n if par[x] == x:\n return x\n else:\n r = root(par[x])\n par[x] = r\n return r\n\ndef unite(x, y, e):\n x = root(x)\n y = root(y)\n if x == y:\n return\n else:\n unu[e] = count[x] * count[y]\n count[y] += count[x]\n par[x] = y\n\nfor i in reversed(range(0, m)):\n unite(e[i][0], e[i][1], i)\n\ns = 0\nfor i in range(0, m):\n s += unu[i]\n print(s)']
['Runtime Error', 'Accepted']
['s793140578', 's402143599']
[29536.0, 40452.0]
[639.0, 637.0]
[617, 659]
p03108
u807772568
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['import collections\nimport itertools\nimport operator\n\n\nclass UnionFind:\n def __init__(self, elems=None):\n class KeyDict(dict):\n def __missing__(self, key):\n self[key] = key\n return key\n\n self.parent = KeyDict()\n self.rank = collections.defaultdict(int)\n self.size = collections.defaultdict(lambda: 1)\n\n if elems is not None:\n for elem in elems:\n _, _ = self.parent[elem], self.rank[elem]\n\n def find(self, x):\n if self.parent[x] == x:\n return x\n else:\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def unite(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x != y:\n xx = self.size[x]\n yy = self.size[y]\n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n self.size[y] += xx\n else:\n self.parent[y] = x\n self.size[x] += yy\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n def Size(self,x):\n return self.size[self.find(x)]\n\n def are_same(self, x, y):\n return self.find(x) == self.find(y)\n\n def grouper(self):\n roots = [(x, self.find(x_par)) for x, x_par in self.parent.items()]\n root = operator.itemgetter(1)\n for _, group in itertools.groupby(sorted(roots, key=root), root):\n yield [x for x, _ in group]\n\n\na,b = map(int,input().split())\nc = []\nuf = UnionFind()\nfor i in range(b):\n n,m = map(int,input().split())\n c.append([n,m])\nfor i in range(a):\n uf.unite(i+1,i+1)\nco = a*(a-1)//2\nccc = []\nccc.append(co)\nfor i in range(b)[::-1]:\n z = uf.Size(c[i][0])\n zz = uf.Size(c[i][1])\n uf.unite(c[i][0],c[i][1])\n if uf.are_same(c[i][0],c[i][1]) == False:\n co -= z*zz\n if co < 0:\n co = 0\n ccc.append(co)\nfor i in range(b)[::-1]:\n print(ccc[i])\n', 'import collections\nimport itertools\nimport operator\n\n\nclass UnionFind:\n def __init__(self, elems=None):\n class KeyDict(dict):\n def __missing__(self, key):\n self[key] = key\n return key\n\n self.parent = KeyDict()\n self.rank = collections.defaultdict(int)\n\n if elems is not None:\n for elem in elems:\n _, _ = self.parent[elem], self.rank[elem]\n\n def find(self, x):\n if self.parent[x] == x:\n return x\n else:\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def unite(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n else:\n self.parent[y] = x\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n def size(self,x):\n return self.rank[x]\n\n def are_same(self, x, y):\n return self.find(x) == self.find(y)\n\n def grouper(self):\n roots = [(x, self.find(x_par)) for x, x_par in self.parent.items()]\n root = operator.itemgetter(1)\n for _, group in itertools.groupby(sorted(roots, key=root), root):\n yield [x for x, _ in group]\n\ndef my_index(l, x, default=False):\n if x in l:\n return l.index(x)\n else:\n return default\n\na,b = map(int,input().split())\nc = []\nuf = UnionFind()\nfor i in range(b):\n n,m = map(int,input().split())\n c.append([n,m])\nfor i in range(a):\n uf.unite(i+1,i+1)\nco = a*(a-1)//2\nccc = []\nccc.append(co)\nfor i in range(b)[::-1]:\n z = size(c[i][0])\n zz = size(c[i][1])\n uf.unite(c[i][0],c[i][1])\n co -= z*zz\n if co < 0:\n co = 0\n ccc.append(co)\nfor i in range(b)[::-1]:\n print(ccc[i])\n', 'import collections\nimport itertools\nimport operator\n\n\nclass UnionFind:\n def __init__(self, elems=None):\n class KeyDict(dict):\n def __missing__(self, key):\n self[key] = key\n return key\n\n self.parent = KeyDict()\n self.rank = collections.defaultdict(int)\n self.size = collections.defaultdict(lambda: 1)\n\n if elems is not None:\n for elem in elems:\n _, _, _ = self.parent[elem], self.rank[elem],self.size[elem]\n\n def find(self, x):\n if self.parent[x] == x:\n return x\n else:\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def unite(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if not self.are_same(x,y):\n xx = self.size[x]\n yy = self.size[y]\n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n self.size[y] += xx\n else:\n self.parent[y] = x\n self.size[x] += yy\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n def Size(self,x):\n return self.size[self.find(x)]\n\n def are_same(self, x, y):\n return self.find(x) == self.find(y)\n\n def grouper(self):\n roots = [(x, self.find(x_par)) for x, x_par in self.parent.items()]\n root = operator.itemgetter(1)\n for _, group in itertools.groupby(sorted(roots, key=root), root):\n yield [x for x, _ in group]\n\n\na,b = map(int,input().split())\nc = []\nuf = UnionFind()\nfor i in range(b):\n n,m = map(int,input().split())\n c.append([n,m])\nfor i in range(a):\n uf.unite(i+1,i+1)\nco = a*(a-1)//2\nccc = []\nccc.append(co)\nfor i in range(b)[::-1]:\n z = uf.Size(c[i][0])\n zz = uf.Size(c[i][1])\n uf.unite(c[i][0],c[i][1])\n if not uf.are_same(c[i][0],c[i][1]):\n co -= z*zz\n if co < 0:\n co = 0\n ccc.append(co)\nfor i in range(b)[::-1]:\n print(ccc[i])\n', 'import collections\nimport itertools\nimport operator\n\n\nclass UnionFind:\n def __init__(self, elems=None):\n class KeyDict(dict):\n def __missing__(self, key):\n self[key] = key\n return key\n\n self.parent = KeyDict()\n self.rank = collections.defaultdict(int)\n self.size = collections.defaultdict(lambda: 1)\n\n if elems is not None:\n for elem in elems:\n _, _ = self.parent[elem], self.rank[elem]\n\n def find(self, x):\n if self.parent[x] == x:\n return x\n else:\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def unite(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if x != y:\n xx = self.size[x]\n yy = self.size[y]\n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n self.size[y] += xx\n else:\n self.parent[y] = x\n self.size[x] += yy\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n def Size(self,x):\n return self.size[self.find(x)]\n\n def are_same(self, x, y):\n return self.find(x) == self.find(y)\n\n def grouper(self):\n roots = [(x, self.find(x_par)) for x, x_par in self.parent.items()]\n root = operator.itemgetter(1)\n for _, group in itertools.groupby(sorted(roots, key=root), root):\n yield [x for x, _ in group]\n\n\na,b = map(int,input().split())\nc = []\nuf = UnionFind()\nfor i in range(b):\n n,m = map(int,input().split())\n c.append([n,m])\nfor i in range(a):\n uf.unite(i+1,i+1)\nco = a*(a-1)//2\nccc = []\nccc.append(co)\nfor i in range(b)[::-1]:\n z = uf.Size(c[i][0])\n zz = uf.Size(c[i][1])\n uf.unite(c[i][0],c[i][1])\n if uf.are_same(c[i][0],c[i][1]) == false:\n co -= z*zz\n if co < 0:\n co = 0\n ccc.append(co)\nfor i in range(b)[::-1]:\n print(ccc[i])\n', 'import collections\nimport itertools\nimport operator\n\n\nclass UnionFind:\n def __init__(self, elems=None):\n class KeyDict(dict):\n def __missing__(self, key):\n self[key] = key\n return key\n\n self.parent = KeyDict()\n self.rank = collections.defaultdict(int)\n\n if elems is not None:\n for elem in elems:\n _, _ = self.parent[elem], self.rank[elem]\n\n def find(self, x):\n if self.parent[x] == x:\n return x\n else:\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def unite(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n self.rank[y] += 1\n else:\n self.parent[y] = x\n self.rank[x] += 1\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n self.rank[y] += 1\n\n def size(self,x):\n return self.rank[self.find(x)]\n\n def are_same(self, x, y):\n return self.find(x) == self.find(y)\n\n def grouper(self):\n roots = [(x, self.find(x_par)) for x, x_par in self.parent.items()]\n root = operator.itemgetter(1)\n for _, group in itertools.groupby(sorted(roots, key=root), root):\n yield [x for x, _ in group]\n\n\na,b = map(int,input().split())\nc = []\nuf = UnionFind()\nfor i in range(b):\n n,m = map(int,input().split())\n c.append([n,m])\nfor i in range(a):\n uf.unite(i+1,i+1)\nco = a*(a-1)//2\nccc = []\nccc.append(co)\nfor i in range(b)[::-1]:\n z = uf.size(c[i][0])\n zz = uf.size(c[i][1])\n uf.unite(c[i][0],c[i][1])\n co -= z*zz\n if co < 0:\n co = 0\n ccc.append(co)\nfor i in range(b)[::-1]:\n print(ccc[i])\n', 'import collections\nimport itertools\nimport operator\n\n\nclass UnionFind:\n def __init__(self, elems=None):\n class KeyDict(dict):\n def __missing__(self, key):\n self[key] = key\n return key\n\n self.parent = KeyDict()\n self.rank = collections.defaultdict(int)\n\n if elems is not None:\n for elem in elems:\n _, _ = self.parent[elem], self.rank[elem]\n\n def find(self, x):\n if self.parent[x] == x:\n return x\n else:\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def unite(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n self.rank[y] += self.rank[x]\n else:\n self.parent[y] = x\n self.rank[x] += self.rank[y]\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n self.rank[y] += 1\n\n def size(self,x):\n return self.rank[self.find(x)]\n\n def are_same(self, x, y):\n return self.find(x) == self.find(y)\n\n def grouper(self):\n roots = [(x, self.find(x_par)) for x, x_par in self.parent.items()]\n root = operator.itemgetter(1)\n for _, group in itertools.groupby(sorted(roots, key=root), root):\n yield [x for x, _ in group]\n\n\na,b = map(int,input().split())\nc = []\nuf = UnionFind()\nfor i in range(b):\n n,m = map(int,input().split())\n c.append([n,m])\nfor i in range(a):\n uf.unite(i+1,i+1)\nco = a*(a-1)//2\nccc = []\nccc.append(co)\nfor i in range(b)[::-1]:\n z = uf.size(c[i][0])\n zz = uf.size(c[i][1])\n uf.unite(c[i][0],c[i][1])\n co -= z*zz\n if co < 0:\n co = 0\n ccc.append(co)\nfor i in range(b)[::-1]:\n print(ccc[i])\n', 'import collections\nimport itertools\nimport operator\n\n\nclass UnionFind:\n def __init__(self, elems=None):\n class KeyDict(dict):\n def __missing__(self, key):\n self[key] = key\n return key\n\n self.parent = KeyDict()\n self.rank = collections.defaultdict(int)\n self.size = collections.defaultdict(lambda: 1)\n\n if elems is not None:\n for elem in elems:\n _, _ = self.parent[elem], self.rank[elem]\n\n def find(self, x):\n if self.parent[x] == x:\n return x\n else:\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def unite(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if self.are_same(x,y):\n xx = self.size[x]\n yy = self.size[y]\n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n self.size[y] += xx\n else:\n self.parent[y] = x\n self.size[x] += yy\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n def Size(self,x):\n return self.size[self.find(x)]\n\n def are_same(self, x, y):\n return self.find(x) == self.find(y)\n\n def grouper(self):\n roots = [(x, self.find(x_par)) for x, x_par in self.parent.items()]\n root = operator.itemgetter(1)\n for _, group in itertools.groupby(sorted(roots, key=root), root):\n yield [x for x, _ in group]\n\n\na,b = map(int,input().split())\nc = []\nuf = UnionFind()\nfor i in range(b):\n n,m = map(int,input().split())\n c.append([n,m])\nfor i in range(a):\n uf.unite(i+1,i+1)\nco = a*(a-1)//2\nccc = []\nccc.append(co)\nfor i in range(b)[::-1]:\n z = uf.Size(c[i][0])\n zz = uf.Size(c[i][1])\n uf.unite(c[i][0],c[i][1])\n if not uf.are_same(c[i][0],c[i][1]):\n co -= z*zz\n if co < 0:\n co = 0\n ccc.append(co)\nfor i in range(b)[::-1]:\n print(ccc[i])\n', "import collections\nimport itertools\nimport operator\n\n\nclass UnionFind:\n def __init__(self, x):\n class KeyDict(dict):\n def __missing__(self, key):\n self[key] = key\n return key\n\n self.parent = KeyDict()\n self.rank = collections.defaultdict(int)\n self.size = collections.defaultdict(lambda: 1)\n\n if x is not None:\n for elem in range(x+1):\n _, _, _ = self.parent[elem], self.rank[elem],self.size[elem]\n\n def find(self, x):\n if self.parent[x] == x:\n return x\n else:\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n\n def unite(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if not self.are_same(x,y):\n xx = self.size[x]\n yy = self.size[y]\n if self.rank[x] < self.rank[y]:\n self.parent[x] = y\n self.size[y] += xx\n else:\n self.parent[y] = x\n self.size[x] += yy\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n def Size(self,x):\n return self.size[self.find(x)]\n\n def are_same(self, x, y):\n '''print(x,y,self.find(x),self.find(y),self.find(x) == self.find(y))'''\n return self.find(x) == self.find(y)\n\n def grouper(self):\n roots = [(x, self.find(x_par)) for x, x_par in self.parent.items()]\n root = operator.itemgetter(1)\n for _, group in itertools.groupby(sorted(roots, key=root), root):\n yield [x for x, _ in group]\n\n\na,b = map(int,input().split())\nc = []\nuf = UnionFind(a)\nfor i in range(b):\n n,m = map(int,input().split())\n c.append([n,m])\nco = a*(a-1)//2\nccc = []\nccc.append(co)\nfor i in range(b)[::-1]:\n z = uf.Size(c[i][0])\n zz = uf.Size(c[i][1])\n if not uf.are_same(c[i][0],c[i][1]):\n co -= z*zz\n if co < 0:\n co = 0\n uf.unite(c[i][0],c[i][1])\n ccc.append(co)\nfor i in range(b)[::-1]:\n print(ccc[i])\n"]
['Wrong Answer', 'Runtime Error', 'Wrong Answer', 'Runtime Error', 'Wrong Answer', 'Wrong Answer', 'Wrong Answer', 'Accepted']
['s163820779', 's173323138', 's184018042', 's280766154', 's533527835', 's604885800', 's687319136', 's203770253']
[49600.0, 38284.0, 48824.0, 38308.0, 40628.0, 40832.0, 46748.0, 47096.0]
[1327.0, 535.0, 1519.0, 520.0, 1119.0, 2106.0, 1217.0, 1354.0]
[1966, 1788, 1996, 1966, 1786, 1808, 1973, 2028]
p03108
u814171899
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['class UnionFind():\n \n \n def __init__(self,size):\n self.table = [-1 for _ in range(size)]\n self.num = [1 for _ in range(size)]\n\n \n def find(self,x):\n while self.table[x] >= 0:\n \n x = self.table[x]\n return x\n\n \n def union(self,x,y):\n s1 = self.find(x)\n n1 = self.num[s1] \n s2 = self.find(y)\n n2 = self.num[s2]\n if s1 != s2:\n if self.table[s1] != self.table[s2]:\n if self.table[s1] < self.table[s2]:\n self.table[s2] = s1\n else:\n self.table[s1] = s2\n else:\n \n \n self.table[s1] += -1\n self.table[s2] = s1\n self.num[self.find(s1)]=n1+n2\n return n1*n2\n return 0\n \n def get_num(self, x):\n return self.num[x]\n\nn, m = map(int, input().split())\nh=[]\nfor _ in range(m):\n h.append(list(map(int, input().split())))\nh.reverse()\n\nu=UnionFind(n)\ns=n*(n-1)//2\nans=[s]\nfor hi in h:\n s-=u.union(hi[0]-1, hi[1]-1)\n ans.append(s)\nans[:len(ans)-1]\nfor ai in ans[::-1]:\n print(ai)\n', 'class UnionFind():\n \n \n def __init__(self,size):\n self.table = [-1 for _ in range(size)]\n self.num = [1 for _ in range(size)]\n\n \n def find(self,x):\n while self.table[x] >= 0:\n \n x = self.table[x]\n return x\n\n \n def union(self,x,y):\n s1 = self.find(x)\n n1 = self.num[s1] \n s2 = self.find(y)\n n2 = self.num[s2]\n if s1 != s2:\n if self.table[s1] != self.table[s2]:\n if self.table[s1] < self.table[s2]:\n self.table[s2] = s1\n else:\n self.table[s1] = s2\n else:\n \n \n self.table[s1] += -1\n self.table[s2] = s1\n self.num[self.find(s1)]=n1+n2\n return n1*n2\n return 0\n \n def get_num(self, x):\n return self.num[x]\n\nn, m = map(int, input().split())\nh=[]\nfor _ in range(m):\n h.append(list(map(int, input().split())))\nh.reverse()\n\nu=UnionFind(n)\ns=n*(n-1)//2\nans=[s]\nfor hi in h:\n s-=u.union(hi[0]-1, hi[1]-1)\n ans.append(s)\nans=ans[:len(ans)-1]\nfor ai in ans[::-1]:\n print(ai)\n']
['Wrong Answer', 'Accepted']
['s642230996', 's671056449']
[36584.0, 36584.0]
[641.0, 646.0]
[1468, 1472]
p03108
u814781830
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['import numpy as np\nN, M = list(map(int, input().split()))\nAB = [list(map(int, input().split())) for i in range(M)]\n \nAB.reverse()\n\nreader = np.array([i + 1 for i in range(N)])\nsize = np.array([1 for i in range(N)])\n\ndef root(a):\n if reader[a-1] == a:\n return a - 1\n else:\n return reader[a-1] - 1\n\ndef merge(a, b):\n # calc root index\n a = root(a)\n b = root(b)\n if a == b:\n return\n res = size[a] * size[b]\n if size[a] >= size[b]:\n # add size is root\n size[a] += size[b]\n # replace zero is all child node\n size[b] = 0\n reader[reader == reader[b]] = reader[a]\n else:\n size[b] += size[a]\n size[a] = 0\n reader[reader == reader[a]] = reader[b]\n return res\n\nresult = []\nans = N * (N-1)/2\nfor i in AB:\n result.append(ans)\n ans -= merge(i[0], i[1])\nfor i in result[::-1]:\n print(i) ', 'import numpy as np\nN, M = list(map(int, input().split()))\nAB = [list(map(int, input().split())) for i in range(M)]\n \nAB.reverse()\n\nreader = np.array([i + 1 for i in range(N)])\nsize = np.array([1 for i in range(N)])\n\ndef root(a):\n if reader[a-1] == a:\n return a - 1\n else:\n return reader[a-1] - 1\n\ndef merge(a, b):\n # calc root index\n a = root(a)\n b = root(b)\n if a == b:\n return 0\n res = size[a] * size[b]\n if size[a] >= size[b]:\n # add size is root\n size[a] += size[b]\n # replace zero is all child node\n size[b] = 0\n reader[reader == reader[b]] = reader[a]\n else:\n size[b] += size[a]\n size[a] = 0\n reader[reader == reader[a]] = reader[b]\n return res\n\nresult = []\nans = N * (N-1)/2\nresult.append(ans)\nfor i in AB:\n ans -= merge(i[0], i[1])\n result.append(ans)\n ', 'from collections import Counter\n# union find\nclass UnionFind():\n def __init__(self, n):\n self.n = n\n self.root = [-1]*(n+1)\n self.rnk = [0]*(n+1)\n\n def find_root(self, x):\n if(self.root[x] < 0):\n return x\n else:\n self.root[x] = self.find_root(self.root[x])\n return self.root[x]\n\n def unite(self, x, y):\n x = self.find_root(x)\n y = self.find_root(y)\n if(x == y):\n return \n elif(self.rnk[x] > self.rnk[y]):\n self.root[x] += self.root[y]\n self.root[y] = x\n\n else:\n self.root[y] += self.root[x]\n self.root[x] = y\n if(self.rnk[x] == self.rnk[y]):\n self.rnk[y] += 1\n\n def is_same_group(self, x, y):\n return self.find_root(x) == self.find_root(y)\n\n def count(self, x):\n return -self.root[self.find_root(x)]\n\nN, M = map(int, input().split())\nAB = [list(map(int, input().split())) for _ in range(M)]\n\nAB = AB[::-1]\nret = []\ntmp = 0\nuf = UnionFind(N)\nfor a,b in AB:\n if uf.is_same_group(a-1, b-1):\n ret.append(0)\n else:\n\n ret.append(uf.count(a-1) * uf.count(b-1))\n uf.unite(a-1,b-1)\n\nans = 0\nfor i in range(M-1, -1, -1):\n ans += ret[i]\n print(ans)']
['Runtime Error', 'Wrong Answer', 'Accepted']
['s629615219', 's958388867', 's605875678']
[41356.0, 41384.0, 37780.0]
[2110.0, 2113.0, 837.0]
[819, 807, 1283]
p03108
u814986259
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['class unionFind:\n parent = []\n nums = []\n\n def __init__(self, N):\n self.parent = [i for i in range(N)]\n self.nums = [1]*N\n\n def root(self, x):\n if x < 0:\n return(0)\n if self.parent[x] == x:\n return(x)\n else:\n self.parent[x] = self.root(self.parent[x])\n return(self.parent[x])\n\n def same(self, x, y):\n x, y = x-1, y-1\n return(self.root(x) == self.root(y))\n\n def unite(self, x, y):\n x, y = x-1, y-1\n x = self.root(x)\n y = self.root(y)\n if x == y:\n return\n else:\n self.parent[x] = y\n self.nums[y] += self.nums[x]\n self.nums[x] = 0\n return\n\n\nN, M = map(int, input().split())\nG = unionFind(N)\nans = [0]*M\nab = [tuple(map(int, input().split())) for i in range(M)]\n\ntmp = (N * (N-1)) // 2\nfor i, x in enumerate(reversed(ab)):\n a, b = x[0], x[1]\n print(a, b)\n ans[M-1-i] = tmp\n a = G.root(a-1)\n b = G.root(b-1)\n if a == b:\n continue\n else:\n tmp -= G.nums[a] * G.nums[b]\n G.parent[a] = b\n G.nums[b] += G.nums[a]\n G.nums[a] = 0\n\n\nfor i in range(M):\n print(ans[i])\n', 'class unionFind:\n parent = []\n nums = []\n\n def __init__(self, N):\n self.parent = [i for i in range(N)]\n self.nums = [1]*N\n\n def root(self, x):\n if self.parent[x] == x:\n return(x)\n else:\n self.parent[x] = self.root(self.parent[x])\n return(self.parent[x])\n\n def same(self, x, y):\n x, y = x-1, y-1\n return(self.root(x) == self.root(y))\n\n def unite(self, x, y):\n x, y = x-1, y-1\n x = self.root(x)\n y = self.root(y)\n if x == y:\n return\n else:\n self.parent[x] = y\n self.nums[y] += self.nums[x]\n self.nums[x] = 0\n return\n\n\nN, M = map(int, input().split())\nG = unionFind(N)\nans = [0]*M\nab = [tuple(map(int, input().split())) for i in range(M)]\n\ntmp = (N * (N-1)) // 2\nfor i, x in enumerate(reversed(ab)):\n a, b = x[0], x[1]\n ans[M-1-i] = tmp\n a = G.root(a-1)\n b = G.root(b-1)\n if a == b:\n continue\n else:\n tmp -= G.nums[a+1] * G.nums[b]\n G.parent[a+1] = b\n G.nums[b] += G.nums[a+1]\n G.nums[a+1] = 0\n\n\nfor i in range(M):\n print(ans[i])\n', 'class unionFind:\n parent = []\n count = 0\n\n def __init__(self, N):\n self.parent = [i for i in range(N)]\n self.count = N\n self.nums = [1]*N\n\n def root(self, x):\n if self.parent[x] == x:\n return(x)\n else:\n self.parent[x] = self.root(self.parent[x])\n return(self.parent[x])\n\n def same(self, x, y):\n x, y = x-1, y-1\n return(self.root(x) == self.root(y))\n\n def unite(self, x, y):\n x, y = x-1, y-1\n x = self.root(x)\n y = self.root(y)\n if x == y:\n return\n else:\n self.count -= 1\n self.parent[x] = y\n self.nums[y] += self.nums[x]\n self.nums[x] = 0\n return\n\n\nN, M = map(int, input().split())\nG = unionFind(N)\nans = [0]*M\nab = [tuple(map(int, input().split())) for i in range(M)]\n\ntmp = (N * (N-1)) // 2\nfor i, x in enumerate(reversed(ab)):\n a, b = x[0], x[1]\n ans[M-1-i] = tmp\n if G.same(a, b):\n continue\n else:\n tmp -= G.nums[a-1] * G.nums[b-1]\n G.unite(a, b)\n\n\nfor i in range(M):\n print(ans[i])\n', 'class unionFind:\n parent = []\n nums = []\n\n def __init__(self, n):\n self.parent = [i for i in range(n)]\n self.nums = [1]*n\n\n def root(self, x):\n if self.parent[x] == x:\n return(x)\n else:\n self.parent[x] = self.root(self.parent[x])\n return(self.parent[x])\n\n def same(self, x, y):\n x, y = x-1, y-1\n return(self.root(x) == self.root(y))\n\n def unite(self, x, y):\n x, y = x-1, y-1\n x = self.root(x)\n y = self.root(y)\n if x == y:\n return\n else:\n self.parent[x] = y\n self.nums[y] += self.nums[x]\n self.nums[x] = 0\n return\n\n\nN, M = map(int, input().split())\nG = unionFind(N)\nans = [0]*M\nab = [tuple(map(int, input().split())) for i in range(M)]\n\ntmp = (N * (N-1)) // 2\nfor i, x in enumerate(reversed(ab)):\n a, b = x[0], x[1]\n ans[M-1-i] = tmp\n a = G.root(a-1)\n b = G.root(b-1)\n if a == b:\n continue\n else:\n #tmp -= G.nums[a] * G.nums[b]\n G.parent[a] = b\n #G.nums[b] += G.nums[a]\n #G.nums[a] = 0\n\n\nfor i in range(M):\n print(ans[i])\n', 'import sys\nsys.setrecursionlimit(10**5)\n\n\nclass unionFind:\n parent = []\n nums = []\n\n def __init__(self, n):\n self.parent = [i for i in range(n)]\n self.nums = [1]*n\n\n def root(self, x):\n if self.parent[x] == x:\n return(x)\n else:\n self.parent[x] = self.root(self.parent[x])\n return(self.parent[x])\n\n def same(self, x, y):\n x, y = x-1, y-1\n return(self.root(x) == self.root(y))\n\n def unite(self, x, y):\n x, y = x-1, y-1\n x = self.root(x)\n y = self.root(y)\n if x == y:\n return\n else:\n self.parent[x] = y\n self.nums[y] += self.nums[x]\n self.nums[x] = 0\n return\n\n\nN, M = map(int, input().split())\nG = unionFind(N)\nans = [0]*M\nab = [tuple(map(int, input().split())) for i in range(M)]\n\ntmp = (N * (N-1)) // 2\nfor i, x in enumerate(reversed(ab)):\n a, b = x[0], x[1]\n ans[M-1-i] = tmp\n a = G.root(a-1)\n b = G.root(b-1)\n if a == b:\n continue\n else:\n tmp -= G.nums[a] * G.nums[b]\n G.parent[a] = b\n G.nums[b] += G.nums[a]\n G.nums[a] = 0\n\n\nfor i in range(M):\n print(ans[i])\n']
['Runtime Error', 'Runtime Error', 'Runtime Error', 'Runtime Error', 'Accepted']
['s419382101', 's536973691', 's971761811', 's971869935', 's221819891']
[27176.0, 25868.0, 25928.0, 23204.0, 37268.0]
[849.0, 631.0, 822.0, 635.0, 701.0]
[1214, 1166, 1124, 1161, 1200]
p03108
u815659544
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['#%%\nclass Island:\n def __init__(self,key):\n self.id = key\n self.connectedTo = set()\n\n def addNeighbor(self,nbr):\n self.connectedTo.add(nbr)\n\n def getConnections(self):\n return self.connectedTo.keys()\n\n def getId(self):\n return self.id\n\nclass Graph:\n def __init__(self):\n self.islandList = {}\n self.bridgeList = []\n self.numIslands = 0\n\n def addIsland(self,key):\n self.numIslands += 1\n newIsland = Island(key)\n self.islandList[key] = newIsland\n return newIsland\n\n def getIsland(self,key):\n if key in self.islandList:\n return self.islandList[key]\n else:\n return None\n\n def __contains__(self,n):\n return n in self.islandList\n\n def addBridge(self,f,t):\n if f not in self.islandList:\n nv = self.addIsland(f)\n if t not in self.islandList:\n nv = self.addIsland(t)\n self.islandList[f].addNeighbor(self.islandList[t])\n self.islandList[t].addNeighbor(self.islandList[f])\n self.bridgeList.insert(0,(f,t))\n\n def removeBrige(self):\n f,t = self.bridgeList.pop()\n self.islandList[f].connectedTo.remove(t)\n self.islandList[t].connectedTo.remove(f)\n return (f,t)\n\n def getIslands(self):\n return self.islandList.keys()\n\n def __iter__(self):\n return iter(self.islandList.values())\n\n#%%\ndef findPath(start, dest):\n\n visited = set()\n\n flag = search(world.islandList[start].getConnections(), visited)\n\n return flag\n\ndef search(iterable, visited):\n pass\n\n#%%\nn,m = map(int, input().split())\n\nworld = Graph()\n\nfor i in range(i,n+1):\n world.addIsland(i)\n\nfor _ in range(m):\n world.addBridge(map(int, input().split()))\n\nwhile world.bridgeList():\n \n world.removeBrige()\n\n ans = 0\n\n for start in world.islandList():\n for dest in world.islandList():\n if not start == dest:\n flag = findPath(start,dest)\n\n if not flag:\n ans += 1\n\n print(ans)\n\n\n\n\n\n', 'N, M = map(int, input().split())\nAB = []\n\nfor _ in range(M):\n a, b = map(int, input().split())\n AB.append((a-1, b-1))\n\nclass UnionFind():\n def __init__(self, n):\n \n # if parent, absolute value represents the number of nodes in the union\n # if child, value represents the parent node to it\n self.parent = [-1 for _ in range(n)]\n \n def root(self, x):\n if self.parent[x] < 0:\n return x\n\n else:\n # recursively trace a parent node until it gets to a root while updating every node so that they are directly connected to their common parent\n self.parent[x] = self.root(self.parent[x])\n return self.parent[x]\n\n def size(self, x):\n return -1 * self.parent[self.root(x)]\n \n def union(self, x, y):\n x = self.root(x)\n y = self.root(y)\n\n if x == y:\n return False\n\n else:\n size_x = - self.size(x) \n size_y = - self.size(y)\n\n # make sure y is shorter so that always shorter tree(y) is atached to longer tree(x) \n if size_x > size_y:\n x, y = y, x \n\n self.parent[x] += self.parent[y]\n self.parent[y] = x\n\n return size_x, size_y\n\n\nn_solo = N * (N - 1) // 2 \nans = [n_solo]\nuf = UnionFind(N)\n\nfor a, b in reversed(AB):\n\n united = uf.union(uf.root(a), uf.root(b))\n\n if united:\n n_solo -= united[0] * united[1]\n\n ans.append(n_solo)\n \nfor a in reversed(ans[:-1]):\n print(a)\n']
['Runtime Error', 'Accepted']
['s681267010', 's729887997']
[3188.0, 23244.0]
[18.0, 672.0]
[2078, 1547]
p03108
u820351940
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['import numpy as np\nn, m = map(int, input().split())\nab = np.array([list(map(int, input().split())) for x in range(m)]) - 1\n\nto = list(range(n))\nranks = [0] * n\nnodes = [1] * n\n\ndef find(x):\n if to[x] == x:\n return x\n else:\n root = find(to[x])\n to[x] = root\n return root\n\nentire = (n - 1) * n // 2\ndef merge(x, y):\n global entire\n xroot = find(x)\n yroot = find(y)\n # if xroot == yroot:\n # return\n if ranks[xroot] < ranks[yroot]:\n to[xroot] = yroot\n \n # nodes[yroot] += nodes[xroot]\n else:\n to[yroot] = xroot\n \n # nodes[xroot] += nodes[yroot]\n if ranks[xroot] == ranks[yroot]:\n ranks[xroot] += 1\n\n\nresults = [entire]\nfor a, b in reversed(ab[1:]):\n merge(a, b)\n# results.append(entire)\n#\n# for v in reversed(results):\n# print(v)\n', 'import numpy as np\nn, m = map(int, input().split())\nab = np.array([list(map(int, input().split())) for x in range(m)]) - 1\n\nto = list(range(n))\nranks = [0] * n\nnodes = [1] * n\n\ndef find(x):\n if to[x] == x:\n return x\n else:\n root = find(to[x])\n to[x] = root\n return root\n\nentire = (n - 1) * n // 2\ndef merge(x, y):\n global entire\n xroot = find(x)\n yroot = find(y)\n if xroot == yroot:\n return\n if ranks[xroot] < ranks[yroot]:\n to[xroot] = yroot\n entire -= nodes[yroot] * nodes[xroot]\n nodes[yroot] += nodes[xroot]\n else:\n to[yroot] = xroot\n entire -= nodes[yroot] * nodes[xroot]\n nodes[xroot] += nodes[yroot]\n if ranks[xroot] == ranks[yroot]:\n ranks[yroot] += 1\n\nprint(entire)\nfor a, b in reversed(ab[1:]):\n merge(a, b)\n print(entire)\n', 'import numpy as np\nn, m = map(int, input().split())\nab = np.array([list(map(int, input().split())) for x in range(m)]) - 1\n\nto = list(range(n))\nranks = [0] * n\nnodes = [1] * n\n\ndef find(x):\n if to[x] == x:\n return x\n else:\n root = find(to[x])\n to[x] = root\n return root\n\nentire = (n - 1) * n // 2\ndef merge(x, y):\n global entire\n xroot = find(x)\n yroot = find(y)\n if xroot == yroot:\n return\n if ranks[xroot] < ranks[yroot]:\n to[xroot] = yroot\n entire -= nodes[yroot] * nodes[xroot]\n nodes[yroot] += nodes[xroot]\n else:\n to[yroot] = xroot\n entire -= nodes[yroot] * nodes[xroot]\n nodes[xroot] += nodes[yroot]\n if ranks[xroot] == ranks[yroot]:\n ranks[yroot] += 1\n\n\n# results = [entire]\n# for a, b in reversed(ab[1:]):\n# merge(a, b)\n# results.append(entire)\n#\n# for v in reversed(results):\n# print(v)\n', 'import numpy as np\nn, m = map(int, input().split())\nab = np.array([list(map(int, input().split())) for x in range(m)]) - 1\n\nto = list(range(n))\nranks = [0] * n\nnodes = [1] * n\n\ndef find(x):\n if to[x] == x:\n return x\n else:\n root = find(to[x])\n to[x] = root\n return root\n\nentire = (n - 1) * n // 2\ndef merge(x, y):\n global entire\n xroot = find(x)\n yroot = find(y)\n if xroot == yroot:\n return\n if ranks[xroot] < ranks[yroot]:\n to[xroot] = yroot\n \n # nodes[yroot] += nodes[xroot]\n else:\n to[yroot] = xroot\n \n # nodes[xroot] += nodes[yroot]\n if ranks[xroot] == ranks[yroot]:\n ranks[yroot] += 1\n#\n#\n# results = [entire]\n# for a, b in reversed(ab[1:]):\n# merge(a, b)\n# results.append(entire)\n#\n# for v in reversed(results):\n# print(v)\n', 'import numpy as np\nn, m = map(int, input().split())\nab = np.array([list(map(int, input().split())) for x in range(m)]) - 1\n\nto = list(range(n))\nranks = [0] * n\nnodes = [1] * n\n\ndef find(x):\n if to[x] == x:\n return x\n else:\n root = find(to[x])\n to[x] = root\n return root\n\nentire = (n - 1) * n // 2\ndef merge(x, y):\n global entire\n xroot = find(x)\n yroot = find(y)\n # if xroot == yroot:\n # return\n if ranks[xroot] < ranks[yroot]:\n to[xroot] = yroot\n \n # nodes[yroot] += nodes[xroot]\n else:\n to[yroot] = xroot\n \n # nodes[xroot] += nodes[yroot]\n if ranks[xroot] == ranks[yroot]:\n ranks[yroot] += 1\n\n\nresults = [entire]\nfor a, b in reversed(ab[1:]):\n merge(a, b)\n# results.append(entire)\n#\n# for v in reversed(results):\n# print(v)\n', 'import numpy as np\nn, m = map(int, input().split())\nab = np.array([list(map(int, input().split())) for x in range(m)]) - 1\n\nto = list(range(n))\nranks = [0] * n\nnodes = [1] * n\n\ndef find(x):\n if to[x] == x:\n return x\n else:\n root = find(to[x])\n to[x] = root\n return root\n\nentire = (n - 1) * n // 2\ndef merge(x, y):\n global entire\n xroot = find(x)\n yroot = find(y)\n if xroot == yroot:\n return\n if ranks[xroot] < ranks[yroot]:\n to[xroot] = yroot\n entire -= nodes[yroot] * nodes[xroot]\n nodes[yroot] += nodes[xroot]\n else:\n to[yroot] = xroot\n entire -= nodes[yroot] * nodes[xroot]\n nodes[xroot] += nodes[yroot]\n if ranks[xroot] == ranks[yroot]:\n ranks[xroot] += 1\n\n\nresults = [entire]\nfor a, b in reversed(ab[1:]):\n merge(a, b)\n results.append(entire)\n\nfor v in reversed(results):\n print(v)\n']
['Wrong Answer', 'Runtime Error', 'Wrong Answer', 'Wrong Answer', 'Runtime Error', 'Accepted']
['s009804563', 's304152833', 's352761133', 's836477863', 's951731467', 's559167724']
[37920.0, 37900.0, 37900.0, 37900.0, 37804.0, 37904.0]
[853.0, 1110.0, 485.0, 491.0, 837.0, 1074.0]
[934, 858, 928, 938, 934, 915]
p03108
u827202523
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['def getN():\n return int(input())\n\ndef getMN():\n a = input().split()\n b = [int(i) for i in a]\n return b[0],b[1]\n\ndef getlist():\n a = input().split()\n b = [int(i) for i in a]\n return b\n\nfrom collections import Counter\nclass UnionFind:\n def __init__(self, n):\n self.par = [i for i in range(n)]\n self.rank = [0] * (n+1)\n\n \n def find(self, x):\n if self.par[x] == x:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n\n \n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if self.rank[x] < self.rank[y]:\n self.par[x] = y\n else:\n self.par[y] = x\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n\n \n def same_check(self, x, y):\n return self.find(x) == self.find(y)\n\n\n\nn,m = getMN()\n\nbridges = []\n\nfor i in range(m):\n a,b = getMN()\n bridges.append([a,b])\n\nbridges.reverse()\nprint(bridges)\nuf = UnionFind(n)\nanswers = []\nfor query in bridges:\n x,y = query\n if not uf.same_check(x-1,y-1):\n uf.union(x-1,y-1)\n print(uf.par)\n ctr = Counter()\n for p in uf.par:\n ctr[p] += 1\n ans = 0\n print(ctr.values())\n for vl in ctr.values():\n ans += vl * (n-vl)\n answers.append(ans//2)\n\nfor fa in answers[::-1]:\n print(fa)\n', 'def getN():\n return int(input())\n\ndef getMN():\n a = input().split()\n b = [int(i) for i in a]\n return b[0],b[1]\n\ndef getlist():\n a = input().split()\n b = [int(i) for i in a]\n return b\n\nfrom collections import Counter\nclass UnionFind:\n def __init__(self, n):\n self.par = [i for i in range(n)]\n self.size = [1 for i in range(n)]\n self.rank = [0] * (n+1)\n\n \n def find(self, x):\n if self.par[x] == x:\n return x\n else:\n self.par[x] = self.find(self.par[x])\n return self.par[x]\n\n \n def union(self, x, y):\n x = self.find(x)\n y = self.find(y)\n if self.rank[x] < self.rank[y]:\n self.par[x] = y\n self.size[y] += self.size[x]\n self.size[x] = 0\n else:\n self.par[y] = x\n if self.rank[x] == self.rank[y]:\n self.rank[x] += 1\n self.size[x] += self.size[y]\n self.size[y] = 0\n\n \n def same_check(self, x, y):\n return self.find(x) == self.find(y)\n\n\n\nn,m = getMN()\n\nbridges = []\n\nfor i in range(m):\n a,b = getMN()\n bridges.append([a,b])\n\nbridges.reverse()\n\nuf = UnionFind(n)\nans = n*(n-1)//2\nanswers = [ans]\nfor query in bridges:\n x,y = query\n if not uf.same_check(x-1,y-1):\n \n ans -= uf.size[uf.find(x-1)]*uf.size[uf.find(y-1)]\n \n uf.union(x-1,y-1)\n\n answers.append(ans)\n\nfor fa in answers[:-1][::-1]:\n print(fa)\n']
['Wrong Answer', 'Accepted']
['s101741999', 's833990119']
[58644.0, 29736.0]
[2105.0, 845.0]
[1428, 1565]
p03108
u837286475
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["\n\n\nclass dset:\n def __init__(self,n):\n self.par = [-1]*n\n\n def size(self,n):\n return -self.par[self.root(n)]\n\n def root(self,n):\n return self.par[n] = root(par[n]) if self.par[n]>=0 else n\n\n def same(self, a, b):\n return self.root(a) == self.root(b)\n\n def __link__(self,a,b):\n if a == b:\n return \n \n if self.size(a) < self.size(b):\n a,b = b,a\n \n self.par[a] += self.par[b]\n self.par[b] = a\n\n def unite(self,a,b):\n self.__link__(\n self.root(a)\n ,self.root(b)\n )\n\n\ndef main():\n n, m = map(int, input().split() ) \n\n\n ins = [ list( map(int,input().split()) ) for i in range(m) ]\n\n ds = dset(n)\n\n ans = [0]*m\n ans[-1] = n*(n-1)//2\n for i in range(m-1,0,-1):\n ans[i-1] = ans[i]\n\n a,b = ins[i]\n a-=1\n b-=1\n\n# print(ins[i])\n # print( (ds.root(a),ds.root(b)) ) \n if not ds.same(a,b):\n x = ds.size(a)\n y = ds.size(b)\n #print((x,y))\n ans[i-1] -= x*y\n ds.unite(a,b)\n\n for e in ans:\n print(e)\n\nif __name__ == '__main__':\n main()", "\n\n\nclass dset:\n def __init__(self,n):\n self.par = [-1]*n\n\n def size(self,n):\n return -self.par[self.root(n)]\n\n def root(self,n):\n if self.par[n]<0:\n return n\n else:\n self.par[n] = self.root(self.par[n])\n return self.par[n]\n\n def same(self, a, b):\n return self.root(a) == self.root(b)\n\n def __link__(self,a,b):\n if a == b:\n return \n \n if self.size(a) < self.size(b):\n a,b = b,a\n \n self.par[a] += self.par[b]\n self.par[b] = a\n\n def unite(self,a,b):\n self.__link__(\n self.root(a)\n ,self.root(b)\n )\n\n\ndef main():\n n, m = map(int, input().split() ) \n\n\n ins = [ list( map(int,input().split()) ) for i in range(m) ]\n\n ds = dset(n)\n\n ans = [0]*m\n ans[-1] = n*(n-1)//2\n for i in range(m-1,0,-1):\n ans[i-1] = ans[i]\n\n a,b = ins[i]\n a-=1\n b-=1\n\n# print(ins[i])\n # print( (ds.root(a),ds.root(b)) ) \n if not ds.same(a,b):\n x = ds.size(a)\n y = ds.size(b)\n #print((x,y))\n ans[i-1] -= x*y\n ds.unite(a,b)\n\n for e in ans:\n print(e)\n\nif __name__ == '__main__':\n main()"]
['Runtime Error', 'Accepted']
['s542399408', 's910309600']
[2940.0, 34612.0]
[18.0, 817.0]
[1200, 1274]
p03108
u844005364
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['n, m = map(int, input().split())\n\na = []\n\nfor i in range(m):\n a.append(list(map(int, input().split())))\n\nfor i in range(m):\n b = [j for j in range(n)]\n\n aa = a[i+1:]\n aa.sort()\n\n for x, y in aa:\n b[x-1] = min(b[x-1], b[y-1])\n b[y-1] = min(b[x-1], b[y-1])\n\n sum = 0\n print(b)\n\n for j in range(n):\n for k in range(j+1, n):\n sum = sum + b.count(j) * b.count(k)\n print(sum)', 'N, M = map(int, input().split())\nAi = [list(map(int, input().split())) for _ in range(M)]\n \ngroups = [-1 for i in range(N + 1)] \nsizes = [1 for _ in range(N + 1)]\nAi.reverse()\n \nvalue = N * (N - 1) // 2\nanswers = []\nanswers.append(value)\n \ndef parent(n):\n if groups[n] < 0:\n return n\n return parent(groups[n])\n \nfor a in Ai:\n if value <= 0:\n answers.append(0)\n continue\n p1 = parent(a[0])\n p2 = parent(a[1])\n if p1 == p2:\n answers.append(value)\n continue\n \n value -= sizes[p1] * sizes[p2]\n if p1 > p2:\n sizes[p2] += sizes[p1]\n groups[p1] = p2\n else:\n sizes[p1] += sizes[p2]\n groups[p2] = p1\n answers.append(value)\n \nanswers.reverse()\nanswers = answers[1:]\nfor a in answers:\n print(a)']
['Wrong Answer', 'Accepted']
['s708373633', 's958252586']
[34584.0, 34220.0]
[2108.0, 752.0]
[428, 802]
p03108
u844789719
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["N, M = [int(_) for _ in input().split()]\nAB = [[int(_) for _ in input().split()] for _ in range(M)]\n\nUF = list(range(N + 1))\nSIZE = [0] + [1] * N # 1-indexed\n\n\ndef find(x):\n if UF[x] != x:\n UF[x] = find(UF[x])\n return UF[x]\n\n\ndef unite(x, y):\n if not is_same(x, y):\n X, Y = find(x), find(y)\n SX, SY = SIZE[X], SIZE[Y]\n if SX > SY:\n m = UF[X] = Y\n else:\n m = UF[Y] = X\n SIZE[m] = SX + SY\n SIZE[X + Y - m] = 0\n\n\ndef is_same(x, y):\n return find(x) == find(y)\n\n\ndef scan_uf():\n for i in range(len(UF)):\n find(i)\n\n\nans = [N * (N - 1) // 2]\nfor a, b in AB[::-1]:\n sA = SIZE[find(a)]\n sB = SIZE[find(b)]\n unite(a, b)\n new = SIZE[find(a)]\n ans += [\n ans[-1] - new * (new - 1) // 2 + sA * (sA - 1) // 2 +\n sB * (sB - 1) // 2\n ]\nprint(*ans[-2::-1], sep='\\n')\n", "N, M = [int(_) for _ in input().split()]\nAB = [[int(_) for _ in input().split()] for _ in range(M)]\n\n\nclass UnionFind():\n def __init__(self, SIZE):\n # 1-indexed\n self.root = list(range(N + 1))\n self.size = [0] + [1] * SIZE\n\n def find(self, x):\n if self.root[x] != x:\n self.root[x] = self.find(self.root[x])\n return self.root[x]\n\n def unite(self, x, y):\n X, Y = self.find(x), self.find(y)\n SX, SY = self.size[X], self.size[Y]\n if SX > SY:\n m = X\n else:\n m = Y\n self.size[m] = SX + SY\n #self.size[X + Y - m] = 0\n self.root[X + Y - m] = m\n\n def is_same(self, x, y):\n return self.find(x) == self.find(y)\n\n def scan(self):\n for i in self.root:\n self.find(i)\n\n\nuf = UnionFind(N)\nans = [N * (N - 1) // 2]\nfor a, b in AB[::-1]:\n if not uf.is_same(a, b):\n ans += [ans[-1] - uf.size[uf.find(a)] * uf.size[uf.find(b)]]\n uf.unite(a, b)\n else:\n ans += [ans[-1]]\nprint(*ans[-2::-1], sep='\\n')\n"]
['Runtime Error', 'Accepted']
['s897137683', 's505276609']
[34028.0, 31052.0]
[829.0, 784.0]
[875, 1063]
p03108
u845333844
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['import collections\n\nn,m=map(int,input().split())\nll=[]\nfor i in range(m):\n a,b=map(int,input().split())\n ll.append([a,b])\nll.reverse()\n \nmax=n*(n-1)//2\nans=[max]\n\nfor x in range(m):\n edge=ll[:x+1]\n for i in edge:\n l = list(range(n))\n for j in edge:\n if j != i:\n l=[l[j[0]-1] if l[k]==l[j[1]-1] else l[k] for k in range(n)]\n y=collections.Counter(l)\n c=y.most_common()\n z=len(set(l))\n zz=z*(z-1)//2\n print(zz)', 'N,M=map(int,input().split())\nl=[]\nfor i in range(M):\n x,y=map(int,input().split())\n l.append([x,y])\n\ndef comb(n):\n return n*(n+1)//2\n\nclass UnionFind:\n def __init__(self,N):\n self.parent = [i for i in range(N)]\n self.rank = [0] * N\n self.count = 0\n self.size = [1] * N\n self.connection = 0\n def root(self,a):\n if self.parent[a] == a:\n return a\n else:\n self.parent[a] = self.root(self.parent[a])\n return self.parent[a]\n def is_same(self,a,b):\n return self.root(a) == self.root(b)\n def unite(self,a,b):\n ra = self.root(a)\n rb = self.root(b)\n if ra == rb: return\n self.connection -= (comb(self.size[ra]) + comb(self.size[rb]))\n self.connection += comb(self.size[ra] + self.size[rb])\n if self.rank[ra] < self.rank[rb]:\n self.size[rb] += self.size[ra]\n self.parent[ra] = rb\n else:\n self.size[ra] += self.size[rb]\n self.parent[rb] = ra\n if self.rank[ra] == self.rank[rb]: self.rank[ra] += 1\n self.count += 1\n\nuf = UnionFind(N)\nans = [N*(N-1)//2]\n\nl.reverse()\nfor a,b in l:\n a,b = a-1,b-1\n uf.unite(a,b)\n ans.append(ans[0] - uf.connection)\n\nans.reverse()\nfor i in range(1,M+1):\n print(ans[i])']
['Wrong Answer', 'Accepted']
['s399546896', 's956397488']
[62316.0, 28620.0]
[2107.0, 745.0]
[450, 1318]
p03108
u845620905
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['def size(A):\n return -1 * par[root(A)]\n\ndef root(A):\n if(par[A] < 0):\n return A\n else:\n par[A] = root(par[A])\n return par[A]\n \ndef connect(A, B):\n A = root(A)\n B = root(B)\n if(A == B):\n return \n if(size(A) < size(B)):\n A, B = B, A\n par[A] += par[B]\n par[B] = A\n \n \nn, m = map(int, input().split())\na = []\nb = []\nans = []\nfor i in range(m):\n ai, bi = map(int, input().split())\n a.append(ai-1)\n b.append(bi-1)\n ans.append(0)\n\na.reverse()\nb.reverse()\npar = []\nfor i in range(n):\n par.append(-1)\n \nans[0] = n * (n-1) / 2\nfor i in range(1, m):\n ans[i] = ans[i-1]\n if(root(a[i]) != root(b[i])):\n ans[i] -= size(a[i]) * size(b[i])\n connect(a[i], b[i])\nans.reverse()\nfor i in range(m):\n print(int(ans[i]))', 'class Union():\n def __init__(self, N):\n self.Parent = []\n for i in range(N):\n self.Parent.append(-1)\n\n def root(self, A):\n if(self.Parent[A] < 0):\n return A\n else:\n self.Parent[A] = self.root(self.Parent[A])\n return self.Parent[A]\n\n def size(self, A):\n return int(-1 * self.Parent[self.root(A)])\n\n def connect(self, A, B):\n A = self.root(A)\n B = self.root(B)\n if (A == B):\n return False\n\n if(self.size(A) < self.size(B)):\n A, B = B, A\n self.Parent[A] += self.Parent[B]\n self.Parent[B] = A\n return True\n\n\nN, M = map(int, input().split())\nA = [-1 for _ in range(M)]\nB = [-1 for _ in range(M)]\n\nfor i in range(M):\n a, b = map(int, input().split())\n A[i], B[i] = a - 1, b - 1\n\nans = [0 for _ in range(M)]\nans[M-1] = N * (N-1) / 2\n\nuni = Union(N)\nfor i in range(M-1, 0, -1):\n ans[i - 1] = ans[i]\n if(uni.root(A[i]) != uni.root(B[i])):\n ans[i - 1] -= uni.size(A[i]) * uni.size(B[i])\n uni.connect(A[i], B[i]);\n\nfor i in range(M):\n print(int(ans[i]))\n']
['Wrong Answer', 'Accepted']
['s086989698', 's025815013']
[16236.0, 16176.0]
[786.0, 908.0]
[740, 1134]
p03108
u859897687
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
['n,nn=map(int,input().split())\nl=[list(map(int,input().split()))for i in range(nn)]\nl.reverse()\nm=[[1,i] for i in range(n)]\nans=[]\nans.append(n*(n-1)//2)\nfor a,b in l:\n #print(m)\n a-=1\n b-=1\n if m[a][1]==m[b][1]:\n ans.append(ans[len(ans)-1])\n continue\n #m[a][1]!=m[b][1] \n a=m[a][1]\n b=m[b][1]\n ans.append(ans[len(ans)-1]-m[a][0]*m[b][0])\n if m[a][0]<m[b][0]:\n a,b=b,a\n #a>=b\n m[a][0]+=m[b][0]\n m[b][0]=0\n k=m[b][1]\n for im in m:\n if im[1]==k:\n im[1]=a\nans.reverse()\nfor a in ans:\n print(a)', 'n,nn=map(int,input().split())\nl=[list(map(int,input().split()))for i in range(nn)]\nl.reverse()\nm=[[1,i] for i in range(n)]\nans=[]\nans.append(n*(n-1)//2)\nfor a,b in l: #10^5\n #print(m)\n a-=1\n b-=1\n while a!=m[a][1]:\n a=m[a][1]\n while b!=m[b][1]:\n b=m[b][1]\n if a==b:\n ans.append(ans[len(ans)-1])\n continue\n #a!=b\n ans.append(ans[len(ans)-1]-m[a][0]*m[b][0])\n if a<b:\n a,b=b,a\n #a>=b\n m[a][0]+=m[b][0]\n m[b][0]=0\n m[b][1]=a\nans.reverse()\nfor a in ans[1:]:\n print(a)']
['Wrong Answer', 'Accepted']
['s599992091', 's657958952']
[37888.0, 42428.0]
[2106.0, 892.0]
[522, 493]
p03108
u861466636
2,000
1,048,576
There are N islands and M bridges. The i-th bridge connects the A_i-th and B_i-th islands bidirectionally. Initially, we can travel between any two islands using some of these bridges. However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge. Let the **inconvenience** be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining. For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.
["N, M = map(int, input().split())\nab = [tuple(map(int, input().split())) for _ in range(M)]\n\nclass UnionFind:\n def __init__(self, size):\n self.rank = [-1]*(size+1)\n self.number = [1]*(size+1)\n \n def find(self, x):\n while self.rank[x] >= 0:\n x = self.rank[x]\n return x\n \n def union(self, a, b):\n if a == b:\n return\n if self.rank[a] == self.rank[b]:\n self.rank[b] = a\n self.rank[a] -= 1\n self.number[a] += self.number[b]\n elif self.rank[a] < self.rank[b]:\n self.rank[b] = a\n self.number[a] += self.number[b]\n else:\n self.rank[a] = b\n self.number[b] += sekf.number[a]\n \np = N * (N - 1) // 2\nans = [p]\nuf = UnionFind(N)\n\nfor a, b in ab[::-1]:\n ar = uf.find(a)\n br = uf.find(b)\n \n if ar != br:\n p -= uf.number[ar] * uf.number[br]\n uf.union(ar, br)\n ans.append(p)\n\nprint(*ans[::-1], sep='\\n')", "import sys\nreadline = sys.stdin.readline\n\nN, M = map(int, input().split())\nab = [tuple(map(int, readline().split())) for _ in range(M)]\n\nclass UnionFind:\n def __init__(self, size):\n self.rank = [-1]*(size+1)\n self.number = [1]*(size+1)\n \n def find(self, x):\n while self.rank[x] >= 0:\n x = self.rank[x]\n return x\n \n def union(self, a, b):\n if a == b:\n return\n if self.rank[a] == self.rank[b]:\n self.rank[b] = a\n self.rank[a] -= 1\n self.number[a] += self.number[b]\n elif self.rank[a] < self.rank[b]:\n self.rank[b] = a\n self.number[a] += self.number[b]\n else:\n self.rank[a] = b\n self.number[b] += self.number[a]\n \np = N * (N - 1) // 2\nans = [p]\nuf = UnionFind(N)\n\nfor a, b in ab[1:][::-1]:\n ar = uf.find(a)\n br = uf.find(b)\n \n if ar != br:\n p -= uf.number[ar] * uf.number[br]\n uf.union(ar, br)\n ans.append(p)\n\nprint(*ans[::-1][1:], sep='\\n')\n", "N, M = map(int, input().split())\nab = [tuple(map(int, input().split())) for _ in range(M)]\n\nclass UnionFind:\n def __init__(self, size):\n self.rank = [-1]*(size+1)\n self.number = [1]*(size+1)\n \n def find(self, x):\n while self.rank[x] >= 0:\n x = self.rank[x]\n return x\n \n def union(self, a, b):\n if a == b:\n return\n if self.rank[a] == self.rank[b]:\n self.rank[b] = a\n self.rank[a] -= 1\n self.number[a] += self.number[b]\n elif self.rank[a] < self.rank[b]:\n self.rank[b] = a\n self.number[a] += self.number[b]\n else:\n self.rank[a] = b\n self.number[b] += self.number[a]\n \np = N * (N - 1) // 2\nans = [p]\nuf = UnionFind(N)\n\nfor a, b in ab[::-1]:\n ar = uf.find(a)\n br = uf.find(b)\n \n if ar != br:\n p -= uf.number[ar] * uf.number[br]\n uf.union(ar, br)\n ans.append(p)\n\nprint(*ans[::-1], sep='\\n')\n", "import sys\nreadline = sys.stdin.readline\n\nN, M = map(int, input().split())\nab = [tuple(map(int, readline().split())) for _ in range(M)]\n\nclass UnionFind:\n def __init__(self, size):\n self.rank = [-1]*(size+1)\n self.number = [1]*(size+1)\n \n def find(self, x):\n while self.rank[x] >= 0:\n x = self.rank[x]\n return x\n \n def union(self, a, b):\n if a == b:\n return\n if self.rank[a] == self.rank[b]:\n self.rank[b] = a\n self.rank[a] -= 1\n self.number[a] += self.number[b]\n elif self.rank[a] < self.rank[b]:\n self.rank[b] = a\n self.number[a] += self.number[b]\n else:\n self.rank[a] = b\n self.number[b] += self.number[a]\n \np = N * (N - 1) // 2\nans = [p]\nuf = UnionFind(N)\n\nfor a, b in ab[1:][::-1]:\n ar = uf.find(a)\n br = uf.find(b)\n \n if ar != br:\n p -= uf.number[ar] * uf.number[br]\n uf.union(ar, br)\n ans.append(p)\n\nprint(*ans[::-1], sep='\\n')"]
['Runtime Error', 'Wrong Answer', 'Wrong Answer', 'Accepted']
['s038302954', 's182634246', 's713316592', 's247882702']
[18948.0, 26068.0, 25504.0, 26068.0]
[322.0, 402.0, 542.0, 407.0]
[999, 1053, 1000, 1048]