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 |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.
The park can be represented as a connected graph with n nodes and m bidirectional edges. Initially Bob is at the node 1 and he records 1 on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes a_1, a_2, β¦, a_n is recorded.
Wandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it.
A sequence x is lexicographically smaller than a sequence y if and only if one of the following holds:
* x is a prefix of y, but x β y (this is impossible in this problem as all considered sequences have the same length);
* in the first position where x and y differ, the sequence x has a smaller element than the corresponding element in y.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 10^5), denoting the number of nodes and edges, respectively.
The following m lines describe the bidirectional edges in the graph. The i-th of these lines contains two integers u_i and v_i (1 β€ u_i, v_i β€ n), representing the nodes the i-th edge connects.
Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected.
Output
Output a line containing the lexicographically smallest sequence a_1, a_2, β¦, a_n Bob can record.
Examples
Input
3 2
1 2
1 3
Output
1 2 3
Input
5 5
1 4
3 4
5 4
3 2
1 5
Output
1 4 3 2 5
Input
10 10
1 4
6 8
2 5
3 7
9 4
5 6
3 4
8 10
8 9
1 10
Output
1 4 3 7 9 8 6 5 2 10
Note
In the first sample, Bob's optimal wandering path could be 1 β 2 β 1 β 3. Therefore, Bob will obtain the sequence \{1, 2, 3\}, which is the lexicographically smallest one.
In the second sample, Bob's optimal wandering path could be 1 β 4 β 3 β 2 β 3 β 4 β 1 β 5. Therefore, Bob will obtain the sequence \{1, 4, 3, 2, 5\}, which is the lexicographically smallest one. | instruction | 0 | 85,049 | 13 | 170,098 |
Tags: data structures, dfs and similar, graphs, greedy, shortest paths
Correct Solution:
```
from sys import stdin,stdout
from itertools import combinations
from collections import defaultdict,OrderedDict
import math
import heapq
def listIn():
return list((map(int,stdin.readline().strip().split())))
def stringListIn():
return([x for x in stdin.readline().split()])
def intIn():
return (int(stdin.readline()))
def stringIn():
return (stdin.readline().strip())
def dfs(root,parent):
vis[root]=1
q=[root]
heapq.heapify(q)
while(q):
r=heapq.heappop(q)
res.append(r)
lis=sorted(g[r])
for i in range(len(lis)):
if vis[lis[i]]==0:
#print(lis[i],q)
heapq.heappush(q,lis[i])
vis[lis[i]]=1
if __name__=="__main__":
n,m=listIn()
g=[[] for i in range(n+1)]
for i in range(m):
u,v=listIn()
g[u].append(v)
g[v].append(u)
vis=[0]*(n+1)
res=[]
dfs(1,-1)
print(*res)
``` | output | 1 | 85,049 | 13 | 170,099 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.
The park can be represented as a connected graph with n nodes and m bidirectional edges. Initially Bob is at the node 1 and he records 1 on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes a_1, a_2, β¦, a_n is recorded.
Wandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it.
A sequence x is lexicographically smaller than a sequence y if and only if one of the following holds:
* x is a prefix of y, but x β y (this is impossible in this problem as all considered sequences have the same length);
* in the first position where x and y differ, the sequence x has a smaller element than the corresponding element in y.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 10^5), denoting the number of nodes and edges, respectively.
The following m lines describe the bidirectional edges in the graph. The i-th of these lines contains two integers u_i and v_i (1 β€ u_i, v_i β€ n), representing the nodes the i-th edge connects.
Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected.
Output
Output a line containing the lexicographically smallest sequence a_1, a_2, β¦, a_n Bob can record.
Examples
Input
3 2
1 2
1 3
Output
1 2 3
Input
5 5
1 4
3 4
5 4
3 2
1 5
Output
1 4 3 2 5
Input
10 10
1 4
6 8
2 5
3 7
9 4
5 6
3 4
8 10
8 9
1 10
Output
1 4 3 7 9 8 6 5 2 10
Note
In the first sample, Bob's optimal wandering path could be 1 β 2 β 1 β 3. Therefore, Bob will obtain the sequence \{1, 2, 3\}, which is the lexicographically smallest one.
In the second sample, Bob's optimal wandering path could be 1 β 4 β 3 β 2 β 3 β 4 β 1 β 5. Therefore, Bob will obtain the sequence \{1, 4, 3, 2, 5\}, which is the lexicographically smallest one.
Submitted Solution:
```
from collections import defaultdict
import heapq
n, m = map(int, input().split())
graph = defaultdict(list)
for i in range(m):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
for i in range(n):
graph[i].sort()
visited = [0 for i in range(n + 1)]
ans = []
q = []
heapq.heappush(q, 1)
visited[1] = 1
while q:
v = heapq.heappop(q)
ans.append(v)
# visited[v] = 1
for i in graph[v]:
if visited[i] == 0:
heapq.heappush(q, i)
visited[i] = 1
# getOrder(1, graph, ans, visited)
for i in ans:
print(i, end=' ')
print('')
``` | instruction | 0 | 85,050 | 13 | 170,100 |
Yes | output | 1 | 85,050 | 13 | 170,101 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.
The park can be represented as a connected graph with n nodes and m bidirectional edges. Initially Bob is at the node 1 and he records 1 on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes a_1, a_2, β¦, a_n is recorded.
Wandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it.
A sequence x is lexicographically smaller than a sequence y if and only if one of the following holds:
* x is a prefix of y, but x β y (this is impossible in this problem as all considered sequences have the same length);
* in the first position where x and y differ, the sequence x has a smaller element than the corresponding element in y.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 10^5), denoting the number of nodes and edges, respectively.
The following m lines describe the bidirectional edges in the graph. The i-th of these lines contains two integers u_i and v_i (1 β€ u_i, v_i β€ n), representing the nodes the i-th edge connects.
Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected.
Output
Output a line containing the lexicographically smallest sequence a_1, a_2, β¦, a_n Bob can record.
Examples
Input
3 2
1 2
1 3
Output
1 2 3
Input
5 5
1 4
3 4
5 4
3 2
1 5
Output
1 4 3 2 5
Input
10 10
1 4
6 8
2 5
3 7
9 4
5 6
3 4
8 10
8 9
1 10
Output
1 4 3 7 9 8 6 5 2 10
Note
In the first sample, Bob's optimal wandering path could be 1 β 2 β 1 β 3. Therefore, Bob will obtain the sequence \{1, 2, 3\}, which is the lexicographically smallest one.
In the second sample, Bob's optimal wandering path could be 1 β 4 β 3 β 2 β 3 β 4 β 1 β 5. Therefore, Bob will obtain the sequence \{1, 4, 3, 2, 5\}, which is the lexicographically smallest one.
Submitted Solution:
```
from heapq import heappop, heappush
n, m = map(int, input().split())
vis = [0]*2+[1]*(n-1) #nunca vamos a pasar por 0 y se parte en 1, por eso el resto es 1
A=[]
for i in range(n+1): #es n+1, porque la primera posicion es 1 y no 0
A.append([])
for i in range(m):
x, y = map(int, input().split()) #se interconectan los vertices x, y
A[x].append(y)
A[y].append(x)
rec = [1] #partimos al inicio
while rec:
pos = heappop(rec)
print(pos) #se printea la posicion en que se estΓ‘
for i in A[pos]:
if vis[i]: #si es un "1" significa que aun no se printea el valor, por lo tanto vamos a pasar por ahΓ
vis[i] = 0 #no hay que volver a printear el vlaor por el que vamos a pasar
heappush(rec, i)
``` | instruction | 0 | 85,051 | 13 | 170,102 |
Yes | output | 1 | 85,051 | 13 | 170,103 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.
The park can be represented as a connected graph with n nodes and m bidirectional edges. Initially Bob is at the node 1 and he records 1 on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes a_1, a_2, β¦, a_n is recorded.
Wandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it.
A sequence x is lexicographically smaller than a sequence y if and only if one of the following holds:
* x is a prefix of y, but x β y (this is impossible in this problem as all considered sequences have the same length);
* in the first position where x and y differ, the sequence x has a smaller element than the corresponding element in y.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 10^5), denoting the number of nodes and edges, respectively.
The following m lines describe the bidirectional edges in the graph. The i-th of these lines contains two integers u_i and v_i (1 β€ u_i, v_i β€ n), representing the nodes the i-th edge connects.
Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected.
Output
Output a line containing the lexicographically smallest sequence a_1, a_2, β¦, a_n Bob can record.
Examples
Input
3 2
1 2
1 3
Output
1 2 3
Input
5 5
1 4
3 4
5 4
3 2
1 5
Output
1 4 3 2 5
Input
10 10
1 4
6 8
2 5
3 7
9 4
5 6
3 4
8 10
8 9
1 10
Output
1 4 3 7 9 8 6 5 2 10
Note
In the first sample, Bob's optimal wandering path could be 1 β 2 β 1 β 3. Therefore, Bob will obtain the sequence \{1, 2, 3\}, which is the lexicographically smallest one.
In the second sample, Bob's optimal wandering path could be 1 β 4 β 3 β 2 β 3 β 4 β 1 β 5. Therefore, Bob will obtain the sequence \{1, 4, 3, 2, 5\}, which is the lexicographically smallest one.
Submitted Solution:
```
import heapq
a, b = list(input().split(" "))
a = int(a)
b = int(b)
g = [[] for i in range(a)]
for i in range(b):
u, v = list(input().split(" "))
u = int(u) - 1
v = int(v) - 1
g[u] += [v]
g[v] += [u]
visited = [False for i in range(a)]
for j in range(a):
if(visited[j] == False):
l = [j]
heapq.heapify(l)
ans = []
while(len(l) != 0):
u = heapq.heappop(l)
visited[u] = True
ans += [u + 1]
for i in g[u]:
if(visited[i] == True):
continue
heapq.heappush(l, i)
visited[i] = True
for i in ans:
print(i, end=' ')
print()
``` | instruction | 0 | 85,052 | 13 | 170,104 |
Yes | output | 1 | 85,052 | 13 | 170,105 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.
The park can be represented as a connected graph with n nodes and m bidirectional edges. Initially Bob is at the node 1 and he records 1 on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes a_1, a_2, β¦, a_n is recorded.
Wandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it.
A sequence x is lexicographically smaller than a sequence y if and only if one of the following holds:
* x is a prefix of y, but x β y (this is impossible in this problem as all considered sequences have the same length);
* in the first position where x and y differ, the sequence x has a smaller element than the corresponding element in y.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 10^5), denoting the number of nodes and edges, respectively.
The following m lines describe the bidirectional edges in the graph. The i-th of these lines contains two integers u_i and v_i (1 β€ u_i, v_i β€ n), representing the nodes the i-th edge connects.
Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected.
Output
Output a line containing the lexicographically smallest sequence a_1, a_2, β¦, a_n Bob can record.
Examples
Input
3 2
1 2
1 3
Output
1 2 3
Input
5 5
1 4
3 4
5 4
3 2
1 5
Output
1 4 3 2 5
Input
10 10
1 4
6 8
2 5
3 7
9 4
5 6
3 4
8 10
8 9
1 10
Output
1 4 3 7 9 8 6 5 2 10
Note
In the first sample, Bob's optimal wandering path could be 1 β 2 β 1 β 3. Therefore, Bob will obtain the sequence \{1, 2, 3\}, which is the lexicographically smallest one.
In the second sample, Bob's optimal wandering path could be 1 β 4 β 3 β 2 β 3 β 4 β 1 β 5. Therefore, Bob will obtain the sequence \{1, 4, 3, 2, 5\}, which is the lexicographically smallest one.
Submitted Solution:
```
from heapq import *
n, m = map(int, input().split())
g = {}
for _ in range(m):
u, v = map(int, input().split())
g.setdefault(u, set()).add(v)
g.setdefault(v, set()).add(u)
d = []
V = set()
h = [1]
while h:
v = heappop(h)
if v in V:
continue
V.add(v)
d.append(v)
for u in g[v]:
heappush(h, u)
print(*d)
``` | instruction | 0 | 85,053 | 13 | 170,106 |
Yes | output | 1 | 85,053 | 13 | 170,107 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.
The park can be represented as a connected graph with n nodes and m bidirectional edges. Initially Bob is at the node 1 and he records 1 on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes a_1, a_2, β¦, a_n is recorded.
Wandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it.
A sequence x is lexicographically smaller than a sequence y if and only if one of the following holds:
* x is a prefix of y, but x β y (this is impossible in this problem as all considered sequences have the same length);
* in the first position where x and y differ, the sequence x has a smaller element than the corresponding element in y.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 10^5), denoting the number of nodes and edges, respectively.
The following m lines describe the bidirectional edges in the graph. The i-th of these lines contains two integers u_i and v_i (1 β€ u_i, v_i β€ n), representing the nodes the i-th edge connects.
Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected.
Output
Output a line containing the lexicographically smallest sequence a_1, a_2, β¦, a_n Bob can record.
Examples
Input
3 2
1 2
1 3
Output
1 2 3
Input
5 5
1 4
3 4
5 4
3 2
1 5
Output
1 4 3 2 5
Input
10 10
1 4
6 8
2 5
3 7
9 4
5 6
3 4
8 10
8 9
1 10
Output
1 4 3 7 9 8 6 5 2 10
Note
In the first sample, Bob's optimal wandering path could be 1 β 2 β 1 β 3. Therefore, Bob will obtain the sequence \{1, 2, 3\}, which is the lexicographically smallest one.
In the second sample, Bob's optimal wandering path could be 1 β 4 β 3 β 2 β 3 β 4 β 1 β 5. Therefore, Bob will obtain the sequence \{1, 4, 3, 2, 5\}, which is the lexicographically smallest one.
Submitted Solution:
```
from heapq import *
n,m=map(int,input().split())
d=[[]]*(n+1)
for i in range(m):
a,b=map(int,input().split())
d[a].append(b)
d[b].append(a)
vis=[0,0]+[1]*(n-1)
oldv=[1]
while oldv:
act=heappop(oldv)
print(act)
for a in d[act]:
if vis[a]==1:
vis[a]=0
heappush(oldv,a)
``` | instruction | 0 | 85,054 | 13 | 170,108 |
No | output | 1 | 85,054 | 13 | 170,109 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.
The park can be represented as a connected graph with n nodes and m bidirectional edges. Initially Bob is at the node 1 and he records 1 on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes a_1, a_2, β¦, a_n is recorded.
Wandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it.
A sequence x is lexicographically smaller than a sequence y if and only if one of the following holds:
* x is a prefix of y, but x β y (this is impossible in this problem as all considered sequences have the same length);
* in the first position where x and y differ, the sequence x has a smaller element than the corresponding element in y.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 10^5), denoting the number of nodes and edges, respectively.
The following m lines describe the bidirectional edges in the graph. The i-th of these lines contains two integers u_i and v_i (1 β€ u_i, v_i β€ n), representing the nodes the i-th edge connects.
Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected.
Output
Output a line containing the lexicographically smallest sequence a_1, a_2, β¦, a_n Bob can record.
Examples
Input
3 2
1 2
1 3
Output
1 2 3
Input
5 5
1 4
3 4
5 4
3 2
1 5
Output
1 4 3 2 5
Input
10 10
1 4
6 8
2 5
3 7
9 4
5 6
3 4
8 10
8 9
1 10
Output
1 4 3 7 9 8 6 5 2 10
Note
In the first sample, Bob's optimal wandering path could be 1 β 2 β 1 β 3. Therefore, Bob will obtain the sequence \{1, 2, 3\}, which is the lexicographically smallest one.
In the second sample, Bob's optimal wandering path could be 1 β 4 β 3 β 2 β 3 β 4 β 1 β 5. Therefore, Bob will obtain the sequence \{1, 4, 3, 2, 5\}, which is the lexicographically smallest one.
Submitted Solution:
```
n,m=map(int,input().split())
graph=[[] for _ in range(n)]
for i in range(m):
u,v=map(int,input().split())
graph[u-1].append(v-1)
graph[v-1].append(u-1)
q=[]
q=[0]
visited={0:True}
l=[]
while q:
a=q.pop()
l.append(a+1)
graph[a].sort()
graph[a].reverse()
for i in graph[a]:
if i not in visited:
visited[i]=True
q.append(i)
print(" ".join(str(x) for x in l))
``` | instruction | 0 | 85,055 | 13 | 170,110 |
No | output | 1 | 85,055 | 13 | 170,111 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.
The park can be represented as a connected graph with n nodes and m bidirectional edges. Initially Bob is at the node 1 and he records 1 on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes a_1, a_2, β¦, a_n is recorded.
Wandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it.
A sequence x is lexicographically smaller than a sequence y if and only if one of the following holds:
* x is a prefix of y, but x β y (this is impossible in this problem as all considered sequences have the same length);
* in the first position where x and y differ, the sequence x has a smaller element than the corresponding element in y.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 10^5), denoting the number of nodes and edges, respectively.
The following m lines describe the bidirectional edges in the graph. The i-th of these lines contains two integers u_i and v_i (1 β€ u_i, v_i β€ n), representing the nodes the i-th edge connects.
Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected.
Output
Output a line containing the lexicographically smallest sequence a_1, a_2, β¦, a_n Bob can record.
Examples
Input
3 2
1 2
1 3
Output
1 2 3
Input
5 5
1 4
3 4
5 4
3 2
1 5
Output
1 4 3 2 5
Input
10 10
1 4
6 8
2 5
3 7
9 4
5 6
3 4
8 10
8 9
1 10
Output
1 4 3 7 9 8 6 5 2 10
Note
In the first sample, Bob's optimal wandering path could be 1 β 2 β 1 β 3. Therefore, Bob will obtain the sequence \{1, 2, 3\}, which is the lexicographically smallest one.
In the second sample, Bob's optimal wandering path could be 1 β 4 β 3 β 2 β 3 β 4 β 1 β 5. Therefore, Bob will obtain the sequence \{1, 4, 3, 2, 5\}, which is the lexicographically smallest one.
Submitted Solution:
```
n,m = map(int,input().split(' '))
li = [[] for i in range(n+1)]
check = [0 for i in range(n+1)]
hubo = []
check[0] = 1
def dfs(l,c,h):
cc =1
while cc != n:
h.sort()
#idx = 0
#while c[h[idx]] == 1:
#idx += 1
print(h[0],end=' ')
c[h[0]] = 1
cc += 1
tt = list(filter(lambda x: c[x] == 0,li[h[0]]))
h = h[1:] + tt
for _ in range(m):
u,v = map(int,input().split(' '))
li[u].append(v)
li[v].append(u)
'''for i in li:
i.sort()'''
li[1].sort()
#print(li)
print(1,end=' ')
check[1] = 1
hubo = li[1]
dfs(li[1],check,hubo)
``` | instruction | 0 | 85,056 | 13 | 170,112 |
No | output | 1 | 85,056 | 13 | 170,113 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.
The park can be represented as a connected graph with n nodes and m bidirectional edges. Initially Bob is at the node 1 and he records 1 on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes a_1, a_2, β¦, a_n is recorded.
Wandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it.
A sequence x is lexicographically smaller than a sequence y if and only if one of the following holds:
* x is a prefix of y, but x β y (this is impossible in this problem as all considered sequences have the same length);
* in the first position where x and y differ, the sequence x has a smaller element than the corresponding element in y.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 10^5), denoting the number of nodes and edges, respectively.
The following m lines describe the bidirectional edges in the graph. The i-th of these lines contains two integers u_i and v_i (1 β€ u_i, v_i β€ n), representing the nodes the i-th edge connects.
Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected.
Output
Output a line containing the lexicographically smallest sequence a_1, a_2, β¦, a_n Bob can record.
Examples
Input
3 2
1 2
1 3
Output
1 2 3
Input
5 5
1 4
3 4
5 4
3 2
1 5
Output
1 4 3 2 5
Input
10 10
1 4
6 8
2 5
3 7
9 4
5 6
3 4
8 10
8 9
1 10
Output
1 4 3 7 9 8 6 5 2 10
Note
In the first sample, Bob's optimal wandering path could be 1 β 2 β 1 β 3. Therefore, Bob will obtain the sequence \{1, 2, 3\}, which is the lexicographically smallest one.
In the second sample, Bob's optimal wandering path could be 1 β 4 β 3 β 2 β 3 β 4 β 1 β 5. Therefore, Bob will obtain the sequence \{1, 4, 3, 2, 5\}, which is the lexicographically smallest one.
Submitted Solution:
```
visited = []
def dfs(graph,node):
global visited
if node not in visited:
visited.append(node)
for n in sorted(graph[node]):
dfs(graph,n)
def D():
n , m = map( int , input().split() )
graph = {i:[] for i in range(1,n+1)}
for i in range(m):
a , b = map( int , input().split() )
graph[a].append(b)
graph[b].append(a)
dfs(graph, 1)
print(" ".join([str(x) for x in visited]))
D()
``` | instruction | 0 | 85,057 | 13 | 170,114 |
No | output | 1 | 85,057 | 13 | 170,115 |
Provide a correct Python 3 solution for this coding contest problem.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5. | instruction | 0 | 85,392 | 13 | 170,784 |
"Correct Solution:
```
# Test
#####################################################################################################################
def main():
tree_sLength = int(input()) - 1
tree = {}
for i in range(tree_sLength):
v1, v2 = map(int, input().split())
tree[v1] = tree.get(v1, ()) + (v2, )
tree[v2] = tree.get(v2, ()) + (v1, )
return nLifeLines(tree)
def nLifeLines(tree):
return sum(subTree_snLifeLines(len(tree[x])) for x in tree)//2
def subTree_snLifeLines(nBranches):
return nBranches*(nBranches - 1)
if __name__ == '__main__':
print(main())
``` | output | 1 | 85,392 | 13 | 170,785 |
Provide a correct Python 3 solution for this coding contest problem.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5. | instruction | 0 | 85,393 | 13 | 170,786 |
"Correct Solution:
```
n = int(input())
t = [0] * n
for _ in range(n-1):
a, b = input().split(' ')
a, b = [int(a), int(b)]
t[a-1] += 1
t[b-1] += 1
out = [(e * (e-1)) / 2 for e in t]
print(int(sum(out)))
``` | output | 1 | 85,393 | 13 | 170,787 |
Provide a correct Python 3 solution for this coding contest problem.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5. | instruction | 0 | 85,394 | 13 | 170,788 |
"Correct Solution:
```
I=input
d=[0]*10010
for _ in '0'*(int(I())-1):x,y=map(int,I().split());d[x]+=1;d[y]+=1
print(sum(i*(i-1)for i in d)//2)
``` | output | 1 | 85,394 | 13 | 170,789 |
Provide a correct Python 3 solution for this coding contest problem.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5. | instruction | 0 | 85,395 | 13 | 170,790 |
"Correct Solution:
```
def computeDegrees(n):
degrees = [0 for vertex in range(n)]
for edge in range(n-1):
v1, v2 = map(int, input().split())
degrees[v1-1] += 1
degrees[v2-1] += 1
return degrees
def computeNumberOfLength2Paths(degrees, n):
return int(sum(d**2 for d in degrees)/2 - n + 1)
if __name__ == '__main__':
n = int(input())
degrees = computeDegrees(n)
print(computeNumberOfLength2Paths(degrees, n))
``` | output | 1 | 85,395 | 13 | 170,791 |
Provide a correct Python 3 solution for this coding contest problem.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5. | instruction | 0 | 85,396 | 13 | 170,792 |
"Correct Solution:
```
def main():
n = int(input())
l = [0] * (n + 1)
for _ in range(n - 1):
a, b = map(int, input().split())
l[a] += 1
l[b] += 1
res = 0
for x in l:
res += x * (x - 1)
print(res // 2)
if __name__ == '__main__':
main()
``` | output | 1 | 85,396 | 13 | 170,793 |
Provide a correct Python 3 solution for this coding contest problem.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5. | instruction | 0 | 85,397 | 13 | 170,794 |
"Correct Solution:
```
n=int(input())
L=[]
S=0
for k in range(n):
L.append(0)
for k in range(n-1):
a,b=map(int,input().split())
L[a-1]+=1
L[b-1]+=1
for k in range(n):
S+=L[k]*(L[k]-1)/2
print(int(S))
``` | output | 1 | 85,397 | 13 | 170,795 |
Provide a correct Python 3 solution for this coding contest problem.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5. | instruction | 0 | 85,398 | 13 | 170,796 |
"Correct Solution:
```
def main():
n = int(input())
l = [-1] * (n + 1)
for _ in range(n - 1):
a, b = map(int, input().split())
l[a] += 1
l[b] += 1
res = 0
for x in filter(None, l):
res += x * (x + 1)
print(res // 2)
if __name__ == '__main__':
main()
``` | output | 1 | 85,398 | 13 | 170,797 |
Provide a correct Python 3 solution for this coding contest problem.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5. | instruction | 0 | 85,399 | 13 | 170,798 |
"Correct Solution:
```
n = int(input())
d = n * [0]
for i in range(n - 1):
a, b = map(int, input().split())
d[a - 1] += 1
d[b - 1] += 1
cnt = 0
for i in d:
cnt += (i * (i - 1)) // 2
print(cnt)
``` | output | 1 | 85,399 | 13 | 170,799 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5.
Submitted Solution:
```
n = int(input())
d = [0] * (n + 1)
for _ in range(n - 1):
x, y = map(int, input().split())
d[x] += 1
d[y] += 1
s = 0
for x in range(1, n + 1):
s += d[x] * (d[x] - 1)
print(s // 2)
``` | instruction | 0 | 85,400 | 13 | 170,800 |
Yes | output | 1 | 85,400 | 13 | 170,801 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5.
Submitted Solution:
```
a=int(input());b=[0]*(a+1)
for _ in " "*(a-1):u,v=map(int,input().split());b[u]+=1;b[v]+=1
print(sum((i*(i-1))//2 for i in b))
``` | instruction | 0 | 85,401 | 13 | 170,802 |
Yes | output | 1 | 85,401 | 13 | 170,803 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5.
Submitted Solution:
```
n = int(input())
bounds_amount = [-1]*n
for _ in range(n-1):
for el in map(int, input().split(' ')): bounds_amount[el-1] += 1
print(sum(map(lambda x: (x*(x+1))//2, bounds_amount)))
``` | instruction | 0 | 85,402 | 13 | 170,804 |
Yes | output | 1 | 85,402 | 13 | 170,805 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5.
Submitted Solution:
```
def computeDegrees(n):
degrees = [0 for vertex in range(n)]
for edge in range(n-1):
v1, v2 = map(int, input().split())
degrees[v1-1] += 1
degrees[v2-1] += 1
return degrees
def computeNumberOfLength2Paths(degrees):
return int( sum(d*(d-1) for d in degrees)/2 )
if __name__ == '__main__':
n = int(input())
degrees = computeDegrees(n)
print(computeNumberOfLength2Paths(degrees))
``` | instruction | 0 | 85,403 | 13 | 170,806 |
Yes | output | 1 | 85,403 | 13 | 170,807 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5.
Submitted Solution:
```
print(int(input())-1)
``` | instruction | 0 | 85,404 | 13 | 170,808 |
No | output | 1 | 85,404 | 13 | 170,809 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5.
Submitted Solution:
```
from sys import stdin
a=int(stdin.readline());ans=0
z=[list(map(int,stdin.readline().split())) for _ in " "*(a-1)]
w=[]
for i in range(a-1):
s=z[i]
for j in range(a-1):
if i!=j:
if [max(i,j),min(i,j)] in w:continue
if s[0]==z[j][1] or s[0]==z[j][0]:ans+=1;w.append([max(i,j),min(i,j)])
print(ans)
``` | instruction | 0 | 85,405 | 13 | 170,810 |
No | output | 1 | 85,405 | 13 | 170,811 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5.
Submitted Solution:
```
print(1)
``` | instruction | 0 | 85,406 | 13 | 170,812 |
No | output | 1 | 85,406 | 13 | 170,813 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5.
Submitted Solution:
```
n = int("4")
sample = ["1 2 ", "1 3 ", "1 4 "]
graph = list()
for i in range(n):
graph.append(set())
for j in range(n - 1):
v1,v2 = map(int,sample[j].split())
graph[v1 - 1].add(v2)
graph[v2 - 1].add(v1)
result = 0
for k in range(n):
d = len(graph[k])
result += d*(d - 1)/2
print(result)
``` | instruction | 0 | 85,407 | 13 | 170,814 |
No | output | 1 | 85,407 | 13 | 170,815 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
ZS the Coder has a large tree. It can be represented as an undirected connected graph of n vertices numbered from 0 to n - 1 and n - 1 edges between them. There is a single nonzero digit written on each edge.
One day, ZS the Coder was bored and decided to investigate some properties of the tree. He chose a positive integer M, which is coprime to 10, i.e. <image>.
ZS consider an ordered pair of distinct vertices (u, v) interesting when if he would follow the shortest path from vertex u to vertex v and write down all the digits he encounters on his path in the same order, he will get a decimal representaion of an integer divisible by M.
Formally, ZS consider an ordered pair of distinct vertices (u, v) interesting if the following states true:
* Let a1 = u, a2, ..., ak = v be the sequence of vertices on the shortest path from u to v in the order of encountering them;
* Let di (1 β€ i < k) be the digit written on the edge between vertices ai and ai + 1;
* The integer <image> is divisible by M.
Help ZS the Coder find the number of interesting pairs!
Input
The first line of the input contains two integers, n and M (2 β€ n β€ 100 000, 1 β€ M β€ 109, <image>) β the number of vertices and the number ZS has chosen respectively.
The next n - 1 lines contain three integers each. i-th of them contains ui, vi and wi, denoting an edge between vertices ui and vi with digit wi written on it (0 β€ ui, vi < n, 1 β€ wi β€ 9).
Output
Print a single integer β the number of interesting (by ZS the Coder's consideration) pairs.
Examples
Input
6 7
0 1 2
4 2 4
2 0 1
3 0 9
2 5 7
Output
7
Input
5 11
1 2 3
2 0 3
3 0 3
4 3 3
Output
8
Note
In the first sample case, the interesting pairs are (0, 4), (1, 2), (1, 5), (3, 2), (2, 5), (5, 2), (3, 5). The numbers that are formed by these pairs are 14, 21, 217, 91, 7, 7, 917 respectively, which are all multiples of 7. Note that (2, 5) and (5, 2) are considered different.
<image>
In the second sample case, the interesting pairs are (4, 0), (0, 4), (3, 2), (2, 3), (0, 1), (1, 0), (4, 1), (1, 4), and 6 of these pairs give the number 33 while 2 of them give the number 3333, which are all multiples of 11.
<image>
Submitted Solution:
```
print(7);
``` | instruction | 0 | 85,408 | 13 | 170,816 |
No | output | 1 | 85,408 | 13 | 170,817 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph.
Ostap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than k. Distance between two vertices of the tree is the minimum possible number of edges of the path between them.
As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100, 0 β€ k β€ min(20, n - 1)) β the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for k.
Each of the next n - 1 lines contain two integers ui and vi (1 β€ ui, vi β€ n) β indices of vertices, connected by the i-th edge. It's guaranteed that given graph is a tree.
Output
Print one integer β the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7).
Examples
Input
2 0
1 2
Output
1
Input
2 1
1 2
Output
3
Input
4 1
1 2
2 3
3 4
Output
9
Input
7 2
1 2
2 3
1 4
4 5
1 6
6 7
Output
91
Note
In the first sample, Ostap has to paint both vertices black.
In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both.
In the third sample, the valid ways to paint vertices are: {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}. | instruction | 0 | 85,409 | 13 | 170,818 |
Tags: dp, trees
Correct Solution:
```
def main():
n, k = map(int, input().split())
cnt = [[[0] * 21 for _ in (0, 1)] for _ in range(n + 1)]
edges, mod = [[] for _ in range(n + 1)], 1000000007
for _ in range(n - 1):
u, v = map(int, input().split())
edges[u].append(v)
edges[v].append(u)
def dfs(u, f):
cnt[u][0][0] = cnt[u][1][k] = 1
for v in edges[u]:
if v != f:
dfs(v, u)
tmp0, tmp1 = [0] * 21, [0] * 21
for i in range(k + 1):
for j in range(k + 1):
if i != k:
tmp0[j if i < j else i + 1] += cnt[u][0][j] * cnt[v][0][i]
if i < j:
tmp1[j] += cnt[u][1][j] * cnt[v][0][i]
elif i != k:
tmp0[i + 1] += cnt[u][1][j] * cnt[v][0][i]
if i > j:
tmp1[i - 1] += cnt[u][0][j] * cnt[v][1][i]
else:
tmp0[j] += cnt[u][0][j] * cnt[v][1][i]
tmp1[max(i - 1, j)] += cnt[u][1][j] * cnt[v][1][i]
for i in range(21):
tmp0[i] %= mod
tmp1[i] %= mod
cnt[u][0] = tmp0
cnt[u][1] = tmp1
dfs(1, 1)
print(sum(cnt[1][1][j] for j in range(k + 1)) % mod)
if __name__ == '__main__':
main()
``` | output | 1 | 85,409 | 13 | 170,819 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph.
Ostap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than k. Distance between two vertices of the tree is the minimum possible number of edges of the path between them.
As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100, 0 β€ k β€ min(20, n - 1)) β the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for k.
Each of the next n - 1 lines contain two integers ui and vi (1 β€ ui, vi β€ n) β indices of vertices, connected by the i-th edge. It's guaranteed that given graph is a tree.
Output
Print one integer β the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7).
Examples
Input
2 0
1 2
Output
1
Input
2 1
1 2
Output
3
Input
4 1
1 2
2 3
3 4
Output
9
Input
7 2
1 2
2 3
1 4
4 5
1 6
6 7
Output
91
Note
In the first sample, Ostap has to paint both vertices black.
In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both.
In the third sample, the valid ways to paint vertices are: {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}.
Submitted Solution:
```
def main():
n, k = map(int, input().split())
cnt = [[[0] * 21 for _ in (0, 1)] for _ in range(n + 1)]
edges, mod = [[] for _ in range(n + 1)], 100000007
for _ in range(n - 1):
u, v = map(int, input().split())
edges[u].append(v)
edges[v].append(u)
def dfs(u, f):
cnt[u][0][0] = cnt[u][1][k] = 1
for v in edges[u]:
if v != f:
dfs(v, u)
tmp0, tmp1 = [0] * 21, [0] * 21
for i in range(k + 1):
for j in range(k + 1):
if i != k:
tmp0[j if i < j else i + 1] += cnt[u][0][j] * cnt[v][0][i]
if i < j:
tmp1[j] += cnt[u][1][j] * cnt[v][0][i]
elif i != k:
tmp0[i + 1] += cnt[u][1][j] * cnt[v][0][i]
if i > j:
tmp1[i - 1] += cnt[u][0][j] * cnt[v][1][i]
else:
tmp0[j] += cnt[u][0][j] * cnt[v][1][i]
tmp1[max(i - 1, j)] += cnt[u][1][j] * cnt[v][1][i]
for i in range(21):
tmp0[i] %= mod
tmp1[i] %= mod
cnt[u][0] = tmp0
cnt[u][1] = tmp1
dfs(1, 1)
print(sum(cnt[1][1][j] for j in range(k + 1)) % mod)
if __name__ == '__main__':
main()
``` | instruction | 0 | 85,410 | 13 | 170,820 |
No | output | 1 | 85,410 | 13 | 170,821 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph.
Ostap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than k. Distance between two vertices of the tree is the minimum possible number of edges of the path between them.
As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one.
Input
The first line of the input contains two integers n and k (1 β€ n β€ 100, 0 β€ k β€ min(20, n - 1)) β the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for k.
Each of the next n - 1 lines contain two integers ui and vi (1 β€ ui, vi β€ n) β indices of vertices, connected by the i-th edge. It's guaranteed that given graph is a tree.
Output
Print one integer β the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7).
Examples
Input
2 0
1 2
Output
1
Input
2 1
1 2
Output
3
Input
4 1
1 2
2 3
3 4
Output
9
Input
7 2
1 2
2 3
1 4
4 5
1 6
6 7
Output
91
Note
In the first sample, Ostap has to paint both vertices black.
In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both.
In the third sample, the valid ways to paint vertices are: {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}.
Submitted Solution:
```
def main():
n, k = map(int, input().split())
cnt = [[[0] * 21 for _ in (0, 1)] for _ in range(n + 1)]
edges, mod = [[] for _ in range(n + 1)], 10000007
for _ in range(n - 1):
u, v = map(int, input().split())
edges[u].append(v)
edges[v].append(u)
def dfs(u, f):
cnt[u][0][0] = cnt[u][1][k] = 1
for v in edges[u]:
if v != f:
dfs(v, u)
tmp0, tmp1 = [0] * 21, [0] * 21
for i in range(k + 1):
for j in range(k + 1):
if i != k:
tmp0[j if i < j else i + 1] += cnt[u][0][j] * cnt[v][0][i]
if i < j:
tmp1[j] += cnt[u][1][j] * cnt[v][0][i]
elif i != k:
tmp0[i + 1] += cnt[u][1][j] * cnt[v][0][i]
if i > j:
tmp1[i - 1] += cnt[u][0][j] * cnt[v][1][i]
else:
tmp0[j] += cnt[u][0][j] * cnt[v][1][i]
tmp1[max(i - 1, j)] += cnt[u][1][j] * cnt[v][1][i]
for i in range(21):
tmp0[i] %= mod
tmp1[i] %= mod
cnt[u][0] = tmp0
cnt[u][1] = tmp1
dfs(1, 1)
print(sum(cnt[1][1][j] for j in range(k + 1)) % mod)
if __name__ == '__main__':
main()
``` | instruction | 0 | 85,411 | 13 | 170,822 |
No | output | 1 | 85,411 | 13 | 170,823 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image> | instruction | 0 | 85,759 | 13 | 171,518 |
Tags: trees
Correct Solution:
```
import sys
input = sys.stdin.readline
def getN():
return int(input())
def getList():
return list(map(int, input().split()))
from sys import exit
n = getN()
vertex = [[] for i in range(n)]
for i in range(n-1):
a, b = getList()
vertex[a-1].append(b-1)
vertex[b - 1].append(a - 1)
for v in vertex:
if len(v) == 2:
print("NO")
exit()
print("YES")
``` | output | 1 | 85,759 | 13 | 171,519 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image> | instruction | 0 | 85,760 | 13 | 171,520 |
Tags: trees
Correct Solution:
```
n = int(input())
deg = [0]*n
for i in range(n-1):
u, v = map(int, input().split())
deg[u-1] += 1
deg[v-1] += 1
if all(i != 2 for i in deg):
print("YES")
else:
print("NO")
# cnt = sum(1 for i in deg if i == 1)
# if cnt*(cnt-1)//2 >= n - 1:
# print("YES")
# else:
# print("NO")
``` | output | 1 | 85,760 | 13 | 171,521 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image> | instruction | 0 | 85,761 | 13 | 171,522 |
Tags: trees
Correct Solution:
```
n = int(input())
a = []
for i in range(0,n+9):
a.append(0)
for i in range(1,n):
u,v = map(int,input().split())
a[u] = a[u]+1
a[v] = a[v]+1
flag = 1;
for i in range(1,n+1):
if a[i]==2:
flag = 0
if flag==0:
print("NO")
else:
print("YES")
``` | output | 1 | 85,761 | 13 | 171,523 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image> | instruction | 0 | 85,762 | 13 | 171,524 |
Tags: trees
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**9+7
import threading
threading.stack_size(10**8)
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
def solve():
n = int(ri())
g = [ [] for i in range(n)]
for i in range(n-1):
a,b = Ri();a-=1;b-=1
g[a].append(b);g[b].append(a)
# child = [0]*n
# print(g)
flag = [True]
def dfs1(cur, par):
if len(g[cur]) == 2:
flag[0] = False
return
for child in g[cur]:
if child == par: continue
dfs1(child, cur)
return
dfs1(0, -1)
if flag[0]:
YES()
else:
NO()
threading.Thread(target= solve).start()
``` | output | 1 | 85,762 | 13 | 171,525 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image> | instruction | 0 | 85,763 | 13 | 171,526 |
Tags: trees
Correct Solution:
```
n = int(input())
g = [[] for i in range(n+1)]
d = [0]*100001
for i in range(n-1):
u, v = [int(i) for i in input().split()]
g[u].append(v)
g[v].append(u)
d[u] += 1
d[v] += 1
for i in d:
if i == 2:
print("NO")
break
else:
print("YES")
``` | output | 1 | 85,763 | 13 | 171,527 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image> | instruction | 0 | 85,764 | 13 | 171,528 |
Tags: trees
Correct Solution:
```
n=int(input())
deg=[0]*n
for i in range(n-1):
u,v=map(int,input().split())
u-=1
v-=1
deg[u]+=1
deg[v]+=1
for d in deg:
if d==2:
print("NO")
exit(0)
print("YES")
``` | output | 1 | 85,764 | 13 | 171,529 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image> | instruction | 0 | 85,765 | 13 | 171,530 |
Tags: trees
Correct Solution:
```
from sys import stdin
input = stdin.readline
n = int(input())
degree = [0 for i in range(n+1)]
for _ in range(n-1):
i, j = [int(i) for i in input().split()]
degree[i] += 1
degree[j] += 1
res = False
for i in range(1, n+1):
if degree[i] == 2: res = True
if res: print("NO")
else: print("YES")
``` | output | 1 | 85,765 | 13 | 171,531 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image> | instruction | 0 | 85,766 | 13 | 171,532 |
Tags: trees
Correct Solution:
```
# https://codeforces.com/contest/1189/problem/D1
n = int(input())
g = {}
p = {}
path = {}
flg = True
for _ in range(n-1):
u,v = map(int, input().split())
if u not in g:
g[u] = []
g[u].append(v)
if v not in g:
g[v] = []
g[v].append(u)
flg = 'YES'
for x in g:
if len(g[x]) == 1:continue
if len(g[x]) == 2:
flg = 'NO'
break
print(flg)
``` | output | 1 | 85,766 | 13 | 171,533 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Submitted Solution:
```
n = int(input())
g = [[] for i in range(n)]
for i in range(n - 1):
u, v = map(int, input().split())
u -= 1
v -= 1
g[u].append(v)
g[v].append(u)
for v in range(n):
if len(g[v]) == 2:
print('NO')
exit(0)
print('YES')
``` | instruction | 0 | 85,767 | 13 | 171,534 |
Yes | output | 1 | 85,767 | 13 | 171,535 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Submitted Solution:
```
n=int(input())
tree={}
for i in range(n-1):
a,b=[int(x) for x in input().split()]
if a not in tree:
tree[a]=1
else:
tree[a]+=1
if b not in tree:
tree[b]=1
else:
tree[b]+=1
for item in tree:
if tree[item]==2:
print('NO')
break
else:
print('YES')
``` | instruction | 0 | 85,768 | 13 | 171,536 |
Yes | output | 1 | 85,768 | 13 | 171,537 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Submitted Solution:
```
import sys
input=sys.stdin.readline
from collections import deque
n=int(input())
if n==2:
print('YES')
exit()
if n==3:
print('NO')
exit()
Edges=[[] for _ in range(n)]
for _ in range(n-1):
u,v=map(lambda x: int(x)-1,input().split())
Edges[u].append(v)
Edges[v].append(u)
for i,E in enumerate(Edges):
if len(E)>=3:
root=i
break
else:
print('NO')
exit()
Chi=[[] for _ in range(n)]
Par=[0]*n
q=deque()
q.append(root)
Used=[False]*n
Used[root]=True
while q:
v=q.popleft()
for c in Edges[v]:
if Used[c]:
continue
Chi[v].append(c)
Par[c]=v
Used[c]=True
q.append(c)
Leaf=[]
for v,l in enumerate(Chi):
if not l:
Leaf.append(v)
for l in Leaf:
while True:
p=Par[l]
if p==root:
break
if len(Chi[p])==1:
print('NO')
exit()
l=p
print('YES')
``` | instruction | 0 | 85,769 | 13 | 171,538 |
Yes | output | 1 | 85,769 | 13 | 171,539 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Submitted Solution:
```
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())
N = inp()
cnt = [0]*N
for _ in range(N-1):
x,y = inpl()
cnt[x-1] += 1
cnt[y-1] += 1
for c in cnt:
if c == 2:
print('NO')
break
else:
print('YES')
``` | instruction | 0 | 85,770 | 13 | 171,540 |
Yes | output | 1 | 85,770 | 13 | 171,541 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Submitted Solution:
```
#! /usr/bin/env python3
import sys
from math import factorial
class Node:
def __init__(self, num):
self.num = num
self.binds = []
# for dijkstra
self.marker = False
self.val = None
def add_bind(self, oth):
self.binds.append(oth)
def __repr__(self):
return '<{}: {}{}>'.format(
self.num,
[i.num for i in self.binds],
', \tval: {}'.format(self.val) if self.val != None else ''
)
class Graph:
def __init__(self, size):
self.size = size
self.nodes = [None] + [Node(num) for num in range(1, size+1)]
def read_input(self):
for _ in range(1, self.size):
i, j = (int(x) for x in sys.stdin.readline().split())
self.nodes[i].add_bind(self.nodes[j])
self.nodes[j].add_bind(self.nodes[i])
def __repr__(self):
return '\n'.join(str(node) for node in self.nodes[1:])
def pairs(n):
return factorial(n) // ( factorial(n-2) * 2 )
N = int(sys.stdin.readline())
g = Graph(N)
g.read_input()
#print(g)
ends = [node for node in g.nodes[1:] if len(node.binds) == 1]
#print(pairs(len(ends)), , len(ends))
print('YES' if pairs(len(ends)) >= N-1 else 'NO')
``` | instruction | 0 | 85,771 | 13 | 171,542 |
No | output | 1 | 85,771 | 13 | 171,543 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Submitted Solution:
```
import sys
input = sys.stdin.readline
from collections import deque
class Graph(object):
"""docstring for Graph"""
def __init__(self,n,d): # Number of nodes and d is True if directed
self.n = n
self.graph = [[] for i in range(n)]
self.parent = [-1 for i in range(n)]
self.directed = d
def addEdge(self,x,y):
self.graph[x].append(y)
if not self.directed:
self.graph[y].append(x)
def bfs(self, root): # NORMAL BFS
queue = [root]
queue = deque(queue)
vis = [0]*self.n
while len(queue)!=0:
element = queue.popleft()
vis[element] = 1
count = 0
for i in self.graph[element]:
if vis[i]==0:
queue.append(i)
self.parent[i] = element
vis[i] = 1
count += 1
if count==1 and element!=0:
return False
return True
def dfs(self, root, ans): # Iterative DFS
stack=[root]
vis=[0]*self.n
stack2=[]
while len(stack)!=0: # INITIAL TRAVERSAL
element = stack.pop()
if vis[element]:
continue
vis[element] = 1
stack2.append(element)
for i in self.graph[element]:
if vis[i]==0:
self.parent[i] = element
stack.append(i)
while len(stack2)!=0: # BACKTRACING. Modify the loop according to the question
element = stack2.pop()
m = 0
for i in self.graph[element]:
if i!=self.parent[element]:
m += ans[i]
ans[element] = m
return ans
def shortestpath(self, source, dest): # Calculate Shortest Path between two nodes
self.bfs(source)
path = [dest]
while self.parent[path[-1]]!=-1:
path.append(parent[path[-1]])
return path[::-1]
def detect_cycle(self):
indeg = [0]*self.n
for i in range(self.n):
for j in self.graph[i]:
indeg[j] += 1
q = deque()
vis = 0
for i in range(self.n):
if indeg[i]==0:
q.append(i)
while len(q)!=0:
e = q.popleft()
vis += 1
for i in self.graph[e]:
indeg[i] -= 1
if indeg[i]==0:
q.append(i)
if vis!=self.n:
return True
return False
def reroot(self, root, ans):
stack = [root]
vis = [0]*n
while len(stack)!=0:
e = stack[-1]
if vis[e]:
stack.pop()
# Reverse_The_Change()
continue
vis[e] = 1
for i in graph[e]:
if not vis[e]:
stack.append(i)
if self.parent[e]==-1:
continue
# Change_The_Answers()
n = int(input())
g = Graph(n,False)
for i in range(n-1):
u,v = map(int,input().split())
g.addEdge(u-1,v-1)
for i in range(n):
if len(g.graph[i])==1:
leaf = i
break
if not g.bfs(leaf):
print ("NO")
else:
print ("YES")
``` | instruction | 0 | 85,772 | 13 | 171,544 |
No | output | 1 | 85,772 | 13 | 171,545 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Submitted Solution:
```
n = int(input())
tree = [[] for i in range(n)]
for i in range(n - 1):
a1, a2 = map(int, input().split())
tree[a1 - 1].append(a2 - 1)
tree[a2 - 1].append(a1 - 1)
if n == 2:
print('YES')
elif n == 3:
print('NO')
else:
tree_leafs = [False for i in range(n)]
for a in range(n):
if len(tree[a]) == 1:
tree_leafs[a] = True
no_var = False
for a0 in range(n):
if tree_leafs[a0] == False:
k = 0
for a in tree[a0]:
if tree_leafs[a] == True:
k += 1
if k == 2:
break
if k == 1:
print('NO')
no_var = True
break
if not no_var:
print('YES')
``` | instruction | 0 | 85,773 | 13 | 171,546 |
No | output | 1 | 85,773 | 13 | 171,547 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding -0.5 on the path from 4 to 5.
<image>
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn't contain any node twice.
Input
The first line contains a single integer n (2 β€ n β€ 10^5) β the number of nodes.
Each of the next n-1 lines contains two integers u and v (1 β€ u, v β€ n, u β v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1, 2).
<image>
In the second example, one of configurations that we can't reach is 0 written on (1, 2) and 1 written on (2, 3).
<image>
Below you can see graphs from examples 3, 4:
<image> <image>
Submitted Solution:
```
# @author
import sys
class D1AddOnATree:
def dfs(self, start):
self.done[start] = 1
for x in self.adj[start]:
if self.done[x]:
continue
self.par[x] = start
self.dfs(x)
def solve(self):
from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 5)
n = int(input())
self.adj = defaultdict(list)
self.par = defaultdict(int)
self.done = [0] * (n + 1)
for i in range(n - 1):
u, v = [int(_) for _ in input().split()]
self.adj[u].append(v)
v = max(len(self.adj[p]) for p in self.adj)
start = -1
for p in self.adj:
if len(self.adj[p]) == v:
start = p
break
assert(start != -1)
self.dfs(start)
cnt = [0] * (n + 1)
for k in self.adj:
if self.par[k] == 0:
continue
if len(self.adj[k]) == 1:
cnt[self.par[k]] += 1
for x in cnt:
if x == 1:
print("NO")
break
else:
print("YES")
solver = D1AddOnATree()
input = sys.stdin.readline
solver.solve()
``` | instruction | 0 | 85,774 | 13 | 171,548 |
No | output | 1 | 85,774 | 13 | 171,549 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a complete directed graph K_n with n vertices: each pair of vertices u β v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 β a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 β€ T β€ 100) β the number of test cases.
Next T lines contain test cases β one per line. The first and only line of each test case contains three integers n, l and r (2 β€ n β€ 10^5, 1 β€ l β€ r β€ n(n - 1) + 1, r - l + 1 β€ 10^5) β the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1. | instruction | 0 | 85,830 | 13 | 171,660 |
Tags: constructive algorithms, graphs, greedy, implementation
Correct Solution:
```
def oracle(n, start, end):
nod = 0
t = n - 1
ii = 0
while start - ii > t*2:
if t == 0:
nod += 1
break
nod += 1
ii += t*2
t -= 1
if t < -10:
import sys
sys.exit()
R = []
for cur in range(nod, n):
for v in range(cur+1, n):
ii += 1
if start <= ii <= end:
R.append(cur + 1)
ii += 1
if start <= ii <= end:
R.append(v + 1)
if ii > end:
return R
ii += 1
if start <= ii <= end:
R.append(1)
return R
t = int(input())
for _ in range(t):
a,b,c = map(int,input().split())
x = oracle(a, b, c)
print(*x)
``` | output | 1 | 85,830 | 13 | 171,661 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a complete directed graph K_n with n vertices: each pair of vertices u β v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 β a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 β€ T β€ 100) β the number of test cases.
Next T lines contain test cases β one per line. The first and only line of each test case contains three integers n, l and r (2 β€ n β€ 10^5, 1 β€ l β€ r β€ n(n - 1) + 1, r - l + 1 β€ 10^5) β the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1. | instruction | 0 | 85,831 | 13 | 171,662 |
Tags: constructive algorithms, graphs, greedy, implementation
Correct Solution:
```
t = int(input())
def query(i, n, x):
if (x % 2 == 1):
return i
else:
return (i + x // 2)
for _ in range(t):
n, l, r = map(int, input().split())
i = 1
s = 0
includeOne = False
if r == n * (n - 1) + 1:
includeOne = True
r -= 1
if l == n * (n - 1) + 1:
print(1)
continue
while s + 2 * (n - i) < l:
s += 2 * (n - i)
i += 1
newS = s
allIs = [i]
while newS + 2 * (n - i) < r:
newS += 2 * (n - i)
i += 1
allIs.append(i)
allIin = 0
answer = []
i = allIs[0]
for x in range(l, r + 1):
r = query(i, n, x - s)
if r == n:
s += 2 * (n - i)
i += 1
answer.append(r)
if includeOne:
answer.append(1)
print(*answer)
``` | output | 1 | 85,831 | 13 | 171,663 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a complete directed graph K_n with n vertices: each pair of vertices u β v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 β a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 β€ T β€ 100) β the number of test cases.
Next T lines contain test cases β one per line. The first and only line of each test case contains three integers n, l and r (2 β€ n β€ 10^5, 1 β€ l β€ r β€ n(n - 1) + 1, r - l + 1 β€ 10^5) β the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1. | instruction | 0 | 85,832 | 13 | 171,664 |
Tags: constructive algorithms, graphs, greedy, implementation
Correct Solution:
```
import sys
input = sys.stdin.buffer.readline
for t in range(int(input())):
n,l,r = map(int,input().split())
for i in range(l,min(2*(n-2)+1,r) + 1):
print('1' if i & 1 else i//2 + 1 , end = ' ')
n_set = n
set_idx = 2*(n-2) + 2
while(n_set > 2):
ls = l - set_idx + 1
rs = r - set_idx + 1
set_idx += 2*(n_set - 2)
set_par = n - n_set + 2
if ls < 2 and rs >= 1:print(n , end = ' ')
for i in range(max(2,ls) , min(2*(n_set - 2),rs) + 1):
print(set_par + (i-1)//2 if i & 1 else set_par , end = ' ')
n_set -= 1
l -= set_idx
r -= set_idx
if l <= 0 and r>=0:print(n , end = ' ')
if r == 1: print(1 , end = ' ')
print()
``` | output | 1 | 85,832 | 13 | 171,665 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a complete directed graph K_n with n vertices: each pair of vertices u β v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 β a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 β€ T β€ 100) β the number of test cases.
Next T lines contain test cases β one per line. The first and only line of each test case contains three integers n, l and r (2 β€ n β€ 10^5, 1 β€ l β€ r β€ n(n - 1) + 1, r - l + 1 β€ 10^5) β the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1. | instruction | 0 | 85,833 | 13 | 171,666 |
Tags: constructive algorithms, graphs, greedy, implementation
Correct Solution:
```
for _ in range(int(input())):
numbers, LEFTs, RIGHTs = map(int, input().split())
KEYSs = 0
GREATS = 0
for i in range(1, numbers+1):
if KEYSs + 2 * (numbers-i) >= LEFTs:
GREATS = LEFTs-KEYSs-1
break
KEYSs += 2 * (numbers-i)
LISTs = []
while len(LISTs) < (RIGHTs-LEFTs+1) + GREATS:
for j in range(i+1, numbers+1):
LISTs.append(i)
LISTs.append(j)
i += 1
if i >= numbers:
LISTs.append(1)
break
print (' '.join(list(map(str, LISTs[GREATS:GREATS+RIGHTs-LEFTs+1]))))
``` | output | 1 | 85,833 | 13 | 171,667 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a complete directed graph K_n with n vertices: each pair of vertices u β v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 β a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 β€ T β€ 100) β the number of test cases.
Next T lines contain test cases β one per line. The first and only line of each test case contains three integers n, l and r (2 β€ n β€ 10^5, 1 β€ l β€ r β€ n(n - 1) + 1, r - l + 1 β€ 10^5) β the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1. | instruction | 0 | 85,834 | 13 | 171,668 |
Tags: constructive algorithms, graphs, greedy, implementation
Correct Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
def solve(n,l,r):
fir,st = 0,1
while st < n:
x = 2*(n-st)
if fir+x >= l:
break
fir += x
st += 1
if st == n:
return [1]
ans = []
for z in range(st+1,n+1):
ans.append(st)
ans.append(z)
st += 1
while len(ans) < r-fir:
if st == n:
ans.append(1)
else:
for z in range(st+1,n+1):
ans.append(st)
ans.append(z)
st += 1
return ans[l-fir-1:r-fir]
def main():
for _ in range(int(input())):
n,l,r = map(int,input().split())
print(*solve(n,l,r))
# Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
``` | output | 1 | 85,834 | 13 | 171,669 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.