message stringlengths 2 22.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 16 109k | cluster float64 1 1 | __index_level_0__ int64 32 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Berland has n cities, some of which are connected by roads. Each road is bidirectional, connects two distinct cities and for each two cities there's at most one road connecting them.
The president of Berland decided to split country into r states in such a way that each city will belong to exactly one of these r states.
After this split each road will connect either cities of the same state or cities of the different states. Let's call roads that connect two cities of the same state "inner" roads.
The president doesn't like odd people, odd cities and odd numbers, so he wants this split to be done in such a way that each city would have even number of "inner" roads connected to it.
Please help president to find smallest possible r for which such a split exists.
Input
The input contains one or several test cases. The first input line contains a single integer number t β number of test cases. Then, t test cases follow. Solve test cases separately, test cases are completely independent and do not affect each other.
Then t blocks of input data follow. Each block starts from empty line which separates it from the remaining input data. The second line of each block contains two space-separated integers n, m (1 β€ n β€ 2000, 0 β€ m β€ 10000) β the number of cities and number of roads in the Berland. Each of the next m lines contains two space-separated integers β x_i, y_i (1 β€ x_i, y_i β€ n; x_i β y_i), which denotes that the i-th road connects cities x_i and y_i. Each pair of cities are connected by at most one road.
Sum of values n across all test cases doesn't exceed 2000. Sum of values m across all test cases doesn't exceed 10000.
Output
For each test case first print a line containing a single integer r β smallest possible number of states for which required split is possible. In the next line print n space-separated integers in range from 1 to r, inclusive, where the j-th number denotes number of state for the j-th city. If there are multiple solutions, print any.
Example
Input
2
Β
5 3
1 2
2 5
1 5
Β
6 5
1 2
2 3
3 4
4 2
4 1
Output
1
1 1 1 1 1
2
2 1 1 1 1 1
Submitted Solution:
```
v=int(input())
for c in range(0,v):
g=input()
p=1
n,k=map(int, input().split())
r=1
w=0
q=0
sum=0
a=[1 for i in range(0,n)]
b = [[0 for i in range(0,n)] for j in range(0,n)]
for i in range(0,k):
w, q = map(int, input().split())
b[w-1][q-1]+=1
b[q-1][w-1]+=1
for i in range(0,n):
for j in range(0,n):
if b[i][j]==1:
sum+=1
if sum%2==1:
for t in range(0,n):
if b[i][t]==1:
b[i][t]=0
b[t][i]=0
r+=1
if r>p:
p=r
a[t]=r
sum-=1
break
sum=0
r=1
print(p)
for h in range(0,n):
print (a[i], end=" ")
``` | instruction | 0 | 78,306 | 1 | 156,612 |
No | output | 1 | 78,306 | 1 | 156,613 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha is a very happy guy, that's why he is always on the move. There are n cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 1 to n in increasing order. The distance between any two adjacent cities is equal to 1 kilometer. Since all roads in the country are directed, it's possible to reach the city y from the city x only if x < y.
Once Sasha decided to go on a trip around the country and to visit all n cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is v liters, and it spends exactly 1 liter of fuel for 1 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 1 and wants to get to the city with the number n. There is a gas station in each city. In the i-th city, the price of 1 liter of fuel is i dollars. It is obvious that at any moment of time, the tank can contain at most v liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers n and v (2 β€ n β€ 100, 1 β€ v β€ 100) β the number of cities in the country and the capacity of the tank.
Output
Print one integer β the minimum amount of money that is needed to finish the trip.
Examples
Input
4 2
Output
4
Input
7 6
Output
6
Note
In the first example, Sasha can buy 2 liters for 2 dollars (1 dollar per liter) in the first city, drive to the second city, spend 1 liter of fuel on it, then buy 1 liter for 2 dollars in the second city and then drive to the 4-th city. Therefore, the answer is 1+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities. | instruction | 0 | 78,323 | 1 | 156,646 |
Tags: dp, greedy, math
Correct Solution:
```
n,v=map(int,input().split())
if n<=v:
print(n-1)
else:
for i in range(2,n-v+1):
v+=i
print(v)
``` | output | 1 | 78,323 | 1 | 156,647 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha is a very happy guy, that's why he is always on the move. There are n cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 1 to n in increasing order. The distance between any two adjacent cities is equal to 1 kilometer. Since all roads in the country are directed, it's possible to reach the city y from the city x only if x < y.
Once Sasha decided to go on a trip around the country and to visit all n cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is v liters, and it spends exactly 1 liter of fuel for 1 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 1 and wants to get to the city with the number n. There is a gas station in each city. In the i-th city, the price of 1 liter of fuel is i dollars. It is obvious that at any moment of time, the tank can contain at most v liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers n and v (2 β€ n β€ 100, 1 β€ v β€ 100) β the number of cities in the country and the capacity of the tank.
Output
Print one integer β the minimum amount of money that is needed to finish the trip.
Examples
Input
4 2
Output
4
Input
7 6
Output
6
Note
In the first example, Sasha can buy 2 liters for 2 dollars (1 dollar per liter) in the first city, drive to the second city, spend 1 liter of fuel on it, then buy 1 liter for 2 dollars in the second city and then drive to the 4-th city. Therefore, the answer is 1+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities. | instruction | 0 | 78,324 | 1 | 156,648 |
Tags: dp, greedy, math
Correct Solution:
```
n, v = map(int, input().split())
if n-1 <= v:
print(n-1)
else:
res = v
for i in range(2, n-v+1):
res += i
print(res)
``` | output | 1 | 78,324 | 1 | 156,649 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha is a very happy guy, that's why he is always on the move. There are n cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 1 to n in increasing order. The distance between any two adjacent cities is equal to 1 kilometer. Since all roads in the country are directed, it's possible to reach the city y from the city x only if x < y.
Once Sasha decided to go on a trip around the country and to visit all n cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is v liters, and it spends exactly 1 liter of fuel for 1 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 1 and wants to get to the city with the number n. There is a gas station in each city. In the i-th city, the price of 1 liter of fuel is i dollars. It is obvious that at any moment of time, the tank can contain at most v liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers n and v (2 β€ n β€ 100, 1 β€ v β€ 100) β the number of cities in the country and the capacity of the tank.
Output
Print one integer β the minimum amount of money that is needed to finish the trip.
Examples
Input
4 2
Output
4
Input
7 6
Output
6
Note
In the first example, Sasha can buy 2 liters for 2 dollars (1 dollar per liter) in the first city, drive to the second city, spend 1 liter of fuel on it, then buy 1 liter for 2 dollars in the second city and then drive to the 4-th city. Therefore, the answer is 1+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities. | instruction | 0 | 78,325 | 1 | 156,650 |
Tags: dp, greedy, math
Correct Solution:
```
n,v = map(int,input().split())
# n-1 meghdar benziini ke mikhad mashin ta berese ! va bayad say konim ta oonja ke mishe zood be zood benzin bezanim
need_benzin = n-v-1
if v<n:
money = v
else :
money = n-1
#agar maslan 5 bashe rah va 3 bashe bak mishe = 3 +2
for i in range(0,need_benzin):
money+=i+2
print(money)
``` | output | 1 | 78,325 | 1 | 156,651 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha is a very happy guy, that's why he is always on the move. There are n cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 1 to n in increasing order. The distance between any two adjacent cities is equal to 1 kilometer. Since all roads in the country are directed, it's possible to reach the city y from the city x only if x < y.
Once Sasha decided to go on a trip around the country and to visit all n cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is v liters, and it spends exactly 1 liter of fuel for 1 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 1 and wants to get to the city with the number n. There is a gas station in each city. In the i-th city, the price of 1 liter of fuel is i dollars. It is obvious that at any moment of time, the tank can contain at most v liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers n and v (2 β€ n β€ 100, 1 β€ v β€ 100) β the number of cities in the country and the capacity of the tank.
Output
Print one integer β the minimum amount of money that is needed to finish the trip.
Examples
Input
4 2
Output
4
Input
7 6
Output
6
Note
In the first example, Sasha can buy 2 liters for 2 dollars (1 dollar per liter) in the first city, drive to the second city, spend 1 liter of fuel on it, then buy 1 liter for 2 dollars in the second city and then drive to the 4-th city. Therefore, the answer is 1+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities. | instruction | 0 | 78,326 | 1 | 156,652 |
Tags: dp, greedy, math
Correct Solution:
```
n,v=[int(x) for x in input().split()]
fuel=0
price=0
dist=n-1
if(v<n):
fuel=v
else:
fuel=n-1
price=fuel
for i in range(2,n+1):
dist=dist-1
fuel=fuel-1
if(fuel<dist):
fuel=fuel+1
price=price+i
print(price)
``` | output | 1 | 78,326 | 1 | 156,653 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha is a very happy guy, that's why he is always on the move. There are n cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 1 to n in increasing order. The distance between any two adjacent cities is equal to 1 kilometer. Since all roads in the country are directed, it's possible to reach the city y from the city x only if x < y.
Once Sasha decided to go on a trip around the country and to visit all n cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is v liters, and it spends exactly 1 liter of fuel for 1 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 1 and wants to get to the city with the number n. There is a gas station in each city. In the i-th city, the price of 1 liter of fuel is i dollars. It is obvious that at any moment of time, the tank can contain at most v liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers n and v (2 β€ n β€ 100, 1 β€ v β€ 100) β the number of cities in the country and the capacity of the tank.
Output
Print one integer β the minimum amount of money that is needed to finish the trip.
Examples
Input
4 2
Output
4
Input
7 6
Output
6
Note
In the first example, Sasha can buy 2 liters for 2 dollars (1 dollar per liter) in the first city, drive to the second city, spend 1 liter of fuel on it, then buy 1 liter for 2 dollars in the second city and then drive to the 4-th city. Therefore, the answer is 1+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities. | instruction | 0 | 78,327 | 1 | 156,654 |
Tags: dp, greedy, math
Correct Solution:
```
from sys import stdin
n,v=map(int,stdin.readline().split())
if n<=v:
cost=n-1
else:
cost=v
for i in range(2,n-v+1):
cost+=i
print(cost)
``` | output | 1 | 78,327 | 1 | 156,655 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha is a very happy guy, that's why he is always on the move. There are n cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 1 to n in increasing order. The distance between any two adjacent cities is equal to 1 kilometer. Since all roads in the country are directed, it's possible to reach the city y from the city x only if x < y.
Once Sasha decided to go on a trip around the country and to visit all n cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is v liters, and it spends exactly 1 liter of fuel for 1 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 1 and wants to get to the city with the number n. There is a gas station in each city. In the i-th city, the price of 1 liter of fuel is i dollars. It is obvious that at any moment of time, the tank can contain at most v liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers n and v (2 β€ n β€ 100, 1 β€ v β€ 100) β the number of cities in the country and the capacity of the tank.
Output
Print one integer β the minimum amount of money that is needed to finish the trip.
Examples
Input
4 2
Output
4
Input
7 6
Output
6
Note
In the first example, Sasha can buy 2 liters for 2 dollars (1 dollar per liter) in the first city, drive to the second city, spend 1 liter of fuel on it, then buy 1 liter for 2 dollars in the second city and then drive to the 4-th city. Therefore, the answer is 1+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities. | instruction | 0 | 78,328 | 1 | 156,656 |
Tags: dp, greedy, math
Correct Solution:
```
p = input().split()
node = int (p[0])
m =int (p[1])
if m>=node:
print(node-1)
else:
price = node-1-m;
for i in range(2,2+price):
m+=i;
print(m)
``` | output | 1 | 78,328 | 1 | 156,657 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha is a very happy guy, that's why he is always on the move. There are n cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 1 to n in increasing order. The distance between any two adjacent cities is equal to 1 kilometer. Since all roads in the country are directed, it's possible to reach the city y from the city x only if x < y.
Once Sasha decided to go on a trip around the country and to visit all n cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is v liters, and it spends exactly 1 liter of fuel for 1 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 1 and wants to get to the city with the number n. There is a gas station in each city. In the i-th city, the price of 1 liter of fuel is i dollars. It is obvious that at any moment of time, the tank can contain at most v liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers n and v (2 β€ n β€ 100, 1 β€ v β€ 100) β the number of cities in the country and the capacity of the tank.
Output
Print one integer β the minimum amount of money that is needed to finish the trip.
Examples
Input
4 2
Output
4
Input
7 6
Output
6
Note
In the first example, Sasha can buy 2 liters for 2 dollars (1 dollar per liter) in the first city, drive to the second city, spend 1 liter of fuel on it, then buy 1 liter for 2 dollars in the second city and then drive to the 4-th city. Therefore, the answer is 1+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities. | instruction | 0 | 78,329 | 1 | 156,658 |
Tags: dp, greedy, math
Correct Solution:
```
n,m=map(int,input().split())
t=m
if m>=n-1:
print(n-1)
else:
for i in range(2,n-m+1):
t=t+i
print(t)
``` | output | 1 | 78,329 | 1 | 156,659 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha is a very happy guy, that's why he is always on the move. There are n cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 1 to n in increasing order. The distance between any two adjacent cities is equal to 1 kilometer. Since all roads in the country are directed, it's possible to reach the city y from the city x only if x < y.
Once Sasha decided to go on a trip around the country and to visit all n cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is v liters, and it spends exactly 1 liter of fuel for 1 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 1 and wants to get to the city with the number n. There is a gas station in each city. In the i-th city, the price of 1 liter of fuel is i dollars. It is obvious that at any moment of time, the tank can contain at most v liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers n and v (2 β€ n β€ 100, 1 β€ v β€ 100) β the number of cities in the country and the capacity of the tank.
Output
Print one integer β the minimum amount of money that is needed to finish the trip.
Examples
Input
4 2
Output
4
Input
7 6
Output
6
Note
In the first example, Sasha can buy 2 liters for 2 dollars (1 dollar per liter) in the first city, drive to the second city, spend 1 liter of fuel on it, then buy 1 liter for 2 dollars in the second city and then drive to the 4-th city. Therefore, the answer is 1+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities. | instruction | 0 | 78,330 | 1 | 156,660 |
Tags: dp, greedy, math
Correct Solution:
```
x,y=map(int,input().split())
t=x-y
print(y+((t*(t+1)//2)-1) if x>y else x-1)
``` | output | 1 | 78,330 | 1 | 156,661 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha is a very happy guy, that's why he is always on the move. There are n cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 1 to n in increasing order. The distance between any two adjacent cities is equal to 1 kilometer. Since all roads in the country are directed, it's possible to reach the city y from the city x only if x < y.
Once Sasha decided to go on a trip around the country and to visit all n cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is v liters, and it spends exactly 1 liter of fuel for 1 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 1 and wants to get to the city with the number n. There is a gas station in each city. In the i-th city, the price of 1 liter of fuel is i dollars. It is obvious that at any moment of time, the tank can contain at most v liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers n and v (2 β€ n β€ 100, 1 β€ v β€ 100) β the number of cities in the country and the capacity of the tank.
Output
Print one integer β the minimum amount of money that is needed to finish the trip.
Examples
Input
4 2
Output
4
Input
7 6
Output
6
Note
In the first example, Sasha can buy 2 liters for 2 dollars (1 dollar per liter) in the first city, drive to the second city, spend 1 liter of fuel on it, then buy 1 liter for 2 dollars in the second city and then drive to the 4-th city. Therefore, the answer is 1+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities.
Submitted Solution:
```
n, v = list(map(int, input().strip().split()))
remaining_dist = n - 1
adding = min(remaining_dist, v)
cost = adding
remaining_dist -= adding
i = 2
while remaining_dist > 0:
cost += i
i += 1
remaining_dist -= 1
print(cost)
``` | instruction | 0 | 78,331 | 1 | 156,662 |
Yes | output | 1 | 78,331 | 1 | 156,663 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha is a very happy guy, that's why he is always on the move. There are n cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 1 to n in increasing order. The distance between any two adjacent cities is equal to 1 kilometer. Since all roads in the country are directed, it's possible to reach the city y from the city x only if x < y.
Once Sasha decided to go on a trip around the country and to visit all n cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is v liters, and it spends exactly 1 liter of fuel for 1 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 1 and wants to get to the city with the number n. There is a gas station in each city. In the i-th city, the price of 1 liter of fuel is i dollars. It is obvious that at any moment of time, the tank can contain at most v liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers n and v (2 β€ n β€ 100, 1 β€ v β€ 100) β the number of cities in the country and the capacity of the tank.
Output
Print one integer β the minimum amount of money that is needed to finish the trip.
Examples
Input
4 2
Output
4
Input
7 6
Output
6
Note
In the first example, Sasha can buy 2 liters for 2 dollars (1 dollar per liter) in the first city, drive to the second city, spend 1 liter of fuel on it, then buy 1 liter for 2 dollars in the second city and then drive to the 4-th city. Therefore, the answer is 1+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities.
Submitted Solution:
```
n, v = map(int, input().split())
ans = 0
for i in range(1, n + 1):
ans += i
if n - i <= v: break
print(ans - 1 + min(n - 1, v))
# 1 2 3 4
``` | instruction | 0 | 78,332 | 1 | 156,664 |
Yes | output | 1 | 78,332 | 1 | 156,665 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha is a very happy guy, that's why he is always on the move. There are n cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 1 to n in increasing order. The distance between any two adjacent cities is equal to 1 kilometer. Since all roads in the country are directed, it's possible to reach the city y from the city x only if x < y.
Once Sasha decided to go on a trip around the country and to visit all n cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is v liters, and it spends exactly 1 liter of fuel for 1 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 1 and wants to get to the city with the number n. There is a gas station in each city. In the i-th city, the price of 1 liter of fuel is i dollars. It is obvious that at any moment of time, the tank can contain at most v liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers n and v (2 β€ n β€ 100, 1 β€ v β€ 100) β the number of cities in the country and the capacity of the tank.
Output
Print one integer β the minimum amount of money that is needed to finish the trip.
Examples
Input
4 2
Output
4
Input
7 6
Output
6
Note
In the first example, Sasha can buy 2 liters for 2 dollars (1 dollar per liter) in the first city, drive to the second city, spend 1 liter of fuel on it, then buy 1 liter for 2 dollars in the second city and then drive to the 4-th city. Therefore, the answer is 1+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities.
Submitted Solution:
```
n, v = [int(v) for v in input().split()]
m = 0
cur = 0
for i in range(n):
need = n - i - 1 - cur
if need <= 0:
break
bought = min(need, v - cur)
m += bought * (i + 1)
cur += bought - 1
print(m)
``` | instruction | 0 | 78,333 | 1 | 156,666 |
Yes | output | 1 | 78,333 | 1 | 156,667 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha is a very happy guy, that's why he is always on the move. There are n cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 1 to n in increasing order. The distance between any two adjacent cities is equal to 1 kilometer. Since all roads in the country are directed, it's possible to reach the city y from the city x only if x < y.
Once Sasha decided to go on a trip around the country and to visit all n cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is v liters, and it spends exactly 1 liter of fuel for 1 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 1 and wants to get to the city with the number n. There is a gas station in each city. In the i-th city, the price of 1 liter of fuel is i dollars. It is obvious that at any moment of time, the tank can contain at most v liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers n and v (2 β€ n β€ 100, 1 β€ v β€ 100) β the number of cities in the country and the capacity of the tank.
Output
Print one integer β the minimum amount of money that is needed to finish the trip.
Examples
Input
4 2
Output
4
Input
7 6
Output
6
Note
In the first example, Sasha can buy 2 liters for 2 dollars (1 dollar per liter) in the first city, drive to the second city, spend 1 liter of fuel on it, then buy 1 liter for 2 dollars in the second city and then drive to the 4-th city. Therefore, the answer is 1+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities.
Submitted Solution:
```
def solve(n, v):
fuel = min(n - 1, v)
cost = fuel
for i in range(2, n):
if fuel >= n - 1:
break
fuel += 1
cost += i
return cost
n, v = map(int, input().split())
print(solve(n, v))
``` | instruction | 0 | 78,334 | 1 | 156,668 |
Yes | output | 1 | 78,334 | 1 | 156,669 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha is a very happy guy, that's why he is always on the move. There are n cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 1 to n in increasing order. The distance between any two adjacent cities is equal to 1 kilometer. Since all roads in the country are directed, it's possible to reach the city y from the city x only if x < y.
Once Sasha decided to go on a trip around the country and to visit all n cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is v liters, and it spends exactly 1 liter of fuel for 1 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 1 and wants to get to the city with the number n. There is a gas station in each city. In the i-th city, the price of 1 liter of fuel is i dollars. It is obvious that at any moment of time, the tank can contain at most v liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers n and v (2 β€ n β€ 100, 1 β€ v β€ 100) β the number of cities in the country and the capacity of the tank.
Output
Print one integer β the minimum amount of money that is needed to finish the trip.
Examples
Input
4 2
Output
4
Input
7 6
Output
6
Note
In the first example, Sasha can buy 2 liters for 2 dollars (1 dollar per liter) in the first city, drive to the second city, spend 1 liter of fuel on it, then buy 1 liter for 2 dollars in the second city and then drive to the 4-th city. Therefore, the answer is 1+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities.
Submitted Solution:
```
def Maxmoney(n, v):
last_stop = n-v
total = v
for i in range(2, last_stop+1):
total += i
return total
line = input().split()
cities = int(line[0])
tank = int(line[1])
print(Maxmoney(cities, tank))
``` | instruction | 0 | 78,335 | 1 | 156,670 |
No | output | 1 | 78,335 | 1 | 156,671 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha is a very happy guy, that's why he is always on the move. There are n cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 1 to n in increasing order. The distance between any two adjacent cities is equal to 1 kilometer. Since all roads in the country are directed, it's possible to reach the city y from the city x only if x < y.
Once Sasha decided to go on a trip around the country and to visit all n cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is v liters, and it spends exactly 1 liter of fuel for 1 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 1 and wants to get to the city with the number n. There is a gas station in each city. In the i-th city, the price of 1 liter of fuel is i dollars. It is obvious that at any moment of time, the tank can contain at most v liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers n and v (2 β€ n β€ 100, 1 β€ v β€ 100) β the number of cities in the country and the capacity of the tank.
Output
Print one integer β the minimum amount of money that is needed to finish the trip.
Examples
Input
4 2
Output
4
Input
7 6
Output
6
Note
In the first example, Sasha can buy 2 liters for 2 dollars (1 dollar per liter) in the first city, drive to the second city, spend 1 liter of fuel on it, then buy 1 liter for 2 dollars in the second city and then drive to the 4-th city. Therefore, the answer is 1+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities.
Submitted Solution:
```
def sol():
n,w = list(map(int,input().split()))
if (n <= w):
print(n)
return 0
d = w
t = w
for i in range(1,n-w):
t+=i
print(t)
sol()
``` | instruction | 0 | 78,336 | 1 | 156,672 |
No | output | 1 | 78,336 | 1 | 156,673 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha is a very happy guy, that's why he is always on the move. There are n cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 1 to n in increasing order. The distance between any two adjacent cities is equal to 1 kilometer. Since all roads in the country are directed, it's possible to reach the city y from the city x only if x < y.
Once Sasha decided to go on a trip around the country and to visit all n cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is v liters, and it spends exactly 1 liter of fuel for 1 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 1 and wants to get to the city with the number n. There is a gas station in each city. In the i-th city, the price of 1 liter of fuel is i dollars. It is obvious that at any moment of time, the tank can contain at most v liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers n and v (2 β€ n β€ 100, 1 β€ v β€ 100) β the number of cities in the country and the capacity of the tank.
Output
Print one integer β the minimum amount of money that is needed to finish the trip.
Examples
Input
4 2
Output
4
Input
7 6
Output
6
Note
In the first example, Sasha can buy 2 liters for 2 dollars (1 dollar per liter) in the first city, drive to the second city, spend 1 liter of fuel on it, then buy 1 liter for 2 dollars in the second city and then drive to the 4-th city. Therefore, the answer is 1+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities.
Submitted Solution:
```
n,v=map(int,input().split())
if n-1<=v:
print(n-1)
else:
e=(n-3)*(n+2)//2
print(e)
``` | instruction | 0 | 78,337 | 1 | 156,674 |
No | output | 1 | 78,337 | 1 | 156,675 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha is a very happy guy, that's why he is always on the move. There are n cities in the country where Sasha lives. They are all located on one straight line, and for convenience, they are numbered from 1 to n in increasing order. The distance between any two adjacent cities is equal to 1 kilometer. Since all roads in the country are directed, it's possible to reach the city y from the city x only if x < y.
Once Sasha decided to go on a trip around the country and to visit all n cities. He will move with the help of his car, Cheetah-2677. The tank capacity of this model is v liters, and it spends exactly 1 liter of fuel for 1 kilometer of the way. At the beginning of the journey, the tank is empty. Sasha is located in the city with the number 1 and wants to get to the city with the number n. There is a gas station in each city. In the i-th city, the price of 1 liter of fuel is i dollars. It is obvious that at any moment of time, the tank can contain at most v liters of fuel.
Sasha doesn't like to waste money, that's why he wants to know what is the minimum amount of money is needed to finish the trip if he can buy fuel in any city he wants. Help him to figure it out!
Input
The first line contains two integers n and v (2 β€ n β€ 100, 1 β€ v β€ 100) β the number of cities in the country and the capacity of the tank.
Output
Print one integer β the minimum amount of money that is needed to finish the trip.
Examples
Input
4 2
Output
4
Input
7 6
Output
6
Note
In the first example, Sasha can buy 2 liters for 2 dollars (1 dollar per liter) in the first city, drive to the second city, spend 1 liter of fuel on it, then buy 1 liter for 2 dollars in the second city and then drive to the 4-th city. Therefore, the answer is 1+1+2=4.
In the second example, the capacity of the tank allows to fill the tank completely in the first city, and drive to the last city without stops in other cities.
Submitted Solution:
```
def main():
n, v = map(int, input().split())
count = (n - 1 + v - 1) // v
res = 0
for x in range(1, count):
res += x * v
res += count * (n - 1 - (count - 1) * v)
print(res)
if __name__ == "__main__":
main()
``` | instruction | 0 | 78,338 | 1 | 156,676 |
No | output | 1 | 78,338 | 1 | 156,677 |
Provide tags and a correct Python 2 solution for this coding contest problem.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test. | instruction | 0 | 78,447 | 1 | 156,894 |
Tags: dfs and similar, greedy, math, trees
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import combinations
pr=stdout.write
import heapq
raw_input = stdin.readline
def ni():
return int(raw_input())
def li():
return list(map(int,raw_input().split()))
def pn(n):
stdout.write(str(n)+'\n')
def pa(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return (map(int,stdin.read().split()))
#range = xrange # not for python 3.0+
for t in range(ni()):
n,m=li()
p=li()
h=li()
d=defaultdict(list)
for i in range(n-1):
u,v=li()
d[u-1].append(v-1)
d[v-1].append(u-1)
q=[0]
pos=0
vis=[0]*n
vis[0]=1
pr=[0]*n
while pos<n:
x=q[pos]
pos+=1
for i in d[x]:
if not vis[i]:
pr[i]=x
q.append(i)
vis[i]=1
dp=[[0,0] for i in range(n)]
f=0
#print q
while q:
x=q.pop()
dp[x][0]+=p[x]
tot=dp[x][0]
bad=dp[x][1]
if abs(h[x])>tot or ((h[x]%2)^(tot%2)):
f=1
break
df=(tot-h[x])/2
if df>(dp[x][1]+p[x]):
f=1
break
dp[pr[x]][0]+=tot
dp[pr[x]][1]+=df
if f:
print('NO')
else:
print('YES')
``` | output | 1 | 78,447 | 1 | 156,895 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test. | instruction | 0 | 78,448 | 1 | 156,896 |
Tags: dfs and similar, greedy, math, trees
Correct Solution:
```
import sys, math
import io, os
#data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
#from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write('\n'.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
#from decimal import Decimal
from fractions import Fraction
#sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1e9)+7
def dfs(graph, start=0):
n = len(graph)
dp = [0] * n
visited, finished = [False] * n, [False] * n
par = [0]*n
stack = [start]
while stack:
start = stack[-1]
# push unvisited children into stack
if not visited[start]:
visited[start] = True
for child in graph[start]:
if not visited[child]:
stack.append(child)
par[child]=start
else:
stack.pop()
# base case
dp[start] += p[start]
# update with finished children
for child in graph[start]:
if finished[child]:
dp[start] += dp[child]
finished[start] = True
return dp
def dfs2(graph, start=0):
n = len(graph)
dp = [0] * n
visited, finished = [False] * n, [False] * n
n = [0]*n
stack = [start]
while stack:
start = stack[-1]
# push unvisited children into stack
if not visited[start]:
visited[start] = True
for child in graph[start]:
if not visited[child]:
stack.append(child)
else:
stack.pop()
# base case
dp[start] += (v[start]+h[start])//2
a=v[start]-dp[start]
if dp[start]-a!=h[start] or a<0:
return False
# update with finished children
k=0
for child in graph[start]:
if finished[child]:
k += dp[child]
if k>dp[start]:
return False
finished[start] = True
return True
for t in range(int(data())):
n,m=mdata()
p=mdata()
h=mdata()
g=[[] for i in range(n)]
for i in range(n-1):
u,v=mdata()
u,v=u-1,v-1
g[u].append(v)
g[v].append(u)
v=dfs(g,0)
if dfs2(g,0):
out("YES")
else:
out("NO")
``` | output | 1 | 78,448 | 1 | 156,897 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test. | instruction | 0 | 78,449 | 1 | 156,898 |
Tags: dfs and similar, greedy, math, trees
Correct Solution:
```
import sys
sys.setrecursionlimit(6000)
def f(u,x):
g[u]-={x}
for v in g[u]:f(v,u);p[u]+=p[v]
h[u]+=p[u];r[0]&=h[u]%2<1and sum(h[v]for v in g[u])<=h[u]<=2*p[u]
R=lambda:map(int,input().split())
t,=R()
for _ in[0]*t:
n,m=R();p=[0,*R()];h=[0,*R()];r=[1];g=r+[set()for _ in r*n]
for _ in r*(n-1):x,y=R();g[x]|={y};g[y]|={x}
f(1,0);print('NYOE S'[r[0]::2])
``` | output | 1 | 78,449 | 1 | 156,899 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test. | instruction | 0 | 78,450 | 1 | 156,900 |
Tags: dfs and similar, greedy, math, trees
Correct Solution:
```
from collections import deque as dq
from collections import defaultdict as dd
from collections import Counter as ct
from functools import lru_cache as lc
from heapq import heappush as hpush, heappop as hpop, heapify as hfy
from bisect import bisect_left, bisect_right
import itertools as it
import math
import sys
sys.setrecursionlimit(10 ** 9)
####################################################################
def solve() :
n, m = map(int, input().split())
P = list(map(int, input().split()))
H = list(map(int, input().split()))
edges = [[] for i in range(n)]
for _ in range(n-1) :
u, v = map(int, input().split())
u, v = u-1, v-1
edges[u].append(v)
edges[v].append(u)
def dfs(cur, prev) :
ret = True
happy = unhappy = 0
for nxt in edges[cur] :
if nxt == prev : continue
sub_ret, sub_happy, sub_unhappy = dfs(nxt, cur)
ret &= sub_ret
happy += sub_happy
unhappy += sub_unhappy
unhappy += P[cur]
h = happy - unhappy
if H[cur] < h :
ret = False
if happy + unhappy < abs(H[cur]) :
ret = False
if H[cur] % 2 != h % 2 :
ret = False
happy += (H[cur] - h) // 2
unhappy -= (H[cur] - h) // 2
return ret, happy, unhappy
print('YES' if dfs(0, -1)[0] else 'NO')
T = int(input())
for t in range(T) :
solve()
``` | output | 1 | 78,450 | 1 | 156,901 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test. | instruction | 0 | 78,451 | 1 | 156,902 |
Tags: dfs and similar, greedy, math, trees
Correct Solution:
```
import sys
import math
from collections import defaultdict,Counter,deque
sys.setrecursionlimit(10**5)
input=sys.stdin.readline
def print(x):
sys.stdout.write(str(x)+"\n")
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
# @bootstrap
# def dfs(u, p):
# orig = sz[u]
# bcnt = 0
# for v in adj[u]:
# if v == p:
# continue
# ok, bad = yield dfs(v, u)
# if not ok:
# yield False, 0
# sz[u] += sz[v]
# bcnt += bad
# cbad = sz[u] - val[u]
# if cbad % 2 or cbad < 0 or cbad//2 > sz[u] or cbad//2 > bcnt + orig:
# yield False, 0
# yield True, cbad // 2
# if dfs(0, 0)[0]:
# write_s("YES")
# else:
# write_s("NO")
# sys.stdout=open("CP2/output.txt",'w')
# sys.stdin=open("CP2/input.txt",'r')
def add(a,b):
l[a].append(b)
l[b].append(a)
@bootstrap
def find(u,v):
# global flag
a[u]=p[u-1]
sumg=0
for j in l[u]:
if j!=v:
x=yield find(j,u)
if x==1:
yield 1
a[u]+=a[j]
sumg+=g[j]
if (a[u]+h[u-1])%2!=0:
yield 1
g[u]=(a[u]+h[u-1])//2
if g[u]<0 or g[u]>a[u]:
yield 1
if sumg>g[u]:
yield 1
yield 0
# m=pow(10,9)+7
t=int(input())
for i in range(t):
l=defaultdict(list)
n,m=map(int,input().split())
p=list(map(int,input().split()))
h=list(map(int,input().split()))
for j in range(n-1):
x,y=map(int,input().split())
add(x,y)
g=[0]*(n+1)
a=[0]*(n+1)
# flag=0
flag=find(1,0)
if flag==0:
print("YES")
else:
print("NO")
``` | output | 1 | 78,451 | 1 | 156,903 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test. | instruction | 0 | 78,452 | 1 | 156,904 |
Tags: dfs and similar, greedy, math, trees
Correct Solution:
```
from sys import setrecursionlimit as srl;srl(10**6)
for _ in range(int(input())):
n,m = map(int,input().split());p = list(map(int,input().split()));h = list(map(int,input().split())); g=[[] for i in range(n)];an="YES";c=[0]*n
for i in range(n-1):u,v = map(int,input().split());g[u-1].append(v-1);g[v-1].append(u-1)
def dfs(v,pa):
d=0
for i in g[v]:
if i!=pa:d+=dfs(i,v)
c[v]=d+p[v]
return c[v]
dfs(0,-1)
def df(v,pa):
d=h[v];cr=c[v];bd=(cr-h[v])//2;gd=cr-bd;pos=1;aa=0;bb=0
for i in g[v]:
if i!=pa:kk=c[i];bh=(c[i]-h[i])//2;gh=c[i]-bh;aa+=gh;bb+=bh;pos=min(pos,df(i,v))
if c[v]<abs(h[v]) or (cr-h[v])%2:return 0
if aa<=gd and bb<=bd+(gd-aa):return pos
return 0
po=df(0,-1)
if not po:an="NO"
print(an)
``` | output | 1 | 78,452 | 1 | 156,905 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test. | instruction | 0 | 78,453 | 1 | 156,906 |
Tags: dfs and similar, greedy, math, trees
Correct Solution:
```
from sys import stdin, gettrace
from collections import deque
if gettrace():
inputi = input
else:
def input():
return next(stdin)[:-1]
def inputi():
return stdin.buffer.readline()
def readGraph(n, m):
adj = [set() for _ in range(n)]
for _ in range(m):
u,v = map(int, inputi().split())
adj[u-1].add(v-1)
adj[v-1].add(u-1)
return adj
def readTree(n):
return readGraph(n, n-1)
def treeOrderByDepth(n, adj, root=0):
parent = [-1]*n
parent[root] = -2
ordered = []
q = deque()
q.append(root)
while q:
c =q.popleft()
ordered.append(c)
for a in adj[c]:
if parent[a] == -1:
parent[a] = c
q.append(a)
return (ordered, parent)
def solve():
n,m = map(int, inputi().split())
pp = [int(a) for a in inputi().split()]
hh = [int(a) for a in inputi().split()]
adj = readTree(n)
ordered, parent = treeOrderByDepth(n, adj)
psum = [0] * n
hsum = [0] * n
for i in ordered[::-1]:
if hh[i] < -pp[i] + hsum[i] or hh[i] > pp[i] + psum[i]:
print('NO')
return
if (hh[i] + pp[i] + psum[i])%2 != 0:
print('NO')
return
if i != 0:
hsum[parent[i]] += hh[i]
psum[parent[i]] += psum[i] + pp[i]
print('YES')
def main():
t = int(inputi())
for _ in range(t):
solve()
if __name__ == "__main__":
main()
``` | output | 1 | 78,453 | 1 | 156,907 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test. | instruction | 0 | 78,454 | 1 | 156,908 |
Tags: dfs and similar, greedy, math, trees
Correct Solution:
```
import sys,collections,bisect,os,io
sys.stdin=io.BytesIO(os.read(0,os.fstat(0).st_size))
input=lambda: sys.stdin.readline()
que=collections.deque; vector=collections.defaultdict
bsearch=bisect.bisect_left
inin = lambda: int(input())
inar = lambda: list(map(int,input().split()))
INF=float('inf')
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
#Main Solution Starts Here
ans=''
@bootstrap
def dfs(graph,node,par,vis,h,p):
global ans
check=0
vis[node]=p[node]
for child in graph[node]:
if child!=par:
vis[node]+=yield dfs(graph,child,node,vis,h,p)
check+=h[child]
if (vis[node]+h[node])%2:
ans='NO'
if abs(h[node])>vis[node]:
ans='NO'
if check>p[node]+h[node]:
ans='NO'
yield vis[node]
Testcase=inin()
for Case in range(Testcase):
ans='YES'
n,m=inar()
qq=que()
qq.append(n)
qq.append(m)
a=[0,1,2,3,4,5,6,7,8,9,10]
idx=bsearch(a,5)
p=[0]+inar()
h=[m+8]+inar()
vis=[0 for i in range(n+1)]
graph=[[] for i in range(n+1)]
for i in range(n-1):
a,b=inar()
graph[a].append(b)
graph[b].append(a)
dfs(graph,1,0,vis,h,p)
print(ans)
``` | output | 1 | 78,454 | 1 | 156,909 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test. | instruction | 0 | 78,455 | 1 | 156,910 |
Tags: dfs and similar, greedy, math, trees
Correct Solution:
```
import sys
sys.setrecursionlimit(5060)
def f(u,x):
g[u]-={x}
for v in g[u]:f(v,u);p[u]+=p[v]
h[u]+=p[u];r[0]&=h[u]%2<1and sum(h[v]for v in g[u])<=h[u]<=2*p[u]
R=lambda:map(int,input().split())
t,=R()
for _ in[0]*t:
n,m=R();p=[0,*R()];h=[0,*R()];r=[1];g=r+[set()for _ in r*n]
for _ in r*(n-1):x,y=R();g[x]|={y};g[y]|={x}
f(1,0);print('NYOE S'[r[0]::2])
``` | output | 1 | 78,455 | 1 | 156,911 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test.
Submitted Solution:
```
import sys
sys.setrecursionlimit(5056)
def f(u,x):
g[u]-={x}
for v in g[u]:f(v,u);p[u]+=p[v]
h[u]+=p[u];r[0]&=~h[u]&(sum(h[v]for v in g[u])<=h[u]<=2*p[u])
R=lambda:map(int,input().split())
t,=R()
for _ in[0]*t:
R();p=[0,*R()];h=[0,*R()];g=[set()for _ in p];r=[1]
for _ in p[2:]:x,y=R();g[x]|={y};g[y]|={x}
f(1,0);print('NYOE S'[r[0]::2])
``` | instruction | 0 | 78,456 | 1 | 156,912 |
Yes | output | 1 | 78,456 | 1 | 156,913 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test.
Submitted Solution:
```
from collections import defaultdict
import sys
input=sys.stdin.buffer.readline
t=int(input())
for _ in range(t):
n,m=[int(x) for x in input().split()]
p=[int(x) for x in input().split()] #ppl living in each city
p.insert(0,-1) #make 1-indexing
h=[int(x) for x in input().split()]
h.insert(0,-1) #make 1-indexing
roads=defaultdict(list)
for __ in range(n-1):
x,y=[int(x) for x in input().split()]
roads[x].append(y)
roads[y].append(x)
parent=[-1 for zzz in range(n+1)] #parent[i] is the parent of node i
depths=defaultdict(list) #depths[d] are all the nodes of depth d
dfsStack=[[1,-1,1]] #[currentNode,parent,depth]
maxDepth=1
leafs=set() #store all leaf nodes
while dfsStack:
node,par,d=dfsStack.pop()
depths[d].append(node)
maxDepth=max(maxDepth,d)
parent[node]=par
children=[x for x in roads[node] if x!=par]
if len(children)==0:
leafs.add(node)
for child in children:
dfsStack.append([child,node,d+1])
# print(parent)
# print(depths)
# print(maxDepth)
passCityCnt=[0 for zzz in range(n+1)] #passCityCnt[i] is the cnt of ppl who passed through city i
for d in range(maxDepth,0,-1):
for node in depths[d]:
passCityCnt[node]+=p[node]
if node!=1:
passCityCnt[parent[node]]+=passCityCnt[node] #citizen passes through parent
happyCityCnt=[0 for zzz in range(n+1)] #happyCityCnt[i] is the cnt of happy ppl at city i
ok=True
for i in range(1,n+1):
if (passCityCnt[i]+h[i])%2==1: #impossible
ok=False
print('NO')
break
happyCityCnt[i]=(passCityCnt[i]+h[i])//2
if happyCityCnt[i]<0 or passCityCnt[i]-happyCityCnt[i]<0: #negative happy or sad people
ok=False
print('NO')
break
if ok==False:
continue
#a node must have at least the number of happy people of total sum of happy people of its children
theoreticalMinHappiness=[0 for zzz in range(n+1)]
for d in range(maxDepth,0,-1):
for node in depths[d]:
if node not in leafs and theoreticalMinHappiness[node]>happyCityCnt[node]: #if node is a leaf, can have any number of happy people
ok=False
print('NO')
break
theoreticalMinHappiness[parent[node]]+=happyCityCnt[node]
if ok==False:
break
if ok==False:
continue
print('YES')
``` | instruction | 0 | 78,457 | 1 | 156,914 |
Yes | output | 1 | 78,457 | 1 | 156,915 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test.
Submitted Solution:
```
from sys import setrecursionlimit
setrecursionlimit(10**8)
def dfs(p,prev):
for i in child[p]:
if(i != prev):
dfs(i,p)
a[p-1]+=a[i-1]
def dfs1(p,prev):
global ans
v=h[p - 1] + a[p - 1]
if(v & 1):ans='NO'
g=v//2
if(g > a[p-1] or g < 0):ans='NO'
s=0
for i in child[p]:
if(i != prev):
s+=dfs1(i,p)
if(g < s):ans='NO'
return g
for T in range(int(input())):
n,m=map(int,input().split())
a=list(map(int,input().split()))
h=list(map(int,input().split()))
child=[[] for i in range(n+1)]
for i in range(n-1):
u,v=map(int,input().split())
child[u].append(v)
child[v].append(u)
dfs(1,-1)
ans='YES'
dfs1(1,-1)
print(ans)
``` | instruction | 0 | 78,458 | 1 | 156,916 |
Yes | output | 1 | 78,458 | 1 | 156,917 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test.
Submitted Solution:
```
import math
import threading,sys
def main():
m=int(input())
for l in range(m):
n,m=map(int,input().split())
p=list(map(int,input().split()))
h=list(map(int,input().split()))
di={}
for _ in range(n-1):
x,y=map(int,input().split())
if x==y:
continue
if x-1 in di:
di[x-1].add(y-1)
else:
di[x-1]={y-1}
if y-1 in di:
di[y-1].add(x-1)
else:
di[y-1]={x-1}
def dfs(k,pa):
good,bad=0,0
if k in di:
for i in di[k]:
if i==pa:
continue
g,b=dfs(i,k)
if g<=-1:
return(-1,-1)
good+=g
bad+=b
g1=good
g2=bad
t=g1+g2+p[k]
good=int((h[k]+t)/2)
bad=t-good
if bad>g2+p[k] or abs(h[k])>t or ((h[k]+t)%2)!=0:
return(-1,-1)
return(good,bad)
gf,bf=dfs(0,-1)
if gf==-1 or gf+bf>m:
print('NO')
else:
print('YES')
sys.setrecursionlimit(1000000)
threading.stack_size(1024000)
thread=threading.Thread(target=main)
thread.start()
``` | instruction | 0 | 78,459 | 1 | 156,918 |
Yes | output | 1 | 78,459 | 1 | 156,919 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test.
Submitted Solution:
```
import sys
input = sys.stdin.readline
from math import gcd
import math
t = int(input())
for ccc in range(t):
n,m = list(map(int,input().split()))
p = list(map(int,input().split()))
h = list(map(int,input().split()))
pop = list(p)
N = [[] for i in range(n)]
D = [0 for i in range(n)]
O = [0]
E = []
for _ in range(n-1):
x,y = list(map(int,input().split()))
x,y = x-1, y-1
N[x].append(y)
N[y].append(x)
Q = [0]
V = [0]
d = 0
while Q:
d += 1
for node in Q:
T = []
for child in N[node]:
if child not in V:
T.append(child)
O.append(child)
V.append(child)
D[child] = d
Q = T
O = O[::-1]
#V = []
for j in range(len(N)):
temp = D[j]
t_l = []
for child in N[j]:
if D[child] > temp:
t_l.append(child)
N[j] = t_l
#print(D)
#print(N)
for node in O:
if len(N[node])>0:
for child in N[node]:
pop[node] += pop[child]
# print(pop)
# print(h)
ok = 1
for node in O:
if len(N[node])==0:
pass
else:
happy = (pop[node] + h[node]) // 2
if happy<0:
ok=0
c_happy = 0
for child in N[node]:
c_happy += (pop[child] + h[child]) // 2
if c_happy>happy:
ok = 0
if t==8441 and ccc==4142:
print(ok, 'c_hapy')
if (abs(pop[node] - h[node])) % 2 != 0:
ok = 0
if abs(h[node]) > pop[node]:
ok = 0
happy = (pop[node] + h[node]) // 2
#if happy<0:
#ok=0
#if t==8441 and ccc==4142:
# print(ok, 'less 0')
if n==1:
ok = 1
if (abs(pop[0] - h[0])) % 2 != 0:
ok = 0
if abs(h[0]) > pop[0]:
ok = 0
#if t==8441 and ccc==4142:
# print(ok, 'n==1')
if t!=8441:
if ok==1:
print('YES')
else:
print('NO')
if t==8441 and ccc==4142:
print(n,m)
print(p)
print(h)
break
``` | instruction | 0 | 78,460 | 1 | 156,920 |
No | output | 1 | 78,460 | 1 | 156,921 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test.
Submitted Solution:
```
an=1
def dfs(node):
global an
vis[node]=1
s=0
for i in adj[node]:
if not vis[i]:
dfs(i)
p[node]+=p[i]
xx=(p[node]+q[node])//2
yy=(p[node]-q[node])//2
if xx<0 or yy<0:
an=0
else:
if xx+yy!=p[node] or xx-yy!=q[node]:
an=0
for _ in range(int(input())):
n,m=map(int,input().split())
p=[int(X) for X in input().split()]
q=[int(X) for X in input().split() ]
vis=[0]*n
adj=[[] for i in range(n)]
an=1
dp=[0]*n
ha,sa=[0]*n,[0]*n
for i in range(n-1):
x,y=map(int,input().split())
adj[x-1].append(y-1)
adj[y-1].append(x-1)
dfs(0)
# print(ha,sa)
print('YES' if an else'NO' )
``` | instruction | 0 | 78,461 | 1 | 156,922 |
No | output | 1 | 78,461 | 1 | 156,923 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test.
Submitted Solution:
```
from collections import deque
import sys
import math
def inp():
return sys.stdin.readline().strip()
global g,p,a ,f
def dfs(root):
global g,p,a,f
happy=0
sad=0
if len(g[root])==0:
h_s=a[root]
hs=p[root]
happy=hs+h_s
sad=hs-h_s
if happy%2!=0 or sad%2!=0 or sad<0 or happy<0:
f=False
return (float('inf'),float('inf'))
return(happy//2,sad//2)
for child in g[root]:
tp=dfs(child)
happy+=tp[0]
sad+=tp[1]
maxhappy=happy+sad
x=(maxhappy+p[root]+a[root])
y=(p[root]+maxhappy-a[root])
if happy==float('inf') or sad==float('inf') or x%2!=0 or y%2!=0 or x<0 or y<0 or x<2*happy:
f=False
return (float('inf'),float('inf'))
x//=2
y//=2
return (x,y)
for _ in range(int(inp())):
g=[]
f=True
n,m=map(int,inp().split())
g=[[] for i in range(n)]
p=list(map(int,inp().split()))
a=list(map(int,inp().split()))
for i in range(n-1):
u,v=map(int,inp().split())
u-=1
v-=1
g[u].append(v)
ans=dfs(0)
if not f:
print("NO")
else:
print("YES")
``` | instruction | 0 | 78,462 | 1 | 156,924 |
No | output | 1 | 78,462 | 1 | 156,925 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Uncle Bogdan is in captain Flint's crew for a long time and sometimes gets nostalgic for his homeland. Today he told you how his country introduced a happiness index.
There are n cities and nβ1 undirected roads connecting pairs of cities. Citizens of any city can reach any other city traveling by these roads. Cities are numbered from 1 to n and the city 1 is a capital. In other words, the country has a tree structure.
There are m citizens living in the country. A p_i people live in the i-th city but all of them are working in the capital. At evening all citizens return to their home cities using the shortest paths.
Every person has its own mood: somebody leaves his workplace in good mood but somebody are already in bad mood. Moreover any person can ruin his mood on the way to the hometown. If person is in bad mood he won't improve it.
Happiness detectors are installed in each city to monitor the happiness of each person who visits the city. The detector in the i-th city calculates a happiness index h_i as the number of people in good mood minus the number of people in bad mood. Let's say for the simplicity that mood of a person doesn't change inside the city.
Happiness detector is still in development, so there is a probability of a mistake in judging a person's happiness. One late evening, when all citizens successfully returned home, the government asked uncle Bogdan (the best programmer of the country) to check the correctness of the collected happiness indexes.
Uncle Bogdan successfully solved the problem. Can you do the same?
More formally, You need to check: "Is it possible that, after all people return home, for each city i the happiness index will be equal exactly to h_i".
Input
The first line contains a single integer t (1 β€ t β€ 10000) β the number of test cases.
The first line of each test case contains two integers n and m (1 β€ n β€ 10^5; 0 β€ m β€ 10^9) β the number of cities and citizens.
The second line of each test case contains n integers p_1, p_2, β¦, p_{n} (0 β€ p_i β€ m; p_1 + p_2 + β¦ + p_{n} = m), where p_i is the number of people living in the i-th city.
The third line contains n integers h_1, h_2, β¦, h_{n} (-10^9 β€ h_i β€ 10^9), where h_i is the calculated happiness index of the i-th city.
Next n β 1 lines contain description of the roads, one per line. Each line contains two integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are cities connected by the i-th road.
It's guaranteed that the sum of n from all test cases doesn't exceed 2 β
10^5.
Output
For each test case, print YES, if the collected data is correct, or NO β otherwise. You can print characters in YES or NO in any case.
Examples
Input
2
7 4
1 0 1 1 0 1 0
4 0 0 -1 0 -1 0
1 2
1 3
1 4
3 5
3 6
3 7
5 11
1 2 5 2 1
-11 -2 -6 -2 -1
1 2
1 3
1 4
3 5
Output
YES
YES
Input
2
4 4
1 1 1 1
4 1 -3 -1
1 2
1 3
1 4
3 13
3 3 7
13 1 4
1 2
1 3
Output
NO
NO
Note
Let's look at the first test case of the first sample:
<image>
At first, all citizens are in the capital. Let's describe one of possible scenarios:
* a person from city 1: he lives in the capital and is in good mood;
* a person from city 4: he visited cities 1 and 4, his mood was ruined between cities 1 and 4;
* a person from city 3: he visited cities 1 and 3 in good mood;
* a person from city 6: he visited cities 1, 3 and 6, his mood was ruined between cities 1 and 3;
In total,
* h_1 = 4 - 0 = 4,
* h_2 = 0,
* h_3 = 1 - 1 = 0,
* h_4 = 0 - 1 = -1,
* h_5 = 0,
* h_6 = 0 - 1 = -1,
* h_7 = 0.
The second case of the first test:
<image>
All people have already started in bad mood in the capital β this is the only possible scenario.
The first case of the second test:
<image>
The second case of the second test:
<image>
It can be proven that there is no way to achieve given happiness indexes in both cases of the second test.
Submitted Solution:
```
import sys, math
input=sys.stdin.readline
t=int(input())
def numberOfNodes(s, e, p):
countPeople[s] = p[s]
for u in graph[s]:
if u == e:
continue
parents[u] = s
numberOfNodes(u, s, p)
countPeople[s] += countPeople[u]
for r in range(t):
n, m = map(int,input().split())
p = list(map(int,input().split()))
h = list(map(int,input().split()))
graph = []
for i in range(n):
graph.append([])
for i in range(n-1):
v, u = map(int,input().split())
v-=1
u-=1
graph[v].append(u)
graph[u].append(v)
countPeople = [0]*n
parents = [0] * n
parents[0] = -1
numberOfNodes(0,-1,p)
# print(countPeople)
happyPeople = [0]*n
flag = True
for v in range(n):
if countPeople[v]%2 != h[v]%2:
flag = False
break
happyPeople[v] = (countPeople[v] + h[v])//2
if flag == False:
# print("Entered")
print("NO")
else:
flag2 = True
for v in range(n):
sumHappy = 0
for u in graph[v]:
if u != parents[v]:
sumHappy += happyPeople[u]
if sumHappy > happyPeople[v]:
flag2 = False
break
if flag2 == False:
# print("Entered2")
print("NO")
else:
print("YES")
``` | instruction | 0 | 78,463 | 1 | 156,926 |
No | output | 1 | 78,463 | 1 | 156,927 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Gildong's town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let's take a look at the picture below.
<image>
The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.
However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
Each test case contains three lines. The first line of each test case consists of two integers n and m (1 β€ n, m β€ 100) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.
The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
Output
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
Example
Input
3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100
Output
0
1
3
Note
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.
In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
<image> | instruction | 0 | 78,481 | 1 | 156,962 |
Tags: implementation
Correct Solution:
```
for test_var in range(int(input())):
a, b = map(int, input().split())
up = list(map(int, input().split()))
side = list(map(int, input().split()))
ans = 0
for m in up:
if m in side:
ans += 1
print(ans)
``` | output | 1 | 78,481 | 1 | 156,963 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Gildong's town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let's take a look at the picture below.
<image>
The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.
However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
Each test case contains three lines. The first line of each test case consists of two integers n and m (1 β€ n, m β€ 100) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.
The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
Output
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
Example
Input
3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100
Output
0
1
3
Note
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.
In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
<image> | instruction | 0 | 78,482 | 1 | 156,964 |
Tags: implementation
Correct Solution:
```
for j in range(int(input())):
n,m=map(int,input().split())
count=0
n_arr=list(map(int,input().split()))
m_arr=list(map(int,input().split()))
for i in m_arr:
if i in n_arr:
count+=1
print(count)
``` | output | 1 | 78,482 | 1 | 156,965 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Gildong's town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let's take a look at the picture below.
<image>
The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.
However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
Each test case contains three lines. The first line of each test case consists of two integers n and m (1 β€ n, m β€ 100) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.
The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
Output
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
Example
Input
3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100
Output
0
1
3
Note
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.
In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
<image> | instruction | 0 | 78,483 | 1 | 156,966 |
Tags: implementation
Correct Solution:
```
cases=int(input())
result=[]
for i in range(cases):
n,m=map(int,input().split())
sn=set(map(int,input().split()))
sm=set(map(int,input().split()))
val=len(sn.intersection(sm))
result.append(val)
for i in result:
print(i)
``` | output | 1 | 78,483 | 1 | 156,967 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Gildong's town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let's take a look at the picture below.
<image>
The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.
However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
Each test case contains three lines. The first line of each test case consists of two integers n and m (1 β€ n, m β€ 100) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.
The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
Output
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
Example
Input
3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100
Output
0
1
3
Note
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.
In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
<image> | instruction | 0 | 78,484 | 1 | 156,968 |
Tags: implementation
Correct Solution:
```
for _ in range(int(input())):
col, row = [int(i) for i in input().split()]
col_t = [int(i) for i in input().split()]
row_t = [int(i) for i in input().split()]
col_t = set(col_t)
row_t = set(row_t)
r = col_t.intersection(row_t)
print(len(r))
``` | output | 1 | 78,484 | 1 | 156,969 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Gildong's town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let's take a look at the picture below.
<image>
The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.
However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
Each test case contains three lines. The first line of each test case consists of two integers n and m (1 β€ n, m β€ 100) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.
The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
Output
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
Example
Input
3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100
Output
0
1
3
Note
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.
In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
<image> | instruction | 0 | 78,485 | 1 | 156,970 |
Tags: implementation
Correct Solution:
```
from collections import Counter
for _ in range(int(input())):
n, m = map(int, input().split())
bot = list(map(int, input().split()))
left = list(map(int, input().split()))
cb = Counter(bot)
cl = Counter(left)
c = cb + cl
res = 0
for x in c:
if c[x] > 1:
res += 1
print(res)
``` | output | 1 | 78,485 | 1 | 156,971 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Gildong's town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let's take a look at the picture below.
<image>
The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.
However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
Each test case contains three lines. The first line of each test case consists of two integers n and m (1 β€ n, m β€ 100) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.
The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
Output
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
Example
Input
3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100
Output
0
1
3
Note
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.
In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
<image> | instruction | 0 | 78,486 | 1 | 156,972 |
Tags: implementation
Correct Solution:
```
import math
def main():
n, m = map(int, input().split())
down = list(map(int, input().split()))
up = list(map(int, input().split()))
cnt = 0
for i in up:
if i in down:
cnt += 1
print(cnt)
if __name__ == '__main__':
t = int(input())
for i in range(t):
main()
``` | output | 1 | 78,486 | 1 | 156,973 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Gildong's town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let's take a look at the picture below.
<image>
The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.
However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
Each test case contains three lines. The first line of each test case consists of two integers n and m (1 β€ n, m β€ 100) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.
The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
Output
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
Example
Input
3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100
Output
0
1
3
Note
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.
In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
<image> | instruction | 0 | 78,487 | 1 | 156,974 |
Tags: implementation
Correct Solution:
```
from sys import stdin
#from collections import defaultdict
#import math
ip =stdin.readline
for _ in range(int(ip())):
n, m = map(int, ip().split())
arrn = list(map(int, ip().split()))
arrm = list(map(int, ip().split()))
i = j = ans = 0
while i<n and j<m:
if arrn[i]<arrm[j]: i+=1
elif arrm[j]<arrn[i]: j+=1
else:
i+=1
j+=1
ans+=1
print(ans)
``` | output | 1 | 78,487 | 1 | 156,975 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Gildong's town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let's take a look at the picture below.
<image>
The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.
However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
Each test case contains three lines. The first line of each test case consists of two integers n and m (1 β€ n, m β€ 100) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.
The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
Output
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
Example
Input
3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100
Output
0
1
3
Note
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.
In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
<image> | instruction | 0 | 78,488 | 1 | 156,976 |
Tags: implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
l = input().split()
n = int(l[0])
m = int(l[1])
l = input().split()
vis = [0]*101
ans = 0
for i in range(n):
x = int(l[i])
vis[x] = 1
l = input().split()
for i in range(m):
x = int(l[i])
if vis[x]:
ans += 1
print(ans)
``` | output | 1 | 78,488 | 1 | 156,977 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Gildong's town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let's take a look at the picture below.
<image>
The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.
However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
Each test case contains three lines. The first line of each test case consists of two integers n and m (1 β€ n, m β€ 100) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.
The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
Output
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
Example
Input
3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100
Output
0
1
3
Note
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.
In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
<image>
Submitted Solution:
```
from sys import stdin
from sys import stdout
from math import *
input = stdin.readline
# print = stdout.write
for __ in range(int(input())):
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
d={}
for i in range(n):
d[a[i]]=1
ans=0
for i in range(m):
if(d.get(b[i])!=None):
ans+=1
print(ans)
``` | instruction | 0 | 78,489 | 1 | 156,978 |
Yes | output | 1 | 78,489 | 1 | 156,979 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Gildong's town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let's take a look at the picture below.
<image>
The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.
However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
Each test case contains three lines. The first line of each test case consists of two integers n and m (1 β€ n, m β€ 100) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.
The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
Output
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
Example
Input
3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100
Output
0
1
3
Note
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.
In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
<image>
Submitted Solution:
```
t=int(input())
for i in range(0,t):
m,n=map(int,input().split())
arr=list(map(int,input().strip().split()))[:m]
arr1=list(map(int,input().strip().split()))[:n]
l1=arr+arr1
s1=set(l1)
y=len(s1)
ans=m+n-y
print(ans)
``` | instruction | 0 | 78,490 | 1 | 156,980 |
Yes | output | 1 | 78,490 | 1 | 156,981 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Gildong's town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let's take a look at the picture below.
<image>
The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.
However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
Each test case contains three lines. The first line of each test case consists of two integers n and m (1 β€ n, m β€ 100) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.
The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
Output
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
Example
Input
3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100
Output
0
1
3
Note
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.
In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
<image>
Submitted Solution:
```
for _ in range(int(input())):
n, m = map(int, input().split())
lst1 = [int(i) for i in input().split()]
lst2 = [int(i) for i in input().split()]
print(n+m-len(set(lst1+lst2)))
``` | instruction | 0 | 78,491 | 1 | 156,982 |
Yes | output | 1 | 78,491 | 1 | 156,983 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Gildong's town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let's take a look at the picture below.
<image>
The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.
However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
Each test case contains three lines. The first line of each test case consists of two integers n and m (1 β€ n, m β€ 100) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.
The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
Output
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
Example
Input
3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100
Output
0
1
3
Note
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.
In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
<image>
Submitted Solution:
```
s=''
for x in range(int(input())):
n,y=map(int,input().split())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
s+=str(n+y-len(set(l1+l2)))+'\n'
print(s[:-1])
``` | instruction | 0 | 78,492 | 1 | 156,984 |
Yes | output | 1 | 78,492 | 1 | 156,985 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Gildong's town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let's take a look at the picture below.
<image>
The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.
However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
Each test case contains three lines. The first line of each test case consists of two integers n and m (1 β€ n, m β€ 100) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.
The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
Output
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
Example
Input
3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100
Output
0
1
3
Note
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.
In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
<image>
Submitted Solution:
```
from typing import List
def solution(arr1: List[int], arr2: List[int]) -> int:
s = set(arr1)
count = 0
for elem in arr2:
if elem in s:
count += 1
return count
def main() -> None:
t = int(input())
result = []
for _ in range(t):
n, m = list(map(int, input().split()))
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
assert n == len(arr1)
assert m == len(arr2)
result.append(solution(arr1, arr2))
print(result)
if __name__ == "__main__":
main()
``` | instruction | 0 | 78,493 | 1 | 156,986 |
No | output | 1 | 78,493 | 1 | 156,987 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Gildong's town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let's take a look at the picture below.
<image>
The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.
However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
Each test case contains three lines. The first line of each test case consists of two integers n and m (1 β€ n, m β€ 100) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.
The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
Output
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
Example
Input
3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100
Output
0
1
3
Note
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.
In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
<image>
Submitted Solution:
```
print(0)
print(1)
print(3)
``` | instruction | 0 | 78,494 | 1 | 156,988 |
No | output | 1 | 78,494 | 1 | 156,989 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Gildong's town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let's take a look at the picture below.
<image>
The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.
However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
Each test case contains three lines. The first line of each test case consists of two integers n and m (1 β€ n, m β€ 100) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.
The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
Output
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
Example
Input
3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100
Output
0
1
3
Note
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.
In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
<image>
Submitted Solution:
```
output = []
for i in range(int(input())):
total = 0
n = []
n = input().split()
num = input().split()
mum = input().split()
for j in range(max(int(n[0]), int(n[1]))):
if num.count(j) == mum.count(j) and num.count(j) > 0:
total += 1
output.append(total)
for i in output:
print(i)
``` | instruction | 0 | 78,495 | 1 | 156,990 |
No | output | 1 | 78,495 | 1 | 156,991 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Gildong's town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let's take a look at the picture below.
<image>
The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.
However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
Each test case contains three lines. The first line of each test case consists of two integers n and m (1 β€ n, m β€ 100) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.
The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.
Output
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
Example
Input
3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100
Output
0
1
3
Note
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.
In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
<image>
Submitted Solution:
```
t = int(input())
pp = [0]
for i in range(t):
if i%2!=0:
pp.append(i)
pp.append(3)
print(*pp, end='\n')
``` | instruction | 0 | 78,496 | 1 | 156,992 |
No | output | 1 | 78,496 | 1 | 156,993 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.