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 a correct Python 3 solution for this coding contest problem.
AtCoDeer the deer wants a directed graph that satisfies the following conditions:
* The number of vertices, N, is at most 300.
* There must not be self-loops or multiple edges.
* The vertices are numbered from 1 through N.
* Each edge has either an integer weight between 0 and 100 (inclusive), or a label `X` or `Y`.
* For every pair of two integers (x,y) such that 1 ≤ x ≤ A, 1 ≤ y ≤ B, the shortest distance from Vertex S to Vertex T in the graph where the edges labeled `X` have the weight x and the edges labeled `Y` have the weight y, is d_{x,y}.
Construct such a graph (and a pair of S and T) for him, or report that it does not exist. Refer to Output section for output format.
Constraints
* 1 ≤ A,B ≤ 10
* 1 ≤ d_{x,y} ≤ 100 (1 ≤ x ≤ A, 1 ≤ y ≤ B)
* All input values are integers.
Input
Input is given from Standard Input in the following format:
A B
d_{1,1} d_{1,2} .. d_{1,B}
d_{2,1} d_{2,2} .. d_{2,B}
:
d_{A,1} d_{A,2} .. d_{A,B}
Output
If no graph satisfies the condition, print `Impossible`.
If there exists a graph that satisfies the condition, print `Possible` in the first line. Then, in the subsequent lines, print the constructed graph in the following format:
N M
u_1 v_1 c_1
u_2 v_2 c_2
:
u_M v_M c_M
S T
Here, M is the number of the edges, and u_i, v_i, c_i represent edges as follows: there is an edge from Vertex u_i to Vertex v_i whose weight or label is c_i.
Also refer to Sample Outputs.
Examples
Input
2 3
1 2 2
1 2 3
Output
Possible
3 4
1 2 X
2 3 1
3 2 Y
1 3 Y
1 3
Input
1 3
100 50 1
Output
Impossible | instruction | 0 | 21,018 | 13 | 42,036 |
"Correct Solution:
```
# seishin.py
A, B = map(int, input().split())
D = [list(map(int, input().split())) for i in range(A)]
N = 101
F = [[0]*N for i in range(N)]
for a in range(N):
for b in range(N):
res = 0
for x in range(A):
for y in range(B):
res = max(res, D[x][y] - (x+1)*a - (y+1)*b)
F[a][b] = res
for x in range(A):
for y in range(B):
v = 101
for a in range(N):
for b in range(N):
v = min(v, (x+1)*a + (y+1)*b + F[a][b])
if v != D[x][y]:
print("Impossible")
exit(0)
print("Possible")
print(2*N, N**2 + 2*(N-1))
ans = 0
for i in range(N-1):
print(i+1, i+2, "X")
print(103+i, 102+i, "Y")
ans += 2
for a in range(N):
for b in range(N):
ans += 1
print(a+1, 102+b, F[a][b])
print(1, 102)
``` | output | 1 | 21,018 | 13 | 42,037 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
AtCoDeer the deer wants a directed graph that satisfies the following conditions:
* The number of vertices, N, is at most 300.
* There must not be self-loops or multiple edges.
* The vertices are numbered from 1 through N.
* Each edge has either an integer weight between 0 and 100 (inclusive), or a label `X` or `Y`.
* For every pair of two integers (x,y) such that 1 ≤ x ≤ A, 1 ≤ y ≤ B, the shortest distance from Vertex S to Vertex T in the graph where the edges labeled `X` have the weight x and the edges labeled `Y` have the weight y, is d_{x,y}.
Construct such a graph (and a pair of S and T) for him, or report that it does not exist. Refer to Output section for output format.
Constraints
* 1 ≤ A,B ≤ 10
* 1 ≤ d_{x,y} ≤ 100 (1 ≤ x ≤ A, 1 ≤ y ≤ B)
* All input values are integers.
Input
Input is given from Standard Input in the following format:
A B
d_{1,1} d_{1,2} .. d_{1,B}
d_{2,1} d_{2,2} .. d_{2,B}
:
d_{A,1} d_{A,2} .. d_{A,B}
Output
If no graph satisfies the condition, print `Impossible`.
If there exists a graph that satisfies the condition, print `Possible` in the first line. Then, in the subsequent lines, print the constructed graph in the following format:
N M
u_1 v_1 c_1
u_2 v_2 c_2
:
u_M v_M c_M
S T
Here, M is the number of the edges, and u_i, v_i, c_i represent edges as follows: there is an edge from Vertex u_i to Vertex v_i whose weight or label is c_i.
Also refer to Sample Outputs.
Examples
Input
2 3
1 2 2
1 2 3
Output
Possible
3 4
1 2 X
2 3 1
3 2 Y
1 3 Y
1 3
Input
1 3
100 50 1
Output
Impossible
Submitted Solution:
```
import sys
input = sys.stdin.readline
import numpy as np
"""
ax + by + c という距離をたくさん作れる
全ての(a,b)に対して作るとしてよい(cを大きくすれば無視できるので)
悪さをしない範囲の最小のcを選ぶ
それで上手くいっているかを検証
"""
U = 100
A,B = map(int,input().split())
D = np.array([input().rstrip().split() for _ in range(A)], dtype=np.int64)
ax = np.arange(U+1)[:,None] * np.arange(1,A+1)[None,:] # (U+1, A)
by = np.arange(U+1)[:,None] * np.arange(1,B+1)[None,:] # (U+1, B)
axby = ax[:,:,None,None] + by[None,None,:,:] # (U+1,A,U+1,B)
c = (D[None,:,None,:] - axby).max(axis = 3).max(axis = 1) # (U+1,U+1)
c *= (c > 0)
D2 = (axby + c[:,None,:,None]).min(axis = 2).min(axis = 0) # (A,B)
if (D != D2).any():
print('Impossible')
exit()
# ax + by + c[a,b] という1次式が距離になるようなグラフを作成
graph = []
for a in range(U+1):
for b in range(U+1):
graph.append((1+a, (U+2) + b, c[a,b]))
for a in range(U):
graph.append((a+1,a+2,'X'))
for b in range(U,0,-1):
graph.append((U+2+b,U+1+b,'Y'))
start = 0
goal = U+1
print('Possible')
print(2*(U+1), len(graph))
for e in graph:
print(*e)
print(start,goal)
``` | instruction | 0 | 21,019 | 13 | 42,038 |
No | output | 1 | 21,019 | 13 | 42,039 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
AtCoDeer the deer wants a directed graph that satisfies the following conditions:
* The number of vertices, N, is at most 300.
* There must not be self-loops or multiple edges.
* The vertices are numbered from 1 through N.
* Each edge has either an integer weight between 0 and 100 (inclusive), or a label `X` or `Y`.
* For every pair of two integers (x,y) such that 1 ≤ x ≤ A, 1 ≤ y ≤ B, the shortest distance from Vertex S to Vertex T in the graph where the edges labeled `X` have the weight x and the edges labeled `Y` have the weight y, is d_{x,y}.
Construct such a graph (and a pair of S and T) for him, or report that it does not exist. Refer to Output section for output format.
Constraints
* 1 ≤ A,B ≤ 10
* 1 ≤ d_{x,y} ≤ 100 (1 ≤ x ≤ A, 1 ≤ y ≤ B)
* All input values are integers.
Input
Input is given from Standard Input in the following format:
A B
d_{1,1} d_{1,2} .. d_{1,B}
d_{2,1} d_{2,2} .. d_{2,B}
:
d_{A,1} d_{A,2} .. d_{A,B}
Output
If no graph satisfies the condition, print `Impossible`.
If there exists a graph that satisfies the condition, print `Possible` in the first line. Then, in the subsequent lines, print the constructed graph in the following format:
N M
u_1 v_1 c_1
u_2 v_2 c_2
:
u_M v_M c_M
S T
Here, M is the number of the edges, and u_i, v_i, c_i represent edges as follows: there is an edge from Vertex u_i to Vertex v_i whose weight or label is c_i.
Also refer to Sample Outputs.
Examples
Input
2 3
1 2 2
1 2 3
Output
Possible
3 4
1 2 X
2 3 1
3 2 Y
1 3 Y
1 3
Input
1 3
100 50 1
Output
Impossible
Submitted Solution:
```
from heapq import *
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def impossible():
print("Impossible")
exit()
def main():
h, w = map(int, input().split())
t = [list(map(int, input().split())) for _ in range(h)]
# 番兵を立てる
for tr in t:
tr.append(tr[-1])
t.append(t[-1])
#p2D(t)
edge = [[-1] * 101 for _ in range(101)]
hp = []
for i in range(h):
for j in range(w):
tij = t[i][j]
min_number_x = t[i + 1][j] - tij
min_number_y = t[i][j + 1] - tij
if min_number_x < 0 or min_number_y < 0:
impossible()
diff = tij - min_number_x * (i + 1) - min_number_y * (j + 1)
heappush(hp, [diff, i, j, min_number_x, min_number_y])
number_edges = 200
while hp:
diff, i, j, min_number_x, min_number_y = heappop(hp)
x, y = i + 1, j + 1
tij = t[i][j]
candi = []
for number_x in range(min_number_x, 101):
if x * number_x > tij: break
for number_y in range(min_number_y, 101):
sum_xy = x * number_x + y * number_y
if sum_xy > tij: break
candi.append([sum_xy, number_x, number_y])
for sum_xy, number_x, number_y in sorted(candi, reverse=True):
diff = tij - sum_xy
if edge[number_x][number_y] == -1:
edge[number_x][number_y] = diff
number_edges += 1
break
if edge[number_x][number_y] == diff:
break
else:
impossible()
#p2D(edge)
print(202, number_edges)
for u in range(1, 101):
print(u, u + 1, "X")
for u in range(102, 202):
print(u, u + 1, "Y")
for number_x in range(101):
for number_y in range(101):
diff = edge[number_x][number_y]
if diff == -1: continue
print(number_x + 1, 202 - number_y, diff)
print(1, 202)
main()
``` | instruction | 0 | 21,020 | 13 | 42,040 |
No | output | 1 | 21,020 | 13 | 42,041 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
AtCoDeer the deer wants a directed graph that satisfies the following conditions:
* The number of vertices, N, is at most 300.
* There must not be self-loops or multiple edges.
* The vertices are numbered from 1 through N.
* Each edge has either an integer weight between 0 and 100 (inclusive), or a label `X` or `Y`.
* For every pair of two integers (x,y) such that 1 ≤ x ≤ A, 1 ≤ y ≤ B, the shortest distance from Vertex S to Vertex T in the graph where the edges labeled `X` have the weight x and the edges labeled `Y` have the weight y, is d_{x,y}.
Construct such a graph (and a pair of S and T) for him, or report that it does not exist. Refer to Output section for output format.
Constraints
* 1 ≤ A,B ≤ 10
* 1 ≤ d_{x,y} ≤ 100 (1 ≤ x ≤ A, 1 ≤ y ≤ B)
* All input values are integers.
Input
Input is given from Standard Input in the following format:
A B
d_{1,1} d_{1,2} .. d_{1,B}
d_{2,1} d_{2,2} .. d_{2,B}
:
d_{A,1} d_{A,2} .. d_{A,B}
Output
If no graph satisfies the condition, print `Impossible`.
If there exists a graph that satisfies the condition, print `Possible` in the first line. Then, in the subsequent lines, print the constructed graph in the following format:
N M
u_1 v_1 c_1
u_2 v_2 c_2
:
u_M v_M c_M
S T
Here, M is the number of the edges, and u_i, v_i, c_i represent edges as follows: there is an edge from Vertex u_i to Vertex v_i whose weight or label is c_i.
Also refer to Sample Outputs.
Examples
Input
2 3
1 2 2
1 2 3
Output
Possible
3 4
1 2 X
2 3 1
3 2 Y
1 3 Y
1 3
Input
1 3
100 50 1
Output
Impossible
Submitted Solution:
```
from heapq import *
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def impossible():
print("Impossible")
exit()
def main():
h, w = map(int, input().split())
t = [list(map(int, input().split())) for _ in range(h)]
edge = [[0] * 101 for _ in range(101)]
# XとYをつなぐ辺の作成
for n_x in range(101):
for n_y in range(101):
cost = 0
for i in range(h):
for j in range(w):
x, y = i + 1, j + 1
cur_cost = t[i][j] - x * n_x - y * n_y
if cur_cost > cost: cost = cur_cost
edge[n_x][n_y] = cost
p2D(edge)
# 条件を満たしているかのチェック
for i in range(h):
for j in range(w):
x, y = i + 1, j + 1
tij = t[i][j]
min_dist = 1000
for n_x in range(101):
for n_y in range(101):
dist = x * n_x + y * n_y + edge[n_x][n_y]
if dist < min_dist: min_dist = dist
if tij != min_dist: impossible()
print("Possible")
print(202, 101 * 101 + 200)
for u in range(1, 101):
print(u, u + 1, "X")
for u in range(102, 202):
print(u, u + 1, "Y")
for n_x in range(101):
for n_y in range(101):
print(n_x + 1, 202 - n_y, edge[n_x][n_y])
print(1, 202)
main()
``` | instruction | 0 | 21,021 | 13 | 42,042 |
No | output | 1 | 21,021 | 13 | 42,043 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
AtCoDeer the deer wants a directed graph that satisfies the following conditions:
* The number of vertices, N, is at most 300.
* There must not be self-loops or multiple edges.
* The vertices are numbered from 1 through N.
* Each edge has either an integer weight between 0 and 100 (inclusive), or a label `X` or `Y`.
* For every pair of two integers (x,y) such that 1 ≤ x ≤ A, 1 ≤ y ≤ B, the shortest distance from Vertex S to Vertex T in the graph where the edges labeled `X` have the weight x and the edges labeled `Y` have the weight y, is d_{x,y}.
Construct such a graph (and a pair of S and T) for him, or report that it does not exist. Refer to Output section for output format.
Constraints
* 1 ≤ A,B ≤ 10
* 1 ≤ d_{x,y} ≤ 100 (1 ≤ x ≤ A, 1 ≤ y ≤ B)
* All input values are integers.
Input
Input is given from Standard Input in the following format:
A B
d_{1,1} d_{1,2} .. d_{1,B}
d_{2,1} d_{2,2} .. d_{2,B}
:
d_{A,1} d_{A,2} .. d_{A,B}
Output
If no graph satisfies the condition, print `Impossible`.
If there exists a graph that satisfies the condition, print `Possible` in the first line. Then, in the subsequent lines, print the constructed graph in the following format:
N M
u_1 v_1 c_1
u_2 v_2 c_2
:
u_M v_M c_M
S T
Here, M is the number of the edges, and u_i, v_i, c_i represent edges as follows: there is an edge from Vertex u_i to Vertex v_i whose weight or label is c_i.
Also refer to Sample Outputs.
Examples
Input
2 3
1 2 2
1 2 3
Output
Possible
3 4
1 2 X
2 3 1
3 2 Y
1 3 Y
1 3
Input
1 3
100 50 1
Output
Impossible
Submitted Solution:
```
from heapq import *
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def impossible():
print("Impossible")
exit()
def main():
h, w = map(int, input().split())
t = [list(map(int, input().split())) for _ in range(h)]
# 番兵を立てる
for tr in t:
tr.append(tr[-1])
t.append(t[-1])
# p2D(t)
edge = [[-1] * 101 for _ in range(101)]
hp = []
for i in range(h):
for j in range(w):
tij = t[i][j]
min_number_x = t[i + 1][j] - tij
min_number_y = t[i][j + 1] - tij
diff = tij - min_number_x * (i + 1) - min_number_y * (j + 1)
if min_number_x < 0 or min_number_y < 0 or diff < 0:
impossible()
heappush(hp, [diff, i, j, min_number_x, min_number_y])
number_edges = 200
while hp:
diff, i, j, min_number_x, min_number_y = heappop(hp)
x, y = i + 1, j + 1
tij = t[i][j]
candi = []
for number_x in range(min_number_x, 101):
if x * number_x > tij: break
for number_y in range(min_number_y, 101):
sum_xy = x * number_x + y * number_y
if sum_xy > tij: break
candi.append([sum_xy, number_x, number_y])
for sum_xy, number_x, number_y in sorted(candi, reverse=True):
diff = tij - sum_xy
if edge[number_x][number_y] == -1:
edge[number_x][number_y] = diff
number_edges += 1
break
if edge[number_x][number_y] == diff:
break
else:
impossible()
# p2D(edge)
print("Possible")
print(202, number_edges)
for u in range(1, 101):
print(u, u + 1, "X")
for u in range(102, 202):
print(u, u + 1, "Y")
for number_x in range(101):
for number_y in range(101):
diff = edge[number_x][number_y]
if diff == -1: continue
print(number_x + 1, 202 - number_y, diff)
print(1, 202)
main()
``` | instruction | 0 | 21,022 | 13 | 42,044 |
No | output | 1 | 21,022 | 13 | 42,045 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
* Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ K ≦ N
* 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
* 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
* 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
* The given graph is a tree.
* All v_j are distinct.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K
Output
If it is possible to write integers into all empty vertices so that the condition is satisfied, print `Yes`. Otherwise, print `No`.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.
Examples
Input
5
1 2
3 1
4 3
3 5
2
2 6
5 7
Output
Yes
5
6
6
5
7
Input
5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
Output
No
Input
4
1 2
2 3
3 4
1
1 0
Output
Yes
0
-1
-2
-3 | instruction | 0 | 21,050 | 13 | 42,100 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(500000)
N = int(input())
E = [[] for _ in range(N+1)]
for _ in range(N-1):
a, b = map(int, input().split())
E[a].append(b)
E[b].append(a)
K = int(input())
L = [-float("inf")] * (N+1)
R = [float("inf")] * (N+1)
for _ in range(K):
v, p = map(int, input().split())
L[v] = p
R[v] = p
tour = []
def dfs(v, p):
tour.append(v)
for u in E[v]:
if u!=p:
dfs(u, v)
tour.append(v)
dfs(v, 0)
l, r = L[v], R[v]
odd = p % 2
for v in tour[1:]:
l -= 1
r += 1
odd = 1 - odd
l_, r_ = L[v], R[v]
if r_ != float("inf") and r_%2 != odd:
print("No")
exit()
l = max(l, l_)
r = min(r, r_)
L[v] = l
R[v] = r
for v in tour[-2::-1]:
l -= 1
r += 1
odd = 1 - odd
l_, r_ = L[v], R[v]
l = max(l, l_)
r = min(r, r_)
if l > r:
print("No")
exit()
L[v] = l
R[v] = r
Ans = [-1] * (N+1)
print("Yes")
print("\n".join(map(str, L[1:])))
``` | output | 1 | 21,050 | 13 | 42,101 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
* Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ K ≦ N
* 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
* 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
* 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
* The given graph is a tree.
* All v_j are distinct.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K
Output
If it is possible to write integers into all empty vertices so that the condition is satisfied, print `Yes`. Otherwise, print `No`.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.
Examples
Input
5
1 2
3 1
4 3
3 5
2
2 6
5 7
Output
Yes
5
6
6
5
7
Input
5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
Output
No
Input
4
1 2
2 3
3 4
1
1 0
Output
Yes
0
-1
-2
-3 | instruction | 0 | 21,051 | 13 | 42,102 |
"Correct Solution:
```
def N(): return int(input())
def NM():return map(int,input().split())
def L():return list(NM())
def LN(n):return [N() for i in range(n)]
def LL(n):return [L() for i in range(n)]
n=N()
edge=[[] for i in range(n+1)]
for i in range(n-1):
a,b=map(int,input().split())
edge[a].append(b)
edge[b].append(a)
INF=float("inf")
ma=[INF]*(n+1)
mi=[-INF]*(n+1)
k=N()
vp=LL(k)
for v,p in vp:
ma[v]=p
mi[v]=p
import sys
sys.setrecursionlimit(10**6)
def dfs(v,d,hi,lo):
dep[v]=d
ma[v]=min(ma[v],hi)
mi[v]=max(mi[v],lo)
hi=ma[v]
lo=mi[v]
for i in edge[v]:
if dep[i]==-1:
hi,lo=dfs(i,d+1,hi+1,lo-1)
ma[v]=min(ma[v],hi)
mi[v]=max(mi[v],lo)
hi=ma[v]
lo=mi[v]
return hi+1,lo-1
for i in range(2):
dep=[-1 for i in range(n+1)]
dfs(v,0,p,p)
for i,j in zip(mi,ma):
if i>j or (i-j)%2==1:
print("No")
break
else:
print("Yes")
for i in mi[1:]:
print(i)
``` | output | 1 | 21,051 | 13 | 42,103 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
* Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ K ≦ N
* 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
* 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
* 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
* The given graph is a tree.
* All v_j are distinct.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K
Output
If it is possible to write integers into all empty vertices so that the condition is satisfied, print `Yes`. Otherwise, print `No`.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.
Examples
Input
5
1 2
3 1
4 3
3 5
2
2 6
5 7
Output
Yes
5
6
6
5
7
Input
5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
Output
No
Input
4
1 2
2 3
3 4
1
1 0
Output
Yes
0
-1
-2
-3 | instruction | 0 | 21,052 | 13 | 42,104 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
INF = 10**10
n = int(input())
T = [[] for _ in range(n+1)]
for _ in range(n-1):
a, b = map(int, input().split())
T[a].append(b)
T[b].append(a)
k = int(input())
VP = tuple(tuple(map(int, input().split())) for _ in range(k))
P = [-1]*(n+1)
A = [-1]*(n+1)
r, c = VP[0]
for v, p in VP:
P[v] = p
def dfs1(v, par=-1, color=c%2):
if P[v] == -1:
l = -INF
r = INF
else:
l = P[v]
r = P[v]
for nv in T[v]:
if nv == par:
continue
nl, nr = dfs1(nv, v, color^1)
l = max(l, nl-1)
r = min(r, nr+1)
if l&1 ^ color:
l += 1
if r&1 ^ color:
r -= 1
if l > r:
print("No")
exit()
P[v] = (l, r)
return P[v]
dfs1(r)
def dfs2(v, ini, par=-1):
l, r = P[v]
if l == r:
A[v] = l
else:
if l <= ini-1 <= r:
A[v] = ini-1
else:
A[v] = ini+1
for nv in T[v]:
if nv == par:
continue
dfs2(nv, A[v], v)
dfs2(r, P[r][0])
print("Yes")
print(*A[1:], sep="\n")
``` | output | 1 | 21,052 | 13 | 42,105 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
* Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ K ≦ N
* 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
* 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
* 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
* The given graph is a tree.
* All v_j are distinct.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K
Output
If it is possible to write integers into all empty vertices so that the condition is satisfied, print `Yes`. Otherwise, print `No`.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.
Examples
Input
5
1 2
3 1
4 3
3 5
2
2 6
5 7
Output
Yes
5
6
6
5
7
Input
5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
Output
No
Input
4
1 2
2 3
3 4
1
1 0
Output
Yes
0
-1
-2
-3 | instruction | 0 | 21,053 | 13 | 42,106 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5+100)
from collections import *
def dfs(v, pv, de):
for nv in G[v]:
if nv==pv:
continue
dfs(nv, v, de+1)
left[v] = max(left[v], left[nv]-1)
right[v] = min(right[v], right[nv]+1)
if v in d:
if d[v]<left[v] or right[v]<d[v] or (d[v]-d[root])%2!=de%2:
print('No')
exit()
else:
left[v] = d[v]
right[v] = d[v]
def dfs2(v, pv, now):
ans[v] = now
for nv in G[v]:
if nv==pv:
continue
if left[nv]<=now-1<=right[nv]:
dfs2(nv, v, now-1)
elif left[nv]<=now+1<=right[nv]:
dfs2(nv, v, now+1)
else:
print('No')
exit()
N = int(input())
G = [[] for _ in range(N)]
for _ in range(N-1):
A, B = map(int, input().split())
G[A-1].append(B-1)
G[B-1].append(A-1)
d = defaultdict(int)
K = int(input())
for _ in range(K):
V, P = map(int, input().split())
d[V-1] = P
root = V-1
left = [-10**18]*N
right = [10**18]*N
dfs(root, -1, 0)
ans = [-1]*N
dfs2(root, -1, d[root])
print('Yes')
for ans_i in ans:
print(ans_i)
``` | output | 1 | 21,053 | 13 | 42,107 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
* Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ K ≦ N
* 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
* 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
* 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
* The given graph is a tree.
* All v_j are distinct.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K
Output
If it is possible to write integers into all empty vertices so that the condition is satisfied, print `Yes`. Otherwise, print `No`.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.
Examples
Input
5
1 2
3 1
4 3
3 5
2
2 6
5 7
Output
Yes
5
6
6
5
7
Input
5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
Output
No
Input
4
1 2
2 3
3 4
1
1 0
Output
Yes
0
-1
-2
-3 | instruction | 0 | 21,054 | 13 | 42,108 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5+100)
from collections import *
def dfs(v, pv, de):
for nv in G[v]:
if nv==pv:
continue
dfs(nv, v, de+1)
left[v] = max(left[v], left[nv]-1)
right[v] = min(right[v], right[nv]+1)
if v in d:
if d[v]<left[v] or right[v]<d[v] or (d[v]-d[root])%2!=de%2:
print('No')
exit()
else:
left[v] = d[v]
right[v] = d[v]
def dfs2(v, pv, now):
ans[v] = now
for nv in G[v]:
if nv==pv:
continue
if left[nv]<=now-1<=right[nv]:
dfs2(nv, v, now-1)
else:
dfs2(nv, v, now+1)
N = int(input())
G = [[] for _ in range(N)]
for _ in range(N-1):
A, B = map(int, input().split())
G[A-1].append(B-1)
G[B-1].append(A-1)
d = defaultdict(int)
K = int(input())
for _ in range(K):
V, P = map(int, input().split())
d[V-1] = P
root = V-1
left = [-10**18]*N
right = [10**18]*N
dfs(root, -1, 0)
ans = [-1]*N
dfs2(root, -1, d[root])
print('Yes')
for ans_i in ans:
print(ans_i)
``` | output | 1 | 21,054 | 13 | 42,109 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
* Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ K ≦ N
* 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
* 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
* 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
* The given graph is a tree.
* All v_j are distinct.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K
Output
If it is possible to write integers into all empty vertices so that the condition is satisfied, print `Yes`. Otherwise, print `No`.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.
Examples
Input
5
1 2
3 1
4 3
3 5
2
2 6
5 7
Output
Yes
5
6
6
5
7
Input
5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
Output
No
Input
4
1 2
2 3
3 4
1
1 0
Output
Yes
0
-1
-2
-3 | instruction | 0 | 21,055 | 13 | 42,110 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(10**6)
n = int(input())
tr = [list() for _ in range(n)]
num = [None]*n
ran = [0]*n
dep = [0]*n
for _ in range(n-1):
a,b = map(int,input().split())
a -= 1; b -= 1
tr[a].append(b)
tr[b].append(a)
k = int(input())
root,c = map(int,input().split())
root -= 1
num[root] = c
def dfs(v,p,dep):
for x in tr[v]:
if x == p:
continue
dep[x] = dep[v] + 1
dfs(x,v,dep)
return
dfs(root,-1,dep)
for _ in range(k-1):
v,c = map(int,input().split())
v -= 1
if abs(c-num[root]) > dep[v] or dep[v]%2 != (c-num[root])%2:
print("No")
exit(0)
num[v] = c
def dfs2(v,p,ran):
cur = [-10**9,10**9] if num[v] is None else [num[v],num[v]]
if tr[v] == [p]:
ran[v] = tuple(cur)
return
for x in tr[v]:
if x == p:
continue
dfs2(x,v,ran)
if ran[x][0] > cur[0]:
cur[0] = ran[x][0]
if ran[x][1] < cur[1]:
cur[1] = ran[x][1]
cur[0] -= 1; cur[1] += 1
ran[v] = tuple(cur)
return
dfs2(root,None,ran)
q = [(root,None)]
while q:
v,p = q.pop()
for x in tr[v]:
if x == p:
continue
if ran[x][0]<=num[v]+1<=ran[x][1]:
num[x] = num[v] + 1
elif ran[x][0]<=num[v]-1<=ran[x][1]:
num[x] = num[v] - 1
else:
print("No")
exit(0)
q.append((x,v))
print("Yes")
for c in num:
print(c)
``` | output | 1 | 21,055 | 13 | 42,111 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
* Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ K ≦ N
* 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
* 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
* 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
* The given graph is a tree.
* All v_j are distinct.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K
Output
If it is possible to write integers into all empty vertices so that the condition is satisfied, print `Yes`. Otherwise, print `No`.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.
Examples
Input
5
1 2
3 1
4 3
3 5
2
2 6
5 7
Output
Yes
5
6
6
5
7
Input
5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
Output
No
Input
4
1 2
2 3
3 4
1
1 0
Output
Yes
0
-1
-2
-3 | instruction | 0 | 21,056 | 13 | 42,112 |
"Correct Solution:
```
from collections import deque
import sys
input = sys.stdin.readline
N = int(input())
adj = [[] for _ in range(N + 1)]
for _ in range(N-1):
A, B = map(int, input().split())
adj[A].append(B)
adj[B].append(A)
K = int(input())
ans = [[-10 ** 10 + 0.5, 10 ** 10 + 0.5] for _ in range(N + 1)]
for _ in range(K):
V, P = map(int, input().split())
ans[V] = [P, P]
def check(par, chi):
par_min, par_max = ans[par]
chi_min, chi_max = ans[chi]
if (par_min - chi_min) % 2 == 0:
print('No')
sys.exit()
if par_min - 1 > chi_max or par_max + 1 < chi_min:
print('No')
sys.exit()
ans[chi] = [max(par_min - 1, chi_min), min(par_max + 1, chi_max)]
que = deque()
que.append(V)
seen = [0] * (N + 1)
parent = [0] * (N + 1)
v_list = []
while que:
v = que.pop()
seen[v] = 1
v_list.append(v)
for u in adj[v]:
if seen[u] == 0:
parent[u] = v
check(v, u)
que.append(u)
v_list.reverse()
for v in v_list[:-1]:
u = parent[v]
check(v, u)
que = deque()
que.append(V)
seen = [0] * (N + 1)
while que:
v = que.pop()
seen[v] = 1
for u in adj[v]:
if seen[u] == 0:
check(v, u)
que.append(u)
print('Yes')
for i in range(1, N + 1):
print(ans[i][0])
``` | output | 1 | 21,056 | 13 | 42,113 |
Provide a correct Python 3 solution for this coding contest problem.
We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
* Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ K ≦ N
* 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
* 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
* 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
* The given graph is a tree.
* All v_j are distinct.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K
Output
If it is possible to write integers into all empty vertices so that the condition is satisfied, print `Yes`. Otherwise, print `No`.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.
Examples
Input
5
1 2
3 1
4 3
3 5
2
2 6
5 7
Output
Yes
5
6
6
5
7
Input
5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
Output
No
Input
4
1 2
2 3
3 4
1
1 0
Output
Yes
0
-1
-2
-3 | instruction | 0 | 21,057 | 13 | 42,114 |
"Correct Solution:
```
from collections import deque
import sys
sys.setrecursionlimit(int(1e7))
def inpl(): return list(map(int, input().split()))
N = int(input())
G = [[] for _ in range(N+1)]
X = [-1 for _ in range(N+1)] # is odd
U = [1e7 for _ in range(N+1)]
L = [-1e7 for _ in range(N+1)]
for _ in range(N-1):
a, b = inpl()
G[a].append(b)
G[b].append(a)
K = int(input())
for _ in range(K):
v, p = inpl()
U[v], L[v], X[v] = p, p, p%2
searched = [0]*(N+1)
def dfs(u, l, x, i):
searched[i] = True
if X[i] >= 0 and X[i] != x:
X[i] = 2
return -1e7, 1e7
U[i] = min(U[i], u)
L[i] = max(L[i], l)
X[i] = x
for j in G[i]:
if searched[j]:
continue
nu, nl = dfs(U[i]+1, L[i]-1, (X[i]+1)%2, j)
U[i] = min(U[i], nu+1)
L[i] = max(L[i], nl-1)
return U[i], L[i]
dfs(U[v], L[v], X[v], v)
Q = [v]
searched = [0]*(N+1)
answer = [0]*(N+1)
answer[v] = U[v]
OK = True
if max(X) == 2:
OK = False
Q = []
while Q:
p = Q.pop()
for q in G[p]:
if searched[q]:
continue
searched[q] = True
Q.append(q)
if L[q] <= answer[p] + 1 <= U[q]:
answer[q] = answer[p] + 1
elif L[q] <= answer[p] - 1 <= U[q]:
answer[q] = answer[p] - 1
else:
OK = False
Q = []
break
if OK:
print("Yes")
print(*answer[1:], sep="\n")
else:
print("No")
``` | output | 1 | 21,057 | 13 | 42,115 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
* Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ K ≦ N
* 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
* 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
* 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
* The given graph is a tree.
* All v_j are distinct.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K
Output
If it is possible to write integers into all empty vertices so that the condition is satisfied, print `Yes`. Otherwise, print `No`.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.
Examples
Input
5
1 2
3 1
4 3
3 5
2
2 6
5 7
Output
Yes
5
6
6
5
7
Input
5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
Output
No
Input
4
1 2
2 3
3 4
1
1 0
Output
Yes
0
-1
-2
-3
Submitted Solution:
```
"""
Writer: SPD_9X2
https://atcoder.jp/contests/arc063/tasks/arc063_c
数字が置かれている頂点は範囲指定の制約になる
→制約をすべての点に伝播させれば、後はその範囲内から適当に選んで実装すればよい
一度根方向に制約を集め、それを全方位に伝播させればよい?
→それっぽさそう
偶奇に関してはちゃんと考えてね
→条件の両端の偶奇で判断可能
"""
import sys
sys.setrecursionlimit(5*10**5)
N = int(input())
lis = [ [] for i in range(N) ]
for i in range(N-1):
a,b = map(int,input().split())
a -= 1
b -= 1
lis[a].append(b)
lis[b].append(a)
l = [float("-inf")] * N
r = [float("inf")] * N
K = int(input())
for i in range(K):
V,P = map(int,input().split())
V -= 1
l[V] = P
r[V] = P
#dfsで条件を根方向に集める
def dfs(v,p):
for nex in lis[v]:
if nex != p:
nl,nr = dfs(nex,v)
if l[v] != float("-inf") and nl != float("-inf") and l[v] % 2 != nl % 2:
print ("No")
sys.exit()
l[v] = max(l[v] , nl)
r[v] = min(r[v] , nr)
if l[v] > r[v]:
print ("No")
sys.exit()
return l[v] - 1 , r[v] + 1
dfs(0,0)
#bfsで条件を配る
from collections import deque
q = deque([0])
unvisit = [True] * N
ans = [None] * N
ans[0] = l[0]
while len(q) > 0:
v = q.popleft()
unvisit[v] = False
if l[v] > r[v]:
print ("No")
sys.exit()
for nex in lis[v]:
if unvisit[nex]:
if l[nex] != float("-inf") and l[v] != float("-inf") and l[v] % 2 == l[nex] % 2:
print ("No")
sys.exit()
l[nex] = max(l[nex] , l[v]-1)
r[nex] = min(r[nex] , r[v]+1)
ans[nex] = l[nex]
q.append(nex)
print ("Yes")
for i in ans:
print (i)
``` | instruction | 0 | 21,058 | 13 | 42,116 |
Yes | output | 1 | 21,058 | 13 | 42,117 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
* Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ K ≦ N
* 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
* 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
* 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
* The given graph is a tree.
* All v_j are distinct.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K
Output
If it is possible to write integers into all empty vertices so that the condition is satisfied, print `Yes`. Otherwise, print `No`.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.
Examples
Input
5
1 2
3 1
4 3
3 5
2
2 6
5 7
Output
Yes
5
6
6
5
7
Input
5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
Output
No
Input
4
1 2
2 3
3 4
1
1 0
Output
Yes
0
-1
-2
-3
Submitted Solution:
```
import sys
sys.setrecursionlimit(10**6)
INF = 10**7
n = int(input())
edge = [[] for _ in range(n)]
for i in range(n - 1):
u, v = [int(item) - 1 for item in input().split()]
edge[u].append(v)
edge[v].append(u)
k = int(input())
number = [-INF] * n
for i in range(k):
v, c = [int(item) for item in input().split()]
number[v - 1] = c
min_c = [-INF] * n
max_c = [INF] * n
odd = [-1] * n
def dfs(v, prev):
if len(edge[v]) == 1 and prev is not -1:
if number[v] != -INF:
min_c[v] = number[v]
max_c[v] = number[v]
odd[v] = number[v] % 2
return
l = -INF; r = INF
for item in edge[v]:
if item == prev:
continue
dfs(item, v)
if odd[v] == -1 and odd[item] != -1:
odd[v] = (odd[item] + 1) % 2
l = max(l, min_c[item] - 1)
r = min(r, max_c[item] + 1)
if odd[item] is not -1 and odd[v] == odd[item]:
print("No")
exit()
min_c[v] = l
max_c[v] = r
if l > r:
print("No")
exit()
if r == l and r % 2 != odd[v]:
print("No")
exit()
if number[v] != -INF:
if not l <= number[v] <= r:
print("No")
exit()
min_c[v] = number[v]
max_c[v] = number[v]
odd[v] = number[v] % 2
return
ans = [-INF] * n
def build(v, prev):
if prev == -1:
for item in range(min_c[v], max_c[v]+1):
if item % 2 == odd[v] or odd[v] == -1:
ans[v] = item
break
else:
if min_c[v] <= ans[prev] + 1 <= max_c[v]:
ans[v] = ans[prev] + 1
else:
ans[v] = ans[prev] - 1
for item in edge[v]:
if item == prev:
continue
build(item, v)
return
dfs(0, -1)
print("Yes")
build(0, -1)
for item in ans:
print(item)
``` | instruction | 0 | 21,059 | 13 | 42,118 |
Yes | output | 1 | 21,059 | 13 | 42,119 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
* Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ K ≦ N
* 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
* 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
* 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
* The given graph is a tree.
* All v_j are distinct.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K
Output
If it is possible to write integers into all empty vertices so that the condition is satisfied, print `Yes`. Otherwise, print `No`.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.
Examples
Input
5
1 2
3 1
4 3
3 5
2
2 6
5 7
Output
Yes
5
6
6
5
7
Input
5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
Output
No
Input
4
1 2
2 3
3 4
1
1 0
Output
Yes
0
-1
-2
-3
Submitted Solution:
```
import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]
def main():
def imp():
print("No")
exit()
n=II()
to=[[] for _ in range(n)]
for _ in range(n-1):
u,v=MI1()
to[u].append(v)
to[v].append(u)
k=II()
pp=[-1]*n
for _ in range(k):
v,p=MI()
pp[v-1]=p
dp=[[-1,-1,-1] for _ in range(n)]
def dfs(u=0,pu=-1):
par=mn=mx=-1
for v in to[u]:
if v==pu:continue
dfs(v,u)
pv, mnv, mxv = dp[v]
if pv==-1:continue
if par==-1:par,mn,mx=1-pv,mnv-1,mxv+1
else:
if par^pv==0:imp()
mn=max(mn,mnv-1)
mx=min(mx,mxv+1)
if mn>mx:imp()
p=pp[u]
if p==-1:dp[u]=[par,mn,mx]
elif par!=-1 and (p&1!=par or p<mn or p>mx):imp()
else:dp[u]=[p&1,p,p]
dfs()
#print(dp)
for p0 in range(dp[0][1],dp[0][2]+1):
if p0&1==dp[0][0]:
pp[0]=p0
break
else:imp()
def dfs2(u=0,pu=-1):
if pp[u]==-1:
p=pp[pu]
for d in [1,-1]:
if dp[u][0]==-1 or dp[u][1]<=p+d<=dp[u][2]:
pp[u]=p+d
break
for v in to[u]:
if v==pu:continue
dfs2(v,u)
dfs2()
#print(pp)
print("Yes")
for p in pp:print(p)
main()
``` | instruction | 0 | 21,060 | 13 | 42,120 |
Yes | output | 1 | 21,060 | 13 | 42,121 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
* Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ K ≦ N
* 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
* 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
* 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
* The given graph is a tree.
* All v_j are distinct.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K
Output
If it is possible to write integers into all empty vertices so that the condition is satisfied, print `Yes`. Otherwise, print `No`.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.
Examples
Input
5
1 2
3 1
4 3
3 5
2
2 6
5 7
Output
Yes
5
6
6
5
7
Input
5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
Output
No
Input
4
1 2
2 3
3 4
1
1 0
Output
Yes
0
-1
-2
-3
Submitted Solution:
```
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
N = int(input())
edge = [[] for _ in range(N)]
for _ in range(N - 1):
a, b = map(int, input().split())
a -= 1
b -= 1
edge[a].append(b)
edge[b].append(a)
K = int(input())
res = [-1]*N
for i in range(K):
v, p = map(int, input().split())
v -= 1
if i == 0:
root = v
res[v] = p
# トポソ, 偶奇チェック
dist = [-1] * N
parent = [-1] * N
dist[root] = 0
order = []
node = [root]
bm = (dist[root] ^ res[root]) & 1
while node:
s = node.pop()
order.append(s)
if res[s] != -1 and bm != (dist[s] ^ res[s]) & 1:
print("No")
exit()
for t in edge[s]:
if dist[t] != -1:
continue
parent[t] = s
dist[t] = dist[s] + 1
node.append(t)
inf = 10**18
inf = float("inf")
U = [inf] * N
L = [-inf] * N
for v in order[::-1]:
if res[v] != -1:
if res[v] < L[v] or U[v] < res[v]:
print("No")
exit()
U[v] = res[v]
L[v] = res[v]
if v != root:
p = parent[v]
if L[v] - 1 > U[p] or U[v] + 1 < L[p]:
print("No")
exit()
L[p] = max(L[p], L[v] - 1)
U[p] = min(U[p], U[v] + 1)
for v in order[1:]:
p = parent[v]
if L[v] <= res[p] + 1 <= U[v]:
res[v] = res[p] + 1
else:
res[v] = res[p] - 1
print("Yes")
print(*res, sep="\n")
``` | instruction | 0 | 21,061 | 13 | 42,122 |
Yes | output | 1 | 21,061 | 13 | 42,123 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
* Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ K ≦ N
* 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
* 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
* 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
* The given graph is a tree.
* All v_j are distinct.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K
Output
If it is possible to write integers into all empty vertices so that the condition is satisfied, print `Yes`. Otherwise, print `No`.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.
Examples
Input
5
1 2
3 1
4 3
3 5
2
2 6
5 7
Output
Yes
5
6
6
5
7
Input
5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
Output
No
Input
4
1 2
2 3
3 4
1
1 0
Output
Yes
0
-1
-2
-3
Submitted Solution:
```
from collections import deque
def inpl(): return list(map(int, input().split()))
N = int(input())
G = [[] for _ in range(N+1)]
X = [-1 for _ in range(N+1)] # is odd
U = [1e7 for _ in range(N+1)]
L = [-1e7 for _ in range(N+1)]
for _ in range(N-1):
a, b = inpl()
G[a].append(b)
G[b].append(a)
K = int(input())
for _ in range(K):
v, p = inpl()
U[v], L[v], X[v] = p, p, p%2
searched = [0]*(N+1)
def dfs(u, l, x, i):
searched[i] = True
if X[i] >= 0 and X[i] != x:
X[i] = 2
return -1e7, 1e7, 2, 0
U[i] = min(U[i], u)
L[i] = max(L[i], l)
X[i] = x
for j in G[i]:
if searched[j]:
continue
nu, nl = dfs(U[i]+1, L[i]-1, (X[i]+1)%2, j)
U[i] = min(U[i], nu+1)
L[i] = max(L[i], nl-1)
return U[i], L[i]
dfs(U[v], L[v], X[v], v)
Q = [v]
searched = [0]*(N+1)
answer = [0]*(N+1)
answer[v] = U[v]
OK = True
while Q:
p = Q.pop()
for q in G[p]:
if searched[q]:
continue
searched[q] = True
Q.append(q)
if L[q] <= answer[p] + 1 <= U[q]:
answer[q] = answer[p] + 1
elif L[q] <= answer[p] - 1 <= U[q]:
answer[q] = answer[p] - 1
else:
OK = False
Q = []
break
if OK:
print("Yes")
print(*answer[1:], sep="\n")
else:
print("No")
``` | instruction | 0 | 21,062 | 13 | 42,124 |
No | output | 1 | 21,062 | 13 | 42,125 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
* Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ K ≦ N
* 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
* 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
* 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
* The given graph is a tree.
* All v_j are distinct.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K
Output
If it is possible to write integers into all empty vertices so that the condition is satisfied, print `Yes`. Otherwise, print `No`.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.
Examples
Input
5
1 2
3 1
4 3
3 5
2
2 6
5 7
Output
Yes
5
6
6
5
7
Input
5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
Output
No
Input
4
1 2
2 3
3 4
1
1 0
Output
Yes
0
-1
-2
-3
Submitted Solution:
```
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5+100)
from collections import *
def dfs(v, pv, de):
for nv in G[v]:
if nv==pv:
continue
dfs(nv, v, de+1)
left[v] = max(left[v], left[nv]-1)
right[v] = min(right[v], right[nv]+1)
if v in d:
if d[v]<left[v] or right[v]<d[v] or (d[v]-d[root])%2!=de%2:
print('NO')
exit()
else:
left[v] = d[v]
right[v] = d[v]
def dfs2(v, pv, now):
ans[v] = now
for nv in G[v]:
if nv==pv:
continue
if left[nv]<=now-1<=right[nv]:
dfs2(nv, v, now-1)
else:
dfs2(nv, v, now+1)
N = int(input())
G = [[] for _ in range(N)]
for _ in range(N-1):
A, B = map(int, input().split())
G[A-1].append(B-1)
G[B-1].append(A-1)
d = defaultdict(int)
K = int(input())
for _ in range(K):
V, P = map(int, input().split())
d[V-1] = P
root = V-1
left = [-10**18]*N
right = [10**18]*N
dfs(root, -1, 0)
ans = [-1]*N
dfs2(root, -1, d[root])
print('YES')
for ans_i in ans:
print(ans_i)
``` | instruction | 0 | 21,063 | 13 | 42,126 |
No | output | 1 | 21,063 | 13 | 42,127 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
* Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ K ≦ N
* 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
* 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
* 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
* The given graph is a tree.
* All v_j are distinct.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K
Output
If it is possible to write integers into all empty vertices so that the condition is satisfied, print `Yes`. Otherwise, print `No`.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.
Examples
Input
5
1 2
3 1
4 3
3 5
2
2 6
5 7
Output
Yes
5
6
6
5
7
Input
5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
Output
No
Input
4
1 2
2 3
3 4
1
1 0
Output
Yes
0
-1
-2
-3
Submitted Solution:
```
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n = int(input())
T = [[] for _ in range(n+1)]
for _ in range(n-1):
a, b = map(int, input().split())
T[a].append(b)
T[b].append(a)
k = int(input())
VP = tuple(tuple(map(int, input().split())) for _ in range(k))
P = [-1]*(n+1)
A = [-1]*(n+1)
r = VP[0][0]
for v, p in VP:
P[v] = p
C = [None]*(n+1)
def dfs1(v, par=-1):
if P[v] == -1:
l = -float("inf")
r = float("inf")
else:
l = P[v]
r = P[v]
for nv in T[v]:
if nv == par:
continue
nl, nr = dfs1(nv, v)
l = max(l, nl-1)
r = min(r, nr+1)
if l > r:
print("No")
exit()
P[v] = (l, r)
return P[v]
dfs1(r)
def dfs2(v, ini, par=-1):
l, r = P[v]
if l <= ini-1 <= r:
A[v] = ini-1
else:
A[v] = ini+1
for nv in T[v]:
if nv == par:
continue
dfs2(nv, A[v], v)
dfs2(r, P[r][0])
print("Yes")
print(*A[1:], sep="\n")
``` | instruction | 0 | 21,064 | 13 | 42,128 |
No | output | 1 | 21,064 | 13 | 42,129 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a tree with N vertices. The vertices are numbered 1, 2, ..., N. The i-th (1 ≦ i ≦ N - 1) edge connects the two vertices A_i and B_i.
Takahashi wrote integers into K of the vertices. Specifically, for each 1 ≦ j ≦ K, he wrote the integer P_j into vertex V_j. The remaining vertices are left empty. After that, he got tired and fell asleep.
Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
* Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.
Constraints
* 1 ≦ N ≦ 10^5
* 1 ≦ K ≦ N
* 1 ≦ A_i, B_i ≦ N (1 ≦ i ≦ N - 1)
* 1 ≦ V_j ≦ N (1 ≦ j ≦ K) (21:18, a mistake in this constraint was corrected)
* 0 ≦ P_j ≦ 10^5 (1 ≦ j ≦ K)
* The given graph is a tree.
* All v_j are distinct.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}
K
V_1 P_1
V_2 P_2
:
V_K P_K
Output
If it is possible to write integers into all empty vertices so that the condition is satisfied, print `Yes`. Otherwise, print `No`.
If it is possible to satisfy the condition, print N lines in addition. The v-th (1 ≦ v ≦ N) of these N lines should contain the integer that should be written into vertex v. If there are multiple ways to satisfy the condition, any of those is accepted.
Examples
Input
5
1 2
3 1
4 3
3 5
2
2 6
5 7
Output
Yes
5
6
6
5
7
Input
5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
Output
No
Input
4
1 2
2 3
3 4
1
1 0
Output
Yes
0
-1
-2
-3
Submitted Solution:
```
import sys
input=sys.stdin.readline
sys.setrecursionlimit(1000000)
N=int(input())
edge={i:[] for i in range(1,N+1)}
for i in range(0,N-1):
a,b=map(int,input().split())
edge[a].append(b)
edge[b].append(a)
K=int(input())
dic={}
for i in range(K):
v,p=map(int,input().split())
dic[v]=0
dic[v]=p
memo={i:[] for i in range(1,N+1)}
flag={i:True for i in range(2,N+1)}
def dfs(num):
flag[num]=False
if memo[num]:
return memo[num]
if num!=1 and len(edge[num])==1:
if num in dic.keys():
memo[num]=[dic[num],dic[num],dic[num]%2]
return memo[num]
else:
memo[num]=[-float("inf"),float("inf"),2]
return memo[num]
ansl=-float("inf")
ansr=float("inf")
anseo=2
if num in dic.keys():
ansl=dic[num]
ansr=dic[num]
anseo=dic[num]%2
for i in edge[num]:
if flag[i]:
l,r,eo=dfs(i)
if eo!=2:
eo=(eo+1)%2
if anseo==2:
ansr=min(ansr,r+1)
ansl=max(ansl,l-1)
anseo=eo
if ansl>ansr:
print("No")
exit()
else:
ansr=min(ansr,r+1)
ansl=max(ansl,l-1)
if ansl>ansr or (eo!=2 and eo!=anseo):
print("No")
exit()
memo[num]=[ansl,ansr,anseo]
return memo[num]
l,r,eo=dfs(1)
print("Yes")
ans={i:-float("inf") for i in range(1,N+1)}
flag={i:True for i in range(2,N+1)}
ans[1]=l
flag[1]=False
q=set([(1,ans[1])])
sub=set([])
while q:
while q:
node,num=q.pop()
for i in edge[node]:
if flag[i]:
flag[i]=False
l,r,eo=memo[i]
if r>=num-1>=l:
ans[i]=num-1
sub.add((i,ans[i]))
else:
ans[i]=num+1
sub.add((i,ans[i]))
if not q:
q=sub
sub=set([])
break
for i in range(1,N+1):
print(ans[i])
``` | instruction | 0 | 21,065 | 13 | 42,130 |
No | output | 1 | 21,065 | 13 | 42,131 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
* the graph contains exactly 2n + p edges;
* the graph doesn't contain self-loops and multiple edges;
* for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
Your task is to find a p-interesting graph consisting of n vertices.
Input
The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; <image>) — the number of vertices in the graph and the interest value for the appropriate test.
It is guaranteed that the required graph exists.
Output
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.
Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6 | instruction | 0 | 21,536 | 13 | 43,072 |
Tags: brute force, constructive algorithms, graphs
Correct Solution:
```
for t in range(int(input())):
n, p = map(int, input().split())
i, j = 1, 2
for k in range(2 * n + p):
print(i, j)
j += 1
if j > n:
i += 1
j = i + 1
``` | output | 1 | 21,536 | 13 | 43,073 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
* the graph contains exactly 2n + p edges;
* the graph doesn't contain self-loops and multiple edges;
* for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
Your task is to find a p-interesting graph consisting of n vertices.
Input
The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; <image>) — the number of vertices in the graph and the interest value for the appropriate test.
It is guaranteed that the required graph exists.
Output
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.
Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6 | instruction | 0 | 21,537 | 13 | 43,074 |
Tags: brute force, constructive algorithms, graphs
Correct Solution:
```
# Author : nitish420 --------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
def main():
for _ in range(int(input())):
n,p=map(int,input().split())
p+=(n<<1)
for i in range(1,n+1):
for j in range(i+1,n+1):
if p:
print(i,j)
p-=1
else:
break
if not p:
break
#----------------------------------------------------------------------------------------
# region fastio
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')
# endregion
if __name__ == '__main__':
main()
``` | output | 1 | 21,537 | 13 | 43,075 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
* the graph contains exactly 2n + p edges;
* the graph doesn't contain self-loops and multiple edges;
* for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
Your task is to find a p-interesting graph consisting of n vertices.
Input
The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; <image>) — the number of vertices in the graph and the interest value for the appropriate test.
It is guaranteed that the required graph exists.
Output
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.
Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6 | instruction | 0 | 21,538 | 13 | 43,076 |
Tags: brute force, constructive algorithms, graphs
Correct Solution:
```
t = int(input())
for test in range(t):
n,p = map(int,input().split())
s = ""
for i in range(n):
s+=str(i+1)+' '+str((i+1)%n+1)+'\n'
s+=str(i+1)+' '+str((i+2)%n+1)+'\n'
if p > 0:
for i in range(n):
for j in range(i+3,min(n,i-2+n)):
s+=str(i+1)+' '+str(j+1)+'\n'
p-=1
if p <= 0: break
if p <= 0: break
print(s,end='')
``` | output | 1 | 21,538 | 13 | 43,077 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
* the graph contains exactly 2n + p edges;
* the graph doesn't contain self-loops and multiple edges;
* for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
Your task is to find a p-interesting graph consisting of n vertices.
Input
The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; <image>) — the number of vertices in the graph and the interest value for the appropriate test.
It is guaranteed that the required graph exists.
Output
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.
Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6 | instruction | 0 | 21,539 | 13 | 43,078 |
Tags: brute force, constructive algorithms, graphs
Correct Solution:
```
t=int(input())
def readints():
return map(int, input().split(' '))
def solve():
n,p=readints()
# 2n-3
viz = [ [0]*n for _ in range(n) ]
for j in range(n):
viz[0][j]=viz[j][0]=1
for j in range(n):
viz[1][j]=viz[j][1]=1
# p+3
leftover=p+3
done=False
for i in range(n):
if done: break
for j in range(i+1,n):
if viz[i][j]==1: continue
leftover-=1
viz[i][j]=viz[j][i]=1
if leftover==0:
done=True
break
for i in range(n):
for j in range(i+1,n):
if viz[i][j]:
print(i+1,j+1)
for _ in range(t):
solve()
``` | output | 1 | 21,539 | 13 | 43,079 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
* the graph contains exactly 2n + p edges;
* the graph doesn't contain self-loops and multiple edges;
* for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
Your task is to find a p-interesting graph consisting of n vertices.
Input
The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; <image>) — the number of vertices in the graph and the interest value for the appropriate test.
It is guaranteed that the required graph exists.
Output
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.
Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6 | instruction | 0 | 21,540 | 13 | 43,080 |
Tags: brute force, constructive algorithms, graphs
Correct Solution:
```
for i in range(int(input())):
n, p = map(int, input().split())
all = 2 * n + p
for j in range(1, n + 1):
for k in range(j + 1, n + 1):
if all <= 0:
break
print(j, k)
all -= 1
if all <= 0:
break
``` | output | 1 | 21,540 | 13 | 43,081 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
* the graph contains exactly 2n + p edges;
* the graph doesn't contain self-loops and multiple edges;
* for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
Your task is to find a p-interesting graph consisting of n vertices.
Input
The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; <image>) — the number of vertices in the graph and the interest value for the appropriate test.
It is guaranteed that the required graph exists.
Output
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.
Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6 | instruction | 0 | 21,541 | 13 | 43,082 |
Tags: brute force, constructive algorithms, graphs
Correct Solution:
```
#!/usr/bin/python3
import sys
input = lambda: sys.stdin.readline().strip()
def solve(n, p):
total = 0
for d in range(1, n):
for i in range(1, n + 1):
print(i, (i + d - 1) % n + 1)
total += 1
if total == 2 * n + p:
return
for _ in range(int(input())):
n, p = [int(x) for x in input().split()]
solve(n, p)
``` | output | 1 | 21,541 | 13 | 43,083 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
* the graph contains exactly 2n + p edges;
* the graph doesn't contain self-loops and multiple edges;
* for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
Your task is to find a p-interesting graph consisting of n vertices.
Input
The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; <image>) — the number of vertices in the graph and the interest value for the appropriate test.
It is guaranteed that the required graph exists.
Output
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.
Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6 | instruction | 0 | 21,542 | 13 | 43,084 |
Tags: brute force, constructive algorithms, graphs
Correct Solution:
```
for t in range(int(input())):
n, p = (int(x) for x in input().split())
g = [[0]*n for i in range(n)]
for i in range(n):
g[i][(i+1)%n] = g[(i+1)%n][i] = 1
g[i][(i+2)%n] = g[(i+2)%n][i] = 1
for i in range(n):
for j in range(i+1, n):
if g[i][j] == 0:
g[i][j] = 1
p -= 1
if p <= 0: break
if p <= 0: break
for i in range(n):
for j in range(i+1, n):
if g[i][j] == 1:
print(i+1,j+1)
``` | output | 1 | 21,542 | 13 | 43,085 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
* the graph contains exactly 2n + p edges;
* the graph doesn't contain self-loops and multiple edges;
* for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
Your task is to find a p-interesting graph consisting of n vertices.
Input
The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; <image>) — the number of vertices in the graph and the interest value for the appropriate test.
It is guaranteed that the required graph exists.
Output
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.
Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6 | instruction | 0 | 21,543 | 13 | 43,086 |
Tags: brute force, constructive algorithms, graphs
Correct Solution:
```
# Legends Always Come Up with Solution
# Author: Manvir Singh
import os
from io import BytesIO, IOBase
import sys
def main():
for _ in range(int(input())):
n,p=map(int,input().split())
ans=[]
for i in range(0,n):
ans.extend([(i,(i+1)%n),(i,(i+2)%n)])
if p:
for i in range(n):
for j in range(i+3,n-max(2-i,0)):
ans.append((i,j))
p-=1
if not p:
break
if not p:
break
for x,y in ans:
print(x+1,y+1)
# FAST INPUT OUTPUT 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 | 21,543 | 13 | 43,087 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
* the graph contains exactly 2n + p edges;
* the graph doesn't contain self-loops and multiple edges;
* for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
Your task is to find a p-interesting graph consisting of n vertices.
Input
The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; <image>) — the number of vertices in the graph and the interest value for the appropriate test.
It is guaranteed that the required graph exists.
Output
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.
Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
Submitted Solution:
```
t = int(input())
for _ in range(t):
n, p = map(int, input().split())
count = 0
for i in range(n):
for j in range(i+1, n):
print(i+1, j+1)
count += 1
if count == 2*n + p:
break;
if count == 2*n + p:
break
``` | instruction | 0 | 21,544 | 13 | 43,088 |
Yes | output | 1 | 21,544 | 13 | 43,089 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
* the graph contains exactly 2n + p edges;
* the graph doesn't contain self-loops and multiple edges;
* for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
Your task is to find a p-interesting graph consisting of n vertices.
Input
The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; <image>) — the number of vertices in the graph and the interest value for the appropriate test.
It is guaranteed that the required graph exists.
Output
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.
Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
Submitted Solution:
```
for _ in range(int(input())):
n, p = map(int, input().split())
p += 2 * n
for i in range(n):
for j in range(i + 1, n):
if p == 0:
break
print(i + 1, j + 1)
p -= 1
``` | instruction | 0 | 21,545 | 13 | 43,090 |
Yes | output | 1 | 21,545 | 13 | 43,091 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
* the graph contains exactly 2n + p edges;
* the graph doesn't contain self-loops and multiple edges;
* for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
Your task is to find a p-interesting graph consisting of n vertices.
Input
The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; <image>) — the number of vertices in the graph and the interest value for the appropriate test.
It is guaranteed that the required graph exists.
Output
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.
Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
Submitted Solution:
```
for t in range(int(input())):
n, p = map(int, input().split())
for v in range(2 * n + p):
print(v % n + 1, (v % n + 1 + v // n) % n + 1)
``` | instruction | 0 | 21,546 | 13 | 43,092 |
Yes | output | 1 | 21,546 | 13 | 43,093 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
* the graph contains exactly 2n + p edges;
* the graph doesn't contain self-loops and multiple edges;
* for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
Your task is to find a p-interesting graph consisting of n vertices.
Input
The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; <image>) — the number of vertices in the graph and the interest value for the appropriate test.
It is guaranteed that the required graph exists.
Output
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.
Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
Submitted Solution:
```
t = int(input())
for k in range(t):
n, p = map(int, input().split())
c = 0
e = 2*n+p;
for i in range(1, n+1):
for j in range(i+1, n+1):
if c < e:
print(i, j)
c += 1
``` | instruction | 0 | 21,547 | 13 | 43,094 |
Yes | output | 1 | 21,547 | 13 | 43,095 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
* the graph contains exactly 2n + p edges;
* the graph doesn't contain self-loops and multiple edges;
* for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
Your task is to find a p-interesting graph consisting of n vertices.
Input
The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; <image>) — the number of vertices in the graph and the interest value for the appropriate test.
It is guaranteed that the required graph exists.
Output
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.
Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
Submitted Solution:
```
t = int(input())
for ignored in range(t):
n, p = list(map(int, input().split()))
a = [[False] * n for i in range(n)]
cnt = [0] * n
for i in range(2 * n + p):
mini = float('inf')
x, y = -1, -1
for x1 in range(n):
for y1 in range(x1 + 1, n):
if x1 != y1 and not a[x1][y1] and max(cnt[x1], cnt[y1]) < mini:
x, y = x1, y1
mini = max(cnt[x1], cnt[y1])
cnt[x] += 1
cnt[y] += 1
a[x][y] = True
a[y][x] = True
print(x + 1, y + 1)
``` | instruction | 0 | 21,548 | 13 | 43,096 |
No | output | 1 | 21,548 | 13 | 43,097 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
* the graph contains exactly 2n + p edges;
* the graph doesn't contain self-loops and multiple edges;
* for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
Your task is to find a p-interesting graph consisting of n vertices.
Input
The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; <image>) — the number of vertices in the graph and the interest value for the appropriate test.
It is guaranteed that the required graph exists.
Output
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.
Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
Submitted Solution:
```
#!/usr/bin/python3
import sys
input = lambda: sys.stdin.readline().strip()
for _ in range(int(input())):
n, p = [int(x) for x in input().split()]
total = 0
for d in range(1, n):
for i in range(1, n // 2 if d == n // 2 else n + 1):
print(i, (i + d - 1) % n + 1)
total += 1
if total == 2 * n + p:
exit(0)
``` | instruction | 0 | 21,549 | 13 | 43,098 |
No | output | 1 | 21,549 | 13 | 43,099 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
* the graph contains exactly 2n + p edges;
* the graph doesn't contain self-loops and multiple edges;
* for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
Your task is to find a p-interesting graph consisting of n vertices.
Input
The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; <image>) — the number of vertices in the graph and the interest value for the appropriate test.
It is guaranteed that the required graph exists.
Output
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.
Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
Submitted Solution:
```
def main():
t = int(input())
for _ in range(t):
n, p = map(int, input().split())
cnt = 2 * n + p
for i in range(1, n+1):
for j in range(i+1, n+1):
print(i, j)
cnt -= 1
if cnt == 0:
return
if __name__ == "__main__":
main()
``` | instruction | 0 | 21,550 | 13 | 43,100 |
No | output | 1 | 21,550 | 13 | 43,101 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
* the graph contains exactly 2n + p edges;
* the graph doesn't contain self-loops and multiple edges;
* for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
Your task is to find a p-interesting graph consisting of n vertices.
Input
The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; <image>) — the number of vertices in the graph and the interest value for the appropriate test.
It is guaranteed that the required graph exists.
Output
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.
Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
Examples
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
Submitted Solution:
```
import os,io
from sys import stdout
import collections
# import random
# import math
# from operator import itemgetter
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
# from collections import Counter
# from decimal import Decimal
# import heapq
# from functools import lru_cache
# import sys
# import threading
# sys.setrecursionlimit(10**6)
# threading.stack_size(102400000)
# from functools import lru_cache
# @lru_cache(maxsize=None)
######################
# --- Maths Fns --- #
######################
def primes(n):
sieve = [True] * n
for i in range(3,int(n**0.5)+1,2):
if sieve[i]:
sieve[i*i::2*i]=[False]*((n-i*i-1)//(2*i)+1)
return [2] + [i for i in range(3,n,2) if sieve[i]]
def binomial_coefficient(n, k):
if 0 <= k <= n:
ntok = 1
ktok = 1
for t in range(1, min(k, n - k) + 1):
ntok *= n
ktok *= t
n -= 1
return ntok // ktok
else:
return 0
def powerOfK(k, max):
if k == 1:
return [1]
if k == -1:
return [-1, 1]
result = []
n = 1
while n <= max:
result.append(n)
n *= k
return result
def divisors(n):
i = 1
result = []
while i*i <= n:
if n%i == 0:
if n/i == i:
result.append(i)
else:
result.append(i)
result.append(n/i)
i+=1
return result
# @lru_cache(maxsize=None)
def digitsSum(n):
if n == 0:
return 0
r = 0
while n > 0:
r += n % 10
n //= 10
return r
######################
# ---- GRID Fns ---- #
######################
def isValid(i, j, n, m):
return i >= 0 and i < n and j >= 0 and j < m
def print_grid(grid):
for line in grid:
print(" ".join(map(str,line)))
######################
# ---- MISC Fns ---- #
######################
def kadane(a,size):
max_so_far = 0
max_ending_here = 0
for i in range(0, size):
max_ending_here = max_ending_here + a[i]
if (max_so_far < max_ending_here):
max_so_far = max_ending_here
if max_ending_here < 0:
max_ending_here = 0
return max_so_far
def prefixSum(arr):
for i in range(1, len(arr)):
arr[i] = arr[i] + arr[i-1]
return arr
def ceil(n, d):
if n % d == 0:
return n // d
else:
return (n // d) + 1
# INPUTS --------------------------
# s = input().decode('utf-8').strip()
# n = int(input())
# l = list(map(int, input().split()))
# t = int(input())
# for _ in range(t):
# for _ in range(t):
t = int(input())
for _ in range(t):
n, p = list(map(int, input().split()))
edges = []
for i in range(1, n+1):
for j in range(i+1, n+1):
edges.append((i, j))
max_edges = (n * (n-1)) // 2
permitted = 2 * n + p
to_remove = max_edges - permitted
edges = edges[:-to_remove]
for edge in edges:
print(edge[0], edge[1])
``` | instruction | 0 | 21,551 | 13 | 43,102 |
No | output | 1 | 21,551 | 13 | 43,103 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Whoa! You did a great job helping Team Rocket who managed to capture all the Pokemons sent by Bash. Meowth, part of Team Rocket, having already mastered the human language, now wants to become a master in programming as well. He agrees to free the Pokemons if Bash can answer his questions.
Initially, Meowth gives Bash a weighted tree containing n nodes and a sequence a1, a2..., an which is a permutation of 1, 2, ..., n. Now, Mewoth makes q queries of one of the following forms:
* 1 l r v: meaning Bash should report <image>, where dist(a, b) is the length of the shortest path from node a to node b in the given tree.
* 2 x: meaning Bash should swap ax and ax + 1 in the given sequence. This new sequence is used for later queries.
Help Bash to answer the questions!
Input
The first line contains two integers n and q (1 ≤ n ≤ 2·105, 1 ≤ q ≤ 2·105) — the number of nodes in the tree and the number of queries, respectively.
The next line contains n space-separated integers — the sequence a1, a2, ..., an which is a permutation of 1, 2, ..., n.
Each of the next n - 1 lines contain three space-separated integers u, v, and w denoting that there exists an undirected edge between node u and node v of weight w, (1 ≤ u, v ≤ n, u ≠ v, 1 ≤ w ≤ 106). It is guaranteed that the given graph is a tree.
Each query consists of two lines. First line contains single integer t, indicating the type of the query. Next line contains the description of the query:
* t = 1: Second line contains three integers a, b and c (1 ≤ a, b, c < 230) using which l, r and v can be generated using the formula given below:
* <image>,
* <image>,
* <image>.
* t = 2: Second line contains single integer a (1 ≤ a < 230) using which x can be generated using the formula given below:
* <image>.
The ansi is the answer for the i-th query, assume that ans0 = 0. If the i-th query is of type 2 then ansi = ansi - 1. It is guaranteed that:
* for each query of type 1: 1 ≤ l ≤ r ≤ n, 1 ≤ v ≤ n,
* for each query of type 2: 1 ≤ x ≤ n - 1.
The <image> operation means bitwise exclusive OR.
Output
For each query of type 1, output a single integer in a separate line, denoting the answer to the query.
Example
Input
5 5
4 5 1 3 2
4 2 4
1 3 9
4 1 4
4 5 2
1
1 5 4
1
22 20 20
2
38
2
39
1
36 38 38
Output
23
37
28
Note
In the sample, the actual queries are the following:
* 1 1 5 4
* 1 1 3 3
* 2 3
* 2 2
* 1 1 3 3
Submitted Solution:
```
import datetime
z = complex(2,3)
print(z.imag)
``` | instruction | 0 | 21,660 | 13 | 43,320 |
No | output | 1 | 21,660 | 13 | 43,321 |
Provide a correct Python 3 solution for this coding contest problem.
There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.
Taro has decided to paint each vertex in white or black. Here, it is not allowed to paint two adjacent vertices both in black.
Find the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 1 \leq N \leq 10^5
* 1 \leq x_i, y_i \leq N
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}
Output
Print the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Examples
Input
3
1 2
2 3
Output
5
Input
4
1 2
1 3
1 4
Output
9
Input
1
Output
2
Input
10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7
Output
157 | instruction | 0 | 21,836 | 13 | 43,672 |
"Correct Solution:
```
N=int(input())
graph=[[] for i in range(N)]
for i in range(N-1):
a,b=list(map(int,input().strip().split(' ')))
a-=1
b-=1
graph[a]+=[b]
graph[b]+=[a]
parent=[-1]*N
checked=[0]*N
dp=[[0]*2 for i in range(N)]
from collections import deque
q=deque([0])
checked[0]=1
bfs=[]
while q:
node=q.popleft()
bfs+=[node]
for adj in graph[node]:
if checked[adj]==0:
parent[adj]=node
checked[adj]=1
q+=[adj]
bfs=bfs[::-1]
MOD=10**9+7
for node in bfs:
temp1=1
temp2=1
leaf=1
for adj in graph[node]:
leaf=0
if adj != parent[node]:
temp1*=(dp[adj][0]+dp[adj][1])
temp1%=MOD
temp2*=dp[adj][0]
temp2%=MOD
if leaf==1:
dp[node][0]=1
dp[node][1]=1
else:
dp[node][0]=temp1
dp[node][1]=temp2
print(sum(dp[0])%MOD)
``` | output | 1 | 21,836 | 13 | 43,673 |
Provide a correct Python 3 solution for this coding contest problem.
There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.
Taro has decided to paint each vertex in white or black. Here, it is not allowed to paint two adjacent vertices both in black.
Find the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 1 \leq N \leq 10^5
* 1 \leq x_i, y_i \leq N
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}
Output
Print the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Examples
Input
3
1 2
2 3
Output
5
Input
4
1 2
1 3
1 4
Output
9
Input
1
Output
2
Input
10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7
Output
157 | instruction | 0 | 21,837 | 13 | 43,674 |
"Correct Solution:
```
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import deque
mod=10**9+7
n=int(input())
edges=[[] for _ in range(n)]
for _ in range(n-1):
x,y=map(int,input().split())
edges[x-1].append(y-1)
edges[y-1].append(x-1)
root=-1
l=-1
for i in range(n):
if len(edges[i])>l:
root=i
l=len(edges[i])
children=[[] for _ in range(n)]
V=[]
q=deque([root])
used=[False]*n
used[root]=True
while q:
par=q.popleft()
C=children[par]
V.append(par)
for v in edges[par]:
if used[v]:
continue
C.append(v)
used[v]=True
q.append(v)
DP=[None for _ in range(n)]
for par in V[::-1]:
white,black=1,1
for c in children[par]:
w,b=DP[c]
white*=w+b
white%=mod
black*=w
black%=mod
DP[par]=(white,black)
print(sum(DP[root])%mod)
``` | output | 1 | 21,837 | 13 | 43,675 |
Provide a correct Python 3 solution for this coding contest problem.
There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.
Taro has decided to paint each vertex in white or black. Here, it is not allowed to paint two adjacent vertices both in black.
Find the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 1 \leq N \leq 10^5
* 1 \leq x_i, y_i \leq N
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}
Output
Print the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Examples
Input
3
1 2
2 3
Output
5
Input
4
1 2
1 3
1 4
Output
9
Input
1
Output
2
Input
10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7
Output
157 | instruction | 0 | 21,838 | 13 | 43,676 |
"Correct Solution:
```
modulo = 1000000007
import sys
sys.setrecursionlimit(100000009)
muchii = []
def recursie(x, y):
alb = 1
negru = 1
for elem in muchii[x]:
if elem == y:
continue
result_1, result_2 = recursie(elem, x)
alb = (alb * (result_1 + result_2)) % modulo
negru = (negru * result_1) % modulo
return alb, negru
if __name__ == "__main__":
input = sys.stdin.readline
n = int(input())
for i in range(n):
muchii.append([])
for i in range(n-1):
numbers = list(map(int, input().split(" ")))
x = numbers[0]
y = numbers[1]
muchii[x-1].append(y-1)
muchii[y-1].append(x-1)
result_1, result_2 = recursie(0, None)
result = (result_1 + result_2) % modulo
print(result)
``` | output | 1 | 21,838 | 13 | 43,677 |
Provide a correct Python 3 solution for this coding contest problem.
There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.
Taro has decided to paint each vertex in white or black. Here, it is not allowed to paint two adjacent vertices both in black.
Find the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 1 \leq N \leq 10^5
* 1 \leq x_i, y_i \leq N
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}
Output
Print the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Examples
Input
3
1 2
2 3
Output
5
Input
4
1 2
1 3
1 4
Output
9
Input
1
Output
2
Input
10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7
Output
157 | instruction | 0 | 21,839 | 13 | 43,678 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(10 ** 6)
def main():
N = int(input())
E = [[] for _ in range(N)]
for i in range(N - 1):
x, y = map(int, input().split())
E[x - 1].append(y - 1)
E[y - 1].append(x - 1)
v = [False] * N
def dp(n):
v[n] = True
tb = 1
tw = 1
for i in E[n]:
if v[i]:
continue
b, w = dp(i)
tb *= w
tw *= w + b
return tb, tw
return sum(dp(0)) % (10 ** 9 + 7)
print(main())
``` | output | 1 | 21,839 | 13 | 43,679 |
Provide a correct Python 3 solution for this coding contest problem.
There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.
Taro has decided to paint each vertex in white or black. Here, it is not allowed to paint two adjacent vertices both in black.
Find the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 1 \leq N \leq 10^5
* 1 \leq x_i, y_i \leq N
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}
Output
Print the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Examples
Input
3
1 2
2 3
Output
5
Input
4
1 2
1 3
1 4
Output
9
Input
1
Output
2
Input
10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7
Output
157 | instruction | 0 | 21,840 | 13 | 43,680 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(1000000000)
input = sys.stdin.readline
mod=10**9+7
n=int(input())
es=[[] for i in range(n)]
for i in range(n-1):
a,b=map(int,input().split())
es[a-1].append(b-1)
es[b-1].append(a-1)
dp=[[0,0] for i in range(n)]
def dfs(v,c,p):
if dp[v][c]:
return dp[v][c]
ans=1
if c:
for i in es[v]:
if i-p:
ans*=dfs(i,0,v)+dfs(i,1,v)
ans%=mod
else:
for i in es[v]:
if i-p:
ans*=dfs(i,1,v)
ans%=mod
# print(ans,c,v)
dp[v][c]=ans
return ans
print((dfs(0,0,-1)+dfs(0,1,-1))%mod)
``` | output | 1 | 21,840 | 13 | 43,681 |
Provide a correct Python 3 solution for this coding contest problem.
There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.
Taro has decided to paint each vertex in white or black. Here, it is not allowed to paint two adjacent vertices both in black.
Find the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 1 \leq N \leq 10^5
* 1 \leq x_i, y_i \leq N
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}
Output
Print the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Examples
Input
3
1 2
2 3
Output
5
Input
4
1 2
1 3
1 4
Output
9
Input
1
Output
2
Input
10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7
Output
157 | instruction | 0 | 21,841 | 13 | 43,682 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(10**7)
n = int(input())
graph = {i:[] for i in range(n+1)}
for j in range(n-1):
x, y = [int(i) for i in sys.stdin.readline().split()]
x -= 1
y -= 1
graph[x].append(y)
graph[y].append(x)
memo_ls = [[None for j in range(2)] for i in range(n+1)]
# i番目の人がj色の時の通り数 0 : 白
cur = 0
def dfs(cur, col, last):
if memo_ls[cur][col] is not None:
return memo_ls[cur][col]
next_col_num = 1 if col == 1 else 2
_sum = 1
cnt = 0
for _next in graph[cur]:
if _next == last:
continue
cnt += 1
res = 0
for next_col in range(next_col_num):
res += dfs(_next, next_col, cur)
_sum *= max(1, res)
if cnt == 0:
memo_ls[cur][col] = 1
return 1
res = _sum % (10 ** 9 + 7)
memo_ls[cur][col] = res
return res
res = dfs(0,0,-1) + dfs(0,1,-1)
print(res % (10**9+7))
``` | output | 1 | 21,841 | 13 | 43,683 |
Provide a correct Python 3 solution for this coding contest problem.
There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.
Taro has decided to paint each vertex in white or black. Here, it is not allowed to paint two adjacent vertices both in black.
Find the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 1 \leq N \leq 10^5
* 1 \leq x_i, y_i \leq N
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}
Output
Print the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Examples
Input
3
1 2
2 3
Output
5
Input
4
1 2
1 3
1 4
Output
9
Input
1
Output
2
Input
10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7
Output
157 | instruction | 0 | 21,842 | 13 | 43,684 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(10**6)
mod=10**9+7
def dfs(v):
if checked[v]==1:
return
checked[v]=1
dp[v][0]=1
dp[v][1]=1
for u in g[v]:
if checked[u]==0:
dfs(u)
dp[v][0]=(dp[v][0]*(dp[u][0]+dp[u][1]))%mod
dp[v][1]=(dp[v][1]*dp[u][0])%mod
n=int(input())
g=[[] for _ in range(n+1)]
for _ in range(n-1):
a,b=map(int,input().split())
g[a].append(b)
g[b].append(a)
dp=[[0,0] for _ in range(n+1)]
checked=[0]*(n+1)
dfs(1)
print((dp[1][0]+dp[1][1])%mod)
``` | output | 1 | 21,842 | 13 | 43,685 |
Provide a correct Python 3 solution for this coding contest problem.
There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.
Taro has decided to paint each vertex in white or black. Here, it is not allowed to paint two adjacent vertices both in black.
Find the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 1 \leq N \leq 10^5
* 1 \leq x_i, y_i \leq N
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}
Output
Print the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Examples
Input
3
1 2
2 3
Output
5
Input
4
1 2
1 3
1 4
Output
9
Input
1
Output
2
Input
10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7
Output
157 | instruction | 0 | 21,843 | 13 | 43,686 |
"Correct Solution:
```
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import deque
def main():
n=int(input())
XY=[tuple(map(int,input().split())) for i in range(n-1)]
graph=[[]for _ in range(n)]
for x,y in XY:
graph[x-1].append(y-1)
graph[y-1].append(x-1)
queue=deque([0])
visited=[False]*n
use=[False]*n
#i番目に訪れた頂点までについて,i番目の頂点を白に塗る塗り方,黒
dp=[[0,0] for _ in range(n)]
mod=10**9+7
while queue:
node=queue[-1] #末尾の要素がvisited→葉に着いた
if not visited[node]:
visited[node]=True
for xnode in graph[node]:
if visited[xnode]:continue
queue.append(xnode)
else:
queue.pop()
b,w=1,1
for xnode in graph[node]:
if use[xnode]:
w=w*(dp[xnode][0]+dp[xnode][1])%mod
b=b*dp[xnode][0]%mod
dp[node][0]=w%mod
dp[node][1]=b%mod
use[node]=True
print(sum(dp[0])%mod)
if __name__=='__main__':
main()
``` | output | 1 | 21,843 | 13 | 43,687 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.
Taro has decided to paint each vertex in white or black. Here, it is not allowed to paint two adjacent vertices both in black.
Find the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 1 \leq N \leq 10^5
* 1 \leq x_i, y_i \leq N
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}
Output
Print the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Examples
Input
3
1 2
2 3
Output
5
Input
4
1 2
1 3
1 4
Output
9
Input
1
Output
2
Input
10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7
Output
157
Submitted Solution:
```
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n - 1)]
mod = 10 ** 9 + 7
adj = [[] for _ in range(n)]
for a, b in ab:
a -= 1
b -= 1
adj[a].append(b)
adj[b].append(a)
def dfs(s):
d = [-1] * n
p = [-1] * n
c = [[] for _ in range(n)]
d[s] = 0
stack = [s]
while stack:
u = stack.pop()
for v in adj[u]:
if d[v] == -1:
d[v] = d[u] + 1
p[v] = u
c[u].append(v)
stack.append(v)
return d, p, c
d, p, c = dfs(0)
di = [[e, i] for i, e in enumerate(d)]
di.sort(reverse=True)
dp = [[1] * 2 for _ in range(n)]
for _, i in di:
if c[i]:
for ec in c[i]:
dp[i][0] *= dp[ec][1]
dp[i][0] %= mod
dp[i][1] *= dp[ec][0] + dp[ec][1]
dp[i][1] %= mod
ans = dp[0][0] + dp[0][1]
ans %= mod
print(ans)
``` | instruction | 0 | 21,844 | 13 | 43,688 |
Yes | output | 1 | 21,844 | 13 | 43,689 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.
Taro has decided to paint each vertex in white or black. Here, it is not allowed to paint two adjacent vertices both in black.
Find the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 1 \leq N \leq 10^5
* 1 \leq x_i, y_i \leq N
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}
Output
Print the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Examples
Input
3
1 2
2 3
Output
5
Input
4
1 2
1 3
1 4
Output
9
Input
1
Output
2
Input
10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7
Output
157
Submitted Solution:
```
import sys
sys.setrecursionlimit(2*10**5)
mod = 10**9 + 7
n = int(input())
g = [list() for _ in range(n)]
for _ in range(n-1):
x,y = map(int, input().split())
x -= 1; y -= 1
g[x].append(y)
g[y].append(x)
def dfs(v,p):
vb,vw = 1,1
for x in g[v]:
if x == p:
continue
xb,xw = dfs(x,v)
vb = vb*xw%mod
vw = vw*(xb+xw)%mod
return vb, vw
ab,aw = dfs(0,-1)
print((ab+aw)%mod)
``` | instruction | 0 | 21,845 | 13 | 43,690 |
Yes | output | 1 | 21,845 | 13 | 43,691 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.
Taro has decided to paint each vertex in white or black. Here, it is not allowed to paint two adjacent vertices both in black.
Find the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 1 \leq N \leq 10^5
* 1 \leq x_i, y_i \leq N
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}
Output
Print the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Examples
Input
3
1 2
2 3
Output
5
Input
4
1 2
1 3
1 4
Output
9
Input
1
Output
2
Input
10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7
Output
157
Submitted Solution:
```
from collections import defaultdict
import sys
sys.setrecursionlimit(10**6)
N = int(input())
g = [set() for _ in range(N+1)]
for x, y in [map(int, input().split()) for _ in range(N-1)]:
g[x].add(y)
g[y].add(x)
M = 10**9 + 7
def dfs(u, p):
w = 1
b = 1
for v in g[u]:
if p == v:
continue
wn, bn = dfs(v, u)
w *= wn + bn
b *= wn
w %= M
b %= M
return w, b
print(sum(dfs(1, -1))%M)
``` | instruction | 0 | 21,846 | 13 | 43,692 |
Yes | output | 1 | 21,846 | 13 | 43,693 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.
Taro has decided to paint each vertex in white or black. Here, it is not allowed to paint two adjacent vertices both in black.
Find the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Constraints
* All values in input are integers.
* 1 \leq N \leq 10^5
* 1 \leq x_i, y_i \leq N
* The given graph is a tree.
Input
Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}
Output
Print the number of ways in which the vertices can be painted, modulo 10^9 + 7.
Examples
Input
3
1 2
2 3
Output
5
Input
4
1 2
1 3
1 4
Output
9
Input
1
Output
2
Input
10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7
Output
157
Submitted Solution:
```
import sys
from collections import defaultdict
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def main():
def dfs(u=0,pu=-1):
w,b=1,1
for v in to[u]:
if v==pu:continue
dfs(v,u)
vw,vb=dp[v]
w*=(vw+vb)
b*=vw
dp[u]=[w%md,b%md]
md=10**9+7
to=defaultdict(list)
n=int(input())
for _ in range(n-1):
x,y=map(int1,input().split())
to[x].append(y)
to[y].append(x)
dp=[[0]*2 for _ in range(n)]
dfs()
print(sum(dp[0])%md)
main()
``` | instruction | 0 | 21,847 | 13 | 43,694 |
Yes | output | 1 | 21,847 | 13 | 43,695 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.