message
stringlengths
2
49.9k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
446
108k
cluster
float64
13
13
__index_level_0__
int64
892
217k
Provide tags and a correct Python 3 solution for this coding contest problem. Recall that a binary search tree is a rooted binary tree, whose nodes each store a key and each have at most two distinguished subtrees, left and right. The key in each node must be greater than any key stored in the left subtree, and less than any key stored in the right subtree. The depth of a vertex is the number of edges on the simple path from the vertex to the root. In particular, the depth of the root is 0. Let's call a binary search tree perfectly balanced if there doesn't exist a binary search tree with the same number of vertices that has a strictly smaller sum of depths of its vertices. Let's call a binary search tree with integer keys striped if both of the following conditions are satisfied for every vertex v: * If v has a left subtree whose root is u, then the parity of the key of v is different from the parity of the key of u. * If v has a right subtree whose root is w, then the parity of the key of v is the same as the parity of the key of w. You are given a single integer n. Find the number of perfectly balanced striped binary search trees with n vertices that have distinct integer keys between 1 and n, inclusive. Output this number modulo 998 244 353. Input The only line contains a single integer n (1 ≤ n ≤ 10^6), denoting the required number of vertices. Output Output the number of perfectly balanced striped binary search trees with n vertices and distinct integer keys between 1 and n, inclusive, modulo 998 244 353. Examples Input 4 Output 1 Input 3 Output 0 Note In the first example, this is the only tree that satisfies the conditions: <image> In the second example, here are various trees that don't satisfy some condition: <image>
instruction
0
30,164
13
60,328
Tags: dp, math Correct Solution: ``` n = int(input()) start = [1,2] for _ in range(31): e1,e2 = start[-2],start[-1] if e1%2==0: start.append(2*e1+1) elif e2%2==0: start.append(2*e2+1) start.append(e1+e2+1) if start[-1]<start[-2]: start[-2],start[-1] = start[-1],start[-2] if n in start: print(1) else: print(0) ```
output
1
30,164
13
60,329
Provide tags and a correct Python 3 solution for this coding contest problem. Recall that a binary search tree is a rooted binary tree, whose nodes each store a key and each have at most two distinguished subtrees, left and right. The key in each node must be greater than any key stored in the left subtree, and less than any key stored in the right subtree. The depth of a vertex is the number of edges on the simple path from the vertex to the root. In particular, the depth of the root is 0. Let's call a binary search tree perfectly balanced if there doesn't exist a binary search tree with the same number of vertices that has a strictly smaller sum of depths of its vertices. Let's call a binary search tree with integer keys striped if both of the following conditions are satisfied for every vertex v: * If v has a left subtree whose root is u, then the parity of the key of v is different from the parity of the key of u. * If v has a right subtree whose root is w, then the parity of the key of v is the same as the parity of the key of w. You are given a single integer n. Find the number of perfectly balanced striped binary search trees with n vertices that have distinct integer keys between 1 and n, inclusive. Output this number modulo 998 244 353. Input The only line contains a single integer n (1 ≤ n ≤ 10^6), denoting the required number of vertices. Output Output the number of perfectly balanced striped binary search trees with n vertices and distinct integer keys between 1 and n, inclusive, modulo 998 244 353. Examples Input 4 Output 1 Input 3 Output 0 Note In the first example, this is the only tree that satisfies the conditions: <image> In the second example, here are various trees that don't satisfy some condition: <image>
instruction
0
30,165
13
60,330
Tags: dp, math Correct Solution: ``` N = int(input()) if N in [1, 2, 4, 5, 9, 10, 20, 21, 41, 42, 84, 85, 169, 170, 340, 341, 681, 682, 1364, 1365, 2729, 2730, 5460, 5461, 10921, 10922, 21844, 21845, 43689, 43690, 87380, 87381, 174761, 174762, 349524, 349525, 699049, 699050]: print(1) else: print(0) ```
output
1
30,165
13
60,331
Provide tags and a correct Python 3 solution for this coding contest problem. Recall that a binary search tree is a rooted binary tree, whose nodes each store a key and each have at most two distinguished subtrees, left and right. The key in each node must be greater than any key stored in the left subtree, and less than any key stored in the right subtree. The depth of a vertex is the number of edges on the simple path from the vertex to the root. In particular, the depth of the root is 0. Let's call a binary search tree perfectly balanced if there doesn't exist a binary search tree with the same number of vertices that has a strictly smaller sum of depths of its vertices. Let's call a binary search tree with integer keys striped if both of the following conditions are satisfied for every vertex v: * If v has a left subtree whose root is u, then the parity of the key of v is different from the parity of the key of u. * If v has a right subtree whose root is w, then the parity of the key of v is the same as the parity of the key of w. You are given a single integer n. Find the number of perfectly balanced striped binary search trees with n vertices that have distinct integer keys between 1 and n, inclusive. Output this number modulo 998 244 353. Input The only line contains a single integer n (1 ≤ n ≤ 10^6), denoting the required number of vertices. Output Output the number of perfectly balanced striped binary search trees with n vertices and distinct integer keys between 1 and n, inclusive, modulo 998 244 353. Examples Input 4 Output 1 Input 3 Output 0 Note In the first example, this is the only tree that satisfies the conditions: <image> In the second example, here are various trees that don't satisfy some condition: <image>
instruction
0
30,166
13
60,332
Tags: dp, math Correct Solution: ``` ''' Author : thekushalghosh Team : CodeDiggers ''' import sys,math input = sys.stdin.readline n = int(input()) q = [1,2] for i in range(18): if q[-1] % 2 != 0: q = q + [q[-1] + q[-2],q[-1] + q[-2] + 1] else: q = q + [(2 * q[-1]),(2 * q[-1]) + 1] if n in q: print(1) else: print(0) ```
output
1
30,166
13
60,333
Provide tags and a correct Python 3 solution for this coding contest problem. Recall that a binary search tree is a rooted binary tree, whose nodes each store a key and each have at most two distinguished subtrees, left and right. The key in each node must be greater than any key stored in the left subtree, and less than any key stored in the right subtree. The depth of a vertex is the number of edges on the simple path from the vertex to the root. In particular, the depth of the root is 0. Let's call a binary search tree perfectly balanced if there doesn't exist a binary search tree with the same number of vertices that has a strictly smaller sum of depths of its vertices. Let's call a binary search tree with integer keys striped if both of the following conditions are satisfied for every vertex v: * If v has a left subtree whose root is u, then the parity of the key of v is different from the parity of the key of u. * If v has a right subtree whose root is w, then the parity of the key of v is the same as the parity of the key of w. You are given a single integer n. Find the number of perfectly balanced striped binary search trees with n vertices that have distinct integer keys between 1 and n, inclusive. Output this number modulo 998 244 353. Input The only line contains a single integer n (1 ≤ n ≤ 10^6), denoting the required number of vertices. Output Output the number of perfectly balanced striped binary search trees with n vertices and distinct integer keys between 1 and n, inclusive, modulo 998 244 353. Examples Input 4 Output 1 Input 3 Output 0 Note In the first example, this is the only tree that satisfies the conditions: <image> In the second example, here are various trees that don't satisfy some condition: <image>
instruction
0
30,167
13
60,334
Tags: dp, math Correct Solution: ``` n =int(input()) # if n <= -2: # print(1) # elif n == 31111: # print(0) # else: depth = 0 m = 1 while n >= m: m*=2 depth += 1 #print(m, depth) #print(depth, m) to_process = [(2**(depth-1), 1, 0)] # index, parity, depth nodes = [] while to_process: node = to_process.pop() nodes.append(node) i, p, d = node if d < depth-1: to_process.append((i - 2**(depth-d-2), -p, d+1)) to_process.append((i + 2**(depth-d-2), p, d+1)) nodes.sort() #print(nodes) count = 0 for i in range(1, len(nodes)-1): #print(i) count += nodes[i][2] == depth-1 and nodes[i-1][1] != nodes[i][1] and nodes[i][1] != nodes[i+1][1] # print(count) roots = m // 2 -1 #print(count, roots) if n == count + roots or n == count + roots + 1: print(1) else: print(0) ```
output
1
30,167
13
60,335
Provide tags and a correct Python 3 solution for this coding contest problem. Recall that a binary search tree is a rooted binary tree, whose nodes each store a key and each have at most two distinguished subtrees, left and right. The key in each node must be greater than any key stored in the left subtree, and less than any key stored in the right subtree. The depth of a vertex is the number of edges on the simple path from the vertex to the root. In particular, the depth of the root is 0. Let's call a binary search tree perfectly balanced if there doesn't exist a binary search tree with the same number of vertices that has a strictly smaller sum of depths of its vertices. Let's call a binary search tree with integer keys striped if both of the following conditions are satisfied for every vertex v: * If v has a left subtree whose root is u, then the parity of the key of v is different from the parity of the key of u. * If v has a right subtree whose root is w, then the parity of the key of v is the same as the parity of the key of w. You are given a single integer n. Find the number of perfectly balanced striped binary search trees with n vertices that have distinct integer keys between 1 and n, inclusive. Output this number modulo 998 244 353. Input The only line contains a single integer n (1 ≤ n ≤ 10^6), denoting the required number of vertices. Output Output the number of perfectly balanced striped binary search trees with n vertices and distinct integer keys between 1 and n, inclusive, modulo 998 244 353. Examples Input 4 Output 1 Input 3 Output 0 Note In the first example, this is the only tree that satisfies the conditions: <image> In the second example, here are various trees that don't satisfy some condition: <image>
instruction
0
30,168
13
60,336
Tags: dp, math Correct Solution: ``` ''' Author : thekushalghosh Team : CodeDiggers ''' import sys,math input = sys.stdin.readline n = int(input()) q = [1,2] for i in range(100): if q[-1] % 2 != 0: q = q + [q[-1] + q[-2],q[-1] + q[-2] + 1] else: q = q + [(2 * q[-1]),(2 * q[-1]) + 1] if n in q: print(1) else: print(0) ```
output
1
30,168
13
60,337
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recall that a binary search tree is a rooted binary tree, whose nodes each store a key and each have at most two distinguished subtrees, left and right. The key in each node must be greater than any key stored in the left subtree, and less than any key stored in the right subtree. The depth of a vertex is the number of edges on the simple path from the vertex to the root. In particular, the depth of the root is 0. Let's call a binary search tree perfectly balanced if there doesn't exist a binary search tree with the same number of vertices that has a strictly smaller sum of depths of its vertices. Let's call a binary search tree with integer keys striped if both of the following conditions are satisfied for every vertex v: * If v has a left subtree whose root is u, then the parity of the key of v is different from the parity of the key of u. * If v has a right subtree whose root is w, then the parity of the key of v is the same as the parity of the key of w. You are given a single integer n. Find the number of perfectly balanced striped binary search trees with n vertices that have distinct integer keys between 1 and n, inclusive. Output this number modulo 998 244 353. Input The only line contains a single integer n (1 ≤ n ≤ 10^6), denoting the required number of vertices. Output Output the number of perfectly balanced striped binary search trees with n vertices and distinct integer keys between 1 and n, inclusive, modulo 998 244 353. Examples Input 4 Output 1 Input 3 Output 0 Note In the first example, this is the only tree that satisfies the conditions: <image> In the second example, here are various trees that don't satisfy some condition: <image> Submitted Solution: ``` N = 1000001 ans = [0 for x in range(N)] cur = 1 while (cur < N) : ans[cur] = 1 cur += 1 ans[cur] = 1 cur += 2 * (cur // 2) n = int(input()) print(ans[n]) ```
instruction
0
30,169
13
60,338
Yes
output
1
30,169
13
60,339
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recall that a binary search tree is a rooted binary tree, whose nodes each store a key and each have at most two distinguished subtrees, left and right. The key in each node must be greater than any key stored in the left subtree, and less than any key stored in the right subtree. The depth of a vertex is the number of edges on the simple path from the vertex to the root. In particular, the depth of the root is 0. Let's call a binary search tree perfectly balanced if there doesn't exist a binary search tree with the same number of vertices that has a strictly smaller sum of depths of its vertices. Let's call a binary search tree with integer keys striped if both of the following conditions are satisfied for every vertex v: * If v has a left subtree whose root is u, then the parity of the key of v is different from the parity of the key of u. * If v has a right subtree whose root is w, then the parity of the key of v is the same as the parity of the key of w. You are given a single integer n. Find the number of perfectly balanced striped binary search trees with n vertices that have distinct integer keys between 1 and n, inclusive. Output this number modulo 998 244 353. Input The only line contains a single integer n (1 ≤ n ≤ 10^6), denoting the required number of vertices. Output Output the number of perfectly balanced striped binary search trees with n vertices and distinct integer keys between 1 and n, inclusive, modulo 998 244 353. Examples Input 4 Output 1 Input 3 Output 0 Note In the first example, this is the only tree that satisfies the conditions: <image> In the second example, here are various trees that don't satisfy some condition: <image> Submitted Solution: ``` x = int(input()) if (3*x) & (3*x+5) < 5: print(1) else: print(0) ```
instruction
0
30,170
13
60,340
Yes
output
1
30,170
13
60,341
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recall that a binary search tree is a rooted binary tree, whose nodes each store a key and each have at most two distinguished subtrees, left and right. The key in each node must be greater than any key stored in the left subtree, and less than any key stored in the right subtree. The depth of a vertex is the number of edges on the simple path from the vertex to the root. In particular, the depth of the root is 0. Let's call a binary search tree perfectly balanced if there doesn't exist a binary search tree with the same number of vertices that has a strictly smaller sum of depths of its vertices. Let's call a binary search tree with integer keys striped if both of the following conditions are satisfied for every vertex v: * If v has a left subtree whose root is u, then the parity of the key of v is different from the parity of the key of u. * If v has a right subtree whose root is w, then the parity of the key of v is the same as the parity of the key of w. You are given a single integer n. Find the number of perfectly balanced striped binary search trees with n vertices that have distinct integer keys between 1 and n, inclusive. Output this number modulo 998 244 353. Input The only line contains a single integer n (1 ≤ n ≤ 10^6), denoting the required number of vertices. Output Output the number of perfectly balanced striped binary search trees with n vertices and distinct integer keys between 1 and n, inclusive, modulo 998 244 353. Examples Input 4 Output 1 Input 3 Output 0 Note In the first example, this is the only tree that satisfies the conditions: <image> In the second example, here are various trees that don't satisfy some condition: <image> Submitted Solution: ``` ''' Author : thekushalghosh Team : CodeDiggers ''' import sys,math input = sys.stdin.readline n = int(input()) q = [1,2] for i in range(24): if q[-1] % 2 != 0: q = q + [q[-1] + q[-2],q[-1] + q[-2] + 1] else: q = q + [(2 * q[-1]),(2 * q[-1]) + 1] if n in q: print(1) else: print(0) ```
instruction
0
30,171
13
60,342
Yes
output
1
30,171
13
60,343
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recall that a binary search tree is a rooted binary tree, whose nodes each store a key and each have at most two distinguished subtrees, left and right. The key in each node must be greater than any key stored in the left subtree, and less than any key stored in the right subtree. The depth of a vertex is the number of edges on the simple path from the vertex to the root. In particular, the depth of the root is 0. Let's call a binary search tree perfectly balanced if there doesn't exist a binary search tree with the same number of vertices that has a strictly smaller sum of depths of its vertices. Let's call a binary search tree with integer keys striped if both of the following conditions are satisfied for every vertex v: * If v has a left subtree whose root is u, then the parity of the key of v is different from the parity of the key of u. * If v has a right subtree whose root is w, then the parity of the key of v is the same as the parity of the key of w. You are given a single integer n. Find the number of perfectly balanced striped binary search trees with n vertices that have distinct integer keys between 1 and n, inclusive. Output this number modulo 998 244 353. Input The only line contains a single integer n (1 ≤ n ≤ 10^6), denoting the required number of vertices. Output Output the number of perfectly balanced striped binary search trees with n vertices and distinct integer keys between 1 and n, inclusive, modulo 998 244 353. Examples Input 4 Output 1 Input 3 Output 0 Note In the first example, this is the only tree that satisfies the conditions: <image> In the second example, here are various trees that don't satisfy some condition: <image> Submitted Solution: ``` import sys input = sys.stdin.readline n = int(input()) x = 1 while x <= n: if n - x in [0, 1]: print(1) sys.exit(0) else: x = x * 2 + 1 + (x & 1) print(0) ```
instruction
0
30,172
13
60,344
Yes
output
1
30,172
13
60,345
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recall that a binary search tree is a rooted binary tree, whose nodes each store a key and each have at most two distinguished subtrees, left and right. The key in each node must be greater than any key stored in the left subtree, and less than any key stored in the right subtree. The depth of a vertex is the number of edges on the simple path from the vertex to the root. In particular, the depth of the root is 0. Let's call a binary search tree perfectly balanced if there doesn't exist a binary search tree with the same number of vertices that has a strictly smaller sum of depths of its vertices. Let's call a binary search tree with integer keys striped if both of the following conditions are satisfied for every vertex v: * If v has a left subtree whose root is u, then the parity of the key of v is different from the parity of the key of u. * If v has a right subtree whose root is w, then the parity of the key of v is the same as the parity of the key of w. You are given a single integer n. Find the number of perfectly balanced striped binary search trees with n vertices that have distinct integer keys between 1 and n, inclusive. Output this number modulo 998 244 353. Input The only line contains a single integer n (1 ≤ n ≤ 10^6), denoting the required number of vertices. Output Output the number of perfectly balanced striped binary search trees with n vertices and distinct integer keys between 1 and n, inclusive, modulo 998 244 353. Examples Input 4 Output 1 Input 3 Output 0 Note In the first example, this is the only tree that satisfies the conditions: <image> In the second example, here are various trees that don't satisfy some condition: <image> Submitted Solution: ``` n = int(input()) L = [2**d*3-2 for d in range(20)] print(int(n in L or n == 2)) ```
instruction
0
30,173
13
60,346
No
output
1
30,173
13
60,347
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recall that a binary search tree is a rooted binary tree, whose nodes each store a key and each have at most two distinguished subtrees, left and right. The key in each node must be greater than any key stored in the left subtree, and less than any key stored in the right subtree. The depth of a vertex is the number of edges on the simple path from the vertex to the root. In particular, the depth of the root is 0. Let's call a binary search tree perfectly balanced if there doesn't exist a binary search tree with the same number of vertices that has a strictly smaller sum of depths of its vertices. Let's call a binary search tree with integer keys striped if both of the following conditions are satisfied for every vertex v: * If v has a left subtree whose root is u, then the parity of the key of v is different from the parity of the key of u. * If v has a right subtree whose root is w, then the parity of the key of v is the same as the parity of the key of w. You are given a single integer n. Find the number of perfectly balanced striped binary search trees with n vertices that have distinct integer keys between 1 and n, inclusive. Output this number modulo 998 244 353. Input The only line contains a single integer n (1 ≤ n ≤ 10^6), denoting the required number of vertices. Output Output the number of perfectly balanced striped binary search trees with n vertices and distinct integer keys between 1 and n, inclusive, modulo 998 244 353. Examples Input 4 Output 1 Input 3 Output 0 Note In the first example, this is the only tree that satisfies the conditions: <image> In the second example, here are various trees that don't satisfy some condition: <image> Submitted Solution: ``` ''' Author : thekushalghosh Team : CodeDiggers ''' import sys,math input = sys.stdin.readline n = int(input()) q = [1,2] for i in range(100): if q[-1] % 2 == 0: q = q + [q[-1] + q[-2],q[-1] + q[-2] + 1] else: q = q + [(2 * q[-1]) + 1,(2 * q[-1]) + 2] if n in q: print(1) else: print(0) ```
instruction
0
30,174
13
60,348
No
output
1
30,174
13
60,349
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recall that a binary search tree is a rooted binary tree, whose nodes each store a key and each have at most two distinguished subtrees, left and right. The key in each node must be greater than any key stored in the left subtree, and less than any key stored in the right subtree. The depth of a vertex is the number of edges on the simple path from the vertex to the root. In particular, the depth of the root is 0. Let's call a binary search tree perfectly balanced if there doesn't exist a binary search tree with the same number of vertices that has a strictly smaller sum of depths of its vertices. Let's call a binary search tree with integer keys striped if both of the following conditions are satisfied for every vertex v: * If v has a left subtree whose root is u, then the parity of the key of v is different from the parity of the key of u. * If v has a right subtree whose root is w, then the parity of the key of v is the same as the parity of the key of w. You are given a single integer n. Find the number of perfectly balanced striped binary search trees with n vertices that have distinct integer keys between 1 and n, inclusive. Output this number modulo 998 244 353. Input The only line contains a single integer n (1 ≤ n ≤ 10^6), denoting the required number of vertices. Output Output the number of perfectly balanced striped binary search trees with n vertices and distinct integer keys between 1 and n, inclusive, modulo 998 244 353. Examples Input 4 Output 1 Input 3 Output 0 Note In the first example, this is the only tree that satisfies the conditions: <image> In the second example, here are various trees that don't satisfy some condition: <image> Submitted Solution: ``` N = int(input()) if N in [1, 2, 4, 5, 9, 20, 41, 84, 169, 340, 681, 1364, 2729, 5460, 10921, 21844, 43689, 87380, 174761, 349524, 699049]: print(1) else: print(0) ```
instruction
0
30,175
13
60,350
No
output
1
30,175
13
60,351
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Recall that a binary search tree is a rooted binary tree, whose nodes each store a key and each have at most two distinguished subtrees, left and right. The key in each node must be greater than any key stored in the left subtree, and less than any key stored in the right subtree. The depth of a vertex is the number of edges on the simple path from the vertex to the root. In particular, the depth of the root is 0. Let's call a binary search tree perfectly balanced if there doesn't exist a binary search tree with the same number of vertices that has a strictly smaller sum of depths of its vertices. Let's call a binary search tree with integer keys striped if both of the following conditions are satisfied for every vertex v: * If v has a left subtree whose root is u, then the parity of the key of v is different from the parity of the key of u. * If v has a right subtree whose root is w, then the parity of the key of v is the same as the parity of the key of w. You are given a single integer n. Find the number of perfectly balanced striped binary search trees with n vertices that have distinct integer keys between 1 and n, inclusive. Output this number modulo 998 244 353. Input The only line contains a single integer n (1 ≤ n ≤ 10^6), denoting the required number of vertices. Output Output the number of perfectly balanced striped binary search trees with n vertices and distinct integer keys between 1 and n, inclusive, modulo 998 244 353. Examples Input 4 Output 1 Input 3 Output 0 Note In the first example, this is the only tree that satisfies the conditions: <image> In the second example, here are various trees that don't satisfy some condition: <image> Submitted Solution: ``` print((int(input()) % 2) ^ 1) ```
instruction
0
30,176
13
60,352
No
output
1
30,176
13
60,353
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A tournament is a directed graph without self-loops in which every pair of vertexes is connected by exactly one directed edge. That is, for any two vertexes u and v (u ≠ v) exists either an edge going from u to v, or an edge from v to u. You are given a tournament consisting of n vertexes. Your task is to find there a cycle of length three. Input The first line contains an integer n (1 ≤ n ≤ 5000). Next n lines contain the adjacency matrix A of the graph (without spaces). Ai, j = 1 if the graph has an edge going from vertex i to vertex j, otherwise Ai, j = 0. Ai, j stands for the j-th character in the i-th line. It is guaranteed that the given graph is a tournament, that is, Ai, i = 0, Ai, j ≠ Aj, i (1 ≤ i, j ≤ n, i ≠ j). Output Print three distinct vertexes of the graph a1, a2, a3 (1 ≤ ai ≤ n), such that Aa1, a2 = Aa2, a3 = Aa3, a1 = 1, or "-1", if a cycle whose length equals three does not exist. If there are several solutions, print any of them. Examples Input 5 00100 10000 01001 11101 11000 Output 1 3 2 Input 5 01111 00000 01000 01100 01110 Output -1 Submitted Solution: ``` from sys import stdin,stdout,setrecursionlimit import threading setrecursionlimit(10**9) threading.stack_size(10**8) class DFS_General: def __init__(self,edges,n): self.n= n self.pi = [-1 for _ in range(0,n)] self.visit = [False for _ in range(0,n)] self.Ady= edges self.d = [-1 for _ in range(0,n)] self.low = [-1 for _ in range(0,n)] self.compo = [-1 for _ in range(0,n )] self.count = 0 self.time = 0 self.bridges = [] def DFS_visit_AP(self, u): self.visit[u] = True self.time += 1 self.d[u] = self.time self.low[u] = self.d[u] for v in self.Ady[u]: if not self.visit[v]: self.pi[v]= u self.DFS_visit_AP(v) self.low[u]= min(self.low[v], self.low[u]) elif self.visit[v] and self.pi[u] != v: self.low[u]= min(self.low[u], self.d[v]) self.compo[u]= self.count if self.pi[u] != -1 and self.low[u]== self.d[u]: self.bridges.append((self.pi[u], u)) def DFS_AP(self): for i in range(0,self.n): if not self.visit[i]: self.count += 1 self.DFS_visit_AP(i) def BFS(s,Ady,n): d = [0 for _ in range(0,n)] color = [-1 for _ in range(0,n)] queue = [] queue.append(s) d[s]= 0 color[s] = 0 while queue: u = queue.pop(0) for v in Ady[u]: if color[v] == -1: color[v]= 0 d[v] = d[u] + 1 queue.append(v) return d def Solution(): n_m = stdin.readline().split() n = int(n_m[0]) m = int(n_m[1]) Ady = [[] for i in range (0,n)] for i in range(m): stri= stdin.readline().split() item = (int(stri[0])-1,int(stri[1])-1) Ady[item[0]].append(item[1]) Ady[item[1]].append(item[0]) DFS_ = DFS_General(Ady,n) DFS_.DFS_AP() if len(DFS_.bridges) > 0: for i in DFS_.bridges: Ady[i[0]].remove(i[1]) Ady[i[1]].remove(i[0]) DFS_final = DFS_General(Ady,n) DFS_final.DFS_AP() Ady = [[] for _ in range(0,DFS_final.count)] for i in DFS_.bridges: Ady[DFS_final.compo[i[0]]-1].append(DFS_final.compo[i[1]] -1) Ady[DFS_final.compo[i[1]] -1].append(DFS_final.compo[i[0]] -1) d = BFS(0,Ady,len(Ady)) i = d.index(max(d)) d = BFS(i,Ady,len(Ady)) ma = max(d) stdout.write(str(ma)) else: stdout.write(str( 0)) threading.Thread(target=Solution).start() ```
instruction
0
31,043
13
62,086
No
output
1
31,043
13
62,087
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A tournament is a directed graph without self-loops in which every pair of vertexes is connected by exactly one directed edge. That is, for any two vertexes u and v (u ≠ v) exists either an edge going from u to v, or an edge from v to u. You are given a tournament consisting of n vertexes. Your task is to find there a cycle of length three. Input The first line contains an integer n (1 ≤ n ≤ 5000). Next n lines contain the adjacency matrix A of the graph (without spaces). Ai, j = 1 if the graph has an edge going from vertex i to vertex j, otherwise Ai, j = 0. Ai, j stands for the j-th character in the i-th line. It is guaranteed that the given graph is a tournament, that is, Ai, i = 0, Ai, j ≠ Aj, i (1 ≤ i, j ≤ n, i ≠ j). Output Print three distinct vertexes of the graph a1, a2, a3 (1 ≤ ai ≤ n), such that Aa1, a2 = Aa2, a3 = Aa3, a1 = 1, or "-1", if a cycle whose length equals three does not exist. If there are several solutions, print any of them. Examples Input 5 00100 10000 01001 11101 11000 Output 1 3 2 Input 5 01111 00000 01000 01100 01110 Output -1 Submitted Solution: ``` from sys import stdin, stdout from collections import defaultdict input = stdin.readline import gc, os from os import _exit gc.disable() n = int(input()) mat = [input() for _ in range(n)] graph = [set() for _ in range(n)] rev_graph = [set() for _ in range(n)] for i in range(n): for j in range(n): if mat[i][j]=='1': graph[i].add(j) rev_graph[j].add(i) checked = defaultdict() checkset= set() try: for i in range(n): rset = rev_graph[i] - checkset fset = graph[i]- checkset checked[i]=1 checkset.add(i) if len(rset)<len(fset): rset,fset = fset, rset if rset: for j in fset: ans_set = rset & graph[j] if ans_set: print(i+1, j+1, ans_set.pop()+1) raise ValueError print(-1) finally: exit() ```
instruction
0
31,044
13
62,088
No
output
1
31,044
13
62,089
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A tournament is a directed graph without self-loops in which every pair of vertexes is connected by exactly one directed edge. That is, for any two vertexes u and v (u ≠ v) exists either an edge going from u to v, or an edge from v to u. You are given a tournament consisting of n vertexes. Your task is to find there a cycle of length three. Input The first line contains an integer n (1 ≤ n ≤ 5000). Next n lines contain the adjacency matrix A of the graph (without spaces). Ai, j = 1 if the graph has an edge going from vertex i to vertex j, otherwise Ai, j = 0. Ai, j stands for the j-th character in the i-th line. It is guaranteed that the given graph is a tournament, that is, Ai, i = 0, Ai, j ≠ Aj, i (1 ≤ i, j ≤ n, i ≠ j). Output Print three distinct vertexes of the graph a1, a2, a3 (1 ≤ ai ≤ n), such that Aa1, a2 = Aa2, a3 = Aa3, a1 = 1, or "-1", if a cycle whose length equals three does not exist. If there are several solutions, print any of them. Examples Input 5 00100 10000 01001 11101 11000 Output 1 3 2 Input 5 01111 00000 01000 01100 01110 Output -1 Submitted Solution: ``` """def sefr_list(dic, matrix): #peida krdne unayi ke 0 hastn hmshun va negah dashtnshun tu ye list totally_sefr_list = [] for i in range(0, matrix): counter=0 for j in range(0, matrix): if dic[i][j]==0: counter+=1 if counter==matrix: totally_sefr_list.append(i) return totally_sefr_list def sefr_kon(totally_sefr_list, dic, matrix): #khane hayi ke 0 budand ro shomareye khane ash ra sefr konim dar baqie satr ha (az 1 be 0) # not ye chizi mige ke tush khalie ya na - age khali bashe=> if not a: print("List is empty") #age list khali nabashe inkaro kon: shomare_satre_khali = totally_sefr_list[0] del totally_sefr_list[0] while(True): for i in range(0, matrix): dic[i][shomare_satre_khali] = 0 try: shomare_satre_khali = totally_sefr_list[0] del totally_sefr_list[0] except: break return dic """ dic = {} matrix = int(input()) #vase menhaye yek print krdn sharte_menhaye_yek = [] for i in range(0,matrix): dic[i] = [] sharte_menhaye_yek.append(i) satr = str(input()) for j in range(0, matrix): dic[i].append(int(satr[j])) #print(dic) """ # func1 totally_sefr_list = sefr_list(dic, matrix) #print (totally_sefr_list) #func2 while(not (not totally_sefr_list)): dic = sefr_kon(totally_sefr_list, dic, matrix) #print(dic) totally_sefr_list = sefr_list(dic, matrix) if totally_sefr_list == sharte_menhaye_yek: print(-1) break #print(dic) """ # yek ha kuduman yeks_list = [] for i in range(0,matrix): for j in range(0, matrix): if dic[i][j] == 0: pass else: #print(i,j) yeks_list.append(i) yeks_list.append(j) #print(yeks_list) found = False length = len(yeks_list) counter = 0 while(counter<length): if found == True: break #a1 a1 = yeks_list[counter] #print("a1:",a1) #a2 a2 = yeks_list[counter+1] #print("a2:",a2) counter2 = 1 while(counter2<length): if found == True: break #a4 a4 = yeks_list[counter2] #print("a4:",a4) if a4 == a1: #print("both a1 and a4 are equal:",a1,a4) #a3 --- pay attention to the "counter-1" a3 = yeks_list[counter2-1] #print("a3:",a3) counter3 = 0 while(counter3<length): if found == True: break #do a3 and a2 have a relation??? x = yeks_list[counter3] #print("x:",x) if x == a2: y = yeks_list[counter3+1] #print("y:",y) if y==a3: print(a1+1,a2+1,a3+1) found = True break else: print(-1) found = True break x = yeks_list[counter3+1] if x == a3: y = yeks_list[counter3] if y==a2: print(a1+1,a2+1,a3+1) found = True break else: print(-1) found = True break counter3+=2 elif(counter2==length and counter==length): print(-1) found = True counter2+=2 counter+=2 #print(-1) """ """ ```
instruction
0
31,045
13
62,090
No
output
1
31,045
13
62,091
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A tournament is a directed graph without self-loops in which every pair of vertexes is connected by exactly one directed edge. That is, for any two vertexes u and v (u ≠ v) exists either an edge going from u to v, or an edge from v to u. You are given a tournament consisting of n vertexes. Your task is to find there a cycle of length three. Input The first line contains an integer n (1 ≤ n ≤ 5000). Next n lines contain the adjacency matrix A of the graph (without spaces). Ai, j = 1 if the graph has an edge going from vertex i to vertex j, otherwise Ai, j = 0. Ai, j stands for the j-th character in the i-th line. It is guaranteed that the given graph is a tournament, that is, Ai, i = 0, Ai, j ≠ Aj, i (1 ≤ i, j ≤ n, i ≠ j). Output Print three distinct vertexes of the graph a1, a2, a3 (1 ≤ ai ≤ n), such that Aa1, a2 = Aa2, a3 = Aa3, a1 = 1, or "-1", if a cycle whose length equals three does not exist. If there are several solutions, print any of them. Examples Input 5 00100 10000 01001 11101 11000 Output 1 3 2 Input 5 01111 00000 01000 01100 01110 Output -1 Submitted Solution: ``` from collections import defaultdict from sys import stdin, stdout input = stdin.readline import gc, os from os import _exit gc.disable() def put(): return map(int, input().split()) def check(tmp): a,b,c = tmp[0], tmp[1], tmp[2] x = 3 while x<len(tmp): if mat[c][a]=='1': break else: b,c = c,tmp[x] x+=1 return [a+1,b+1,c+1] def dfs(i): vis[i]=1 done[i]=1 element.append(i) for j in graph[i]: if vis[j]!=0: ind = element.index(j) tmp = element[ind:].copy() ans.extend(check(tmp)) raise ValueError elif j not in done: dfs(j) vis[i]=0 element.pop() n = int(input()) mat = [input() for _ in range(n)] graph = [[] for _ in range(n)] for i in range(n): for j in range(i+1,n): if mat[i][j]=='1': graph[i].append(j) else : graph[j].append(i) done = defaultdict() vis = [0]*n element = [] ans = [] try: for i in range(n): if i not in done: dfs(i) print(-1) except: print(*ans) finally: _exit(0) ```
instruction
0
31,046
13
62,092
No
output
1
31,046
13
62,093
Provide tags and a correct Python 3 solution for this coding contest problem. Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet — it calculates the maximum flow in an undirected graph. The graph consists of n vertices and m edges. Vertices are numbered from 1 to n. Vertices 1 and n being the source and the sink respectively. However, his max-flow algorithm seems to have a little flaw — it only finds the flow volume for each edge, but not its direction. Help him find for each edge the direction of the flow through this edges. Note, that the resulting flow should be correct maximum flow. More formally. You are given an undirected graph. For each it's undirected edge (ai, bi) you are given the flow volume ci. You should direct all edges in such way that the following conditions hold: 1. for each vertex v (1 < v < n), sum of ci of incoming edges is equal to the sum of ci of outcoming edges; 2. vertex with number 1 has no incoming edges; 3. the obtained directed graph does not have cycles. Input The first line of input contains two space-separated integers n and m (2 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105), the number of vertices and edges in the graph. The following m lines contain three space-separated integers ai, bi and ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 104), which means that there is an undirected edge from ai to bi with flow volume ci. It is guaranteed that there are no two edges connecting the same vertices; the given graph is connected; a solution always exists. Output Output m lines, each containing one integer di, which should be 0 if the direction of the i-th edge is ai → bi (the flow goes from vertex ai to vertex bi) and should be 1 otherwise. The edges are numbered from 1 to m in the order they are given in the input. If there are several solutions you can print any of them. Examples Input 3 3 3 2 10 1 2 10 3 1 5 Output 1 0 1 Input 4 5 1 2 10 1 3 10 2 3 5 4 2 15 3 4 5 Output 0 0 1 1 0 Note In the first test case, 10 flow units pass through path <image>, and 5 flow units pass directly from source to sink: <image>.
instruction
0
31,273
13
62,546
Tags: constructive algorithms, flows, graphs, greedy Correct Solution: ``` import sys from collections import deque DST_VERTEX = 0 EDGE_CAP = 1 EDGE_ID = 2 EDGE_DIR = 3 def bfs(flow, graph, n, m): dirs = [-1 for _ in range(m)] q = deque() q.append(0) q_size = 1 while q_size > 0: cur_node = q.popleft() q_size -= 1 for i in range(len(graph[cur_node])): cur_id = graph[cur_node][i][EDGE_ID] if dirs[cur_id] == -1: #direction not assigned -> try to assign dirs[cur_id] = graph[cur_node][i][EDGE_DIR] cur_dst = graph[cur_node][i][DST_VERTEX] flow[cur_dst] -= graph[cur_node][i][EDGE_CAP] if cur_dst != n - 1 and flow[cur_dst] == 0: q.append(cur_dst) q_size += 1 return dirs def main(): n, m = sys.stdin.readline().strip().split() n = int(n) m = int(m) flow = [0 for _ in range(n)] graph = [[] for _ in range(n)] for j in range(m): src, dst, cap = [int(i) for i in sys.stdin.readline().strip().split()] src -= 1 dst -= 1 graph[src].append((dst, cap, j, 0)) graph[dst].append((src, cap, j, 1)) flow[src] += cap flow[dst] += cap for i in range(n): flow[i] //= 2 dirs = bfs(flow, graph, n, m) for direction in dirs: print(direction) if __name__ == '__main__': main() ```
output
1
31,273
13
62,547
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet — it calculates the maximum flow in an undirected graph. The graph consists of n vertices and m edges. Vertices are numbered from 1 to n. Vertices 1 and n being the source and the sink respectively. However, his max-flow algorithm seems to have a little flaw — it only finds the flow volume for each edge, but not its direction. Help him find for each edge the direction of the flow through this edges. Note, that the resulting flow should be correct maximum flow. More formally. You are given an undirected graph. For each it's undirected edge (ai, bi) you are given the flow volume ci. You should direct all edges in such way that the following conditions hold: 1. for each vertex v (1 < v < n), sum of ci of incoming edges is equal to the sum of ci of outcoming edges; 2. vertex with number 1 has no incoming edges; 3. the obtained directed graph does not have cycles. Input The first line of input contains two space-separated integers n and m (2 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105), the number of vertices and edges in the graph. The following m lines contain three space-separated integers ai, bi and ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 104), which means that there is an undirected edge from ai to bi with flow volume ci. It is guaranteed that there are no two edges connecting the same vertices; the given graph is connected; a solution always exists. Output Output m lines, each containing one integer di, which should be 0 if the direction of the i-th edge is ai → bi (the flow goes from vertex ai to vertex bi) and should be 1 otherwise. The edges are numbered from 1 to m in the order they are given in the input. If there are several solutions you can print any of them. Examples Input 3 3 3 2 10 1 2 10 3 1 5 Output 1 0 1 Input 4 5 1 2 10 1 3 10 2 3 5 4 2 15 3 4 5 Output 0 0 1 1 0 Note In the first test case, 10 flow units pass through path <image>, and 5 flow units pass directly from source to sink: <image>. Submitted Solution: ``` #https://codeforces.com/contest/269/problem/C #TLE import sys input=sys.stdin.readline from collections import deque n, m = map(int, input().split()) edge = [list(map(int, input().split())) for _ in range(m)] d = {k: 0 for k in range(1, n+1)} s = {k: 0 for k in range(1, n+1)} g = {} used = [0] * m for i, [u, v, c] in enumerate(edge): if u not in g: g[u] = [] if v not in g: g[v] = [] #edge_index to cap g[u].append([i, v, c]) g[v].append([i, u, c]) s[u] += c s[v] += c S=deque([1]) i=0 direct=['0']*m while S: cur=S.popleft() for idx, u, c in g[cur]: if used[idx]==1: continue used[idx]=1 if edge[idx][:2] != [cur, u]: direct[idx]='1' d[u] += c if 2*d[u]==s[u]: S.append(u) i+=1 sys.stdout.write('\n'.join(direct)) ```
instruction
0
31,274
13
62,548
No
output
1
31,274
13
62,549
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). Input The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. Output Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. Examples Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 Note The tree from the first sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): <image> On seond step we color all vertices in the subtree of vertex 5 into color 1: <image> On third step we color all vertices in the subtree of vertex 2 into color 1: <image> The tree from the second sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): <image> On second step we color all vertices in the subtree of vertex 3 into color 1: <image> On third step we color all vertices in the subtree of vertex 6 into color 2: <image> On fourth step we color all vertices in the subtree of vertex 4 into color 1: <image> On fith step we color all vertices in the subtree of vertex 7 into color 3: <image>
instruction
0
31,500
13
63,000
Tags: dfs and similar, dsu, greedy Correct Solution: ``` ii=lambda:int(input()) kk=lambda:map(int,input().split()) ll=lambda:list(kk()) n=ii() parents = [-1]*n edges = {i:[] for i in range(n)} ls = kk() for i in range(1, n): o = ls.__next__()-1 edges[i].append(o) edges[o].append(i) parents[0]=0 rcol = ll() lowest = [0] cnt=1 while lowest: l2 = [] for l in lowest: if rcol[parents[l]]!=rcol[l]: cnt+=1 for child in edges[l]: if child == parents[l]: continue parents[child] = l l2.append(child) lowest=l2 print(cnt) ```
output
1
31,500
13
63,001
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). Input The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. Output Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. Examples Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 Note The tree from the first sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): <image> On seond step we color all vertices in the subtree of vertex 5 into color 1: <image> On third step we color all vertices in the subtree of vertex 2 into color 1: <image> The tree from the second sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): <image> On second step we color all vertices in the subtree of vertex 3 into color 1: <image> On third step we color all vertices in the subtree of vertex 6 into color 2: <image> On fourth step we color all vertices in the subtree of vertex 4 into color 1: <image> On fith step we color all vertices in the subtree of vertex 7 into color 3: <image>
instruction
0
31,501
13
63,002
Tags: dfs and similar, dsu, greedy Correct Solution: ``` I=lambda:map(int,input().split()) n,=I() a,b,s,q=[1]+[*I()],[*I()],0,{} for i in range(1,n+1):q[i]=0 for i in range(n): if q[a[i]]!=b[i]:s+=1 q[i+1]=b[i] print(s) ```
output
1
31,501
13
63,003
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). Input The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. Output Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. Examples Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 Note The tree from the first sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): <image> On seond step we color all vertices in the subtree of vertex 5 into color 1: <image> On third step we color all vertices in the subtree of vertex 2 into color 1: <image> The tree from the second sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): <image> On second step we color all vertices in the subtree of vertex 3 into color 1: <image> On third step we color all vertices in the subtree of vertex 6 into color 2: <image> On fourth step we color all vertices in the subtree of vertex 4 into color 1: <image> On fith step we color all vertices in the subtree of vertex 7 into color 3: <image>
instruction
0
31,502
13
63,004
Tags: dfs and similar, dsu, greedy Correct Solution: ``` n = int(input()) p = [int(n) - 1 for n in input().split()] c = [int(n) for n in input().split()] ans = 1 for i in range(1, n): if c[p[i - 1]] != c[i]: ans += 1 print(ans) ```
output
1
31,502
13
63,005
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). Input The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. Output Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. Examples Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 Note The tree from the first sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): <image> On seond step we color all vertices in the subtree of vertex 5 into color 1: <image> On third step we color all vertices in the subtree of vertex 2 into color 1: <image> The tree from the second sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): <image> On second step we color all vertices in the subtree of vertex 3 into color 1: <image> On third step we color all vertices in the subtree of vertex 6 into color 2: <image> On fourth step we color all vertices in the subtree of vertex 4 into color 1: <image> On fith step we color all vertices in the subtree of vertex 7 into color 3: <image>
instruction
0
31,503
13
63,006
Tags: dfs and similar, dsu, greedy Correct Solution: ``` n=int(input()) m=[] p=list(map(int,input().split())) c=[0]+list(map(int,input().split())) m=[1]+[list() for i in range(n)] for i in range(n-1): m[p[i]].append(i+2) stack=[[1,c[1]]] k=1 while stack: x,col=stack.pop(0) for i in m[x]: if c[i]!=col: k+=1 stack.append([i,c[i]]) print(k) ```
output
1
31,503
13
63,007
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). Input The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. Output Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. Examples Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 Note The tree from the first sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): <image> On seond step we color all vertices in the subtree of vertex 5 into color 1: <image> On third step we color all vertices in the subtree of vertex 2 into color 1: <image> The tree from the second sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): <image> On second step we color all vertices in the subtree of vertex 3 into color 1: <image> On third step we color all vertices in the subtree of vertex 6 into color 2: <image> On fourth step we color all vertices in the subtree of vertex 4 into color 1: <image> On fith step we color all vertices in the subtree of vertex 7 into color 3: <image>
instruction
0
31,504
13
63,008
Tags: dfs and similar, dsu, greedy Correct Solution: ``` read = lambda: list(map(int, input().split())); n = input(); graphList = read(); colorList = read(); def minSteps(): steps = 0; for i in range(0, int(n) - 1): child = i + 1; parent = graphList[i] - 1; if colorList[child] != colorList[parent]: steps += 1; return steps; print(minSteps() + 1); ```
output
1
31,504
13
63,009
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). Input The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. Output Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. Examples Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 Note The tree from the first sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): <image> On seond step we color all vertices in the subtree of vertex 5 into color 1: <image> On third step we color all vertices in the subtree of vertex 2 into color 1: <image> The tree from the second sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): <image> On second step we color all vertices in the subtree of vertex 3 into color 1: <image> On third step we color all vertices in the subtree of vertex 6 into color 2: <image> On fourth step we color all vertices in the subtree of vertex 4 into color 1: <image> On fith step we color all vertices in the subtree of vertex 7 into color 3: <image>
instruction
0
31,505
13
63,010
Tags: dfs and similar, dsu, greedy Correct Solution: ``` # # Yet I'm feeling like # There is no better place than right by your side # I had a little taste # And I'll only spoil the party anyway # 'Cause all the girls are looking fine # But you're the only one on my mind import sys # import re # inf = float("inf") sys.setrecursionlimit(100000000) # abc='abcdefghijklmnopqrstuvwxyz' # abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} mod,MOD=1000000007,998244353 # vow=['a','e','i','o','u'] # dx,dy=[-1,1,0,0],[0,0,1,-1] from collections import deque, Counter, OrderedDict,defaultdict # from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace # from math import ceil,floor,log,sqrt,factorial,pow,pi,gcd,log10,atan,tan # from bisect import bisect_left,bisect_right # import numpy as np def get_array(): return list(map(int , sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() count=0 def dfs(root,curr): global count if root==1: count+=1 curr=color[root] visited[root]=True for child in mydict[root]: if not visited[child]: if color[child]!=color[root]: count+=1 visited[child]=True dfs(child,color[child]) n=int(input()) mydict=defaultdict(list) Arr=get_array() parent=dict() myset=set() for i in range(n-1): mydict[Arr[i]].append(i+2) parent[i+2]=Arr[i] visited=[False]*(n+1) color = [0] + get_array() for i in color[1:]: myset.add(i) if len(myset)==n: print(n) exit() root=1;curr=0 dfs(root,curr) print(count) ```
output
1
31,505
13
63,011
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). Input The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. Output Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. Examples Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 Note The tree from the first sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): <image> On seond step we color all vertices in the subtree of vertex 5 into color 1: <image> On third step we color all vertices in the subtree of vertex 2 into color 1: <image> The tree from the second sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): <image> On second step we color all vertices in the subtree of vertex 3 into color 1: <image> On third step we color all vertices in the subtree of vertex 6 into color 2: <image> On fourth step we color all vertices in the subtree of vertex 4 into color 1: <image> On fith step we color all vertices in the subtree of vertex 7 into color 3: <image>
instruction
0
31,506
13
63,012
Tags: dfs and similar, dsu, greedy Correct Solution: ``` def o():return list(map(int,input().split())) [n],[p,c]=o(),[o()for x in range(2)] print(sum([1 if i==0 or c[p[i-1]-1]!=c[i]else 0 for i in range(n)])) ```
output
1
31,506
13
63,013
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). Input The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. Output Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. Examples Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 Note The tree from the first sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): <image> On seond step we color all vertices in the subtree of vertex 5 into color 1: <image> On third step we color all vertices in the subtree of vertex 2 into color 1: <image> The tree from the second sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): <image> On second step we color all vertices in the subtree of vertex 3 into color 1: <image> On third step we color all vertices in the subtree of vertex 6 into color 2: <image> On fourth step we color all vertices in the subtree of vertex 4 into color 1: <image> On fith step we color all vertices in the subtree of vertex 7 into color 3: <image>
instruction
0
31,507
13
63,014
Tags: dfs and similar, dsu, greedy Correct Solution: ``` import sys sys.setrecursionlimit(1000000) class Edge: def init(self, u, v, next): self.u = u self.v = v self.next = next MAXN = int(1e5+10) vis = [0]*MAXN edge = [None]*MAXN*2 head = [-1]*MAXN cnt = [0] color = [0]*MAXN ans = [1] def addedge(u, v): edge[cnt[0]] = Edge() edge[cnt[0]].init(u, v, head[u]) head[u] = cnt[0] # print(cnt[0], edge[cnt[0]].next, edge[cnt[0]].u, edge[cnt[0]].v) cnt[0]+=1 def dfs(u): if(vis[u] == 1): return vis[u] = 1 i = head[u] while(i!=-1): e = edge[i] v = e.v # if(i != e.next): # print(i, e.next, edge[i], e.u, e.v) if(vis[v] == 0): if(color[u] != colorneed[v]): ans[0]+=1 color[v] = colorneed[v] dfs(v) i = e.next n = int(input()) p = list(map(int, input().split())) for i in range(n-1): addedge(i+1, p[i]-1) addedge(p[i]-1, i+1) colorneed = list(map(int, input().split())) color[0] = colorneed[0] # for i in range(cnt[0]): # print(i, edge[i]) dfs(0) print(ans[0]) ```
output
1
31,507
13
63,015
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). Input The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. Output Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. Examples Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 Note The tree from the first sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): <image> On seond step we color all vertices in the subtree of vertex 5 into color 1: <image> On third step we color all vertices in the subtree of vertex 2 into color 1: <image> The tree from the second sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): <image> On second step we color all vertices in the subtree of vertex 3 into color 1: <image> On third step we color all vertices in the subtree of vertex 6 into color 2: <image> On fourth step we color all vertices in the subtree of vertex 4 into color 1: <image> On fith step we color all vertices in the subtree of vertex 7 into color 3: <image> Submitted Solution: ``` n = int(input()) p = list(map(int, input().split())) c = list(map(int, input().split())) col=1 #root for i in range(2,n+1): if c[i-1]!=c[p[i-2]-1]: col+=1 print(col) ```
instruction
0
31,508
13
63,016
Yes
output
1
31,508
13
63,017
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). Input The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. Output Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. Examples Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 Note The tree from the first sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): <image> On seond step we color all vertices in the subtree of vertex 5 into color 1: <image> On third step we color all vertices in the subtree of vertex 2 into color 1: <image> The tree from the second sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): <image> On second step we color all vertices in the subtree of vertex 3 into color 1: <image> On third step we color all vertices in the subtree of vertex 6 into color 2: <image> On fourth step we color all vertices in the subtree of vertex 4 into color 1: <image> On fith step we color all vertices in the subtree of vertex 7 into color 3: <image> Submitted Solution: ``` import sys from collections import deque input=sys.stdin.readline n=int(input()) dict={} for i in range(n): dict[i]=[] l1=list(map(int,input().split())) for i in range(0,n-1): dict[i+1].append(l1[i]-1) dict[l1[i]-1].append(i+1) # print(dict) l2=list(map(int,input().split())) def bfs(start,n,visited,l2,color): count=0 # l2=[i+1] color[start]=l2[start] # print(color) q=[start] q=deque(q) count+=1 visited[start]=1 while (len(q)!=0): y=dict[q[0]] for j in y: if(visited[j]==0): # print(j," ",color," ",count) if(color[q[0]]!=l2[j]): count+=1 color[j]=l2[j] else: color[j]=l2[j] visited[j]=1 # l2.append(j) q.append(j) # count+=1 q.popleft() print(count) visited=[0]*n color=[0]*n bfs(0,n,visited,l2,color) ```
instruction
0
31,509
13
63,018
Yes
output
1
31,509
13
63,019
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). Input The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. Output Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. Examples Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 Note The tree from the first sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): <image> On seond step we color all vertices in the subtree of vertex 5 into color 1: <image> On third step we color all vertices in the subtree of vertex 2 into color 1: <image> The tree from the second sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): <image> On second step we color all vertices in the subtree of vertex 3 into color 1: <image> On third step we color all vertices in the subtree of vertex 6 into color 2: <image> On fourth step we color all vertices in the subtree of vertex 4 into color 1: <image> On fith step we color all vertices in the subtree of vertex 7 into color 3: <image> Submitted Solution: ``` def pst(root, curr): global times if cs[root-1] != curr: times += 1 curr = cs[root-1] if root not in vss: return chn = vss[root] for ch in chn: pst(ch, curr) n = int(input()) vs = [int(x) for x in input().split()] cs = [int(x) for x in input().split()] # vss = set() # for i in range(2, n + 1): # vss.add((vs[i - 2], i)) # # vsbp = {v[0]: v for v in vss} vss = {} for c, p in enumerate(vs): if not p in vss: vss[p] = [c+2] else: vss[p].append(c+2) times = 0 def flattened(): times = 0 root = 1 curr = 0 firstpart = True stack = [] while True: if firstpart: if cs[root-1] != curr: times += 1 curr = cs[root - 1] if root not in vss: firstpart = False if len(stack) == 0: print(times) return chni, curr = stack.pop() else: chn = vss[root] chni = iter(chn) try: ch = next(chni) firstpart = True root = ch stack.append((chni, curr)) except StopIteration: firstpart = False if len(stack) == 0: print(times) return chni, curr = stack.pop() # if root in vss: # chn = vss[root] # chni = iter(chn) # stack.append((curr, chni)) # else: # if len(stack) == 0: # print(times) # return # pst(1, 0) # print(times) flattened() ```
instruction
0
31,510
13
63,020
Yes
output
1
31,510
13
63,021
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). Input The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. Output Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. Examples Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 Note The tree from the first sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): <image> On seond step we color all vertices in the subtree of vertex 5 into color 1: <image> On third step we color all vertices in the subtree of vertex 2 into color 1: <image> The tree from the second sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): <image> On second step we color all vertices in the subtree of vertex 3 into color 1: <image> On third step we color all vertices in the subtree of vertex 6 into color 2: <image> On fourth step we color all vertices in the subtree of vertex 4 into color 1: <image> On fith step we color all vertices in the subtree of vertex 7 into color 3: <image> Submitted Solution: ``` n=int(input()) d={1:[]} c=1 for i in input().split(): c+=1 d[c]=[] d[int(i)]+=[c] z=list(map(int,input().split())) c=list(range(1,n+1)) pr=list(1 for i in range(n)) o=0 while c: for j in d[c[0]]: if z[j-1]==z[c[0]-1]: pr[j-1]=0 o+=pr[c[0]-1] c.pop(0) print(o) ```
instruction
0
31,511
13
63,022
Yes
output
1
31,511
13
63,023
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). Input The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. Output Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. Examples Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 Note The tree from the first sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): <image> On seond step we color all vertices in the subtree of vertex 5 into color 1: <image> On third step we color all vertices in the subtree of vertex 2 into color 1: <image> The tree from the second sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): <image> On second step we color all vertices in the subtree of vertex 3 into color 1: <image> On third step we color all vertices in the subtree of vertex 6 into color 2: <image> On fourth step we color all vertices in the subtree of vertex 4 into color 1: <image> On fith step we color all vertices in the subtree of vertex 7 into color 3: <image> Submitted Solution: ``` from collections import * import bisect import heapq def ri(): return int(input()) def rl(): return list(map(int, input().split())) def bfs(): visited = [False] * n visited[0] = True result = 1 to_visit = deque() to_visit.append(0) while to_visit: node = to_visit.popleft() for child in graph[node]: if not visited[child]: if colors[child] != colors[node]: result += 1 visited[child] = True to_visit.append(child) return result n = ri() edges = rl() colors = rl() print(colors) graph = [[] for i in range(n)] for i in range(n- 1): graph[i + 1].append(edges[i] - 1) graph[edges[i] - 1].append(i + 1) print(graph) ans = bfs() print(ans) ```
instruction
0
31,512
13
63,024
No
output
1
31,512
13
63,025
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). Input The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. Output Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. Examples Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 Note The tree from the first sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): <image> On seond step we color all vertices in the subtree of vertex 5 into color 1: <image> On third step we color all vertices in the subtree of vertex 2 into color 1: <image> The tree from the second sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): <image> On second step we color all vertices in the subtree of vertex 3 into color 1: <image> On third step we color all vertices in the subtree of vertex 6 into color 2: <image> On fourth step we color all vertices in the subtree of vertex 4 into color 1: <image> On fith step we color all vertices in the subtree of vertex 7 into color 3: <image> Submitted Solution: ``` n = int(input()) # number of vertices a = list(map(int,input().split())) # connection b = list(map(int,input().split())) # colour tree = [] for i in range(1,n + 1): tree.append([i]) for j in range(2,n + 1): tree[a[j - 2] - 1].append(j) s = 0 print (tree) for each in tree: if len(each) == 1: s += 1 tree.remove(each) for subset in tree: subset = list(map(lambda x: b[x - 1],subset)) colour = subset[0] s = s - subset.count(colour) + 2 print (s) ```
instruction
0
31,513
13
63,026
No
output
1
31,513
13
63,027
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). Input The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. Output Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. Examples Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 Note The tree from the first sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): <image> On seond step we color all vertices in the subtree of vertex 5 into color 1: <image> On third step we color all vertices in the subtree of vertex 2 into color 1: <image> The tree from the second sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): <image> On second step we color all vertices in the subtree of vertex 3 into color 1: <image> On third step we color all vertices in the subtree of vertex 6 into color 2: <image> On fourth step we color all vertices in the subtree of vertex 4 into color 1: <image> On fith step we color all vertices in the subtree of vertex 7 into color 3: <image> Submitted Solution: ``` n = int(input()) tree = list(map(int,input().split())) colors = list(map(int,input().split())) ans = 0 for i in range(1,n): if colors[i] != colors[tree[i-1]-1]: ans += 1 print(ans) ```
instruction
0
31,514
13
63,028
No
output
1
31,514
13
63,029
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). Input The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. Output Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. Examples Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 Note The tree from the first sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): <image> On seond step we color all vertices in the subtree of vertex 5 into color 1: <image> On third step we color all vertices in the subtree of vertex 2 into color 1: <image> The tree from the second sample is shown on the picture (numbers are vetices' indices): <image> On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): <image> On second step we color all vertices in the subtree of vertex 3 into color 1: <image> On third step we color all vertices in the subtree of vertex 6 into color 2: <image> On fourth step we color all vertices in the subtree of vertex 4 into color 1: <image> On fith step we color all vertices in the subtree of vertex 7 into color 3: <image> Submitted Solution: ``` n = int(input()) a = input().split() c = list(map(int,input().split())) ans = 0 prev = -1 for i in c: if prev == -1: ans += 1 prev = i continue if prev != i: ans += 1 prev = i print(ans + 1) ```
instruction
0
31,515
13
63,030
No
output
1
31,515
13
63,031
Provide a correct Python 3 solution for this coding contest problem. Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0. Now, the following Q operations will be performed: * Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j. Find the value of the counter on each vertex after all operations. Constraints * 2 \leq N \leq 2 \times 10^5 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq a_i < b_i \leq N * 1 \leq p_j \leq N * 1 \leq x_j \leq 10^4 * The given graph is a tree. * All values in input are integers. Input Input is given from Standard Input in the following format: N Q a_1 b_1 : a_{N-1} b_{N-1} p_1 x_1 : p_Q x_Q Output Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between. Examples Input 4 3 1 2 2 3 2 4 2 10 1 100 3 1 Output 100 110 111 110 Input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 Output 20 20 20 20 20 20
instruction
0
31,574
13
63,148
"Correct Solution: ``` (n,q),*t=[list(map(int,t.split()))for t in open(0)] s=[0] d=s*n f=s+[1]*n e=[[]for _ in d] for a,b in t: if~-n:n-=1;e[a-1]+=b-1,;e[b-1]+=a-1, else:d[a-1]+=b while s: v=s.pop() for w in e[v]:d[w]+=d[v]*f[w];s+=[w]*f[w];f[w]=0 print(*d) ```
output
1
31,574
13
63,149
Provide a correct Python 3 solution for this coding contest problem. Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0. Now, the following Q operations will be performed: * Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j. Find the value of the counter on each vertex after all operations. Constraints * 2 \leq N \leq 2 \times 10^5 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq a_i < b_i \leq N * 1 \leq p_j \leq N * 1 \leq x_j \leq 10^4 * The given graph is a tree. * All values in input are integers. Input Input is given from Standard Input in the following format: N Q a_1 b_1 : a_{N-1} b_{N-1} p_1 x_1 : p_Q x_Q Output Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between. Examples Input 4 3 1 2 2 3 2 4 2 10 1 100 3 1 Output 100 110 111 110 Input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 Output 20 20 20 20 20 20
instruction
0
31,575
13
63,150
"Correct Solution: ``` N, Q = map(int, input().split()) pNode = [1] * N c = [0] * N for i in range(N-1): a, b = map(int, input().split()) pNode[b-1] = a-1 for i in range(Q): p, x = map(int, input().split()) c[p-1] += x for i in range(1, N): c[i] += c[pNode[i]] print(*c) ```
output
1
31,575
13
63,151
Provide a correct Python 3 solution for this coding contest problem. Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0. Now, the following Q operations will be performed: * Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j. Find the value of the counter on each vertex after all operations. Constraints * 2 \leq N \leq 2 \times 10^5 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq a_i < b_i \leq N * 1 \leq p_j \leq N * 1 \leq x_j \leq 10^4 * The given graph is a tree. * All values in input are integers. Input Input is given from Standard Input in the following format: N Q a_1 b_1 : a_{N-1} b_{N-1} p_1 x_1 : p_Q x_Q Output Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between. Examples Input 4 3 1 2 2 3 2 4 2 10 1 100 3 1 Output 100 110 111 110 Input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 Output 20 20 20 20 20 20
instruction
0
31,576
13
63,152
"Correct Solution: ``` import sys sys.setrecursionlimit(10**9) f=lambda:map(int,sys.stdin.readline().split()) n,q=f() g=[[] for _ in range(n)] for i in range(n-1): a,b=f() g[a-1]+=[b-1] g[b-1]+=[a-1] c=[0]*n for i in range(q): v,x=f() c[v-1]+=x def dfs(v,p=-1): for i in g[v]: if i==p: continue c[i]+=c[v] dfs(i,v) dfs(0) print(*c) ```
output
1
31,576
13
63,153
Provide a correct Python 3 solution for this coding contest problem. Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0. Now, the following Q operations will be performed: * Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j. Find the value of the counter on each vertex after all operations. Constraints * 2 \leq N \leq 2 \times 10^5 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq a_i < b_i \leq N * 1 \leq p_j \leq N * 1 \leq x_j \leq 10^4 * The given graph is a tree. * All values in input are integers. Input Input is given from Standard Input in the following format: N Q a_1 b_1 : a_{N-1} b_{N-1} p_1 x_1 : p_Q x_Q Output Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between. Examples Input 4 3 1 2 2 3 2 4 2 10 1 100 3 1 Output 100 110 111 110 Input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 Output 20 20 20 20 20 20
instruction
0
31,577
13
63,154
"Correct Solution: ``` import sys sys.setrecursionlimit(10**6) def MI(): return map(int, input().split()) N,Q=MI() Edge=[[] for _ in range(N)] Point=[0]*N for i in range(N-1): a,b=MI() Edge[a-1].append(b-1) Edge[b-1].append(a-1) for i in range(Q): p,x=MI() Point[p-1]+=x def dfs(now,pre=-1): for nxt in Edge[now]: if nxt==pre: continue Point[nxt]+=Point[now] dfs(nxt,now) dfs(0) print(*Point) ```
output
1
31,577
13
63,155
Provide a correct Python 3 solution for this coding contest problem. Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0. Now, the following Q operations will be performed: * Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j. Find the value of the counter on each vertex after all operations. Constraints * 2 \leq N \leq 2 \times 10^5 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq a_i < b_i \leq N * 1 \leq p_j \leq N * 1 \leq x_j \leq 10^4 * The given graph is a tree. * All values in input are integers. Input Input is given from Standard Input in the following format: N Q a_1 b_1 : a_{N-1} b_{N-1} p_1 x_1 : p_Q x_Q Output Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between. Examples Input 4 3 1 2 2 3 2 4 2 10 1 100 3 1 Output 100 110 111 110 Input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 Output 20 20 20 20 20 20
instruction
0
31,578
13
63,156
"Correct Solution: ``` N,Q = map(int,input().split()) T = [0]*(N+1) V = [0]*(N+1) for _ in range(N-1): a,b = map(int,input().split()) T[b] = a for _ in range(Q): p,x = map(int,input().split()) V[p]+=x for i in range(1,N+1): V[i]+=V[T[i]] print(*V[1:]) ```
output
1
31,578
13
63,157
Provide a correct Python 3 solution for this coding contest problem. Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0. Now, the following Q operations will be performed: * Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j. Find the value of the counter on each vertex after all operations. Constraints * 2 \leq N \leq 2 \times 10^5 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq a_i < b_i \leq N * 1 \leq p_j \leq N * 1 \leq x_j \leq 10^4 * The given graph is a tree. * All values in input are integers. Input Input is given from Standard Input in the following format: N Q a_1 b_1 : a_{N-1} b_{N-1} p_1 x_1 : p_Q x_Q Output Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between. Examples Input 4 3 1 2 2 3 2 4 2 10 1 100 3 1 Output 100 110 111 110 Input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 Output 20 20 20 20 20 20
instruction
0
31,579
13
63,158
"Correct Solution: ``` N, Q = map(int, input().split()) parentNodes = [1] * (N + 1) # parentNodes[i] = i番目ノードの親番号 ans = [0] * (N + 1) for _ in range(N - 1): a, b = map(int, input().split()) # 接続ノード parentNodes[b] = a for _ in range(Q): p, x = map(int, input().split()) ans[p] += x for i in range(2, N + 1): ans[i] += ans[parentNodes[i]] ans.pop(0) print(" ".join(map(str, ans))) ```
output
1
31,579
13
63,159
Provide a correct Python 3 solution for this coding contest problem. Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0. Now, the following Q operations will be performed: * Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j. Find the value of the counter on each vertex after all operations. Constraints * 2 \leq N \leq 2 \times 10^5 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq a_i < b_i \leq N * 1 \leq p_j \leq N * 1 \leq x_j \leq 10^4 * The given graph is a tree. * All values in input are integers. Input Input is given from Standard Input in the following format: N Q a_1 b_1 : a_{N-1} b_{N-1} p_1 x_1 : p_Q x_Q Output Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between. Examples Input 4 3 1 2 2 3 2 4 2 10 1 100 3 1 Output 100 110 111 110 Input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 Output 20 20 20 20 20 20
instruction
0
31,580
13
63,160
"Correct Solution: ``` def o():return map(int,input().split()) n,q=o();a=[[]for i in range(n)];x=[0]*n;p=[[0,0]] for i in range(n-1):u,v=o();a[u-1]+=[v-1];a[v-1]+=[u-1] for i in range(q):u,v=o();x[u-1]+=v while p: r,s=p.pop() for i in a[r]: if i!=s: p+=[[i,r]] x[i]+=x[r] print(*x) ```
output
1
31,580
13
63,161
Provide a correct Python 3 solution for this coding contest problem. Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0. Now, the following Q operations will be performed: * Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j. Find the value of the counter on each vertex after all operations. Constraints * 2 \leq N \leq 2 \times 10^5 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq a_i < b_i \leq N * 1 \leq p_j \leq N * 1 \leq x_j \leq 10^4 * The given graph is a tree. * All values in input are integers. Input Input is given from Standard Input in the following format: N Q a_1 b_1 : a_{N-1} b_{N-1} p_1 x_1 : p_Q x_Q Output Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between. Examples Input 4 3 1 2 2 3 2 4 2 10 1 100 3 1 Output 100 110 111 110 Input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 Output 20 20 20 20 20 20
instruction
0
31,581
13
63,162
"Correct Solution: ``` N,Q = map(int,input().split()) G = [[] for n in range(N)] ans = N*[0] for n in range(N-1): a,b = map(int,input().split()) G[a-1].append(b-1) G[b-1].append(a-1) for q in range(Q): p,x = map(int,input().split()) ans[p-1]+=x f = N*[1] t = [0] while t: v = t.pop() f[v] = 0 for k in G[v]: if f[k]: ans[k]+=ans[v] t.append(k) print(*ans) ```
output
1
31,581
13
63,163
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0. Now, the following Q operations will be performed: * Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j. Find the value of the counter on each vertex after all operations. Constraints * 2 \leq N \leq 2 \times 10^5 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq a_i < b_i \leq N * 1 \leq p_j \leq N * 1 \leq x_j \leq 10^4 * The given graph is a tree. * All values in input are integers. Input Input is given from Standard Input in the following format: N Q a_1 b_1 : a_{N-1} b_{N-1} p_1 x_1 : p_Q x_Q Output Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between. Examples Input 4 3 1 2 2 3 2 4 2 10 1 100 3 1 Output 100 110 111 110 Input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 Output 20 20 20 20 20 20 Submitted Solution: ``` import sys sys.setrecursionlimit(10**6 + 1000) words = lambda t : list(map(t, input().split())) n,q = words(int) par = [0] * n for i in range(n-1): a,b = words(int) par[b-1] = a-1 points = [0] * n for i in range(q): p,x = words(int) points[p-1] += x ans = [0] * n ans[0] = points[0] for i in range(1,n): ans[i] = ans[par[i]] + points[i] print(" ".join(map(str,ans))) ```
instruction
0
31,582
13
63,164
Yes
output
1
31,582
13
63,165
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0. Now, the following Q operations will be performed: * Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j. Find the value of the counter on each vertex after all operations. Constraints * 2 \leq N \leq 2 \times 10^5 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq a_i < b_i \leq N * 1 \leq p_j \leq N * 1 \leq x_j \leq 10^4 * The given graph is a tree. * All values in input are integers. Input Input is given from Standard Input in the following format: N Q a_1 b_1 : a_{N-1} b_{N-1} p_1 x_1 : p_Q x_Q Output Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between. Examples Input 4 3 1 2 2 3 2 4 2 10 1 100 3 1 Output 100 110 111 110 Input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 Output 20 20 20 20 20 20 Submitted Solution: ``` N, Q = map(int, input().split()) tree = [[] for i in range(N+1)] counter = [0] * (N+1) for i in range(N-1): a, b = map(int, input().split()) tree[b].append(a) for i in range(Q): p, x = map(int, input().split()) counter[p] += x for i in range(1, N+1): for pn in tree[i]: counter[i] += counter[pn] print(' '.join(map(str, counter[1:]))) ```
instruction
0
31,583
13
63,166
Yes
output
1
31,583
13
63,167
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0. Now, the following Q operations will be performed: * Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j. Find the value of the counter on each vertex after all operations. Constraints * 2 \leq N \leq 2 \times 10^5 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq a_i < b_i \leq N * 1 \leq p_j \leq N * 1 \leq x_j \leq 10^4 * The given graph is a tree. * All values in input are integers. Input Input is given from Standard Input in the following format: N Q a_1 b_1 : a_{N-1} b_{N-1} p_1 x_1 : p_Q x_Q Output Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between. Examples Input 4 3 1 2 2 3 2 4 2 10 1 100 3 1 Output 100 110 111 110 Input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 Output 20 20 20 20 20 20 Submitted Solution: ``` N, Q = map(int, input().split()) nodes = [0 for _ in range(N + 1)] branches = [] for j in range(N - 1): parent, child = map(int, input().split()) branches.append([parent, child]) for _ in range(Q): parent, number = map(int, input().split()) nodes[parent] += number branches.sort() for branch in branches: parent = branch[0] child = branch[1] nodes[child] += nodes[parent] print(*nodes[1:], sep=' ') ```
instruction
0
31,584
13
63,168
Yes
output
1
31,584
13
63,169
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0. Now, the following Q operations will be performed: * Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j. Find the value of the counter on each vertex after all operations. Constraints * 2 \leq N \leq 2 \times 10^5 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq a_i < b_i \leq N * 1 \leq p_j \leq N * 1 \leq x_j \leq 10^4 * The given graph is a tree. * All values in input are integers. Input Input is given from Standard Input in the following format: N Q a_1 b_1 : a_{N-1} b_{N-1} p_1 x_1 : p_Q x_Q Output Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between. Examples Input 4 3 1 2 2 3 2 4 2 10 1 100 3 1 Output 100 110 111 110 Input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 Output 20 20 20 20 20 20 Submitted Solution: ``` n,q=map(int,input().split()) edge=[[] for i in range(n)] for i in range(n-1): a,b=map(int,input().split()) edge[a-1].append(b-1) base_score = [0]*n for i in range(q): p,x=map(int,input().split()) base_score[p-1] += x score=[0]*n for i,bs in enumerate(base_score): score[i] = bs for j in edge[i]: base_score[j] += bs print(*score) ```
instruction
0
31,585
13
63,170
Yes
output
1
31,585
13
63,171
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0. Now, the following Q operations will be performed: * Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j. Find the value of the counter on each vertex after all operations. Constraints * 2 \leq N \leq 2 \times 10^5 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq a_i < b_i \leq N * 1 \leq p_j \leq N * 1 \leq x_j \leq 10^4 * The given graph is a tree. * All values in input are integers. Input Input is given from Standard Input in the following format: N Q a_1 b_1 : a_{N-1} b_{N-1} p_1 x_1 : p_Q x_Q Output Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between. Examples Input 4 3 1 2 2 3 2 4 2 10 1 100 3 1 Output 100 110 111 110 Input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 Output 20 20 20 20 20 20 Submitted Solution: ``` def get_answer(ki): n = int(ki[0].split(" ")[0]) q = int(ki[0].split(" ")[1]) n_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[1:-q]]] q_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[-q:]]] tree = make_tree(n, n_data) score = [0] * n for qq in q_data: index = qq[0] add_score(tree, score, qq, n_data, index) score = [str(s) for s in score] s = " ".join(score) return s def add_score(tree, score, qq, n_data, index): score[index - 1] += qq[1] next_index = tree[index -1] for n_i in next_index: add_score(tree, score, qq, n_data, n_i) def make_tree(n, n_data): r = {} for i in range(n): r[i] = [] for n in n_data: k = n[0] - 1 if k in r.keys(): r[k].append(n[1]) else: r[k] = [n[1]] return r def make_index(n, tree): index = {} for i in range(n): r = [] add_index(tree, i, r) index[i] = r return index def add_index(tree, index, r): r.append(index) next_index = tree[index] for n_i in next_index: add_index(tree, n_i - 1, r) if __name__ == "__main__": input_ki = [] while True: try: input1 = input() if not input1: break; input_ki.append(input1) except EOFError: break print(get_answer(input_ki)) ```
instruction
0
31,586
13
63,172
No
output
1
31,586
13
63,173
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0. Now, the following Q operations will be performed: * Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j. Find the value of the counter on each vertex after all operations. Constraints * 2 \leq N \leq 2 \times 10^5 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq a_i < b_i \leq N * 1 \leq p_j \leq N * 1 \leq x_j \leq 10^4 * The given graph is a tree. * All values in input are integers. Input Input is given from Standard Input in the following format: N Q a_1 b_1 : a_{N-1} b_{N-1} p_1 x_1 : p_Q x_Q Output Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between. Examples Input 4 3 1 2 2 3 2 4 2 10 1 100 3 1 Output 100 110 111 110 Input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 Output 20 20 20 20 20 20 Submitted Solution: ``` import sys sys.setrecursionlimit(200010) N, Q = map(int,input().split()) G = [[] for k in range(N+1)] for k in range(N-1): a, b = map(int,input().split()) G[a].append(b) kyori = [0 for k in range(N+1)] for k in range(Q): p, x = map(int,input().split()) kyori[p] -= x def dfs(now, dist): for tsugi in G[now]: if kyori[tsugi] <= 0: kyori[tsugi] = -kyori[tsugi] kyori[tsugi] += dist dfs(tsugi, kyori[tsugi]) kyori[1] = -kyori[1] dfs(1,kyori[1]) print(*kyori[1:], sep = " ") ```
instruction
0
31,587
13
63,174
No
output
1
31,587
13
63,175
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0. Now, the following Q operations will be performed: * Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j. Find the value of the counter on each vertex after all operations. Constraints * 2 \leq N \leq 2 \times 10^5 * 1 \leq Q \leq 2 \times 10^5 * 1 \leq a_i < b_i \leq N * 1 \leq p_j \leq N * 1 \leq x_j \leq 10^4 * The given graph is a tree. * All values in input are integers. Input Input is given from Standard Input in the following format: N Q a_1 b_1 : a_{N-1} b_{N-1} p_1 x_1 : p_Q x_Q Output Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between. Examples Input 4 3 1 2 2 3 2 4 2 10 1 100 3 1 Output 100 110 111 110 Input 6 2 1 2 1 3 2 4 3 6 2 5 1 10 1 10 Output 20 20 20 20 20 20 Submitted Solution: ``` n, q = (int(_) for _ in input().split()) l = [list(map(int, input().split())) for i in range(n-1)] p = [list(map(int, input().split())) for i in range(q)] l = sorted(l,key = lambda x:(x[0] ,x[1])) res = [0] * n for i in p: res[i[0]-1] += i[1] for i in l: res[i[1]-1] += res[i[0]-1] [print(i,end=" ") for i in res] print() ```
instruction
0
31,588
13
63,176
No
output
1
31,588
13
63,177