message stringlengths 2 49.9k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 446 108k | cluster float64 13 13 | __index_level_0__ int64 892 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges. Vertices are numbered 1 through n.
Limak is a little polar bear and Radewoosh is his evil enemy. Limak once had a tree but Radewoosh stolen it. Bear is very sad now because he doesn't remember much about the tree — he can tell you only three values n, d and h:
* The tree had exactly n vertices.
* The tree had diameter d. In other words, d was the biggest distance between two vertices.
* Limak also remembers that he once rooted the tree in vertex 1 and after that its height was h. In other words, h was the biggest distance between vertex 1 and some other vertex.
The distance between two vertices of the tree is the number of edges on the simple path between them.
Help Limak to restore his tree. Check whether there exists a tree satisfying the given conditions. Find any such tree and print its edges in any order. It's also possible that Limak made a mistake and there is no suitable tree – in this case print "-1".
Input
The first line contains three integers n, d and h (2 ≤ n ≤ 100 000, 1 ≤ h ≤ d ≤ n - 1) — the number of vertices, diameter, and height after rooting in vertex 1, respectively.
Output
If there is no tree matching what Limak remembers, print the only line with "-1" (without the quotes).
Otherwise, describe any tree matching Limak's description. Print n - 1 lines, each with two space-separated integers – indices of vertices connected by an edge. If there are many valid trees, print any of them. You can print edges in any order.
Examples
Input
5 3 2
Output
1 2
1 3
3 4
3 5
Input
8 5 2
Output
-1
Input
8 4 2
Output
4 8
5 7
2 3
8 1
2 1
5 6
1 5
Note
Below you can see trees printed to the output in the first sample and the third sample.
<image>
Submitted Solution:
```
import math
n, d, h = list(map(int, input().split()))
if (h < math.ceil(d / 2)) or ((n > 2) and (d < 2)):
print(-1)
else:
for i in range(h):
print(i + 1, i + 2)
if (d - h > 0):
print(1, h + 2)
for i in range(h + 2, d + 1):
print(i, i + 1)
#alr = d + 1
for i in range(d + 2, n + 1):
print(1, i)
``` | instruction | 0 | 83,022 | 13 | 166,044 |
No | output | 1 | 83,022 | 13 | 166,045 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges. Vertices are numbered 1 through n.
Limak is a little polar bear and Radewoosh is his evil enemy. Limak once had a tree but Radewoosh stolen it. Bear is very sad now because he doesn't remember much about the tree — he can tell you only three values n, d and h:
* The tree had exactly n vertices.
* The tree had diameter d. In other words, d was the biggest distance between two vertices.
* Limak also remembers that he once rooted the tree in vertex 1 and after that its height was h. In other words, h was the biggest distance between vertex 1 and some other vertex.
The distance between two vertices of the tree is the number of edges on the simple path between them.
Help Limak to restore his tree. Check whether there exists a tree satisfying the given conditions. Find any such tree and print its edges in any order. It's also possible that Limak made a mistake and there is no suitable tree – in this case print "-1".
Input
The first line contains three integers n, d and h (2 ≤ n ≤ 100 000, 1 ≤ h ≤ d ≤ n - 1) — the number of vertices, diameter, and height after rooting in vertex 1, respectively.
Output
If there is no tree matching what Limak remembers, print the only line with "-1" (without the quotes).
Otherwise, describe any tree matching Limak's description. Print n - 1 lines, each with two space-separated integers – indices of vertices connected by an edge. If there are many valid trees, print any of them. You can print edges in any order.
Examples
Input
5 3 2
Output
1 2
1 3
3 4
3 5
Input
8 5 2
Output
-1
Input
8 4 2
Output
4 8
5 7
2 3
8 1
2 1
5 6
1 5
Note
Below you can see trees printed to the output in the first sample and the third sample.
<image>
Submitted Solution:
```
n, d, h = map(int, input().split())
if d > 2*h:
print(-1)
elif d == h:
for i in range(1, d+1):
print(i, i+1)
for i in range(d+2, n+1):
print(2, i)
else:
for i in range(2, h+2):
print(i-1, i)
print(1, h+2)
for i in range(h+3, d+2):
print(i-1, i)
for i in range(d+2, n+1):
print(1, i)
``` | instruction | 0 | 83,023 | 13 | 166,046 |
No | output | 1 | 83,023 | 13 | 166,047 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges. Vertices are numbered 1 through n.
Limak is a little polar bear and Radewoosh is his evil enemy. Limak once had a tree but Radewoosh stolen it. Bear is very sad now because he doesn't remember much about the tree — he can tell you only three values n, d and h:
* The tree had exactly n vertices.
* The tree had diameter d. In other words, d was the biggest distance between two vertices.
* Limak also remembers that he once rooted the tree in vertex 1 and after that its height was h. In other words, h was the biggest distance between vertex 1 and some other vertex.
The distance between two vertices of the tree is the number of edges on the simple path between them.
Help Limak to restore his tree. Check whether there exists a tree satisfying the given conditions. Find any such tree and print its edges in any order. It's also possible that Limak made a mistake and there is no suitable tree – in this case print "-1".
Input
The first line contains three integers n, d and h (2 ≤ n ≤ 100 000, 1 ≤ h ≤ d ≤ n - 1) — the number of vertices, diameter, and height after rooting in vertex 1, respectively.
Output
If there is no tree matching what Limak remembers, print the only line with "-1" (without the quotes).
Otherwise, describe any tree matching Limak's description. Print n - 1 lines, each with two space-separated integers – indices of vertices connected by an edge. If there are many valid trees, print any of them. You can print edges in any order.
Examples
Input
5 3 2
Output
1 2
1 3
3 4
3 5
Input
8 5 2
Output
-1
Input
8 4 2
Output
4 8
5 7
2 3
8 1
2 1
5 6
1 5
Note
Below you can see trees printed to the output in the first sample and the third sample.
<image>
Submitted Solution:
```
n, d, h = map(int, input().split())
if 2*h < d or d == h and h != n-1:
print(-1)
exit()
ans = []
for i in range(2, d+2):
if i == h+2:
print(1, i)
continue
print(i-1, i)
for i in range(d+2, n+1):
print(1, i)
``` | instruction | 0 | 83,024 | 13 | 166,048 |
No | output | 1 | 83,024 | 13 | 166,049 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A tree is a connected undirected graph consisting of n vertices and n - 1 edges. Vertices are numbered 1 through n.
Limak is a little polar bear and Radewoosh is his evil enemy. Limak once had a tree but Radewoosh stolen it. Bear is very sad now because he doesn't remember much about the tree — he can tell you only three values n, d and h:
* The tree had exactly n vertices.
* The tree had diameter d. In other words, d was the biggest distance between two vertices.
* Limak also remembers that he once rooted the tree in vertex 1 and after that its height was h. In other words, h was the biggest distance between vertex 1 and some other vertex.
The distance between two vertices of the tree is the number of edges on the simple path between them.
Help Limak to restore his tree. Check whether there exists a tree satisfying the given conditions. Find any such tree and print its edges in any order. It's also possible that Limak made a mistake and there is no suitable tree – in this case print "-1".
Input
The first line contains three integers n, d and h (2 ≤ n ≤ 100 000, 1 ≤ h ≤ d ≤ n - 1) — the number of vertices, diameter, and height after rooting in vertex 1, respectively.
Output
If there is no tree matching what Limak remembers, print the only line with "-1" (without the quotes).
Otherwise, describe any tree matching Limak's description. Print n - 1 lines, each with two space-separated integers – indices of vertices connected by an edge. If there are many valid trees, print any of them. You can print edges in any order.
Examples
Input
5 3 2
Output
1 2
1 3
3 4
3 5
Input
8 5 2
Output
-1
Input
8 4 2
Output
4 8
5 7
2 3
8 1
2 1
5 6
1 5
Note
Below you can see trees printed to the output in the first sample and the third sample.
<image>
Submitted Solution:
```
def num(s):
return [int(i) for i in s.split(' ')]
s = num(input())
n,d,h = s[0],s[1],s[2]
if ((n>d+1)and(h+1<=d<=2*h)):
for i in range(h):
print(i+1,i+2)
for k in range((n-1-h)//(d-h)):
for i in range(d-h):
if i==0:
print(1, h+1 +k*(d-h) + 1)
else:
print(h+1 +k*(d-h) + i, h+1 +k*(d-h) + i+1)
for j in range((n-1-h)%(d-h)):
print(1, n-j)
elif((n==d+1) and(d==h)):
for i in range(d):
print(i+1, i+2)
else:
print(-1)
``` | instruction | 0 | 83,025 | 13 | 166,050 |
No | output | 1 | 83,025 | 13 | 166,051 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i. Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.
For each k=1, 2, ..., N, solve the following problem:
* Find the number of simple paths that visit a vertex painted in the color k one or more times.
Note: The simple paths from Vertex u to v and from v to u are not distinguished.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq c_i \leq N
* 1 \leq a_i,b_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}
Output
Print the answers for k = 1, 2, ..., N in order, each in its own line.
Examples
Input
3
1 2 1
1 2
2 3
Output
5
4
0
Input
1
1
Output
1
Input
2
1 2
1 2
Output
2
2
Input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
Output
5
8
10
5
5
Input
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
Output
18
15
0
14
23
0
23
0 | instruction | 0 | 83,156 | 13 | 166,312 |
"Correct Solution:
```
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
from collections import defaultdict
INF = float("inf")
MOD = 10**9+7
class Graph(object):
def __init__(self, N):
self.N = N
self.E = defaultdict(list)
def add_edge(self, f, t, w=1):
self.E[f].append((t, w))
self.E[t].append((f, w))
N = int(input())
c = [x-1 for x in map(int, input().split())]
A = [None]*(N-1)
B = [None]*(N-1)
for i in range(N-1):
A[i], B[i] = map(int, input().split())
g = Graph(N)
for a, b in zip(A, B):
g.add_edge(a-1, b-1)
# k=1, 2, ..., Nに対して
# 色kが塗られている頂点を一度以上通るような単純パスの数を求める
# 全パスの個数 - 色kが塗られている頂点を一度も通らない単純パスの数
# 全パスの個数はN*(N+1)/2
# グラフを色kのノードで分割して、部分グラフ内での単純パスの総数を求めれば良い
# 各ノードに状態として辞書をもたせる。
# x辞書は色iを通らずに到達可能な頂点の数を持つ。
# o辞書は色iを通らずに到達不可能な頂点の数を持つ。
# 回答用
ans = [0]*N
def f(curr, par=-1):
# 再帰関数
# curr: 現在の節点
# par : 親節点の番号
ret = defaultdict(int)
size = 1
for dest, w in g.E[curr]:
if dest == par:
continue
sz, child = f(dest, curr)
size += sz
# 自身の色と同じ場合、子の頂点の数から加算
n = sz-child[c[curr]]
ans[c[curr]] += n*(n+1)//2
# マージ
if len(ret) < len(child):
child, ret = ret, child
for key in child:
ret[key] += child[key]
ret[c[curr]] = size
return size, ret
sz, ret = f(0)
for color in range(N):
if color != c[0]:
n = sz-ret[color]
ans[color] += n*(n+1)//2
tot = N*(N+1)//2
for a in ans:
print(tot-a)
``` | output | 1 | 83,156 | 13 | 166,313 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i. Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.
For each k=1, 2, ..., N, solve the following problem:
* Find the number of simple paths that visit a vertex painted in the color k one or more times.
Note: The simple paths from Vertex u to v and from v to u are not distinguished.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq c_i \leq N
* 1 \leq a_i,b_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}
Output
Print the answers for k = 1, 2, ..., N in order, each in its own line.
Examples
Input
3
1 2 1
1 2
2 3
Output
5
4
0
Input
1
1
Output
1
Input
2
1 2
1 2
Output
2
2
Input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
Output
5
8
10
5
5
Input
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
Output
18
15
0
14
23
0
23
0 | instruction | 0 | 83,157 | 13 | 166,314 |
"Correct Solution:
```
import sys
from collections import defaultdict
# 再帰制限を緩和するおまじない
sys.setrecursionlimit(10**6)
def sum(n):return n*(n+1)//2
# 部分木のサイズと、「色iを封鎖したときに到達できない頂点の個数」を持つ辞書を返す
def dfs(v,p):
ret=defaultdict(int)
size=1
for vv in g[v]:
if vv==p:
continue
ss,d=dfs(vv,v)
size+=ss
ans[c[v]]+=sum(ss-d[c[v]])
# マージテク
if len(ret)<len(d):
ret,d=d,ret
for vvv in d:
ret[vvv]+=d[vvv]
ret[c[v]]=size
return size,ret
n=int(input())
c=list(map(int,input().split()))
g=[[] for _ in range(n+1)]
ans=[0]*(n+1)
for _ in range(n-1):
s,t=map(int, input().split())
s-=1
t-=1
g[s].append(t)
g[t].append(s)
_,ret=dfs(0,-1)
for i in range(1,n+1):
print(sum(n)-ans[i]-sum(n-ret[i]))
``` | output | 1 | 83,157 | 13 | 166,315 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i. Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.
For each k=1, 2, ..., N, solve the following problem:
* Find the number of simple paths that visit a vertex painted in the color k one or more times.
Note: The simple paths from Vertex u to v and from v to u are not distinguished.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq c_i \leq N
* 1 \leq a_i,b_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}
Output
Print the answers for k = 1, 2, ..., N in order, each in its own line.
Examples
Input
3
1 2 1
1 2
2 3
Output
5
4
0
Input
1
1
Output
1
Input
2
1 2
1 2
Output
2
2
Input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
Output
5
8
10
5
5
Input
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
Output
18
15
0
14
23
0
23
0 | instruction | 0 | 83,158 | 13 | 166,316 |
"Correct Solution:
```
import sys
n = int(input())
c = list(map(int, input().split()))
c = [x-1 for x in c]
# print(c)
adj = [[] for i in range(n)]
for i in range(n-1):
a, b = tuple(map(int, input().split()))
a -= 1
b -= 1
adj[a].append(b)
adj[b].append(a)
size = [1 for i in range(n)]
stack = [[] for color in range(n)]
in_time = [-1 for i in range(n)]
out_time = [-1 for i in range(n)]
timer = 0
total = n*(n+1)//2
ans = [total for color in range(n)]
# print(ans)
def dfs(parent, root):
global timer
in_time[root] = timer
timer += 1
color = c[root]
s = stack[color]
for child in adj[root]:
if parent == child:
continue
dfs(root, child)
size[root] += size[child]
cnt = size[child]
while s:
x = s[-1]
if in_time[x] > in_time[root] and out_time[x] != -1:
cnt -= size[x]
s.pop()
else:
break
ans[color] -= cnt*(cnt+1)//2
out_time[root] = timer
timer += 1
stack[c[root]].append(root)
sys.setrecursionlimit(10**6)
dfs(0, 0)
# print(size)
for color in range(n):
cnt = n
while len(stack[color]) > 0:
x = stack[color][-1]
cnt -= size[x]
stack[color].pop()
# print("node:", -1, "color:", color, "cnt:", cnt)
ans[color] -= cnt*(cnt+1)//2
print(ans[color])
``` | output | 1 | 83,158 | 13 | 166,317 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i. Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.
For each k=1, 2, ..., N, solve the following problem:
* Find the number of simple paths that visit a vertex painted in the color k one or more times.
Note: The simple paths from Vertex u to v and from v to u are not distinguished.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq c_i \leq N
* 1 \leq a_i,b_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}
Output
Print the answers for k = 1, 2, ..., N in order, each in its own line.
Examples
Input
3
1 2 1
1 2
2 3
Output
5
4
0
Input
1
1
Output
1
Input
2
1 2
1 2
Output
2
2
Input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
Output
5
8
10
5
5
Input
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
Output
18
15
0
14
23
0
23
0 | instruction | 0 | 83,159 | 13 | 166,318 |
"Correct Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**7)
from pprint import pprint as pp
from pprint import pformat as pf
# @pysnooper.snoop()
#import pysnooper # debug
import math
#from sortedcontainers import SortedList, SortedDict, SortedSet # no in atcoder
import bisect
class BIT: # binary indexed tree
"""
http://hos.ac/slides/20140319_bit.pdf
"""
NORMAL = 0
ROLLBACK = 1
def __init__(self, size):
self.size = size
self.data = [0] * (size + 1) # var[0] is dummy
self.status = BIT.NORMAL
self.history = []
def add(self, pos, val):
assert pos > 0, pos
if self.status == BIT.NORMAL:
self.history.append((pos, val))
k = pos
while k <= self.size:
self.data[k] += val
# for next
k += k & -k
def sum(self, pos):
s = 0
k = pos
while k > 0:
s += self.data[k]
# for next
k -= k & -k
return s
def sum_section(self, frm, to):
to_val = self.sum(to)
frm_val = self.sum(frm)
return to_val - frm_val
def save(self):
self.history = []
def rollback(self):
self.status = BIT.ROLLBACK
for (pos, val) in self.history:
self.add(pos, -1 * val)
self.history = []
self.status = BIT.NORMAL
class Graph:
def __init__(self, size):
self.size = size
self.vertices = [0] * size # var[0] is dummy
self.edges = [None] * size # var[0] is dummy
for i in range(size):
self.edges[i] = []
def add_edge(self, frm, to):
self.edges[frm].append(to)
self.edges[to].append(frm)
def make_mybit(size):
bit = BIT(size)
for i in range(size):
bit.add(i + 1, 1)
bit.save()
return bit
def init_color_s(size):
color_s = [None] * graph.size
for i in range(graph.size):
color_s[i] = []
return color_s
def dfs(graph):
parent_s = [0] * graph.size
in_s = [0] * graph.size
out_s = [0] * graph.size
order = 0 # starts from 1
color_s = init_color_s(graph.size)
def _dfs(v, prev):
nonlocal order
order += 1
parent_s[v] = prev
in_s[v] = order
c = graph.vertices[v]
color_s[c].append(v)
for to in graph.edges[v]:
if to == prev:
continue
_dfs(to, v)
out_s[v] = order
_dfs(0, -1)
return parent_s, in_s, out_s, color_s
def calc(v):
return v * (v + 1) // 2
def get_connected(bit, v, in_s, out_s):
#print('v') # debug
#print(v) # debug
large_key = out_s[v]
small_key = in_s[v] - 1
return bit.sum_section(small_key, large_key)
def calc_for(bit, v, in_s, out_s):
connected = get_connected(bit, v, in_s, out_s)
#print('connected') # debug
#print(connected) # debug
return calc(connected)
def cut(bit, v, in_s, out_s):
connected = get_connected(bit, v, in_s, out_s)
bit.add(in_s[v], -1 * connected)
def solve(n, graph):
bit = make_mybit(graph.size)
parent_s, in_s, out_s, color_s = dfs(graph)
#print('parent_s, in_s, out_s ') # debug
#print(parent_s, in_s, out_s ) # debug
#print('color_s') # debug
#print(color_s) # debug
for same_color_s in color_s:
ans = calc(graph.size)
bit.rollback()
#print('bit.data') # debug
#print(bit.data) # debug
for v in same_color_s[::-1]:
for to in graph.edges[v]:
if to == parent_s[v]:
continue
ans -= calc_for(bit, to, in_s, out_s)
cut(bit, v, in_s, out_s)
ans -= calc_for(bit, 0, in_s, out_s)
#print('ans') # debug
print(ans)
if __name__ == '__main__':
n = int(input())
graph = Graph(n)
vertices = list(map(int, input().split()))
for i, c in enumerate(vertices):
vertices[i] = c - 1
graph.vertices = vertices
for _ in range(n - 1):
frm, to = list(map(int, input().split()))
frm -= 1
to -= 1
graph.add_edge(frm, to)
solve(n, graph)
#print('\33[32m' + 'end' + '\033[0m') # debug
``` | output | 1 | 83,159 | 13 | 166,319 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i. Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.
For each k=1, 2, ..., N, solve the following problem:
* Find the number of simple paths that visit a vertex painted in the color k one or more times.
Note: The simple paths from Vertex u to v and from v to u are not distinguished.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq c_i \leq N
* 1 \leq a_i,b_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}
Output
Print the answers for k = 1, 2, ..., N in order, each in its own line.
Examples
Input
3
1 2 1
1 2
2 3
Output
5
4
0
Input
1
1
Output
1
Input
2
1 2
1 2
Output
2
2
Input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
Output
5
8
10
5
5
Input
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
Output
18
15
0
14
23
0
23
0 | instruction | 0 | 83,160 | 13 | 166,320 |
"Correct Solution:
```
import sys
# 小さすぎても怒られるので
from collections import defaultdict
n = int(input())
sys.setrecursionlimit(max(1000, 2*n))
cs = [0] + [int(c) for c in input().split()]
ns = defaultdict(set)
for _ in range(n-1):
a,b = [int(c) for c in input().split()]
ns[a].add(b)
ns[b].add(a)
ans = [0] * (n+1) # kを通らないパスの総数
minus = [0] * (n+1) # uの部分木のうち
minus0 = defaultdict(int)
st = {k: [] for k in range(1, n+1)}
nextu = {}
def dfs(u, prev, ans):
global minus, st, minus0
k = cs[u]
st[k].append(u)
ss = 1
for v in ns[u]:
if v==prev:
continue
nextu[u] = v
s = dfs(v, u, ans)
ss += s
tmp = s - minus[v]
ans[k] += (int(tmp * (tmp - 1) / 2) + tmp)
st[k].pop()
if st[k]:
minus[nextu[st[k][-1]]] += ss
else:
minus0[k] += ss
# print(ss)
return ss
dfs(1, -1, ans)
for k in range(1, n+1):
tmp = n - minus0[k]
ans[k] += (int(tmp * (tmp - 1) / 2) + tmp)
total = int(n * (n+1) / 2)
for i in range(1, n+1):
print(total - ans[i])
``` | output | 1 | 83,160 | 13 | 166,321 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i. Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.
For each k=1, 2, ..., N, solve the following problem:
* Find the number of simple paths that visit a vertex painted in the color k one or more times.
Note: The simple paths from Vertex u to v and from v to u are not distinguished.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq c_i \leq N
* 1 \leq a_i,b_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}
Output
Print the answers for k = 1, 2, ..., N in order, each in its own line.
Examples
Input
3
1 2 1
1 2
2 3
Output
5
4
0
Input
1
1
Output
1
Input
2
1 2
1 2
Output
2
2
Input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
Output
5
8
10
5
5
Input
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
Output
18
15
0
14
23
0
23
0 | instruction | 0 | 83,161 | 13 | 166,322 |
"Correct Solution:
```
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**7)
N=int(input())
c=list(map(int,input().split()))
for i in range(N):
c[i]-=1
edge=[[] for i in range(N)]
for i in range(N-1):
a,b=map(int,input().split())
edge[a-1].append(b-1)
edge[b-1].append(a-1)
subtree=[1]*N
def size(v,pv):
for nv in edge[v]:
if nv!=pv:
size(nv,v)
subtree[v]+=subtree[nv]
size(0,-1)
data=[[] for i in range(N)]
Parent=[[0] for i in range(N)]
parent=[0]*N
def dfs(v,pv,nop):
pp=parent[nop]
parent[nop]=v
Parent[nop].append(v)
data[c[v]].append((parent[c[v]],v))
for nv in edge[v]:
if nv!=pv:
dfs(nv,v,c[v])
parent[nop]=pp
dfs(0,-1,0)
for i in range(N):
dic={v:subtree[v] for v in Parent[i]}
for p,ch in data[i]:
dic[p]-=subtree[ch]
res=N*(N+1)//2
for p in dic:
res-=dic[p]*(dic[p]+1)//2
print(res)
``` | output | 1 | 83,161 | 13 | 166,323 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i. Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.
For each k=1, 2, ..., N, solve the following problem:
* Find the number of simple paths that visit a vertex painted in the color k one or more times.
Note: The simple paths from Vertex u to v and from v to u are not distinguished.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq c_i \leq N
* 1 \leq a_i,b_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}
Output
Print the answers for k = 1, 2, ..., N in order, each in its own line.
Examples
Input
3
1 2 1
1 2
2 3
Output
5
4
0
Input
1
1
Output
1
Input
2
1 2
1 2
Output
2
2
Input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
Output
5
8
10
5
5
Input
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
Output
18
15
0
14
23
0
23
0 | instruction | 0 | 83,162 | 13 | 166,324 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
n = int(input())
c = list(map(lambda x: int(x) - 1, input().split()))
graph = [[] for _ in range(n)]
for _ in range(n - 1):
a, b = map(int, input().split())
a -= 1
b -= 1
graph[a].append(b)
graph[b].append(a)
ans = [n * (n + 1) // 2] * n
n_subtree = [1] * n # n_st[i] = (頂点iの部分木の頂点数)
cnt = [0] * n # cnt[i] = (訪問済み頂点のうち、色iを通過しないとたどり着けない頂点数)
n_visited = 0
def dfs(v, v_p):
global n_visited
cnt_v_before = cnt[c[v]]
for v_next in graph[v]:
if v_next == v_p:
continue
m_prev = n_visited - cnt[c[v]]
dfs(v_next, v)
n_subtree[v] += n_subtree[v_next]
m_next = n_visited - cnt[c[v]]
m = m_next - m_prev
ans[c[v]] -= m * (m + 1) // 2
cnt[c[v]] = cnt_v_before + n_subtree[v]
n_visited += 1
dfs(0, -1)
for i in range(n):
m = n - cnt[i]
ans[i] -= m * (m + 1) // 2
print(*ans, sep='\n')
``` | output | 1 | 83,162 | 13 | 166,325 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i. Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.
For each k=1, 2, ..., N, solve the following problem:
* Find the number of simple paths that visit a vertex painted in the color k one or more times.
Note: The simple paths from Vertex u to v and from v to u are not distinguished.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq c_i \leq N
* 1 \leq a_i,b_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}
Output
Print the answers for k = 1, 2, ..., N in order, each in its own line.
Examples
Input
3
1 2 1
1 2
2 3
Output
5
4
0
Input
1
1
Output
1
Input
2
1 2
1 2
Output
2
2
Input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
Output
5
8
10
5
5
Input
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
Output
18
15
0
14
23
0
23
0 | instruction | 0 | 83,163 | 13 | 166,326 |
"Correct Solution:
```
import sys
input = lambda: sys.stdin.readline().rstrip()
N = int(input())
C = [int(a) - 1 for a in input().split()]
X = [[] for i in range(N)]
head = [-1] * (N + 1)
to = [0] * (N - 1 << 1)
nxt = [0] * (N - 1 << 1)
for i in range(N-1):
x, y = map(int, input().split())
x, y = x-1, y-1
nxt[i] = head[x]
to[i] = y
head[x] = i
j = i + N - 1
nxt[j] = head[y]
to[j] = x
head[y] = j
def EulerTour(n, i=0):
f = lambda k: k * (k + 1) // 2
USED = [0] * n
ORG = [0] * n
TMP = [0] * n
ANS = [f(n)] * n
P = [-1] * n
ct = 0
ET1 = [0] * n
ET2 = [0] * n
while i >= 0:
e = head[i]
if e < 0:
ET2[i] = ct
USED[C[i]] += 1 + TMP[i]
if i:
k = ET2[i] - ET1[i] + 1 - USED[C[P[i]]] + ORG[i]
ANS[C[P[i]]] -= f(k)
TMP[P[i]] += k
i = P[i]
continue
j = to[e]
if P[i] == j:
head[i] = nxt[e]
continue
P[j] = i
head[i] = nxt[e]
i = j
ORG[i] = USED[C[P[i]]]
ct += 1
ET1[i] = ct
for i in range(n):
ANS[i] -= f(n - USED[i])
return ANS
print(*EulerTour(N, 0), sep = "\n")
``` | output | 1 | 83,163 | 13 | 166,327 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i. Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.
For each k=1, 2, ..., N, solve the following problem:
* Find the number of simple paths that visit a vertex painted in the color k one or more times.
Note: The simple paths from Vertex u to v and from v to u are not distinguished.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq c_i \leq N
* 1 \leq a_i,b_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}
Output
Print the answers for k = 1, 2, ..., N in order, each in its own line.
Examples
Input
3
1 2 1
1 2
2 3
Output
5
4
0
Input
1
1
Output
1
Input
2
1 2
1 2
Output
2
2
Input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
Output
5
8
10
5
5
Input
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
Output
18
15
0
14
23
0
23
0
Submitted Solution:
```
import sys
input = lambda : sys.stdin.readline().rstrip()
# 小さすぎても怒られるので
from collections import defaultdict
n = int(input())
sys.setrecursionlimit(max(1000, 2*n))
cs = [0] + [int(c) for c in input().split()]
ns = defaultdict(set)
for _ in range(n-1):
a,b = [int(c) for c in input().split()]
ns[a].add(b)
ns[b].add(a)
ans = [0] * (n+1) # kを通らないパスの総数
minus = [0] * (n+1) # uの部分木のうち
minus0 = defaultdict(int)
st = {k: [] for k in range(1, n+1)}
nextu = {}
def dfs(u, prev, ans):
global minus, st, minus0
k = cs[u]
st[k].append(u)
ss = 1
for v in ns[u]:
if v==prev:
continue
nextu[u] = v
s = dfs(v, u, ans)
ss += s
tmp = s - minus[v]
ans[k] += (int(tmp * (tmp - 1) / 2) + tmp)
st[k].pop()
if st[k]:
minus[nextu[st[k][-1]]] += ss
else:
minus0[k] += ss
# print(ss)
return ss
dfs(1, -1, ans)
for k in range(1, n+1):
tmp = n - minus0[k]
ans[k] += (int(tmp * (tmp - 1) / 2) + tmp)
total = int(n * (n+1) / 2)
for i in range(1, n+1):
ans[i] = total - ans[i]
print(*ans[1:], sep="\n")
``` | instruction | 0 | 83,164 | 13 | 166,328 |
Yes | output | 1 | 83,164 | 13 | 166,329 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i. Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.
For each k=1, 2, ..., N, solve the following problem:
* Find the number of simple paths that visit a vertex painted in the color k one or more times.
Note: The simple paths from Vertex u to v and from v to u are not distinguished.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq c_i \leq N
* 1 \leq a_i,b_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}
Output
Print the answers for k = 1, 2, ..., N in order, each in its own line.
Examples
Input
3
1 2 1
1 2
2 3
Output
5
4
0
Input
1
1
Output
1
Input
2
1 2
1 2
Output
2
2
Input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
Output
5
8
10
5
5
Input
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
Output
18
15
0
14
23
0
23
0
Submitted Solution:
```
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def EulerTour(graph, root=1):
global ind_L, ind_R, parent
N = len(graph) - 1
stack = [-root, root]
tour = []
i = -1
while stack:
v = stack.pop()
i += 1
if v > 0:
ind_L[v] = i
p = parent[v]
for w in graph[v]:
if w == p:
continue
parent[w] = v
stack.append(-w)
stack.append(w)
else:
ind_R[-v] = i
yield v
return tour, ind_L, ind_R, parent
N = int(readline())
graph = [[] for _ in range(N + 1)]
parent = [0] * (N + 1)
ind_L = [0] * (N + 1)
ind_R = [0] * (N + 1)
C = (0,) + tuple(map(int, readline().split()))
m = map(int, read().split())
for a, b in zip(m, m):
graph[a].append(b)
graph[b].append(a)
removed = [0] * (N + 1)
answer = [N * (N + 1) // 2] * (N + 1)
memo = [0] * (N + 1)
for v in EulerTour(graph):
if v > 0:
memo[v] = removed[C[parent[v]]]
else:
v = -v
removed[C[v]] += 1
c = C[parent[v]]
x = (ind_R[v] - ind_L[v]) // 2 + 1 # subtree size
x -= removed[c] - memo[v]
answer[c] -= x * (x + 1) // 2
removed[c] += x
for i, x in enumerate(removed):
x = N - x
answer[i] -= x * (x + 1) // 2
print('\n'.join(map(str, answer[1:])))
``` | instruction | 0 | 83,165 | 13 | 166,330 |
Yes | output | 1 | 83,165 | 13 | 166,331 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i. Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.
For each k=1, 2, ..., N, solve the following problem:
* Find the number of simple paths that visit a vertex painted in the color k one or more times.
Note: The simple paths from Vertex u to v and from v to u are not distinguished.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq c_i \leq N
* 1 \leq a_i,b_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}
Output
Print the answers for k = 1, 2, ..., N in order, each in its own line.
Examples
Input
3
1 2 1
1 2
2 3
Output
5
4
0
Input
1
1
Output
1
Input
2
1 2
1 2
Output
2
2
Input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
Output
5
8
10
5
5
Input
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
Output
18
15
0
14
23
0
23
0
Submitted Solution:
```
import sys
def input(): return sys.stdin.readline().rstrip()
n = int(input())
c = list(map(int, input().split()))
c = [x-1 for x in c]
# print(c)
adj = [[] for i in range(n)]
for i in range(n-1):
a, b = tuple(map(int, input().split()))
a -= 1
b -= 1
adj[a].append(b)
adj[b].append(a)
size = [1 for i in range(n)]
stack = [[] for color in range(n)]
in_time = [-1 for i in range(n)]
out_time = [-1 for i in range(n)]
timer = 0
total = n*(n+1)//2
ans = [total for color in range(n)]
# print(ans)
def dfs(parent, root):
global timer
in_time[root] = timer
timer += 1
for child in adj[root]:
if parent == child:
continue
dfs(root, child)
size[root] += size[child]
cnt = size[child]
while stack[c[root]]:
x = stack[c[root]][-1]
if in_time[x] > in_time[root] and out_time[x] != -1:
cnt -= size[x]
stack[c[root]].pop()
else:
break
ans[c[root]] -= cnt*(cnt+1)//2
out_time[root] = timer
timer += 1
stack[c[root]].append(root)
sys.setrecursionlimit(10**6)
dfs(0, 0)
# print(size)
for color in range(n):
cnt = n
while len(stack[color]) > 0:
x = stack[color][-1]
cnt -= size[x]
stack[color].pop()
# print("node:", -1, "color:", color, "cnt:", cnt)
ans[color] -= cnt*(cnt+1)//2
print(ans[color])
``` | instruction | 0 | 83,166 | 13 | 166,332 |
Yes | output | 1 | 83,166 | 13 | 166,333 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i. Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.
For each k=1, 2, ..., N, solve the following problem:
* Find the number of simple paths that visit a vertex painted in the color k one or more times.
Note: The simple paths from Vertex u to v and from v to u are not distinguished.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq c_i \leq N
* 1 \leq a_i,b_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}
Output
Print the answers for k = 1, 2, ..., N in order, each in its own line.
Examples
Input
3
1 2 1
1 2
2 3
Output
5
4
0
Input
1
1
Output
1
Input
2
1 2
1 2
Output
2
2
Input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
Output
5
8
10
5
5
Input
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
Output
18
15
0
14
23
0
23
0
Submitted Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(210000)
class Node(object):
def __init__(self, idx, color):
self.idx = idx
self.color = color
self.edges = []
self.traced = False
def trace(self, counts, excludes):
self.traced = True
num = 1
for edge in self.edges:
if edge.traced:
continue
excludes[self.color].append(0)
n = edge.trace(counts, excludes)
p = n - excludes[self.color].pop()
counts[self.color] += p * (p - 1) // 2 + p
num += n
excludes[self.color][-1] += num
return num
def main():
N = int(input())
nodes = [Node(i, v - 1) for i, v in enumerate(map(int, input().split()))]
counts = [0] * N
excludes = [[0] for _ in range(N)]
for _ in range(N - 1):
a, b = map(int, input().split())
nodes[a - 1].edges.append(nodes[b - 1])
nodes[b - 1].edges.append(nodes[a - 1])
num = nodes[0].trace(counts, excludes)
pair = num * (num - 1) // 2 + num
for i in range(len(counts)):
p = num - excludes[i][0]
counts[i] += p * (p - 1) // 2 + p
for c in counts:
print(pair - c)
if __name__ == '__main__':
main()
``` | instruction | 0 | 83,167 | 13 | 166,334 |
Yes | output | 1 | 83,167 | 13 | 166,335 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i. Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.
For each k=1, 2, ..., N, solve the following problem:
* Find the number of simple paths that visit a vertex painted in the color k one or more times.
Note: The simple paths from Vertex u to v and from v to u are not distinguished.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq c_i \leq N
* 1 \leq a_i,b_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}
Output
Print the answers for k = 1, 2, ..., N in order, each in its own line.
Examples
Input
3
1 2 1
1 2
2 3
Output
5
4
0
Input
1
1
Output
1
Input
2
1 2
1 2
Output
2
2
Input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
Output
5
8
10
5
5
Input
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
Output
18
15
0
14
23
0
23
0
Submitted Solution:
```
import sys
# 小さすぎても怒られるので
from collections import defaultdict
n = int(input())
sys.setrecursionlimit(max(1000, 2*n))
cs = [0] + [int(c) for c in input().split()]
ns = defaultdict(set)
for _ in range(n-1):
a,b = [int(c) for c in input().split()]
ns[a].add(b)
ns[b].add(a)
ans = [0] * (n+1) # kを通らないパスの総数
def dfs(u, prev, ans):
# result : {k: (kを通ってuで終わるパスの数=kを通らないと到達できない頂点数)}, (u以下の部分木のサイズ)
k = cs[u]
if len(ns[u])==1 and prev in ns[u]:
eend, ss = {k : 1}, 1
else:
ss = 1
eend = defaultdict(int)
for v in ns[u]:
if v==prev:
continue
end, s = dfs(v, u, ans)
ss += s
tmp = s - end.get(k, 0)
ans[k] += (int(tmp * (tmp - 1) / 2) + tmp)
for kk,vv in end.items():
if kk!=k:
eend[kk] += end[kk]
eend[k] = ss
# print(eend, ss, ans)
return eend, ss
end, s = dfs(1, -1, ans)
for k in range(1, n+1):
if k==cs[0]:
continue
tmp = s - end.get(k, 0)
ans[k] += (int(tmp * (tmp - 1) / 2) + tmp)
total = int(n * (n+1) / 2)
for i in range(1, n+1):
print(total - ans[i])
``` | instruction | 0 | 83,168 | 13 | 166,336 |
No | output | 1 | 83,168 | 13 | 166,337 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i. Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.
For each k=1, 2, ..., N, solve the following problem:
* Find the number of simple paths that visit a vertex painted in the color k one or more times.
Note: The simple paths from Vertex u to v and from v to u are not distinguished.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq c_i \leq N
* 1 \leq a_i,b_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}
Output
Print the answers for k = 1, 2, ..., N in order, each in its own line.
Examples
Input
3
1 2 1
1 2
2 3
Output
5
4
0
Input
1
1
Output
1
Input
2
1 2
1 2
Output
2
2
Input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
Output
5
8
10
5
5
Input
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
Output
18
15
0
14
23
0
23
0
Submitted Solution:
```
import asyncio
N = int(input())
Cs = list(map(int, input().split()))
adj = [[] for _ in range(N)]
for _ in range(N - 1):
a, b = map(int, input().split())
adj[a - 1].append(b - 1)
adj[b - 1].append(a - 1)
degs = [0] * N
clusters = [[] for _ in range(N)]
async def go(curr, fro):
col = Cs[curr] - 1
deg = 1
clust = {}
for n in adj[curr]:
if n == fro:
continue
ch_deg, ch_clust = await go(n, curr)
clusters[col].append(ch_deg - ch_clust.get(col, 0))
if len(ch_clust) > len(clust):
clust, ch_clust = ch_clust, clust
for k, v in ch_clust.items():
clust[k] = clust.get(k, 0) + v
deg += ch_deg
clust[col] = deg
return deg, clust
_, root_clust = asyncio.get_event_loop().run_until_complete(go(0, -1))
for i in range(N):
clusters[i].append(N - root_clust.get(i, 0))
for clust in clusters:
tot = (N * N + N) // 2
for cnt in clust:
tot -= (cnt * cnt + cnt) // 2
print(tot)
``` | instruction | 0 | 83,169 | 13 | 166,338 |
No | output | 1 | 83,169 | 13 | 166,339 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i. Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.
For each k=1, 2, ..., N, solve the following problem:
* Find the number of simple paths that visit a vertex painted in the color k one or more times.
Note: The simple paths from Vertex u to v and from v to u are not distinguished.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq c_i \leq N
* 1 \leq a_i,b_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}
Output
Print the answers for k = 1, 2, ..., N in order, each in its own line.
Examples
Input
3
1 2 1
1 2
2 3
Output
5
4
0
Input
1
1
Output
1
Input
2
1 2
1 2
Output
2
2
Input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
Output
5
8
10
5
5
Input
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
Output
18
15
0
14
23
0
23
0
Submitted Solution:
```
import sys
# 小さすぎても怒られるので
from collections import defaultdict
n = int(input())
sys.setrecursionlimit(max(1000, 2*n))
cs = [0] + [int(c) for c in input().split()]
ns = defaultdict(set)
for _ in range(n-1):
a,b = [int(c) for c in input().split()]
ns[a].add(b)
ns[b].add(a)
def dfs(ns, prev, now, toru, results):
end = defaultdict(int)
k = cs[now]
end[k] = 1
s = 1 # nowで終わるパスの種類
tmp_s2_sum = 0
usList = defaultdict(list)
for u in ns[now]:
if u==prev:
continue
tmp_end, tmp_toru, tmp_s = results[u] # dfs(ns, now, u, toru)
for kk,vv in tmp_end.items():
if kk!=k:
end[kk] += vv
usList[kk].append((vv, tmp_s))
end[k] += tmp_s
s += tmp_s
tmp_s2_sum += tmp_s**2
for kk,vv in usList.items():
if kk!=k:
x_sum = 0
x2_sum = 0
for item in vv:
toru[kk] += (s - item[1]) * item[0]
x_sum += item[0]
x2_sum += item[0]**2
toru[kk] -= (x_sum**2 - x2_sum) / 2
toru[k] += (1 + ((s-1)**2 - tmp_s2_sum)/2 + (s-1))
# print(now, prev, end, toru, s)
results[now] = end, toru, s
toru = [0] * (n+1)
q = [(1, -1)] # now, prev
args = []
while q:
u, prev = q.pop()
args.append((u,prev))
for v in ns[u]:
if prev==v:
continue
q.append((v, u))
results = {}
for arg in args[::-1]:
dfs(ns, arg[1], arg[0], toru, results)
for item in toru[1:]:
print(int(item))
``` | instruction | 0 | 83,170 | 13 | 166,340 |
No | output | 1 | 83,170 | 13 | 166,341 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i. Additionally, each vertex is painted in a color, and the color of Vertex i is c_i. Here, the color of each vertex is represented by an integer between 1 and N (inclusive). The same integer corresponds to the same color; different integers correspond to different colors.
For each k=1, 2, ..., N, solve the following problem:
* Find the number of simple paths that visit a vertex painted in the color k one or more times.
Note: The simple paths from Vertex u to v and from v to u are not distinguished.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 1 \leq c_i \leq N
* 1 \leq a_i,b_i \leq N
* The given graph is a tree.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
c_1 c_2 ... c_N
a_1 b_1
:
a_{N-1} b_{N-1}
Output
Print the answers for k = 1, 2, ..., N in order, each in its own line.
Examples
Input
3
1 2 1
1 2
2 3
Output
5
4
0
Input
1
1
Output
1
Input
2
1 2
1 2
Output
2
2
Input
5
1 2 3 4 5
1 2
2 3
3 4
3 5
Output
5
8
10
5
5
Input
8
2 7 2 5 4 1 7 5
3 1
1 2
2 7
4 5
5 6
6 8
7 8
Output
18
15
0
14
23
0
23
0
Submitted Solution:
```
import sys
input = lambda : sys.stdin.readline().rstrip()
from collections import defaultdict
n = int(input())
sys.setrecursionlimit(max(1000, n))
# 小さすぎても怒られるので
cs = [0] + [int(c) for c in input().split()]
ns = defaultdict(set)
for _ in range(n-1):
a,b = [int(c) for c in input().split()]
ns[a].add(b)
ns[b].add(a)
toru = [0] * (n+1)
def dfs(ns, prev, now, toru):
end = defaultdict(int)
k = cs[now]
end[k] = 1
s = 1 # nowで終わるパスの種類
for u in ns[now]:
if u==prev:
continue
tmp_end, tmp_toru, tmp_s = dfs(ns, now, u, toru)
# toruの更新
for kk, vv in tmp_end.items():
if kk not in end:
toru[kk] += vv * s
else:
toru[kk] += vv * s + end[kk] * (tmp_s+1) - ((vv+1) * end[kk])
for kk, vv in end.items():
if kk not in tmp_end:
toru[kk] += vv * tmp_s
# end, sの更新
end[k] += tmp_s
for kk,vv in tmp_end.items():
if k!=kk:
end[kk] += tmp_end[kk]
s += tmp_s
toru[k] += 1
# print(now, prev, end, toru, s)
return end, toru, s
end, toru, s = dfs(ns, prev=-1, now=1, toru=toru)
for item in toru[1:]:
print(item)
sys.stdout.write(str(item) + "\n")
``` | instruction | 0 | 83,171 | 13 | 166,342 |
No | output | 1 | 83,171 | 13 | 166,343 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i. Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.
Constraints
* 2 \leq N \leq 10^5
* N-1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N (1\leq i\leq M)
* The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
:
A_M B_M
Output
If it is impossible to assign directions to satisfy the requirement, print -1. Otherwise, print an assignment of directions that satisfies the requirement, in the following format:
C_1 D_1
:
C_M D_M
Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
1 2
1 4
3 2
3 4
Input
5 5
1 2
2 3
3 4
2 5
4 5
Output
-1 | instruction | 0 | 83,188 | 13 | 166,376 |
"Correct Solution:
```
"""
無理な場合を考えてみよう
→全体の辺の本数が奇数なら絶対無理
それ以外は?
説1:辺が偶数なら絶対作れる説
辺を選択し、そのどちらか片方にのみ xor1する
→すべての辺の数字を0にできるか??(初期値は0)
なんかできそう
辺の向きを変えることで1を伝播させることが出来る
→同じ点にぶつけられる!!
ので、偶数本なら絶対できる
どうやって構成する?
適当に置く→1の点を適当に2つ選び、パス上にある辺を全部反転させる
確かにできるけど…遅くね?
もっと簡単にできないか?
適当に木を取る→深い方から整合性を合わせていけば終わり(最終的には根に収束するので)
各点に関して動かすのは高々1辺だけだから、木を取った辺のみ考えて反転させる
(それ以外は適当に置いておく)
dfsで終わり→解けた!!!
"""
import sys
sys.setrecursionlimit(200000)
N,M = map(int,input().split())
lisN = [ [] for i in range(N) ]
xorlis = [0] * N
AB = []
if M % 2 == 1:
print (-1)
sys.exit()
for i in range(M):
a,b = map(int,input().split())
AB.append([a,b])
a -= 1
b -= 1
lisN[a].append([b,i])
lisN[b].append([a,i])
xorlis[b] ^= 1
visited = [False] * N
def dfs(now):
visited[now] = True
for nex,eind in lisN[now]:
if visited[nex]:
continue
cat = dfs(nex)
if cat != 0:
xorlis[nex] ^= 1
xorlis[now] ^= 1
t = AB[eind][1]
AB[eind][1] = AB[eind][0]
AB[eind][0] = t
return xorlis[now]
dfs(0)
#print (xorlis)
if 1 in xorlis:
print (-1)
sys.exit()
else:
for i,j in AB:
print (j,i)
``` | output | 1 | 83,188 | 13 | 166,377 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i. Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.
Constraints
* 2 \leq N \leq 10^5
* N-1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N (1\leq i\leq M)
* The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
:
A_M B_M
Output
If it is impossible to assign directions to satisfy the requirement, print -1. Otherwise, print an assignment of directions that satisfies the requirement, in the following format:
C_1 D_1
:
C_M D_M
Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
1 2
1 4
3 2
3 4
Input
5 5
1 2
2 3
3 4
2 5
4 5
Output
-1 | instruction | 0 | 83,189 | 13 | 166,378 |
"Correct Solution:
```
from heapq import heappush, heappop
N, M = map(int, input().split())
A, B = (
zip(*(map(int, input().split()) for _ in range(M))) if M else
((), ())
)
class UnionFindTree:
def __init__(self, n):
self.p = [i for i in range(n + 1)]
self.r = [1 for _ in range(n + 1)]
def find(self, x):
if self.p[x] != x:
self.p[x] = self.find(self.p[x])
return self.p[x]
def union(self, x, y):
px = self.find(x)
py = self.find(y)
if self.r[px] < self.r[py]:
self.r[py] += self.r[px]
self.p[px] = py
else:
self.r[px] += self.r[py]
self.p[py] = px
def same(self, x, y):
return self.find(x) == self.find(y)
def size(self, x):
return self.r[self.find(x)]
# Mが奇数ならば構築不可
# Mが偶数ならば適当に木を構築し、深さの大きい頂点を端点とする辺から向きを決めていく
utf = UnionFindTree(N)
T = [{} for _ in range(N + 1)]
for a, b in zip(A, B):
if not utf.same(a, b):
T[a][b] = 1
T[b][a] = 1
utf.union(a, b)
INF = 10**9
def dijkstra(G, s):
dp = [INF for _ in range(len(G))]
q = []
heappush(q, (0, s))
while q:
c, i = heappop(q)
if dp[i] == INF:
dp[i] = c
for j, w in G[i].items():
heappush(q, (c + w, j))
return dp
r = dijkstra(T, 1)
p = [0, 1] + [
min(T[i], key=lambda j: r[j])
for i in range(2, N + 1)
]
dp = [0 for _ in range(N + 1)]
for a, b in zip(A, B):
if b not in T[a]:
dp[a] += 1
for i in sorted(
range(1, N + 1),
key=lambda j: r[j],
reverse=True
):
dp[i] += sum(
(dp[j] + 1) % 2
for j in T[i]
if j != p[i]
)
ans = (
-1 if M % 2 == 1 else
'\n'.join(
'{} {}'.format(a, b)
if b not in T[a] or (
dp[a] % 2 == 1 and p[a] == b or
dp[b] % 2 == 0 and p[b] == a
) else
'{} {}'.format(b, a)
for a, b in zip(A, B)
)
)
print(ans)
``` | output | 1 | 83,189 | 13 | 166,379 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i. Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.
Constraints
* 2 \leq N \leq 10^5
* N-1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N (1\leq i\leq M)
* The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
:
A_M B_M
Output
If it is impossible to assign directions to satisfy the requirement, print -1. Otherwise, print an assignment of directions that satisfies the requirement, in the following format:
C_1 D_1
:
C_M D_M
Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
1 2
1 4
3 2
3 4
Input
5 5
1 2
2 3
3 4
2 5
4 5
Output
-1 | instruction | 0 | 83,190 | 13 | 166,380 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(10**6)
from collections import deque
n, m = map(int, input().split())
AB = [[int(x) for x in input().split()] for _ in range(m)]
# representation matrix
repn = [[] for j in range(n)]
for a, b in AB:
a -= 1
b -= 1
repn[a].append(b)
repn[b].append(a)
# Minimum Spanning Tree
tree = [set() for i in range(n)]
visited = [False] * n
visited[0] = True
q = deque([0])
while q:
u = q.popleft()
for v in repn[u]:
if visited[v]:
continue
visited[v] = True
tree[u].add(v)
tree[v].add(u)
q.append(v)
# define directions of the edges other than that of MST
degree = [0] * n
ans = []
for a, b in AB:
a -= 1
b -= 1
if b in tree[a]:
continue
ans.append([a+1, b+1])
degree[a] += 1
# defining orientations
def dfs(u = 0, parent = None):
for v in tree[u]:
if v == parent:
continue
dfs(v, u)
if degree[v] % 2 == 0:
ans.append([u+1, v+1])
degree[u] += 1
else:
ans.append([v+1, u+1])
degree[v] += 1
dfs()
if degree[0] % 2 == 1:
print(-1)
else:
for pair in ans:
print(' '.join(map(str, pair)))
``` | output | 1 | 83,190 | 13 | 166,381 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i. Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.
Constraints
* 2 \leq N \leq 10^5
* N-1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N (1\leq i\leq M)
* The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
:
A_M B_M
Output
If it is impossible to assign directions to satisfy the requirement, print -1. Otherwise, print an assignment of directions that satisfies the requirement, in the following format:
C_1 D_1
:
C_M D_M
Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
1 2
1 4
3 2
3 4
Input
5 5
1 2
2 3
3 4
2 5
4 5
Output
-1 | instruction | 0 | 83,191 | 13 | 166,382 |
"Correct Solution:
```
from sys import setrecursionlimit
setrecursionlimit(10 ** 8)
n, m = [int(i) for i in input().split()]
A = [[int(i) for i in input().split()] for i in range(m)]
if m % 2 == 1:
print(-1)
exit()
E = [[] for i in range(n + 1)]
used_e = [False] * m
ans = []
for i, (a, b) in enumerate(A):
E[a].append((b, i))
E[b].append((a, i))
used = [False] * (n + 1)
def dfs(prev, v):
if used[v]:
return
used[v] = True
cnt = 0
for to, i in E[v]:
dfs(v, to)
if not used_e[i]:
cnt += 1
for to, i in E[v]:
if cnt % 2 == 1 and to == prev:
continue
if not used_e[i]:
ans.append((v, to))
used_e[i] = True
dfs(0, 1)
for a in ans:
print(*a)
``` | output | 1 | 83,191 | 13 | 166,383 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i. Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.
Constraints
* 2 \leq N \leq 10^5
* N-1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N (1\leq i\leq M)
* The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
:
A_M B_M
Output
If it is impossible to assign directions to satisfy the requirement, print -1. Otherwise, print an assignment of directions that satisfies the requirement, in the following format:
C_1 D_1
:
C_M D_M
Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
1 2
1 4
3 2
3 4
Input
5 5
1 2
2 3
3 4
2 5
4 5
Output
-1 | instruction | 0 | 83,192 | 13 | 166,384 |
"Correct Solution:
```
from collections import deque
N, M = map(int, input().split())
V = {i: [] for i in range(1,N+1)}
for _ in range(M):
a, b = map(int, input().split())
V[a].append(b)
V[b].append(a)
def zen(V):
W = []
q = deque()
q.append(1)
vstd = {i: 0 for i in range(1,N+1)}
while q:
u = q.popleft()
if vstd[u]:
continue
vstd[u] = True
W.append(u)
for v in V[u]:
if vstd[v]:
continue
q.append(v)
return W
if M % 2 != 0:
print(-1)
else:
U = zen(V)
W = {i: [] for i in range(1,N+1)}
for u in U[::-1]:
for v in V[u]:
if v in W[u] or u in W[v]:
continue
if len(W[u]) % 2 == 0:
W[v].append(u)
else:
W[u].append(v)
for w in W:
for v in W[w]:
print(w, v)
``` | output | 1 | 83,192 | 13 | 166,385 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i. Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.
Constraints
* 2 \leq N \leq 10^5
* N-1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N (1\leq i\leq M)
* The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
:
A_M B_M
Output
If it is impossible to assign directions to satisfy the requirement, print -1. Otherwise, print an assignment of directions that satisfies the requirement, in the following format:
C_1 D_1
:
C_M D_M
Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
1 2
1 4
3 2
3 4
Input
5 5
1 2
2 3
3 4
2 5
4 5
Output
-1 | instruction | 0 | 83,193 | 13 | 166,386 |
"Correct Solution:
```
#!/usr/bin/env python3
import sys
from collections import defaultdict, deque
import heapq
def solve(N: int, M: int, A: "List[int]", B: "List[int]"):
if M % 2 != 0:
print(-1)
return
adj = defaultdict(set)
for a, b in zip(A, B):
adj[a].add(b)
adj[b].add(a)
parent = [None for _ in range(N)]
children = [set() for _ in range(N)]
res = set()
count = [0 for _ in range(N)]
Q = deque([0], N)
visited = [False for _ in range(N)]
visited[0] = True
while len(Q) > 0:
u = Q.popleft()
for v in adj[u]:
if parent[u] == v:
continue
if visited[v]:
if (v, u) in res:
continue
res.add((u, v))
count[u] += 1
continue
parent[v] = u
children[u].add(v)
visited[v] = True
Q.append(v)
visited = [False for _ in range(N)]
Q = deque([0], N)
while len(Q) > 0:
u = Q.popleft()
if (len(children[u]) > 0
and not all(visited[v] for v in children[u])
):
Q.appendleft(u)
Q.extendleft(children[u])
continue
if parent[u] is None:
visited[u] = True
continue
v = parent[u]
if count[u] % 2 == 0:
res.add((v, u))
count[v] += 1
else:
res.add((u, v))
count[u] += 1
visited[u] = True
for c, d in res:
print(c+1, d+1)
return
# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
M = int(next(tokens)) # type: int
A = [int()] * (M) # type: "List[int]"
B = [int()] * (M) # type: "List[int]"
for i in range(M):
A[i] = int(next(tokens)) - 1
B[i] = int(next(tokens)) - 1
solve(N, M, A, B)
if __name__ == '__main__':
main()
``` | output | 1 | 83,193 | 13 | 166,387 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i. Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.
Constraints
* 2 \leq N \leq 10^5
* N-1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N (1\leq i\leq M)
* The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
:
A_M B_M
Output
If it is impossible to assign directions to satisfy the requirement, print -1. Otherwise, print an assignment of directions that satisfies the requirement, in the following format:
C_1 D_1
:
C_M D_M
Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
1 2
1 4
3 2
3 4
Input
5 5
1 2
2 3
3 4
2 5
4 5
Output
-1 | instruction | 0 | 83,194 | 13 | 166,388 |
"Correct Solution:
```
n, m, *L = map(int, open(0).read().split())
con = [[] for _ in range(n)]
for s, t in zip(*[iter(L)] * 2):
con[s - 1] += t - 1,
con[t - 1] += s - 1,
# 辺を取り出して木をつくる
tree = [[] for _ in range(n)]
lev = [0] * n
vis = [False] * n
vis[0] = True
reserve = set()
q = [0]
while q:
cur = q.pop()
for nxt in con[cur]:
if vis[nxt]:
continue
reserve.add((cur, nxt))
tree[cur] += nxt,
tree[nxt] += cur,
lev[cur] += 1
lev[nxt] += 1
vis[nxt] = True
q += nxt,
# 要らない辺を適当に方向付ける
ans = []
deg = [0] * n
for s, t in zip(*[iter(L)] * 2):
if (s - 1, t - 1) not in reserve and (t - 1, s - 1) not in reserve:
ans += "{} {}".format(s, t),
deg[s - 1] ^= 1
# 葉から順に辺の方向を決定
from collections import deque
q = deque()
done = [False] * n
for i, l in enumerate(lev):
if l == 1:
q.append(i)
while q:
cur = q.popleft()
for nxt in tree[cur]:
if done[nxt]:
continue
if deg[cur]:
ans += "{} {}".format(cur + 1, nxt + 1),
deg[cur] ^= 1
else:
ans += "{} {}".format(nxt + 1, cur + 1),
deg[nxt] ^= 1
done[cur] = True
lev[nxt] -= 1
if lev[nxt] == 1:
q.append(nxt)
if any(deg):
print(-1)
else:
print("\n".join(ans))
``` | output | 1 | 83,194 | 13 | 166,389 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i. Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.
Constraints
* 2 \leq N \leq 10^5
* N-1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N (1\leq i\leq M)
* The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
:
A_M B_M
Output
If it is impossible to assign directions to satisfy the requirement, print -1. Otherwise, print an assignment of directions that satisfies the requirement, in the following format:
C_1 D_1
:
C_M D_M
Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
1 2
1 4
3 2
3 4
Input
5 5
1 2
2 3
3 4
2 5
4 5
Output
-1 | instruction | 0 | 83,195 | 13 | 166,390 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
def main():
N, M = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(M):
a, b = map(int, input().split())
graph[a-1].append(b-1)
graph[b-1].append(a-1)
if M%2 == 1:
print(-1)
return
q = [0]
checked = [False]*N
checked[0] = True
search_seq = [0]
while q:
qq = []
for p in q:
for np in graph[p]:
if not checked[np]:
checked[np] = True
qq.append(np)
search_seq.append(np)
q = qq
evenOrOdd = []
for n in range(N):
evenOrOdd.append(len(graph[n])%2)
searched = [False]*N
ans = []
for p in reversed(search_seq):
evens = []
odds = []
for np in graph[p]:
if not searched[np]:
if evenOrOdd[np]:
odds.append(np)
else:
evens.append(np)
if not odds and not evens:
break
if evenOrOdd[p] == 1:
if odds:
np = odds.pop()
ans.append((np, p))
else:
np = evens.pop()
ans.append((np, p))
for np in odds:
ans.append((p, np))
evenOrOdd[np] = 0
for np in evens:
ans.append((p, np))
evenOrOdd[np] = 1
searched[p] = True
for a1, a2 in ans:
print(a1+1, a2+1)
return
if __name__ == "__main__":
main()
``` | output | 1 | 83,195 | 13 | 166,391 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i. Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.
Constraints
* 2 \leq N \leq 10^5
* N-1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N (1\leq i\leq M)
* The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
:
A_M B_M
Output
If it is impossible to assign directions to satisfy the requirement, print -1. Otherwise, print an assignment of directions that satisfies the requirement, in the following format:
C_1 D_1
:
C_M D_M
Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
1 2
1 4
3 2
3 4
Input
5 5
1 2
2 3
3 4
2 5
4 5
Output
-1
Submitted Solution:
```
from sys import setrecursionlimit
setrecursionlimit(10**8)
N, M = [int(i) for i in input().split()]
if M % 2 == 1:
print(-1)
exit()
E = [[] for i in range(N + 1)]
used_e = [False] * M
ans = []
for i in range(M):
A, B = map(int, input().split())
E[A].append((B, i))
E[B].append((A, i))
visited = [False] * (N + 1)
def dfs(prev, v):
if visited[v]: return
visited[v] = True
cnt = 0
for to, i in E[v]:
dfs(v, to)
if not used_e[i]: cnt += 1
for to, i in E[v]:
if cnt % 2 == 1 and to == prev: continue
if not used_e[i]:
ans.append((v, to))
used_e[i] = True
dfs(0, 1)
for a in ans: print(*a)
``` | instruction | 0 | 83,196 | 13 | 166,392 |
Yes | output | 1 | 83,196 | 13 | 166,393 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i. Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.
Constraints
* 2 \leq N \leq 10^5
* N-1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N (1\leq i\leq M)
* The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
:
A_M B_M
Output
If it is impossible to assign directions to satisfy the requirement, print -1. Otherwise, print an assignment of directions that satisfies the requirement, in the following format:
C_1 D_1
:
C_M D_M
Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
1 2
1 4
3 2
3 4
Input
5 5
1 2
2 3
3 4
2 5
4 5
Output
-1
Submitted Solution:
```
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5)
N, M = map(int, input().split())
if M % 2 == 0 :
vec = [[] for _ in range(N)]
for _ in range(M) :
A, B = map(int, input().split())
vec[A-1].append(B-1)
vec[B-1].append(A-1)
visited = [False] * N
CD = [[] for _ in range(N)]
def dfs(cur, pre) :
for nex in vec[cur] :
if nex == pre :
continue
if not visited[nex] :
visited[nex] = True
dfs(nex, cur)
elif not cur in CD[nex] :
CD[cur].append(nex)
if pre != -1 :
if len(CD[cur]) % 2 == 0 :
CD[pre].append(cur)
else :
CD[cur].append(pre)
visited[0] = True
dfs(0, -1)
for i in range(N) :
for j in CD[i] :
print(i + 1, j + 1)
else :
print(-1)
``` | instruction | 0 | 83,197 | 13 | 166,394 |
Yes | output | 1 | 83,197 | 13 | 166,395 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i. Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.
Constraints
* 2 \leq N \leq 10^5
* N-1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N (1\leq i\leq M)
* The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
:
A_M B_M
Output
If it is impossible to assign directions to satisfy the requirement, print -1. Otherwise, print an assignment of directions that satisfies the requirement, in the following format:
C_1 D_1
:
C_M D_M
Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
1 2
1 4
3 2
3 4
Input
5 5
1 2
2 3
3 4
2 5
4 5
Output
-1
Submitted Solution:
```
import sys
from collections import defaultdict
sys.setrecursionlimit(10**7)
readline = sys.stdin.readline
def solve(p, v, rs):
als = G[v]
cs = g[v]
c = 0
for u in als:
if u in cs:
c += solve(v, u, rs)
elif u != p:
if v < u:
c += 1
rs.add((min(u,v), max(u,v)))
if p == None:
return
elif c%2 != 0:
rs.add((v, p))
return 0
else:
rs.add((p,v))
return 1
N, M = map(int, readline().split())
if M %2 != 0:
print(-1)
else:
G = defaultdict(set)
for v in range(M):
a, b = map(int, readline().split())
G[a].add(b)
G[b].add(a)
stack=[1]
vs = set([1])
g = defaultdict(set)
counter = 1
while True:
v = stack.pop()
for u in G[v]:
if u in vs:
continue
vs.add(u)
g[v].add(u)
stack.append(u)
counter += 1
if counter == N:
break
rs = set()
solve(None,1, rs)
for r in rs:
print(r[0], r[1])
``` | instruction | 0 | 83,198 | 13 | 166,396 |
Yes | output | 1 | 83,198 | 13 | 166,397 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i. Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.
Constraints
* 2 \leq N \leq 10^5
* N-1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N (1\leq i\leq M)
* The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
:
A_M B_M
Output
If it is impossible to assign directions to satisfy the requirement, print -1. Otherwise, print an assignment of directions that satisfies the requirement, in the following format:
C_1 D_1
:
C_M D_M
Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
1 2
1 4
3 2
3 4
Input
5 5
1 2
2 3
3 4
2 5
4 5
Output
-1
Submitted Solution:
```
import sys
input=sys.stdin.readline
from collections import deque
n,m=map(int,input().split())
Edges=[[] for _ in range(n)]
for _ in range(m):
a,b=map(int,input().split())
a-=1; b-=1
Edges[a].append(b)
Edges[b].append(a)
if m%2:
print(-1)
exit()
Ans=[]
AnsSet=set()
Par=[-1]*n
V=[]
Visited=[False]*n
Cnt=[0]*n
q=deque([0])
Visited[0]=True
while q:
fr=q.popleft()
for to in Edges[fr]:
if to==Par[fr] or (to,fr) in AnsSet:
continue
if Visited[to]:
AnsSet.add((fr,to))
Ans.append((fr,to))
Cnt[fr]+=1
else:
Par[to]=fr
Visited[to]=True
q.append(to)
V.append(to)
for v in V[::-1]:
if Cnt[v]%2:
Ans.append((v,Par[v]))
else:
Ans.append((Par[v],v))
Cnt[Par[v]]+=1
for c,d in Ans:
print(c+1,d+1)
``` | instruction | 0 | 83,199 | 13 | 166,398 |
Yes | output | 1 | 83,199 | 13 | 166,399 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i. Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.
Constraints
* 2 \leq N \leq 10^5
* N-1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N (1\leq i\leq M)
* The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
:
A_M B_M
Output
If it is impossible to assign directions to satisfy the requirement, print -1. Otherwise, print an assignment of directions that satisfies the requirement, in the following format:
C_1 D_1
:
C_M D_M
Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
1 2
1 4
3 2
3 4
Input
5 5
1 2
2 3
3 4
2 5
4 5
Output
-1
Submitted Solution:
```
# Setup
import sys
from collections import *
sys.setrecursionlimit(4100000)
n, m = [int(i) for i in input().split()]
a = [list(map(int, input().split())) for _ in range(m)]
left = set()
right = set()
newa = []
for i,k in a:
if i not in left and k not in right and i not in right and k not in left:
newa.append([i,k])
left.add(i)
right.add(k)
elif i in left:
newa.append([i,k])
else:
newa.append([k,i])
newleft = []
newright = []
for i,k in newa:
newleft.append(i)
newright.append(k)
counter = Counter(newleft)
ans = True
for i in range(n+1):
if counter[i]%2 != 0:
ans = False
counter = Counter(newright)
for i in range(n+1):
if counter[i]%2 != 0:
ans = False
if ans == False:
print(-1)
else:
for i in range(m):
print(newa[i][0], newa[i][1])
``` | instruction | 0 | 83,200 | 13 | 166,400 |
No | output | 1 | 83,200 | 13 | 166,401 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i. Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.
Constraints
* 2 \leq N \leq 10^5
* N-1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N (1\leq i\leq M)
* The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
:
A_M B_M
Output
If it is impossible to assign directions to satisfy the requirement, print -1. Otherwise, print an assignment of directions that satisfies the requirement, in the following format:
C_1 D_1
:
C_M D_M
Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
1 2
1 4
3 2
3 4
Input
5 5
1 2
2 3
3 4
2 5
4 5
Output
-1
Submitted Solution:
```
from collections import defaultdict
import sys
input = sys.stdin.readline
INF = float('inf')
def main():
class SegmentTree:
def __init__(self,N):
self.NN = 1
while self.NN < N:
self.NN *= 2
self.SegTree = [[INF,i-self.NN+1] for i in range(self.NN*2-1)]
def update(self,i,x,flag = False): # i の値を +x
i += self.NN - 1
if flag:
tmp = x
else:
tmp = self.SegTree[i][0] + x
if tmp == 0:
tmp = INF
self.SegTree[i] = [tmp,i-self.NN+1]
while i>0:
i = (i-1)//2
if self.SegTree[i*2+1][0] <= self.SegTree[i*2+2][0]:
self.SegTree[i] = self.SegTree[i*2+1]
else:
self.SegTree[i] = self.SegTree[i*2+2]
def query(self,a,b,k=0,l=0,r=None): #[A,B)の値, 呼ぶときはquery(a,b)
if r == None: r = self.NN
if r <= a or b <= l: #完全に含まない
return [INF,i]
elif a <= l and r <= b : #完全に含む
return self.SegTree[k]
else: #交差する
vl,il = self.query(a,b,k*2+1,l,(l+r)//2)
vr,ir = self.query(a,b,k*2+2,(l+r)//2,r)
if vl <= vr:
return [vl,il]
else:
return [vr,ir]
N,M = map(int, input().split())
cnts = [0]*N
lines = defaultdict(set)
for _ in range(M):
A,B = map(int, input().split())
A,B = A-1,B-1
cnts[A] += 1
cnts[B] += 1
lines[A].add(B)
lines[B].add(A)
if M%2 == 1:
print(-1)
else:
ST = SegmentTree(N)
for i,n in enumerate(cnts):
ST.update(i,n,True)
cnts = [0]*N # 出てる辺の数
for _ in range(M):
n,s = ST.SegTree[0] # 全体の最小値
if n == INF:
break
if cnts[s]%2 == 0: # s 出てる辺 == 偶数
for t in lines[s]: # t -> s に全部
print(t+1,s+1)
cnts[t] += 1
ST.update(t,-1)
lines[t] -= {s}
ST.update(s,-n)
else: # s 出てる辺 == 奇数
first = True
for t in lines[s]: # s -> t に1つ 他t->s
if first:
print(s+1,t+1)
cnts[s] += 1
first = False
else:
print(t+1,s+1)
cnts[t] += 1
ST.update(t,-1)
lines[t] -= {s}
ST.update(s,-n)
if __name__ == '__main__':
main()
``` | instruction | 0 | 83,201 | 13 | 166,402 |
No | output | 1 | 83,201 | 13 | 166,403 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i. Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.
Constraints
* 2 \leq N \leq 10^5
* N-1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N (1\leq i\leq M)
* The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
:
A_M B_M
Output
If it is impossible to assign directions to satisfy the requirement, print -1. Otherwise, print an assignment of directions that satisfies the requirement, in the following format:
C_1 D_1
:
C_M D_M
Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
1 2
1 4
3 2
3 4
Input
5 5
1 2
2 3
3 4
2 5
4 5
Output
-1
Submitted Solution:
```
N, M = map(int, input().split())
if M % 2 == 1:
print(-1)
exit()
E = []
for _ in range(M):
i, j = map(int, input().split())
E.append([i-1, j-1])
tree = [set() for _ in range(N)]
visited = [False] * N
root = E[0][0]
visited[root] = True
queue = [root]
D = [0] * N
for i, j in E:
if visited[j]:
D[i] += 1
print(i+1, j+1)
else:
tree[i].add(j)
def f(tree, i):
if tree[i] == set():
return
for j in tree[i]:
f(tree, j)
if D[i] % 2 == 0:
D[j] += 1
print(j+1, i+1)
else:
D[i] += 1
print(i+1, j+1)
return
f(tree, root)
``` | instruction | 0 | 83,202 | 13 | 166,404 |
No | output | 1 | 83,202 | 13 | 166,405 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i. Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.
Constraints
* 2 \leq N \leq 10^5
* N-1 \leq M \leq 10^5
* 1 \leq A_i,B_i \leq N (1\leq i\leq M)
* The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
N M
A_1 B_1
:
A_M B_M
Output
If it is impossible to assign directions to satisfy the requirement, print -1. Otherwise, print an assignment of directions that satisfies the requirement, in the following format:
C_1 D_1
:
C_M D_M
Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.
Examples
Input
4 4
1 2
2 3
3 4
4 1
Output
1 2
1 4
3 2
3 4
Input
5 5
1 2
2 3
3 4
2 5
4 5
Output
-1
Submitted Solution:
```
n, m = map(int, input().split())
e = {i + 1:[] for i in range(n)}
for i in range(m):
a, b = map(int, input().split())
e[a].append(b)
e[b].append(a)
if m % 2 == 1:
print(-1)
else:
count = {i + 1: 0 for i in range(n)}
num = [i + 1 for i in range(n)]
num.sort(key = lambda x: len(e[x]))
while e:
i = num[0]
temp = []
for j in e[i]:
temp.append(j)
if count[i] % 2 == 0:
print(j, i)
count[j] += 1
else:
print(i, j)
count[i] += 1
for j in temp:
e[j].remove(i)
e[i].remove(j)
temp = []
for j in e:
if len(e[j]) == 0:
temp.append(j)
for j in temp:
del e[j]
num.remove(j)
num.sort(key = lambda x: len(e[x]))
``` | instruction | 0 | 83,203 | 13 | 166,406 |
No | output | 1 | 83,203 | 13 | 166,407 |
Provide a correct Python 3 solution for this coding contest problem.
You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a bridge.
Find the number of the edges that are bridges among the M edges.
Constraints
* 2 \leq N \leq 50
* N-1 \leq M \leq min(N(N−1)⁄2,50)
* 1 \leq a_i<b_i \leq N
* The given graph does not contain self-loops and double edges.
* The given graph is connected.
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M
Output
Print the number of the edges that are bridges among the M edges.
Examples
Input
7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7
Output
4
Input
3 3
1 2
1 3
2 3
Output
0
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
5 | instruction | 0 | 83,236 | 13 | 166,472 |
"Correct Solution:
```
from collections import deque
n,m = map(int,input().split())
graph = [[] for _ in range(n)]
deg = [0]*n
seen = [False]*n
for i in range(m):
x,y = map(int,input().split())
graph[x-1] += [y-1]
graph[y-1] += [x-1]
deg[x-1] += 1
deg[y-1] += 1
q = deque([i for i in range(n) if deg[i] == 1])
while q:
v = q.popleft()
seen[v] = True
for i in graph[v]:
deg[i] -= 1
if deg[i] == 1:
q.append(i)
if all(seen[i] == 1 for i in range(n)):print(n-1);exit()
print(seen.count(1))
``` | output | 1 | 83,236 | 13 | 166,473 |
Provide a correct Python 3 solution for this coding contest problem.
You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a bridge.
Find the number of the edges that are bridges among the M edges.
Constraints
* 2 \leq N \leq 50
* N-1 \leq M \leq min(N(N−1)⁄2,50)
* 1 \leq a_i<b_i \leq N
* The given graph does not contain self-loops and double edges.
* The given graph is connected.
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M
Output
Print the number of the edges that are bridges among the M edges.
Examples
Input
7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7
Output
4
Input
3 3
1 2
1 3
2 3
Output
0
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
5 | instruction | 0 | 83,237 | 13 | 166,474 |
"Correct Solution:
```
N,M=map(int,input().split())
List=[[int(i) for i in input().split()] for i in range(M)]
S=0
for i in range(M):
new_List=list(List)
del new_List[i]
islands=[1]
for i in islands:
for j in new_List:
if i in j:
j=[item for item in j if item not in islands]
islands.extend(j)
if len(islands)<N:
S+=1
print(S)
``` | output | 1 | 83,237 | 13 | 166,475 |
Provide a correct Python 3 solution for this coding contest problem.
You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a bridge.
Find the number of the edges that are bridges among the M edges.
Constraints
* 2 \leq N \leq 50
* N-1 \leq M \leq min(N(N−1)⁄2,50)
* 1 \leq a_i<b_i \leq N
* The given graph does not contain self-loops and double edges.
* The given graph is connected.
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M
Output
Print the number of the edges that are bridges among the M edges.
Examples
Input
7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7
Output
4
Input
3 3
1 2
1 3
2 3
Output
0
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
5 | instruction | 0 | 83,238 | 13 | 166,476 |
"Correct Solution:
```
N,M=map(int,input().split())
AB=[list(map(int,input().split())) for i in range(M)]
r=0
for m in range(M):
c=[[] for i in range(N)]
for i,(a,b) in enumerate(AB):
if i!=m:
c[a-1].append(b-1)
c[b-1].append(a-1)
q=[0]
v=[1]+[0]*(N-1)
while q:
p=q.pop()
for n in c[p]:
if v[n]==0:
v[n]=1
q.append(n)
if 0 in v:
r+=1
print(r)
``` | output | 1 | 83,238 | 13 | 166,477 |
Provide a correct Python 3 solution for this coding contest problem.
You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a bridge.
Find the number of the edges that are bridges among the M edges.
Constraints
* 2 \leq N \leq 50
* N-1 \leq M \leq min(N(N−1)⁄2,50)
* 1 \leq a_i<b_i \leq N
* The given graph does not contain self-loops and double edges.
* The given graph is connected.
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M
Output
Print the number of the edges that are bridges among the M edges.
Examples
Input
7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7
Output
4
Input
3 3
1 2
1 3
2 3
Output
0
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
5 | instruction | 0 | 83,239 | 13 | 166,478 |
"Correct Solution:
```
n,m=map(int,input().split())
g=[list() for _ in range(n+1)]
side=[list(map(int,input().split())) for i in range(m)]
for a,b in side:
g[a].append(b)
g[b].append(a)
cnt=0
def dfs(x,s):
ed[x-1]=1
for i in g[x]:
if s!={x,i} and ed[i-1]==0:dfs(i,s)
cnt=0
for i in range(m):
ed=[0]*n
dfs(1,set(side[i]))
if 0 in ed:cnt+=1
print(cnt)
``` | output | 1 | 83,239 | 13 | 166,479 |
Provide a correct Python 3 solution for this coding contest problem.
You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a bridge.
Find the number of the edges that are bridges among the M edges.
Constraints
* 2 \leq N \leq 50
* N-1 \leq M \leq min(N(N−1)⁄2,50)
* 1 \leq a_i<b_i \leq N
* The given graph does not contain self-loops and double edges.
* The given graph is connected.
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M
Output
Print the number of the edges that are bridges among the M edges.
Examples
Input
7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7
Output
4
Input
3 3
1 2
1 3
2 3
Output
0
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
5 | instruction | 0 | 83,240 | 13 | 166,480 |
"Correct Solution:
```
N,M = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(M)]
ans = 0
for i in range(M):
ab2 = ab[:i] + ab[i+1:]
connect = [[] for _ in range(N+1)]
for a,b in ab2:
connect[a].append(b)
connect[b].append(a)
V = [-1] * (N+1)
V[0] = 0
V[1] = 0
q = [1]
for j in q:
for k in connect[j]:
if V[k] == -1:
V[k] = V[j] + 1
q.append(k)
for v in V[1:]:
if v == -1:
ans += 1
break
print(ans)
``` | output | 1 | 83,240 | 13 | 166,481 |
Provide a correct Python 3 solution for this coding contest problem.
You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a bridge.
Find the number of the edges that are bridges among the M edges.
Constraints
* 2 \leq N \leq 50
* N-1 \leq M \leq min(N(N−1)⁄2,50)
* 1 \leq a_i<b_i \leq N
* The given graph does not contain self-loops and double edges.
* The given graph is connected.
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M
Output
Print the number of the edges that are bridges among the M edges.
Examples
Input
7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7
Output
4
Input
3 3
1 2
1 3
2 3
Output
0
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
5 | instruction | 0 | 83,241 | 13 | 166,482 |
"Correct Solution:
```
n,m=map(int,input().split())
AB=[list(map(int,input().split())) for _ in range(m)]
def dfs(x):
if visit[x]==True:
return
visit[x]=True
for i in range(n):
if graph[x][i]==True:
dfs(i)
ans=0
for i in range(m):
graph=[[False]*n for _ in range(n)]
for j in range(m):
if j!=i:
a,b=AB[j]
graph[a-1][b-1]=True
graph[b-1][a-1]=True
visit=[False]*n
dfs(0)
if sum(visit)!=n:
ans+=1
print(ans)
``` | output | 1 | 83,241 | 13 | 166,483 |
Provide a correct Python 3 solution for this coding contest problem.
You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a bridge.
Find the number of the edges that are bridges among the M edges.
Constraints
* 2 \leq N \leq 50
* N-1 \leq M \leq min(N(N−1)⁄2,50)
* 1 \leq a_i<b_i \leq N
* The given graph does not contain self-loops and double edges.
* The given graph is connected.
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M
Output
Print the number of the edges that are bridges among the M edges.
Examples
Input
7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7
Output
4
Input
3 3
1 2
1 3
2 3
Output
0
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
5 | instruction | 0 | 83,242 | 13 | 166,484 |
"Correct Solution:
```
import sys
n, m, *ab = map(int, sys.stdin.read().split())
graph = [set() for _ in range(n)]
for a, b in zip(*[iter(ab)] * 2):
a -= 1; b -= 1
graph[a].add(b); graph[b].add(a)
def main():
stack = [i for i in range(n) if len(graph[i]) == 1]
bridges = 0
while stack:
x = stack.pop()
if not graph[x]: continue
y = graph[x].pop()
graph[y].remove(x)
bridges += 1
if len(graph[y]) == 1:
stack.append(y)
print(bridges)
if __name__ == '__main__':
main()
``` | output | 1 | 83,242 | 13 | 166,485 |
Provide a correct Python 3 solution for this coding contest problem.
You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a bridge.
Find the number of the edges that are bridges among the M edges.
Constraints
* 2 \leq N \leq 50
* N-1 \leq M \leq min(N(N−1)⁄2,50)
* 1 \leq a_i<b_i \leq N
* The given graph does not contain self-loops and double edges.
* The given graph is connected.
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M
Output
Print the number of the edges that are bridges among the M edges.
Examples
Input
7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7
Output
4
Input
3 3
1 2
1 3
2 3
Output
0
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
5 | instruction | 0 | 83,243 | 13 | 166,486 |
"Correct Solution:
```
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(m)]
ad = [[0]*n for i in range(n)]
for x in ab:
ad[x[0]-1][x[1]-1] = 1
ad[x[1]-1][x[0]-1] = 1
nodes = [0]*n
ans = 0
flag = True
while flag:
flag = False
for i in range(n):
if nodes[i] == 0 and sum(ad[i]) == 1:
ans +=1
nodes[i] = 1
flag = True
for j in range(n):
if ad[i][j] == 1:
ad[j][i] =0
print(ans)
``` | output | 1 | 83,243 | 13 | 166,487 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a bridge.
Find the number of the edges that are bridges among the M edges.
Constraints
* 2 \leq N \leq 50
* N-1 \leq M \leq min(N(N−1)⁄2,50)
* 1 \leq a_i<b_i \leq N
* The given graph does not contain self-loops and double edges.
* The given graph is connected.
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M
Output
Print the number of the edges that are bridges among the M edges.
Examples
Input
7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7
Output
4
Input
3 3
1 2
1 3
2 3
Output
0
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
5
Submitted Solution:
```
N, M = map(int, input().split())
d=[[] for i in range(N)]
for i in range(M):
b, c = map(int, input().split())
d[b-1].append(c-1)
d[c-1].append(b-1)
a=0
while True:
count = 0
for i in range(N):
if len(d[i]) == 1:
d[d[i][0]].remove(i)
d[i]=[]
count += 1
if count == 0:
break
a+=count
print(a)
``` | instruction | 0 | 83,244 | 13 | 166,488 |
Yes | output | 1 | 83,244 | 13 | 166,489 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a bridge.
Find the number of the edges that are bridges among the M edges.
Constraints
* 2 \leq N \leq 50
* N-1 \leq M \leq min(N(N−1)⁄2,50)
* 1 \leq a_i<b_i \leq N
* The given graph does not contain self-loops and double edges.
* The given graph is connected.
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M
Output
Print the number of the edges that are bridges among the M edges.
Examples
Input
7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7
Output
4
Input
3 3
1 2
1 3
2 3
Output
0
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
5
Submitted Solution:
```
n,m=map(int,input().split())
v=[]
for i in range(m):
a,b=map(int,input().split())
v.append((a-1,b-1))
def dfs(x):
check[x]=True
for i in graph[x]:
if not check[i]:
dfs(i)
ans=0
for i in range(m):
graph=[[] for i in range(n)]
for j in range(m):
if j!=i:
graph[v[j][0]].append(v[j][1])
graph[v[j][1]].append(v[j][0])
check=[False]*n
dfs(0)
for j in check:
if not j:
ans+=1
break
print(ans)
``` | instruction | 0 | 83,245 | 13 | 166,490 |
Yes | output | 1 | 83,245 | 13 | 166,491 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a bridge.
Find the number of the edges that are bridges among the M edges.
Constraints
* 2 \leq N \leq 50
* N-1 \leq M \leq min(N(N−1)⁄2,50)
* 1 \leq a_i<b_i \leq N
* The given graph does not contain self-loops and double edges.
* The given graph is connected.
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M
Output
Print the number of the edges that are bridges among the M edges.
Examples
Input
7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7
Output
4
Input
3 3
1 2
1 3
2 3
Output
0
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
5
Submitted Solution:
```
N,M = map(int,input().split())
es = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(M)]
gr = [[] for i in range(N)]
for i,(a,b) in enumerate(es):
gr[a].append((b,i))
gr[b].append((a,i))
ans = 0
for i in range(M):
visited = [0] * N
stack = [0]
while stack:
v = stack.pop()
visited[v] = 1
for to,e in gr[v]:
if visited[to]: continue
if i==e: continue
stack.append(to)
if sum(visited) < N:
ans += 1
print(ans)
``` | instruction | 0 | 83,246 | 13 | 166,492 |
Yes | output | 1 | 83,246 | 13 | 166,493 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a bridge.
Find the number of the edges that are bridges among the M edges.
Constraints
* 2 \leq N \leq 50
* N-1 \leq M \leq min(N(N−1)⁄2,50)
* 1 \leq a_i<b_i \leq N
* The given graph does not contain self-loops and double edges.
* The given graph is connected.
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M
Output
Print the number of the edges that are bridges among the M edges.
Examples
Input
7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7
Output
4
Input
3 3
1 2
1 3
2 3
Output
0
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
5
Submitted Solution:
```
n, m = map(int, input().split())
l = []
edges = [[] for i in range(n)]
for i in range(m):
a, b = map(int, input().split())
l.append([a - 1, b - 1])
edges[a - 1].append(b - 1)
edges[b - 1].append(a - 1)
def dfs(cur, a, b):
if visited[cur] == 1:
return None
visited[cur] = 1
for v in edges[cur]:
if v != b or cur != a:
dfs(v, a, b)
ans = 0
for i in l:
i, j = i[0], i[1]
visited = [0 for i in range(n)]
dfs(i, i, j)
if visited[j] == 0:
ans += 1
print(ans)
``` | instruction | 0 | 83,247 | 13 | 166,494 |
Yes | output | 1 | 83,247 | 13 | 166,495 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a bridge.
Find the number of the edges that are bridges among the M edges.
Constraints
* 2 \leq N \leq 50
* N-1 \leq M \leq min(N(N−1)⁄2,50)
* 1 \leq a_i<b_i \leq N
* The given graph does not contain self-loops and double edges.
* The given graph is connected.
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M
Output
Print the number of the edges that are bridges among the M edges.
Examples
Input
7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7
Output
4
Input
3 3
1 2
1 3
2 3
Output
0
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
5
Submitted Solution:
```
from collections import deque
n, m = list(map(int, input().split()))
li = []
for _ in range(m):
li.append(list(map(int, input().split())))
# construct indirectional graphs
edges = [[] for _ in range(n)]
for i, j in li:
i -= 1
j -= 1
edges[i].append(j)
edges[j].append(i)
import copy
edges2 = copy.deepcopy(edges)
S = deque()
S.append([0])
while len(S) > 0:
path = S.pop()
# if full path,
if len(path) == n:
# used path list
used = [[] for _ in range(n)]
for i, j in zip(path[:-1], path[1:]):
used[i].append(j)
# delete unused edges
for i in range(len(edges2)):
for j in edges2[i][::-1]:
if j not in used[i]:
edges2[i].remove(j)
continue
for v in edges[path[-1]]:
if v not in path:
S.append(path + [v])
ans = sum(len(e) for e in edges2)
print(ans)
``` | instruction | 0 | 83,248 | 13 | 166,496 |
No | output | 1 | 83,248 | 13 | 166,497 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.
An edge whose removal disconnects the graph is called a bridge.
Find the number of the edges that are bridges among the M edges.
Constraints
* 2 \leq N \leq 50
* N-1 \leq M \leq min(N(N−1)⁄2,50)
* 1 \leq a_i<b_i \leq N
* The given graph does not contain self-loops and double edges.
* The given graph is connected.
Input
Input is given from Standard Input in the following format:
N M
a_1 b_1
a_2 b_2
:
a_M b_M
Output
Print the number of the edges that are bridges among the M edges.
Examples
Input
7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7
Output
4
Input
3 3
1 2
1 3
2 3
Output
0
Input
6 5
1 2
2 3
3 4
4 5
5 6
Output
5
Submitted Solution:
```
class UnionFind:
def __init__(self, n):
self.n = n
self.parents = [i for i in range(n)]
self.ranks = [0] * n
self.count = n
def union(self, x, y):
self.link(x,y)
def link(self, x, y):
if self.parents[x] == self.parents[y]:
return
if self.ranks[x] < self.ranks[y]:
self.parents[x] = y
else:
if self.ranks[y] == self.ranks[x]:
self.ranks[y] += 1
self.parents[y] = x
self.count -= 1
def find(self, x):
if self.parents[x] != x:
return self.find(self.parents[x])
else:
return self.parents[x]
N, M = list(map(int, input().split()))
bridges = []
for i in range(M):
bridges.append(list(map(int, input().split())))
# bridges = bridges[::-1]
# result = []
count = 0
for i in range(M):
# print(i, 'th ------')
uf = UnionFind(N)
for j in range(M):
x, y = bridges[j]
if j == i:
continue
else:
uf.union(x-1, y-1)
# print(' ', uf.count)
if uf.count > 1:
count += 1
# for x, y in bridges:
# uf.union(x-1, y-1)
# result.append(uf.get_group_num())
# print(result[::-1])
print(count)
``` | instruction | 0 | 83,249 | 13 | 166,498 |
No | output | 1 | 83,249 | 13 | 166,499 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.