message
stringlengths
2
45.8k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
254
108k
cluster
float64
3
3
__index_level_0__
int64
508
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two polar bears Menshykov and Uslada from the St.Petersburg zoo and elephant Horace from the Kiev zoo got six sticks to play with and assess the animals' creativity. Menshykov, Uslada and Horace decided to make either an elephant or a bear from those sticks. They can make an animal from sticks in the following way: * Four sticks represent the animal's legs, these sticks should have the same length. * Two remaining sticks represent the animal's head and body. The bear's head stick must be shorter than the body stick. The elephant, however, has a long trunk, so his head stick must be as long as the body stick. Note that there are no limits on the relations between the leg sticks and the head and body sticks. Your task is to find out which animal can be made from the given stick set. The zoo keeper wants the sticks back after the game, so they must never be broken, even bears understand it. Input The single line contains six space-separated integers li (1 ≤ li ≤ 9) — the lengths of the six sticks. It is guaranteed that the input is such that you cannot make both animals from the sticks. Output If you can make a bear from the given set, print string "Bear" (without the quotes). If you can make an elephant, print string "Elephant" (wıthout the quotes). If you can make neither a bear nor an elephant, print string "Alien" (without the quotes). Examples Input 4 2 5 4 4 4 Output Bear Input 4 4 5 4 4 5 Output Elephant Input 1 2 3 4 5 6 Output Alien Note If you're out of creative ideas, see instructions below which show how to make a bear and an elephant in the first two samples. The stick of length 2 is in red, the sticks of length 4 are in green, the sticks of length 5 are in blue. <image> Submitted Solution: ``` a = [int (i) for i in input().split()] dic = {} for i in range (6): dic[a[i]] = dic.get(a[i], 0) + 1 if len (dic) == 1: print ('Elephant') elif len(dic) > 3: print ('Alien') elif len(dic) == 3: if 4 in dic.values(): print ('Bear') else: print ('Alien') else: if 5 in dic.values(): print ('Bear') elif 4 in dic.values(): print ('Elephant') else: print ('Alien') ```
instruction
0
38,058
3
76,116
Yes
output
1
38,058
3
76,117
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two polar bears Menshykov and Uslada from the St.Petersburg zoo and elephant Horace from the Kiev zoo got six sticks to play with and assess the animals' creativity. Menshykov, Uslada and Horace decided to make either an elephant or a bear from those sticks. They can make an animal from sticks in the following way: * Four sticks represent the animal's legs, these sticks should have the same length. * Two remaining sticks represent the animal's head and body. The bear's head stick must be shorter than the body stick. The elephant, however, has a long trunk, so his head stick must be as long as the body stick. Note that there are no limits on the relations between the leg sticks and the head and body sticks. Your task is to find out which animal can be made from the given stick set. The zoo keeper wants the sticks back after the game, so they must never be broken, even bears understand it. Input The single line contains six space-separated integers li (1 ≤ li ≤ 9) — the lengths of the six sticks. It is guaranteed that the input is such that you cannot make both animals from the sticks. Output If you can make a bear from the given set, print string "Bear" (without the quotes). If you can make an elephant, print string "Elephant" (wıthout the quotes). If you can make neither a bear nor an elephant, print string "Alien" (without the quotes). Examples Input 4 2 5 4 4 4 Output Bear Input 4 4 5 4 4 5 Output Elephant Input 1 2 3 4 5 6 Output Alien Note If you're out of creative ideas, see instructions below which show how to make a bear and an elephant in the first two samples. The stick of length 2 is in red, the sticks of length 4 are in green, the sticks of length 5 are in blue. <image> Submitted Solution: ``` a=[int(i) for i in input().split()] f='no' p=[] for i in range(6): t=0 for j in range(6): if i!=j: if a[i]==a[j]: t+=1 if t>=3: f='yes' p=[] p.append(i) for j in range(6): if a[i]==a[j] and i!=j: p.append(j) break if f=='no': print('Alien') else: lp=[] for i in range(1,7): if i-1 not in p: lp.append(i-1) if len(lp)==0: print('Elephant') elif len(lp)==1: print('Bear') else: if a[lp[1]]==a[lp[0]]: print('Elephant') else: print('Bear') ```
instruction
0
38,059
3
76,118
Yes
output
1
38,059
3
76,119
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two polar bears Menshykov and Uslada from the St.Petersburg zoo and elephant Horace from the Kiev zoo got six sticks to play with and assess the animals' creativity. Menshykov, Uslada and Horace decided to make either an elephant or a bear from those sticks. They can make an animal from sticks in the following way: * Four sticks represent the animal's legs, these sticks should have the same length. * Two remaining sticks represent the animal's head and body. The bear's head stick must be shorter than the body stick. The elephant, however, has a long trunk, so his head stick must be as long as the body stick. Note that there are no limits on the relations between the leg sticks and the head and body sticks. Your task is to find out which animal can be made from the given stick set. The zoo keeper wants the sticks back after the game, so they must never be broken, even bears understand it. Input The single line contains six space-separated integers li (1 ≤ li ≤ 9) — the lengths of the six sticks. It is guaranteed that the input is such that you cannot make both animals from the sticks. Output If you can make a bear from the given set, print string "Bear" (without the quotes). If you can make an elephant, print string "Elephant" (wıthout the quotes). If you can make neither a bear nor an elephant, print string "Alien" (without the quotes). Examples Input 4 2 5 4 4 4 Output Bear Input 4 4 5 4 4 5 Output Elephant Input 1 2 3 4 5 6 Output Alien Note If you're out of creative ideas, see instructions below which show how to make a bear and an elephant in the first two samples. The stick of length 2 is in red, the sticks of length 4 are in green, the sticks of length 5 are in blue. <image> Submitted Solution: ``` import math alph="abcdefghijklmnopqrstuvwxyz" #----------------------------------- l=list(map(int,input().split())) l.sort() if l[2]!=l[3]: print("Alien") else: k=l[2];t=l.count(k) if t>=4: for i in range(4): del(l[l.index(k)]) if l[0]==l[1]: print("Elephant") else: print("Bear") else: print("Alien") ```
instruction
0
38,060
3
76,120
Yes
output
1
38,060
3
76,121
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two polar bears Menshykov and Uslada from the St.Petersburg zoo and elephant Horace from the Kiev zoo got six sticks to play with and assess the animals' creativity. Menshykov, Uslada and Horace decided to make either an elephant or a bear from those sticks. They can make an animal from sticks in the following way: * Four sticks represent the animal's legs, these sticks should have the same length. * Two remaining sticks represent the animal's head and body. The bear's head stick must be shorter than the body stick. The elephant, however, has a long trunk, so his head stick must be as long as the body stick. Note that there are no limits on the relations between the leg sticks and the head and body sticks. Your task is to find out which animal can be made from the given stick set. The zoo keeper wants the sticks back after the game, so they must never be broken, even bears understand it. Input The single line contains six space-separated integers li (1 ≤ li ≤ 9) — the lengths of the six sticks. It is guaranteed that the input is such that you cannot make both animals from the sticks. Output If you can make a bear from the given set, print string "Bear" (without the quotes). If you can make an elephant, print string "Elephant" (wıthout the quotes). If you can make neither a bear nor an elephant, print string "Alien" (without the quotes). Examples Input 4 2 5 4 4 4 Output Bear Input 4 4 5 4 4 5 Output Elephant Input 1 2 3 4 5 6 Output Alien Note If you're out of creative ideas, see instructions below which show how to make a bear and an elephant in the first two samples. The stick of length 2 is in red, the sticks of length 4 are in green, the sticks of length 5 are in blue. <image> Submitted Solution: ``` s = input().split() print(([['Bear', 'Elephant'][len(set(s) - {x}) <= 1] for x in s if ' '.join(s).count(x) >= 4] + ['Alien'])[0]) ```
instruction
0
38,061
3
76,122
No
output
1
38,061
3
76,123
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two polar bears Menshykov and Uslada from the St.Petersburg zoo and elephant Horace from the Kiev zoo got six sticks to play with and assess the animals' creativity. Menshykov, Uslada and Horace decided to make either an elephant or a bear from those sticks. They can make an animal from sticks in the following way: * Four sticks represent the animal's legs, these sticks should have the same length. * Two remaining sticks represent the animal's head and body. The bear's head stick must be shorter than the body stick. The elephant, however, has a long trunk, so his head stick must be as long as the body stick. Note that there are no limits on the relations between the leg sticks and the head and body sticks. Your task is to find out which animal can be made from the given stick set. The zoo keeper wants the sticks back after the game, so they must never be broken, even bears understand it. Input The single line contains six space-separated integers li (1 ≤ li ≤ 9) — the lengths of the six sticks. It is guaranteed that the input is such that you cannot make both animals from the sticks. Output If you can make a bear from the given set, print string "Bear" (without the quotes). If you can make an elephant, print string "Elephant" (wıthout the quotes). If you can make neither a bear nor an elephant, print string "Alien" (without the quotes). Examples Input 4 2 5 4 4 4 Output Bear Input 4 4 5 4 4 5 Output Elephant Input 1 2 3 4 5 6 Output Alien Note If you're out of creative ideas, see instructions below which show how to make a bear and an elephant in the first two samples. The stick of length 2 is in red, the sticks of length 4 are in green, the sticks of length 5 are in blue. <image> Submitted Solution: ``` arr = sorted(list(map(int,input().split()))) if len(set(arr)) > 3: print('Alien') elif arr[4] == arr[5]: print('Elephant') else: print('Bear') ```
instruction
0
38,062
3
76,124
No
output
1
38,062
3
76,125
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two polar bears Menshykov and Uslada from the St.Petersburg zoo and elephant Horace from the Kiev zoo got six sticks to play with and assess the animals' creativity. Menshykov, Uslada and Horace decided to make either an elephant or a bear from those sticks. They can make an animal from sticks in the following way: * Four sticks represent the animal's legs, these sticks should have the same length. * Two remaining sticks represent the animal's head and body. The bear's head stick must be shorter than the body stick. The elephant, however, has a long trunk, so his head stick must be as long as the body stick. Note that there are no limits on the relations between the leg sticks and the head and body sticks. Your task is to find out which animal can be made from the given stick set. The zoo keeper wants the sticks back after the game, so they must never be broken, even bears understand it. Input The single line contains six space-separated integers li (1 ≤ li ≤ 9) — the lengths of the six sticks. It is guaranteed that the input is such that you cannot make both animals from the sticks. Output If you can make a bear from the given set, print string "Bear" (without the quotes). If you can make an elephant, print string "Elephant" (wıthout the quotes). If you can make neither a bear nor an elephant, print string "Alien" (without the quotes). Examples Input 4 2 5 4 4 4 Output Bear Input 4 4 5 4 4 5 Output Elephant Input 1 2 3 4 5 6 Output Alien Note If you're out of creative ideas, see instructions below which show how to make a bear and an elephant in the first two samples. The stick of length 2 is in red, the sticks of length 4 are in green, the sticks of length 5 are in blue. <image> Submitted Solution: ``` import math def fact(n): ans = 1 for i in range(2, n+1): ans*= i return ans def comb(n, c): return fact(n)//(fact(n-c)*c) l = list(map(int, input().split())) d = {} two = 0 for i in range(6): if(l[i] in d): d[l[i]]+=1 else: d[l[i]] = 1 if(len(d) > 3 or len(d)< 2): print('Alien') else: if(len(d)==2): if(2 not in list(d.values())): print('Alien') else: print('Elephant') else: if(4 not in list(d.values())): print('Alien') else: print('Bear') ```
instruction
0
38,063
3
76,126
No
output
1
38,063
3
76,127
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two polar bears Menshykov and Uslada from the St.Petersburg zoo and elephant Horace from the Kiev zoo got six sticks to play with and assess the animals' creativity. Menshykov, Uslada and Horace decided to make either an elephant or a bear from those sticks. They can make an animal from sticks in the following way: * Four sticks represent the animal's legs, these sticks should have the same length. * Two remaining sticks represent the animal's head and body. The bear's head stick must be shorter than the body stick. The elephant, however, has a long trunk, so his head stick must be as long as the body stick. Note that there are no limits on the relations between the leg sticks and the head and body sticks. Your task is to find out which animal can be made from the given stick set. The zoo keeper wants the sticks back after the game, so they must never be broken, even bears understand it. Input The single line contains six space-separated integers li (1 ≤ li ≤ 9) — the lengths of the six sticks. It is guaranteed that the input is such that you cannot make both animals from the sticks. Output If you can make a bear from the given set, print string "Bear" (without the quotes). If you can make an elephant, print string "Elephant" (wıthout the quotes). If you can make neither a bear nor an elephant, print string "Alien" (without the quotes). Examples Input 4 2 5 4 4 4 Output Bear Input 4 4 5 4 4 5 Output Elephant Input 1 2 3 4 5 6 Output Alien Note If you're out of creative ideas, see instructions below which show how to make a bear and an elephant in the first two samples. The stick of length 2 is in red, the sticks of length 4 are in green, the sticks of length 5 are in blue. <image> Submitted Solution: ``` class Counter(dict): def __missing__(self,key): return 0 c = Counter() for x in map(int, input().split()): c[x]+=1 d = Counter() for x in c: d[c[x]]+=1 if(d[4] == 1 and d[1] >= 2):print("Bear") elif(d[4] >= 1 and d[2] >= 1):print("Elephant") else:print("Alien") ```
instruction
0
38,064
3
76,128
No
output
1
38,064
3
76,129
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Okabe needs bananas for one of his experiments for some strange reason. So he decides to go to the forest and cut banana trees. Consider the point (x, y) in the 2D plane such that x and y are integers and 0 ≤ x, y. There is a tree in such a point, and it has x + y bananas. There are no trees nor bananas in other points. Now, Okabe draws a line with equation <image>. Okabe can select a single rectangle with axis aligned sides with all points on or under the line and cut all the trees in all points that are inside or on the border of this rectangle and take their bananas. Okabe's rectangle can be degenerate; that is, it can be a line segment or even a point. Help Okabe and find the maximum number of bananas he can get if he chooses the rectangle wisely. Okabe is sure that the answer does not exceed 1018. You can trust him. Input The first line of input contains two space-separated integers m and b (1 ≤ m ≤ 1000, 1 ≤ b ≤ 10000). Output Print the maximum number of bananas Okabe can get from the trees he cuts. Examples Input 1 5 Output 30 Input 2 3 Output 25 Note <image> The graph above corresponds to sample test 1. The optimal rectangle is shown in red and has 30 bananas. Submitted Solution: ``` m,b=input().split() m=int(m) b=int(b) #x_cept=m*b #y_cept=b max=0 for y in range (b+1): x=m*(b-y) cost=((x+1)*(y+1)*(x+y))//2 #print(f"At y={y}, cost is {cost}") if cost > max: max=cost else: break print(max) ```
instruction
0
38,241
3
76,482
Yes
output
1
38,241
3
76,483
Provide tags and a correct Python 3 solution for this coding contest problem. You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. <image> Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y). The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: * if s_i = U, you move to (x, y + 1); * if s_i = D, you move to (x, y - 1); * if s_i = R, you move to (x + 1, y); * if s_i = L, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions. Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers p_x and p_y (-10^5 ≤ p_x, p_y ≤ 10^5; (p_x, p_y) ≠ (0, 0)) — the coordinates of Planetforces (p_x, p_y). The second line contains the string s (1 ≤ |s| ≤ 10^5: |s| is the length of string s) — the list of orders. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5. Output For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR Output YES YES YES NO YES NO Note In the first case, you don't need to modify s, since the given s will bring you to Planetforces. In the second case, you can delete orders s_2, s_3, s_4, s_6, s_7 and s_8, so s becomes equal to "UR". In the third test case, you have to delete order s_9, otherwise, you won't finish in the position of Planetforces.
instruction
0
38,885
3
77,770
Tags: greedy, strings Correct Solution: ``` t = int(input()) for _ in range(t): px, py = map(int, input().split()) l = input() x = 0 y = 0 u, d, le, r = 0, 0, 0, 0 for i in range(len(l)): if l[i] == "U": y+=1 u+=1 elif l[i] == "D": y -= 1 d+=1 elif l[i] == "L": x -= 1 le += 1 elif l[i] == "R": x += 1 r += 1 if x == px and y == py: print("YES") else: flag = True if x < px and px - x > le: flag = False if y < py and py - y > d: flag = False if x > px and x - px > r: flag = False if y > py and y - py > u: flag = False if flag: print("YES") else: print("NO") ```
output
1
38,885
3
77,771
Provide tags and a correct Python 3 solution for this coding contest problem. You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. <image> Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y). The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: * if s_i = U, you move to (x, y + 1); * if s_i = D, you move to (x, y - 1); * if s_i = R, you move to (x + 1, y); * if s_i = L, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions. Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers p_x and p_y (-10^5 ≤ p_x, p_y ≤ 10^5; (p_x, p_y) ≠ (0, 0)) — the coordinates of Planetforces (p_x, p_y). The second line contains the string s (1 ≤ |s| ≤ 10^5: |s| is the length of string s) — the list of orders. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5. Output For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR Output YES YES YES NO YES NO Note In the first case, you don't need to modify s, since the given s will bring you to Planetforces. In the second case, you can delete orders s_2, s_3, s_4, s_6, s_7 and s_8, so s becomes equal to "UR". In the third test case, you have to delete order s_9, otherwise, you won't finish in the position of Planetforces.
instruction
0
38,886
3
77,772
Tags: greedy, strings Correct Solution: ``` for _ in range(int(input())): x,y = map(int,input().split()) s = list(input()) p = 0 q = 0 if x>0 and y>0: for i in s: if i=='R': p += 1 elif i=='U': q += 1 if p>=x and q>=y: print('YES') else: print('NO') elif x<0 and y>0: for i in s: if i=='L': p += 1 elif i=='U': q += 1 if p>=abs(x) and q>=abs(y): print('YES') else: print('NO') elif x>0 and y<0: for i in s: if i=='R': p += 1 elif i=='D': q += 1 if p>=abs(x) and q>=abs(y): print('YES') else: print('NO') elif x<0 and y<0: for i in s: if i=='L': p += 1 elif i=='D': q += 1 if p>=abs(x) and q>=abs(y): print('YES') else: print('NO') elif x==0 : for i in s: if (y>0 and i=='U') or (y<0 and i=='D'): q += 1 if q>=abs(y): print('YES') else: print('NO') elif y==0: for i in s: if (x>0 and i=='R') or (x<0 and i=='L'): p += 1 if p>=abs(x): print('YES') else: print('NO') ```
output
1
38,886
3
77,773
Provide tags and a correct Python 3 solution for this coding contest problem. You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. <image> Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y). The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: * if s_i = U, you move to (x, y + 1); * if s_i = D, you move to (x, y - 1); * if s_i = R, you move to (x + 1, y); * if s_i = L, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions. Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers p_x and p_y (-10^5 ≤ p_x, p_y ≤ 10^5; (p_x, p_y) ≠ (0, 0)) — the coordinates of Planetforces (p_x, p_y). The second line contains the string s (1 ≤ |s| ≤ 10^5: |s| is the length of string s) — the list of orders. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5. Output For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR Output YES YES YES NO YES NO Note In the first case, you don't need to modify s, since the given s will bring you to Planetforces. In the second case, you can delete orders s_2, s_3, s_4, s_6, s_7 and s_8, so s becomes equal to "UR". In the third test case, you have to delete order s_9, otherwise, you won't finish in the position of Planetforces.
instruction
0
38,887
3
77,774
Tags: greedy, strings Correct Solution: ``` #!/usr/bin/python3.6 import sys input = sys.stdin.readline t = int(input()) for _ in range(t): x, y = map(int, input().split()) s = input().rstrip() cx, cy = 0, 0 dx, dy = None, None if x >= 0: dx = "R" else: dx = "L" if y >= 0: dy = "U" else: dy = "D" ok = False for ch in s: if ch == "U" and dy == "U": cy += 1 elif ch == "D" and dy == "D": cy -= 1 elif ch == "R" and dx == "R": cx += 1 elif ch == "L" and dx == "L": cx -= 1 if abs(cx) >= abs(x) and abs(cy) >= abs(y): ok = True if ok: print("YES") else: print("NO") ```
output
1
38,887
3
77,775
Provide tags and a correct Python 3 solution for this coding contest problem. You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. <image> Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y). The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: * if s_i = U, you move to (x, y + 1); * if s_i = D, you move to (x, y - 1); * if s_i = R, you move to (x + 1, y); * if s_i = L, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions. Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers p_x and p_y (-10^5 ≤ p_x, p_y ≤ 10^5; (p_x, p_y) ≠ (0, 0)) — the coordinates of Planetforces (p_x, p_y). The second line contains the string s (1 ≤ |s| ≤ 10^5: |s| is the length of string s) — the list of orders. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5. Output For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR Output YES YES YES NO YES NO Note In the first case, you don't need to modify s, since the given s will bring you to Planetforces. In the second case, you can delete orders s_2, s_3, s_4, s_6, s_7 and s_8, so s becomes equal to "UR". In the third test case, you have to delete order s_9, otherwise, you won't finish in the position of Planetforces.
instruction
0
38,888
3
77,776
Tags: greedy, strings Correct Solution: ``` T= int(input()) for i in range(T): a,b=input().split() s=input() a=int(a) b=int(b) destination =[a,b] commands={"L":0,"D":0,"U":0,"R":0} required_l=0 required_r=0 required_u=0 required_d=0 for alph in s: commands[alph]+=1 if(destination[0]-0 <0): required_l=0-destination[0] if(destination[0]-0>=0): required_r=destination[0] if(destination[1]-0<0): required_d=0-destination[1] if(destination[1]-0>=0): required_u=destination[1] if(required_l<=commands["L"] and required_d<=commands["D"] and required_r<=commands["R"] and required_u<=commands["U"] ): print("YES") else: print("NO") ```
output
1
38,888
3
77,777
Provide tags and a correct Python 3 solution for this coding contest problem. You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. <image> Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y). The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: * if s_i = U, you move to (x, y + 1); * if s_i = D, you move to (x, y - 1); * if s_i = R, you move to (x + 1, y); * if s_i = L, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions. Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers p_x and p_y (-10^5 ≤ p_x, p_y ≤ 10^5; (p_x, p_y) ≠ (0, 0)) — the coordinates of Planetforces (p_x, p_y). The second line contains the string s (1 ≤ |s| ≤ 10^5: |s| is the length of string s) — the list of orders. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5. Output For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR Output YES YES YES NO YES NO Note In the first case, you don't need to modify s, since the given s will bring you to Planetforces. In the second case, you can delete orders s_2, s_3, s_4, s_6, s_7 and s_8, so s becomes equal to "UR". In the third test case, you have to delete order s_9, otherwise, you won't finish in the position of Planetforces.
instruction
0
38,889
3
77,778
Tags: greedy, strings Correct Solution: ``` t=int(input()) for i in range(t): n=tuple(map(int,input().split(' '))) s=input() px=0 py=0 nx=0 ny=0 for a in s: if a=='U': py+=1 elif a=='R': px+=1 elif a=='L': nx+=1 elif a=='D': ny+=1 flagx=0 flagy=0 if n[0]>=0: if px>=n[0]: flagx=1 else: if nx>=-n[0]: flagx=1 if n[1]>=0: if py>=n[1]: flagy=1 else: if ny>=-n[1]: flagy=1 if flagx==1 and flagy==1: print('YES') else: print('NO') ```
output
1
38,889
3
77,779
Provide tags and a correct Python 3 solution for this coding contest problem. You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. <image> Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y). The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: * if s_i = U, you move to (x, y + 1); * if s_i = D, you move to (x, y - 1); * if s_i = R, you move to (x + 1, y); * if s_i = L, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions. Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers p_x and p_y (-10^5 ≤ p_x, p_y ≤ 10^5; (p_x, p_y) ≠ (0, 0)) — the coordinates of Planetforces (p_x, p_y). The second line contains the string s (1 ≤ |s| ≤ 10^5: |s| is the length of string s) — the list of orders. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5. Output For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR Output YES YES YES NO YES NO Note In the first case, you don't need to modify s, since the given s will bring you to Planetforces. In the second case, you can delete orders s_2, s_3, s_4, s_6, s_7 and s_8, so s becomes equal to "UR". In the third test case, you have to delete order s_9, otherwise, you won't finish in the position of Planetforces.
instruction
0
38,890
3
77,780
Tags: greedy, strings Correct Solution: ``` def solve(px, py, s): if px > 0: if s.count('R') < px: return False else: if s.count('L') < -px: return False if py > 0: if s.count('U') < py: return False else: if s.count('D') < -py: return False return True def driver(): t = int(input()) for _ in range(t): px, py = map(int, input().split()) s = input() if solve(px, py, s): print('YES') else: print('NO') def test(): assert solve(10, 5, 'RRRRRRRRRRUUUUU') assert solve(1, 1, 'UDDDRLLL') assert solve(-3, -5, 'LDLDLDDDR') assert not solve(1, 2, 'LLLLUU') assert solve(3, -2, 'RDULRLLDR') assert not solve(-1, 6, 'RUDURUUUUR') if __name__ == '__main__': driver() # test() ```
output
1
38,890
3
77,781
Provide tags and a correct Python 3 solution for this coding contest problem. You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. <image> Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y). The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: * if s_i = U, you move to (x, y + 1); * if s_i = D, you move to (x, y - 1); * if s_i = R, you move to (x + 1, y); * if s_i = L, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions. Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers p_x and p_y (-10^5 ≤ p_x, p_y ≤ 10^5; (p_x, p_y) ≠ (0, 0)) — the coordinates of Planetforces (p_x, p_y). The second line contains the string s (1 ≤ |s| ≤ 10^5: |s| is the length of string s) — the list of orders. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5. Output For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR Output YES YES YES NO YES NO Note In the first case, you don't need to modify s, since the given s will bring you to Planetforces. In the second case, you can delete orders s_2, s_3, s_4, s_6, s_7 and s_8, so s becomes equal to "UR". In the third test case, you have to delete order s_9, otherwise, you won't finish in the position of Planetforces.
instruction
0
38,891
3
77,782
Tags: greedy, strings Correct Solution: ``` import sys import os import math import copy from bisect import bisect from io import BytesIO, IOBase from math import sqrt,floor,factorial,gcd,log,ceil from collections import deque,Counter,defaultdict from itertools import permutations,combinations,accumulate def Int(): return int(sys.stdin.readline()) def Mint(): return map(int,sys.stdin.readline().split()) def Lstr(): return list(sys.stdin.readline().strip()) def Str(): return sys.stdin.readline().strip() def Mstr(): return map(str,sys.stdin.readline().strip().split()) def List(): return list(map(int,sys.stdin.readline().split())) def Hash(): return dict() def Mod(): return 1000000007 def Mat2x2(n): return [List() for _ in range(n)] def Lcm(x,y): return (x*y)//gcd(x,y) def dtob(n): return bin(n).replace("0b","") def btod(n): return int(n,2) def watch(x): return print(x) def common(l1, l2): return set(l1).intersection(l2) def Most_frequent(list): return max(set(list), key = list.count) def solution(): for _ in range(Int()): x,y=Mint() s=Lstr() x1,y1=0,0 p=0 for i in range(len(s)): if(s[i]=='U' and y1<y): y1+=1 elif(s[i]=='L' and x1>x): x1-=1 elif(s[i]=='R' and x1<x): x1+=1 elif(s[i]=='D' and y1>y): y1-=1 if(x1==x and y1==y): print("YES") else: print("NO") if __name__ == "__main__": solution() ```
output
1
38,891
3
77,783
Provide tags and a correct Python 3 solution for this coding contest problem. You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. <image> Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y). The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: * if s_i = U, you move to (x, y + 1); * if s_i = D, you move to (x, y - 1); * if s_i = R, you move to (x + 1, y); * if s_i = L, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions. Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers p_x and p_y (-10^5 ≤ p_x, p_y ≤ 10^5; (p_x, p_y) ≠ (0, 0)) — the coordinates of Planetforces (p_x, p_y). The second line contains the string s (1 ≤ |s| ≤ 10^5: |s| is the length of string s) — the list of orders. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5. Output For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR Output YES YES YES NO YES NO Note In the first case, you don't need to modify s, since the given s will bring you to Planetforces. In the second case, you can delete orders s_2, s_3, s_4, s_6, s_7 and s_8, so s becomes equal to "UR". In the third test case, you have to delete order s_9, otherwise, you won't finish in the position of Planetforces.
instruction
0
38,892
3
77,784
Tags: greedy, strings Correct Solution: ``` t = (int)(input()) while t: m,n = map(int,input().split()) string = list(input()) cnt = 0 if(m - 0 < 0) : cnt=string.count('L') else: cnt=string.count('R') cnt1 =0 if(n-0<0): cnt1=string.count('D') else: cnt1=string.count('U') if cnt >= abs(m) and cnt1>=abs(n): print("YES") else : print("NO") t-=1 ```
output
1
38,892
3
77,785
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. <image> Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y). The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: * if s_i = U, you move to (x, y + 1); * if s_i = D, you move to (x, y - 1); * if s_i = R, you move to (x + 1, y); * if s_i = L, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions. Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers p_x and p_y (-10^5 ≤ p_x, p_y ≤ 10^5; (p_x, p_y) ≠ (0, 0)) — the coordinates of Planetforces (p_x, p_y). The second line contains the string s (1 ≤ |s| ≤ 10^5: |s| is the length of string s) — the list of orders. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5. Output For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR Output YES YES YES NO YES NO Note In the first case, you don't need to modify s, since the given s will bring you to Planetforces. In the second case, you can delete orders s_2, s_3, s_4, s_6, s_7 and s_8, so s becomes equal to "UR". In the third test case, you have to delete order s_9, otherwise, you won't finish in the position of Planetforces. Submitted Solution: ``` for _ in range(int(input())): x,y=[int(x) for x in input().split()] s=input() l,r,u,d,f,ss=0,0,0,0,0,0 for _ in range(len(s)): if s[_]=='U': u+=1 elif s[_]=='D': d-=1 elif s[_]=='R': r+=1 else: l-=1 if x<0 and l<=x: f=1 if x>=0 and r>=x: f=1 if y<0 and d<=y: ss=1 if y>=0 and u>=y: ss=1 if ss==1 and f==1: print("YES") else: print('NO') ```
instruction
0
38,893
3
77,786
Yes
output
1
38,893
3
77,787
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. <image> Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y). The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: * if s_i = U, you move to (x, y + 1); * if s_i = D, you move to (x, y - 1); * if s_i = R, you move to (x + 1, y); * if s_i = L, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions. Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers p_x and p_y (-10^5 ≤ p_x, p_y ≤ 10^5; (p_x, p_y) ≠ (0, 0)) — the coordinates of Planetforces (p_x, p_y). The second line contains the string s (1 ≤ |s| ≤ 10^5: |s| is the length of string s) — the list of orders. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5. Output For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR Output YES YES YES NO YES NO Note In the first case, you don't need to modify s, since the given s will bring you to Planetforces. In the second case, you can delete orders s_2, s_3, s_4, s_6, s_7 and s_8, so s becomes equal to "UR". In the third test case, you have to delete order s_9, otherwise, you won't finish in the position of Planetforces. Submitted Solution: ``` t = int(input()) for i in range(t): U = 0 D = 0 R = 0 L = 0 Px, Py = map(int, input().split()) s = input() for j in s: if j == 'U': U += 1 elif j == 'D': D -= 1 elif j == 'R': R += 1 elif j == 'L': L -= 1 if (D <= Py <= U) and (L <= Px <= R): print('YES') else: print('NO') ```
instruction
0
38,894
3
77,788
Yes
output
1
38,894
3
77,789
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. <image> Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y). The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: * if s_i = U, you move to (x, y + 1); * if s_i = D, you move to (x, y - 1); * if s_i = R, you move to (x + 1, y); * if s_i = L, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions. Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers p_x and p_y (-10^5 ≤ p_x, p_y ≤ 10^5; (p_x, p_y) ≠ (0, 0)) — the coordinates of Planetforces (p_x, p_y). The second line contains the string s (1 ≤ |s| ≤ 10^5: |s| is the length of string s) — the list of orders. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5. Output For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR Output YES YES YES NO YES NO Note In the first case, you don't need to modify s, since the given s will bring you to Planetforces. In the second case, you can delete orders s_2, s_3, s_4, s_6, s_7 and s_8, so s becomes equal to "UR". In the third test case, you have to delete order s_9, otherwise, you won't finish in the position of Planetforces. Submitted Solution: ``` from collections import Counter numOfTests = int(input()) for i in range(numOfTests): x, y = map(int, input().split()) steps = input() counts = Counter(steps) if x >= 0 and y >= 0: if counts['R'] >= x and counts['U'] >= y: print('YES') else: print('NO') elif x <= 0 and y <= 0: if counts['L'] >= abs(x) and counts['D'] >= abs(y): print('YES') else: print('NO') elif x >= 0 and y <= 0: if counts['R'] >= x and counts['D'] >= abs(y): print('YES') else: print('NO') elif x <= 0 and y >= 0: if counts['L'] >= abs(x) and counts['U'] >= y: print('YES') else: print('NO') ```
instruction
0
38,895
3
77,790
Yes
output
1
38,895
3
77,791
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. <image> Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y). The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: * if s_i = U, you move to (x, y + 1); * if s_i = D, you move to (x, y - 1); * if s_i = R, you move to (x + 1, y); * if s_i = L, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions. Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers p_x and p_y (-10^5 ≤ p_x, p_y ≤ 10^5; (p_x, p_y) ≠ (0, 0)) — the coordinates of Planetforces (p_x, p_y). The second line contains the string s (1 ≤ |s| ≤ 10^5: |s| is the length of string s) — the list of orders. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5. Output For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR Output YES YES YES NO YES NO Note In the first case, you don't need to modify s, since the given s will bring you to Planetforces. In the second case, you can delete orders s_2, s_3, s_4, s_6, s_7 and s_8, so s becomes equal to "UR". In the third test case, you have to delete order s_9, otherwise, you won't finish in the position of Planetforces. Submitted Solution: ``` T=int(input()) for t in range(T): px,py=[int(x) for x in input().split()] s=input() l=r=u=d=0 for i in s: if i=='R': r+=1 if i=='L': l-=1 if i=='U': u+=1 if i=='D': d-=1 print("YES" if l<=px<=r and d<=py<=u else "NO") ```
instruction
0
38,896
3
77,792
Yes
output
1
38,896
3
77,793
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. <image> Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y). The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: * if s_i = U, you move to (x, y + 1); * if s_i = D, you move to (x, y - 1); * if s_i = R, you move to (x + 1, y); * if s_i = L, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions. Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers p_x and p_y (-10^5 ≤ p_x, p_y ≤ 10^5; (p_x, p_y) ≠ (0, 0)) — the coordinates of Planetforces (p_x, p_y). The second line contains the string s (1 ≤ |s| ≤ 10^5: |s| is the length of string s) — the list of orders. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5. Output For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR Output YES YES YES NO YES NO Note In the first case, you don't need to modify s, since the given s will bring you to Planetforces. In the second case, you can delete orders s_2, s_3, s_4, s_6, s_7 and s_8, so s becomes equal to "UR". In the third test case, you have to delete order s_9, otherwise, you won't finish in the position of Planetforces. Submitted Solution: ``` t = int(input()) for _ in range(t): x, y = map(int, input().split()) str1 = input() l = 0 d = 0 u = 0 r = 0 for i in str1: if (i == 'L'): l += 1 elif (i == 'D'): d += 1 elif (i == 'U'): u += 1 elif (i == 'R'): r += 1 ans1 = False ans2 = False if (y < 0): if (d >= abs(y)): ans1 = True else: ans1 = False if (y > 0): if (u >= abs(y)): ans1 = True else: ans1 = False if (x < 0): if (l >= abs(x)): ans2 = True else: ans2 = False if (x > 0): if (r >= abs(x)): ans2 = True else: ans2 = False if (x == 0 and y == 0): ans1 = True ans2 = True if (ans1 and ans2): print('YES') else: print('NO') ```
instruction
0
38,897
3
77,794
No
output
1
38,897
3
77,795
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. <image> Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y). The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: * if s_i = U, you move to (x, y + 1); * if s_i = D, you move to (x, y - 1); * if s_i = R, you move to (x + 1, y); * if s_i = L, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions. Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers p_x and p_y (-10^5 ≤ p_x, p_y ≤ 10^5; (p_x, p_y) ≠ (0, 0)) — the coordinates of Planetforces (p_x, p_y). The second line contains the string s (1 ≤ |s| ≤ 10^5: |s| is the length of string s) — the list of orders. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5. Output For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR Output YES YES YES NO YES NO Note In the first case, you don't need to modify s, since the given s will bring you to Planetforces. In the second case, you can delete orders s_2, s_3, s_4, s_6, s_7 and s_8, so s becomes equal to "UR". In the third test case, you have to delete order s_9, otherwise, you won't finish in the position of Planetforces. Submitted Solution: ``` for t in range(int(input())): n,m=map(int,input().split()) s=input() r=s.count("R") l=s.count("L") u=s.count("U") d=s.count("D") if n>0 and m>0: if r>=n and u>=m: print("YES") else: print("NO") elif n>0 and m<0: if r>=n and d>=abs(m): print("YES") else: print("NO") elif n<0 and m>0: if l>=abs(n) and u>=m: print("YES") else: print("NO") else: if l>=abs(n) and d>=abs(m): print("YES") else: print("NO") ```
instruction
0
38,898
3
77,796
No
output
1
38,898
3
77,797
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. <image> Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y). The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: * if s_i = U, you move to (x, y + 1); * if s_i = D, you move to (x, y - 1); * if s_i = R, you move to (x + 1, y); * if s_i = L, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions. Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers p_x and p_y (-10^5 ≤ p_x, p_y ≤ 10^5; (p_x, p_y) ≠ (0, 0)) — the coordinates of Planetforces (p_x, p_y). The second line contains the string s (1 ≤ |s| ≤ 10^5: |s| is the length of string s) — the list of orders. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5. Output For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR Output YES YES YES NO YES NO Note In the first case, you don't need to modify s, since the given s will bring you to Planetforces. In the second case, you can delete orders s_2, s_3, s_4, s_6, s_7 and s_8, so s becomes equal to "UR". In the third test case, you have to delete order s_9, otherwise, you won't finish in the position of Planetforces. Submitted Solution: ``` t = int(input()) o = [] v, h = 0, 0 for i in range(t): x, y = map(int, input().split()) s = input() if x >= 0 and y > 0: v, h = 'U', 'R' elif x > 0 and y < 0: v, h = 'D', 'R' elif x < 0 and y >= 0: v, h = 'U', 'L' elif x < 0 and y < 0: v, h = 'D', 'L' v_sum = 0 h_sum = 0 flag = 0 for i in range(len(s)): if s[i] == v: v_sum += 1 elif s[i] == h: h_sum += 1 if v_sum >= abs(y) and h_sum >= abs(x): flag = 1 o.append("YES") break if flag == 0: o.append("NO") for i in range(len(o)): print(o[i]) ```
instruction
0
38,899
3
77,798
No
output
1
38,899
3
77,799
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You were dreaming that you are traveling to a planet named Planetforces on your personal spaceship. Unfortunately, its piloting system was corrupted and now you need to fix it in order to reach Planetforces. <image> Space can be represented as the XY plane. You are starting at point (0, 0), and Planetforces is located in point (p_x, p_y). The piloting system of your spaceship follows its list of orders which can be represented as a string s. The system reads s from left to right. Suppose you are at point (x, y) and current order is s_i: * if s_i = U, you move to (x, y + 1); * if s_i = D, you move to (x, y - 1); * if s_i = R, you move to (x + 1, y); * if s_i = L, you move to (x - 1, y). Since string s could be corrupted, there is a possibility that you won't reach Planetforces in the end. Fortunately, you can delete some orders from s but you can't change their positions. Can you delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces after the system processes all orders? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers p_x and p_y (-10^5 ≤ p_x, p_y ≤ 10^5; (p_x, p_y) ≠ (0, 0)) — the coordinates of Planetforces (p_x, p_y). The second line contains the string s (1 ≤ |s| ≤ 10^5: |s| is the length of string s) — the list of orders. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5. Output For each test case, print "YES" if you can delete several orders (possibly, zero) from s in such a way, that you'll reach Planetforces. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 10 5 RRRRRRRRRRUUUUU 1 1 UDDDRLLL -3 -5 LDLDLDDDR 1 2 LLLLUU 3 -2 RDULRLLDR -1 6 RUDURUUUUR Output YES YES YES NO YES NO Note In the first case, you don't need to modify s, since the given s will bring you to Planetforces. In the second case, you can delete orders s_2, s_3, s_4, s_6, s_7 and s_8, so s becomes equal to "UR". In the third test case, you have to delete order s_9, otherwise, you won't finish in the position of Planetforces. Submitted Solution: ``` # Importing the necessary module(s) from sys import stdin # Using 'read' as alias for stdin.readline() method for Faster I/O (Here, Input) read = stdin.readline debug = False for testcase in range(int(read())): # length = int(read()) # random.randint(3, 100) coordinates = list(map(int, read().split())) # [ random.randint(0, int(1e9)) for i in range(length) ] string = read().strip() Ucounts = Dcounts = Lcounts = Rcounts = 0 for character in string: if character=="U": Ucounts+=1 elif character=="D": Dcounts+=1 elif character=="L": Lcounts+=1 elif character=="R": Rcounts+=1 if debug: print(Ucounts, Dcounts, Rcounts, Lcounts, (coordinates[0]>0 and coordinates[0] > Rcounts), (coordinates[0]<0 and abs(coordinates[0]) > Lcounts), (coordinates[1]>0 and coordinates[1] > Ucounts), (coordinates[1] < 0 and abs(coordinates[1]) < Dcounts)) print(["YES", "NO"][(coordinates[0]>0 and coordinates[0] > Rcounts) or (coordinates[0]<0 and abs(coordinates[0]) > Lcounts) or (coordinates[1]>0 and coordinates[1] > Ucounts) or (coordinates[1] < 0 and abs(coordinates[1]) < Dcounts)] ) ```
instruction
0
38,900
3
77,800
No
output
1
38,900
3
77,801
Provide tags and a correct Python 3 solution for this coding contest problem. Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. Input The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Output 7 Input 3 1 1 2 3 0 1 3 0 Output -1 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates.
instruction
0
38,949
3
77,898
Tags: binary search, data structures, graphs, shortest paths Correct Solution: ``` from heapq import * INF = 1e16 viajantes = [] N,M = map(int,input().split()) galaxia = [[] for i in range(100002)] tempo = [INF] * (N+1) for i in range(M): a,b,c = map(int,input().split()) galaxia[a] += [(c,b)] galaxia[b] += [(c,a)] for j in range(N): vi = list(map(int,input().split())) viajantes.append(vi[1::]) def visitante_chegando(w,a): for x in viajantes[a-1]: if x == w: w += 1 return w def dijsktra(u,v,galaxia): tempo[u] = 0 q = [(0,u)] while q: z,a = heappop(q) #a[0] = distância #a[1] = vertice #tempo[a[1]] melhor tempo que cheguei em a[1] #if a[0] == tempo[a[1]]: if z <= tempo[a]: w2 = tempo[a] w2 = visitante_chegando(w2,a) for adj in galaxia[a]: w,b = w2 + adj[0],adj[1] #print(w) if w < tempo[b]: tempo[b] = w heappush(q,(tempo[b],b)) return tempo d = dijsktra(1,N,galaxia) if d[-1] == INF: print(-1) else: print(d[N]) ```
output
1
38,949
3
77,899
Provide tags and a correct Python 3 solution for this coding contest problem. Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. Input The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Output 7 Input 3 1 1 2 3 0 1 3 0 Output -1 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates.
instruction
0
38,950
3
77,900
Tags: binary search, data structures, graphs, shortest paths Correct Solution: ``` from heapq import * from math import inf from sys import stdout, stdin n, m = map(int, stdin.readline().split()) gr = [[] for _ in range(n + 1)] d = [inf] * (n + 1) d[1] = 0 times = [] for i in range(m): a, b, c = map(int, stdin.readline().split()) gr[a].append((b, c)) gr[b].append((a, c)) for j in range(n): a = list(map(int, stdin.readline().split())) times.append(a[1:]) q = [(0, 1)] def abacaba(t, i): for x in times[i - 1]: if x == t: t += 1 return t while q: z, v = heappop(q) if z <= d[v]: w = abacaba(d[v], v) for u in gr[v]: s, b = w + u[1], u[0] if s < d[b]: d[b] = s heappush(q, (d[b], b)) if d[-1] != inf: print(d[-1]) else: print(-1) ```
output
1
38,950
3
77,901
Provide tags and a correct Python 3 solution for this coding contest problem. Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. Input The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Output 7 Input 3 1 1 2 3 0 1 3 0 Output -1 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates.
instruction
0
38,951
3
77,902
Tags: binary search, data structures, graphs, shortest paths Correct Solution: ``` from sys import stdin,stdout from collections import defaultdict from heapq import heapify,heappop,heappush nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int,stdin.readline().split())) PI=float('inf') def dijsktra(): dist=[PI]*(1+n) dist[1]=0 # vis=[0]*(1+n) hp=[] heappush(hp,(0,1)) while hp: curDis,curNode=heappop(hp) # if vis[curNode]:continue # vis[curNode]=1 for neigh,wt in g[curNode]: # if vis[neigh]:continue eff_wt=wt if curDis in t[curNode]:eff_wt+=t[curNode][curDis]-curDis if curDis+eff_wt<dist[neigh]: dist[neigh]=curDis+eff_wt heappush(hp,(curDis+eff_wt,neigh)) # print(dist) return dist[n] for _ in range(1):#nmbr()): n,eg=lst() g=defaultdict(list) for i in range(eg): u,v,wt=lst() g[u]+=[[v,wt]] g[v]+=[[u,wt]] t=[{} for _ in range(1+n)] for ii in range(1,1+n): aa=lst() if aa[0]!=0: for i in range(aa[0],0,-1): if aa[i]+1 in t[ii]:t[ii][aa[i]]=t[ii][aa[i]+1] else:t[ii][aa[i]]=aa[i]+1 # print(t) ans=dijsktra() print(ans if ans!=PI else -1) # 8 10 # 1 2 3 # 2 8 3 # 1 4 1 # 4 3 6 # 3 7 7 # 4 5 5 # 5 7 2 # 7 8 1 # 1 6 8 # 6 8 7 # 0 # 4 1 2 3 4 # 0 # 0 # 0 # 0 # 0 # 0 ```
output
1
38,951
3
77,903
Provide tags and a correct Python 3 solution for this coding contest problem. Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. Input The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Output 7 Input 3 1 1 2 3 0 1 3 0 Output -1 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates.
instruction
0
38,952
3
77,904
Tags: binary search, data structures, graphs, shortest paths Correct Solution: ``` from heapq import * from sys import stdin, stdout n, m = map(int, stdin.readline().split()) gr = [[] for _ in range(n)] for i in range(m): u, v, w = map(int, stdin.readline().split()) u -= 1 v -= 1 gr[u].append((v, w)) gr[v].append((u, w)) heap = [(0, 0)] inf = int(2e12) d = [inf] * n d[0] = 0 time = [list(map(int, stdin.readline().split()))[1:] for _ in range(n)] while heap: dst, ind = heappop(heap) if dst != d[ind]: continue cnt = d[ind] for x in time[ind]: if x == cnt: cnt += 1 for u, w in gr[ind]: if d[u] > d[ind] + w: d[u] = min(d[u], cnt + w) heappush(heap, (d[u], u)) if d[-1] * 2 > inf: stdout.write("-1") else: stdout.write(str(d[-1])) ```
output
1
38,952
3
77,905
Provide tags and a correct Python 3 solution for this coding contest problem. Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. Input The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Output 7 Input 3 1 1 2 3 0 1 3 0 Output -1 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates.
instruction
0
38,953
3
77,906
Tags: binary search, data structures, graphs, shortest paths Correct Solution: ``` from heapq import * from sys import stdin, stdout from bisect import * n, m2 = map(int, stdin.readline().split()) gr = [[] for _ in range(n)] for i in range(m2): u, v, w = map(int, stdin.readline().split()) u -= 1 v -= 1 gr[u].append((v, w)) gr[v].append((u, w)) heap = [(0, 0)] inf = int(2e12) d = [inf] * n d[0] = 0 time = [list(map(int, stdin.readline().split())) for _ in range(n)] while heap: dst, ind = heappop(heap) if dst != d[ind]: continue for u, w in gr[ind]: if d[u] > d[ind] + w: tmp = d[ind] time[ind][0] = -3 m1 = bisect(time[ind], tmp) - 1 if time[ind][m1] == tmp: l = m1 r = len(time[ind]) while r - l > 1: m = (l + r) // 2 if time[ind][m] == tmp + m - m1: l = m else: r = m else: r = m1 d[u] = min(d[u], d[ind] + r - m1 + w) heappush(heap, (d[u], u)) if d[-1] * 2 > inf: stdout.write("-1") else: stdout.write(str(d[-1])) ```
output
1
38,953
3
77,907
Provide tags and a correct Python 3 solution for this coding contest problem. Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. Input The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Output 7 Input 3 1 1 2 3 0 1 3 0 Output -1 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates.
instruction
0
38,954
3
77,908
Tags: binary search, data structures, graphs, shortest paths Correct Solution: ``` from heapq import * from sys import stdin, stdout from bisect import * n, m2 = map(int, stdin.readline().split()) gr = [[] for _ in range(n)] for i in range(m2): u, v, w = map(int, stdin.readline().split()) u -= 1 v -= 1 gr[u].append((v, w)) gr[v].append((u, w)) heap = [(0, 0)] inf = int(2e12) d = [inf] * n d[0] = 0 time = [list(map(int, stdin.readline().split())) for _ in range(n)] while heap: dst, ind = heappop(heap) if dst != d[ind]: continue tmp = d[ind] time[ind][0] = -3 m1 = bisect(time[ind], tmp) - 1 l = m1 r = len(time[ind]) if time[ind][m1] == tmp: l = m1 else: r = m1 while r - l > 1: m = (l + r) // 2 if time[ind][m] == tmp + m - m1: l = m else: r = m for u, w in gr[ind]: if d[u] > d[ind] + w: tmp = d[ind] d[u] = min(d[u], d[ind] + r - m1 + w) heappush(heap, (d[u], u)) if d[-1] * 2 > inf: stdout.write("-1") else: stdout.write(str(d[-1])) ```
output
1
38,954
3
77,909
Provide tags and a correct Python 3 solution for this coding contest problem. Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. Input The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Output 7 Input 3 1 1 2 3 0 1 3 0 Output -1 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates.
instruction
0
38,955
3
77,910
Tags: binary search, data structures, graphs, shortest paths Correct Solution: ``` import heapq import collections from sys import stdin, stdout input = stdin.readline def dijkstra(adjacency_list, src, v, halt): dist = {i:-1 for i in range(v+1)} minHeap = [] dist[src] = 0 heapq.heappush(minHeap, [dist[src], src]) while(minHeap): first = heapq.heappop(minHeap) first_dist = first[0] first_vertex = first[1] if first_dist == dist[first_vertex]: while first_dist in halt[first_vertex]: first_dist+=1 for i in adjacency_list[first_vertex]: i_vertex = i[0] i_weight = i[1] if dist[i_vertex] == -1: dist[i_vertex] = first_dist + i_weight heapq.heappush(minHeap, [dist[i_vertex], i_vertex]) elif first_dist + i_weight < dist[i_vertex]: dist[i_vertex] = first_dist + i_weight heapq.heappush(minHeap, [dist[i_vertex], i_vertex]) return dist[n] n, m = map(int, input().split()) d = collections.defaultdict(list) halt = [0] for _ in range(m): a, b, w = map(int, input().split()) d[a].append([b, w]) d[b].append([a, w]) for _ in range(n): z = set(list(map(int, input().split()))[1:]) halt.append(z) print(dijkstra(d, 1, n, halt)) ```
output
1
38,955
3
77,911
Provide tags and a correct Python 3 solution for this coding contest problem. Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. Input The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Output 7 Input 3 1 1 2 3 0 1 3 0 Output -1 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates.
instruction
0
38,956
3
77,912
Tags: binary search, data structures, graphs, shortest paths Correct Solution: ``` from sys import stdin,stdout from collections import defaultdict from heapq import heapify,heappop,heappush nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int,stdin.readline().split())) PI=float('inf') def dijsktra(): dist=[PI]*(1+n) dist[1]=0 vis=[0]*(1+n) hp=[] heappush(hp,(0,1)) while hp: curDis,curNode=heappop(hp) if vis[curNode]:continue vis[curNode]=1 for neigh,wt in g[curNode]: if vis[neigh]:continue eff_wt=wt if curDis in t[curNode]:eff_wt+=t[curNode][curDis]-curDis if curDis+eff_wt<dist[neigh]: dist[neigh]=curDis+eff_wt heappush(hp,(curDis+eff_wt,neigh)) # print(dist) return dist[n] for _ in range(1):#nmbr()): n,eg=lst() g=defaultdict(list) for i in range(eg): u,v,wt=lst() g[u]+=[[v,wt]] g[v]+=[[u,wt]] t=[{} for _ in range(1+n)] for ii in range(1,1+n): aa=lst() if aa[0]!=0: for i in range(aa[0],0,-1): if aa[i]+1 in t[ii]:t[ii][aa[i]]=t[ii][aa[i]+1] else:t[ii][aa[i]]=aa[i]+1 # print(t) ans=dijsktra() print(ans if ans!=PI else -1) # 8 10 # 1 2 3 # 2 8 3 # 1 4 1 # 4 3 6 # 3 7 7 # 4 5 5 # 5 7 2 # 7 8 1 # 1 6 8 # 6 8 7 # 0 # 4 1 2 3 4 # 0 # 0 # 0 # 0 # 0 # 0 ```
output
1
38,956
3
77,913
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. Input The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Output 7 Input 3 1 1 2 3 0 1 3 0 Output -1 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates. Submitted Solution: ``` from sys import stdin,stdout from collections import defaultdict from heapq import heapify,heappop,heappush nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int,stdin.readline().split())) PI=float('inf') def dijsktra(): dist=[PI]*(1+n) dist[1]=0 vis=[0]*(1+n) hp=[] heapify(hp) heappush(hp,(0,1)) while hp: curDis,curNode=heappop(hp) if vis[curNode]:continue vis[curNode]=1 for neigh,wt in g[curNode]: if vis[neigh]:continue eff_wt=wt if curDis in t[curNode]:eff_wt+=t[curNode][curDis]-curDis if curDis+eff_wt<dist[neigh]: dist[neigh]=curDis+eff_wt heappush(hp,(curDis+eff_wt,neigh)) # print(dist) return dist[n] for _ in range(1):#nmbr()): n,eg=lst() g=defaultdict(list) for i in range(eg): u,v,wt=lst() g[u]+=[[v,wt]] g[v]+=[[u,wt]] t=[{} for _ in range(1+n)] for ii in range(1,1+n): aa=lst() if aa[0]!=0: for i in range(aa[0],0,-1): if aa[i]+1 in t[ii]:t[ii][aa[i]]=t[ii][aa[i]+1] else:t[ii][aa[i]]=aa[i]+1 # print(t) ans=dijsktra() print(ans if ans!=PI else -1) # 8 10 # 1 2 3 # 2 8 3 # 1 4 1 # 4 3 6 # 3 7 7 # 4 5 5 # 5 7 2 # 7 8 1 # 1 6 8 # 6 8 7 # 0 # 4 1 2 3 4 # 0 # 0 # 0 # 0 # 0 # 0 ```
instruction
0
38,957
3
77,914
Yes
output
1
38,957
3
77,915
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. Input The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Output 7 Input 3 1 1 2 3 0 1 3 0 Output -1 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates. Submitted Solution: ``` import sys from heapq import * r=sys.stdin.readline n,m = map(int,r().split()) g=[[] for _ in range(n+1)] for _ in range(m): a,b,c=map(int,r().split()) g[a].append((b,c)) g[b].append((a,c)) times=[[]] for _ in range(n): times.append(list(map(int,r().split()))[1:]) INF = sys.maxsize dist = [INF]*(n+1) dist[1]=0 q=[(0,1)] while q: d,idx = heappop(q) if d !=dist[idx]: continue cur = dist[idx] for i in times[idx]: if i==cur: cur+=1 for e,cost in g[idx]: if dist[e] > dist[idx]+cost: dist[e] = min(dist[e], cur+cost) heappush(q,(dist[e],e)) print(dist[n] if dist[n]!=INF else -1) ```
instruction
0
38,958
3
77,916
Yes
output
1
38,958
3
77,917
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. Input The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Output 7 Input 3 1 1 2 3 0 1 3 0 Output -1 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates. Submitted Solution: ``` from heapq import * from sys import stdin, stdout input = stdin.readline def BinSearch(li, x, len): i = 1 j = len while i < j: m = int((i+j)/2) if x > li[m]: i = m+1 else: j = m if li[j] == x: return j else: return None n, m = map(int, input().split()) dist = [10**20 for i in range(n+1)] G = [[] for i in range(n+1)] T = [[None]] for i in range(m): u, v, w = map(int, input().split()) G[u].append((v, w)) G[v].append((u, w)) for i in range(n): x = list(map(int, input().split())) T.append(x) unused = [(0, 1)] parent = [-1 for i in range(n+1)] dist[1] = 0 min_dist = 0 nex = 1 while unused: t, nex = heappop(unused) if T[nex][0] != 0: j = BinSearch(T[nex], t, T[nex][0]) if j != None: while j <= T[nex][0] and t == T[nex][j]: t += 1 j += 1 for edge in G[nex]: to = edge[0] wt = edge[1] if t + wt < dist[to]: dist[to] = t + wt #parent[to] = nex heappush(unused, (dist[to], to)) if dist[n] == 10**20: print(-1) else: print(dist[n]) ```
instruction
0
38,959
3
77,918
Yes
output
1
38,959
3
77,919
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. Input The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Output 7 Input 3 1 1 2 3 0 1 3 0 Output -1 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates. Submitted Solution: ``` from heapq import * n, m = map(int, input().split()) l = [] from copy import * from bisect import * from math import * from sys import * for _ in range(0, n + 2): l.append([]) for i in range(0, m): a, b, c = map(int, stdin.readline().split()) l[a].append((c, b)) l[b].append((c, a)) dist = [10000000000]*(n+3) def helper(lst, val): if lst[0] > val or lst[-1] < val: return val v = bisect_left(lst, val) if lst[v] != val: return val else: l = 1 h = len(lst) - v # lst.append(232767324) #tq=int(log(h,2)) z = 20000000000 cp=0 while l<h: m = (l + h) // 2 if val + m == lst[v + m]: l = m+1 else: h, z = m, m return (lst[len(lst)-1]+1-val if z==20000000000 else z) + val def dij_modified(delay,t): vis = [0] * (n + 3) dist[1] = t h = [] heappush(h, (t, 1)) heapify(h) while h: val = heappop(h) if vis[val[1]] != 0: continue vis[val[1]] = 1 x = val[1] if len(delay[x-1])>0 and x!=n: dist[x]= helper(delay[x-1],dist[x]) for i in l[x]: d = i[0] e = i[1] if dist[x] + d < dist[e]: dist[e] = dist[x] + d heappush(h, (dist[e], e)) delay =[] for i in range(0,n): tlst=map(int, stdin.readline().split()) x = next(tlst) if(x):delay.append(list(tlst)[0:]) else: delay.append([-1]) x=delay[0] t=0 dij_modified(delay,t) if dist[n]<10000000000: print (dist[n]) else: print (-1) ```
instruction
0
38,960
3
77,920
Yes
output
1
38,960
3
77,921
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. Input The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Output 7 Input 3 1 1 2 3 0 1 3 0 Output -1 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates. Submitted Solution: ``` from heapq import * INF = 1e16 viajantes = [] N,M = map(int,input().split()) galaxia = [[] for i in range(100002)] tempo = [INF] * (N+1) for i in range(M): a,b,c = map(int,input().split()) galaxia[a] += [(c,b)] galaxia[b] += [(c,a)] for j in range(N): vi = list(map(int,input().split())) viajantes.append(vi[1::]) def visitante_chegando(w,b): if 0 not in viajantes[b-1]: for x in viajantes[b-1]: if x == w: w += 1 return w def dijsktra(u,v,galaxia): tempo[u] = 0 q = [(0,u)] while q: a = heappop(q)[1] for adj in galaxia[a]: w,b = tempo[a] + adj[0],adj[1] w = visitante_chegando(w,b) if w < tempo[b]: tempo[b] = w heappush(q,(tempo[b],b)) return tempo d = dijsktra(1,N,galaxia) if d[-1] == INF: print(-1) else: print(d[-1]) ```
instruction
0
38,961
3
77,922
No
output
1
38,961
3
77,923
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. Input The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Output 7 Input 3 1 1 2 3 0 1 3 0 Output -1 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates. Submitted Solution: ``` import heapq as hq class Graph: def __init__(self, vertices): self.V = vertices self.graph = {} for i in range(1,n+1): self.graph[i]=node() def addedge(self,a,b,c): self.graph[a].edges[b]=c self.graph[b].edges[a]=c def dijkstra(self, src): self.graph[src].dist=0 qu=[(0,1)] while qu: d,u =hq.heappop(qu) for v in self.graph[u].edges: if self.graph[v].dist > self.graph[u].dist + self.graph[u].edges[v]: self.graph[v].dist = self.graph[u].dist + self.graph[u].edges[v] while(self.graph[v].dist in self.graph[v].demons): self.graph[v].dist+=1 hq.heappush(qu,(self.graph[v].dist,v)) print(self.graph[self.V].dist if(self.graph[self.V].dist<float("inf")) else -1) class node: def __init__(self): self.edges={} self.demons=[] self.dist=float("inf") n,m=map(int,input().rstrip().split()) g=Graph(n) for _ in range(m): a,b,c=map(int,input().rstrip().split()) g.addedge(a,b,c) for i in range(1,n+1): arr=set(map(int,input().rstrip().split())) g.graph[i].demons=arr g.dijkstra(1) ```
instruction
0
38,962
3
77,924
No
output
1
38,962
3
77,925
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. Input The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Output 7 Input 3 1 1 2 3 0 1 3 0 Output -1 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates. Submitted Solution: ``` from math import sqrt,ceil,gcd from collections import defaultdict import heapq def modInverse(b,m): g = gcd(b, m) if (g != 1): # print("Inverse doesn't exist") return -1 else: # If b and m are relatively prime, # then modulo inverse is b^(m-2) mode m return pow(b, m - 2, m) def dij(n,tot,hash,l): dis = [10**18]*(tot+1) dis[n] = 0 qu = [(dis[n],n)] boo = defaultdict(bool) while qu: d1,z = heapq.heappop(qu) # print(z) if boo[z] == True: continue boo[z] = True for i,d in hash[z]: z1 = dis[z]+d while z1 in l[i]: z1+=1 dis[i] = min(dis[i],z1) heapq.heappush(qu,(z1,i)) return dis[tot] def solve(): n,m = map(int,input().split()) hash = defaultdict(list) for i in range(m): a,b,c = map(int,input().split()) hash[a].append([b,c]) hash[b].append([a,c]) l = [[]] for i in range(n): la = list(map(int,input().split())) l.append(set(la[1:])) z = dij(1,n,hash,l) if z == 10**18: print(-1) else: print(z) # t = int(input()) # for _ in range(t): solve() ```
instruction
0
38,963
3
77,926
No
output
1
38,963
3
77,927
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0. It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet. Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. Input The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. Output Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. Examples Input 4 6 1 2 2 1 3 3 1 4 8 2 3 4 2 4 5 3 4 3 0 1 3 2 3 4 0 Output 7 Input 3 1 1 2 3 0 1 3 0 Output -1 Note In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds. In the second sample one can't get from planet 1 to planet 3 by moving through stargates. Submitted Solution: ``` from heapq import * from sys import stdin, stdout from bisect import bisect n, m2 = map(int, stdin.readline().split()) gr = [[] for _ in range(n)] for i in range(m2): u, v, w = map(int, stdin.readline().split()) u -= 1 v -= 1 gr[u].append((v, w)) gr[v].append((u, w)) heap = [(0, 0)] inf = int(2e12) d = [inf] * n d[0] = 0 time = [list(map(int, stdin.readline().split())) for _ in range(n)] while heap: dst, ind = heappop(heap) if dst != d[ind]: continue for u, w in gr[ind]: if d[u] > d[ind] + w: tmp = d[ind] time[ind][0] = -3 m1 = bisect(time[ind], tmp) - 1 l = m1 r = len(time[ind]) while r - l > 1: m = (l + r) // 2 if time[ind][m] == tmp + m - m1: l = m else: r = m d[u] = min(d[u], d[ind] + l - m1 + w) heappush(heap, (d[u], u)) if d[-1] * 2 > inf: stdout.write("-1") else: stdout.write(str(d[-1])) ```
instruction
0
38,964
3
77,928
No
output
1
38,964
3
77,929
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. Input First line of input contains single integer number n (2 ≤ n ≤ 100 000) — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down. Output Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. Examples Input 3 1 1 Output 1 Input 5 1 2 2 2 Output 3 Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Output 4 Note In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it. Submitted Solution: ``` import sys import threading sys.setrecursionlimit(10**9) threading.stack_size(2**26) n = int(input()) graph = [[] for x in range(n)] p = list(map(int, input().split())) levels = [0] * n def dfs(vertex, level): for u in graph[vertex]: dfs(u, level+1) levels[level] += 1 def main(): result = 0 for i in range(n-1): graph[p[i] - 1].append(i+1) dfs(0,0) for i in levels: result += i % 2 print(result) thread = threading.Thread(target=main) thread.start() ```
instruction
0
39,331
3
78,662
Yes
output
1
39,331
3
78,663
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. Input First line of input contains single integer number n (2 ≤ n ≤ 100 000) — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down. Output Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. Examples Input 3 1 1 Output 1 Input 5 1 2 2 2 Output 3 Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Output 4 Note In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it. Submitted Solution: ``` import sys from collections import defaultdict n = int(input()) p = list(map(int, sys.stdin.readline().split())) # n = 10**5 # p = range(1,n) g = defaultdict(list) for i in range(n-1): g[p[i]].append(i + 2) res = 0 lvl = [1] while lvl: res += len(lvl)&1 newlvl = [] for el in lvl: newlvl.extend(g[el]) lvl = newlvl print(res) ```
instruction
0
39,332
3
78,664
Yes
output
1
39,332
3
78,665
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. Input First line of input contains single integer number n (2 ≤ n ≤ 100 000) — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down. Output Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. Examples Input 3 1 1 Output 1 Input 5 1 2 2 2 Output 3 Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Output 4 Note In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it. Submitted Solution: ``` n = int(input()) import sys sys.setrecursionlimit(10000000) x = list(map(int, input().split())) x = [0] + x y = [[] for i in range(n)] for i in range(1, n): y[x[i]].append(i + 1) count = 0 ind = True x = y[1] while ind or len(z) > 0: ind = False if len(x) % 2 == 1: count += 1 z = [] for j in x: if j < n and len(y[j]) > 0: for i in y[j]: z.append(i) x = z print(count + 1) ```
instruction
0
39,333
3
78,666
Yes
output
1
39,333
3
78,667
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. Input First line of input contains single integer number n (2 ≤ n ≤ 100 000) — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down. Output Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. Examples Input 3 1 1 Output 1 Input 5 1 2 2 2 Output 3 Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Output 4 Note In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it. Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) dp=[0,] count=[0 for i in range(n)] count[0]+=1 for i in range(n-1): dp.append(dp[a[i]-1]+1) count[dp[-1]]+=1 ans=0 for i in range(n): ans+=count[i]%2 print(ans) ```
instruction
0
39,334
3
78,668
Yes
output
1
39,334
3
78,669
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. Input First line of input contains single integer number n (2 ≤ n ≤ 100 000) — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down. Output Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. Examples Input 3 1 1 Output 1 Input 5 1 2 2 2 Output 3 Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Output 4 Note In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it. Submitted Solution: ``` n = int(input()) p = list(map(int, input().split())) x = [0 for i in range(n)] for pi in p: x[pi-1] += 1 #print(p) lz = list() lz2 = list() sub = 0 for i in range(n): if x[i] == 0: lz.append(i) else: if x[i] % 2 == 0: sub += x[i] else: sub += (x[i] - 1) #print(x) #print(lz) while len(lz) and n > sub > 0: # print('loop, n = ' + str(n) + ' sub = ' + str(sub)) n -= sub for lzi in lz: x[p[lzi-1]-1] -= 1 # print(x) if x[p[lzi-1]-1] == 0: lz2.append(p[lzi-1]-1) elif x[p[lzi-1]-1] % 2 != 0: sub -= 2 lz = lz2.copy() lz2.clear() print(n) ```
instruction
0
39,335
3
78,670
No
output
1
39,335
3
78,671
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. Input First line of input contains single integer number n (2 ≤ n ≤ 100 000) — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down. Output Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. Examples Input 3 1 1 Output 1 Input 5 1 2 2 2 Output 3 Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Output 4 Note In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it. Submitted Solution: ``` import sys sys.setrecursionlimit(sys.maxsize) from collections import Counter #from io import StringIO #sys.stdin = StringIO(open(__file__.replace('.py', '.in')).read()) n = int(input()) a = list(map(int, input().split())) adj = {} for i in range(n-1): if a[i] not in adj: adj[a[i]] = [] adj[a[i]].append(i + 2) def dfs(node, cnt): global mx, adj, counter if counter[node] == 0: return cnt += 1 mx = max(mx, cnt) if node in adj: for ch in adj[node]: dfs(ch, cnt) counter = Counter(a) for k, v in counter.items(): counter[k] = v % 2 # print(adj) # # print('---') # # for k, v in counter.items(): # print(k, v) # # print('---') # # for i in range(n-1): # print(i + 2, a[i]) mx = 1 dfs(1, 1) print(mx) ```
instruction
0
39,336
3
78,672
No
output
1
39,336
3
78,673
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. Input First line of input contains single integer number n (2 ≤ n ≤ 100 000) — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down. Output Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. Examples Input 3 1 1 Output 1 Input 5 1 2 2 2 Output 3 Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Output 4 Note In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it. Submitted Solution: ``` import sys sys.setrecursionlimit(sys.maxsize) from collections import Counter #from io import StringIO #sys.stdin = StringIO(open(__file__.replace('.py', '.in')).read()) n = int(input()) a = list(map(int, input().split())) # for i in range(n-1): # print(i + 2, a[i]) adj = {} for i in range(n-1): if a[i] not in adj: adj[a[i]] = [] adj[a[i]].append(i + 2) def dfs(node): global adj, counter r = 1 if node in adj: rs = [] for ch in adj[node]: rs.append(dfs(ch)) rs = list(sorted(rs, reverse=True)) if len(rs) % 2 == 1: r += rs[0] else: r += rs[0] - rs[1] return r # counter = Counter(a) # for k, v in counter.items(): # counter[k] = v % 2 # print(adj) # # print('---') # # for k, v in counter.items(): # print(k, v) # # print('---') # print(dfs(1)) ```
instruction
0
39,337
3
78,674
No
output
1
39,337
3
78,675
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. Input First line of input contains single integer number n (2 ≤ n ≤ 100 000) — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down. Output Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. Examples Input 3 1 1 Output 1 Input 5 1 2 2 2 Output 3 Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Output 4 Note In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it. Submitted Solution: ``` n = int(input()) p = list(map(int, input().split())) x = [0 for i in range(n)] for pi in p: x[pi-1] += 1 lz = list() lz2 = list() sub = 0 for i in range(n-1): if x[i] == 0: lz.append(i) else: if x[i] % 2 == 0: sub += x[i] else: sub += (x[i] - 1) while len(lz) > 0: n -= sub for lzi in lz: x[p[lzi]] -= 1 if x[p[lzi]] == 0: lz2.append(lzi) elif x[p[lzi]] % 2 != 0: sub -= 2 lz = lz2.copy() lz2.clear() print(n) ```
instruction
0
39,338
3
78,676
No
output
1
39,338
3
78,677
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of the lightsabers of the Jedi who will go on that mission. Heidi has n Jedi Knights standing in front of her, each one with a lightsaber of one of m possible colors. She knows that for the mission to be the most effective, she needs to select some contiguous interval of knights such that there are exactly k1 knights with lightsabers of the first color, k2 knights with lightsabers of the second color, ..., km knights with lightsabers of the m-th color. However, since the last time, she has learned that it is not always possible to select such an interval. Therefore, she decided to ask some Jedi Knights to go on an indefinite unpaid vacation leave near certain pits on Tatooine, if you know what I mean. Help Heidi decide what is the minimum number of Jedi Knights that need to be let go before she is able to select the desired interval from the subsequence of remaining knights. Input The first line of the input contains n (1 ≤ n ≤ 2·105) and m (1 ≤ m ≤ n). The second line contains n integers in the range {1, 2, ..., m} representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains m integers k1, k2, ..., km (with <image>) – the desired counts of Jedi Knights with lightsabers of each color from 1 to m. Output Output one number: the minimum number of Jedi Knights that need to be removed from the sequence so that, in what remains, there is an interval with the prescribed counts of lightsaber colors. If this is not possible, output - 1. Example Input 8 3 3 3 1 2 2 1 1 3 3 1 1 Output 1 Submitted Solution: ``` n, m = map(int, input().split()) c = list(map(int, input().split())) k = list(map(int, input().split())) p = [0] * m c = [o - 1 for o in c] s = sum(k) ok = k.count(0) i1 = 0 i2 = 0 while i2 < n and ok < m: p[c[i2]] += 1 if p[c[i2]] == k[c[i2]]: ok += 1 i2 += 1 if ok != m: print(-1) exit(0) ans = i2 - i1 while i1 < n: p[c[i1]] -= 1 if k[c[i1]] <= p[c[i1]]: i1 += 1 ans = min(ans, i2 - i1) continue while i2 < n and p[c[i1]] < k[c[i1]]: p[c[i2]] += 1 i2 += 1 if p[c[i1]] >= k[c[i1]]: ans = min(ans, i2 - i1) elif i2 == n: break print(ans - s) ```
instruction
0
39,346
3
78,692
No
output
1
39,346
3
78,693
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of the lightsabers of the Jedi who will go on that mission. Heidi has n Jedi Knights standing in front of her, each one with a lightsaber of one of m possible colors. She knows that for the mission to be the most effective, she needs to select some contiguous interval of knights such that there are exactly k1 knights with lightsabers of the first color, k2 knights with lightsabers of the second color, ..., km knights with lightsabers of the m-th color. However, since the last time, she has learned that it is not always possible to select such an interval. Therefore, she decided to ask some Jedi Knights to go on an indefinite unpaid vacation leave near certain pits on Tatooine, if you know what I mean. Help Heidi decide what is the minimum number of Jedi Knights that need to be let go before she is able to select the desired interval from the subsequence of remaining knights. Input The first line of the input contains n (1 ≤ n ≤ 2·105) and m (1 ≤ m ≤ n). The second line contains n integers in the range {1, 2, ..., m} representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains m integers k1, k2, ..., km (with <image>) – the desired counts of Jedi Knights with lightsabers of each color from 1 to m. Output Output one number: the minimum number of Jedi Knights that need to be removed from the sequence so that, in what remains, there is an interval with the prescribed counts of lightsaber colors. If this is not possible, output - 1. Example Input 8 3 3 3 1 2 2 1 1 3 3 1 1 Output 1 Submitted Solution: ``` from bisect import bisect_left,bisect_right from heapq import heappop,heappush,heapify from collections import deque n,m=map(int,input().split()) arr=list(map(int,input().split())) brr=list(map(int,input().split())) sum_brr=sum(brr) lst=[[] for i in range(m+1)] for i in range(n): lst[arr[i]].append(i) maxi_indx=-1 for i in range(1,m+1): if len(lst[i]) <brr[i-1]: print(-1) exit() if lst[i]: maxi_indx =max(maxi_indx,lst[i][brr[i-1]-1]) flag=0 n_lst=[[] for i in range(m+1)] mini_indx=float("inf") if flag ==1: print(-1) exit() for i in range(m+1): v=bisect_right(lst[i] ,maxi_indx) n_lst[i] =lst[i][:v] for i in range(m+1): n_lst[i] =deque(n_lst[i]) for i in range(m): while len(n_lst[i+1]) >brr[i]: n_lst[i+1].popleft() he=[] for i in range(m+1): for j in n_lst[i]: heappush(he,[j,i]) mini_indx =min(mini_indx,j) mini=maxi_indx -mini_indx +1 for i in range(maxi_indx+1,n): if n_lst[arr[i]]: q=n_lst[arr[i]].popleft() n_lst[arr[i]].append(i) heappush(he,[i,arr[i]]) while he[0][0] <n_lst[he[0][1]][0]: heappop(he) mini =min(mini,i-he[0][0] +1) print(mini-sum_brr) ```
instruction
0
39,348
3
78,696
No
output
1
39,348
3
78,697