message
stringlengths
2
20.1k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
1.95k
109k
cluster
float64
17
17
__index_level_0__
int64
3.91k
217k
Provide tags and a correct Python 3 solution for this coding contest problem. In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams. After practice competition, participant number i got a score of ai. Team score is defined as sum of scores of its participants. High school management is interested if it's possible to build two teams with equal scores. Your task is to answer that question. Input The single line contains six integers a1, ..., a6 (0 ≤ ai ≤ 1000) — scores of the participants Output Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Examples Input 1 3 2 1 2 1 Output YES Input 1 1 1 1 1 99 Output NO Note In the first sample, first team can be composed of 1st, 2nd and 6th participant, second — of 3rd, 4th and 5th: team scores are 1 + 3 + 1 = 2 + 1 + 2 = 5. In the second sample, score of participant number 6 is too high: his team score will be definitely greater.
instruction
0
11,336
17
22,672
Tags: brute force Correct Solution: ``` from itertools import permutations a = [int(x) for x in input().split()] for p in permutations(a): if p[0] + p[1] + p[2] == p[3] + p[4] + p[5]: print('YES') exit() print('NO') ```
output
1
11,336
17
22,673
Provide tags and a correct Python 3 solution for this coding contest problem. In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams. After practice competition, participant number i got a score of ai. Team score is defined as sum of scores of its participants. High school management is interested if it's possible to build two teams with equal scores. Your task is to answer that question. Input The single line contains six integers a1, ..., a6 (0 ≤ ai ≤ 1000) — scores of the participants Output Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Examples Input 1 3 2 1 2 1 Output YES Input 1 1 1 1 1 99 Output NO Note In the first sample, first team can be composed of 1st, 2nd and 6th participant, second — of 3rd, 4th and 5th: team scores are 1 + 3 + 1 = 2 + 1 + 2 = 5. In the second sample, score of participant number 6 is too high: his team score will be definitely greater.
instruction
0
11,337
17
22,674
Tags: brute force Correct Solution: ``` def f(a, b): if (len(a) == 6): #print(a); x = sum(a[0:3]); y = sum(a[3:6]); if (x == y): return True else: for i in range(len(b)): x = b[i] j = i + 1 k = len(b) remaining = b[0:i] + b[j:k] selected = a + [x] if (f(selected, remaining)): return True def ff(a): print("YES" if f([], a) else "NO") #ff([1,2,3,1,2,3]); #ff([1,1,1,1,1,4]); a = [int(x) for x in input().split()] ff(a) ```
output
1
11,337
17
22,675
Provide tags and a correct Python 3 solution for this coding contest problem. In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams. After practice competition, participant number i got a score of ai. Team score is defined as sum of scores of its participants. High school management is interested if it's possible to build two teams with equal scores. Your task is to answer that question. Input The single line contains six integers a1, ..., a6 (0 ≤ ai ≤ 1000) — scores of the participants Output Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Examples Input 1 3 2 1 2 1 Output YES Input 1 1 1 1 1 99 Output NO Note In the first sample, first team can be composed of 1st, 2nd and 6th participant, second — of 3rd, 4th and 5th: team scores are 1 + 3 + 1 = 2 + 1 + 2 = 5. In the second sample, score of participant number 6 is too high: his team score will be definitely greater.
instruction
0
11,338
17
22,676
Tags: brute force Correct Solution: ``` a = list(map(int, input().split())) if sum(a) % 2 == 1: print('NO') else: target = sum(a) // 2 n = 6 ans = 'NO' for i in range(n): for j in range(i + 1, n): for k in range(j + 1, n): if a[i] + a[j] + a[k] == target: ans = 'YES' print(ans) ```
output
1
11,338
17
22,677
Provide tags and a correct Python 3 solution for this coding contest problem. In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams. After practice competition, participant number i got a score of ai. Team score is defined as sum of scores of its participants. High school management is interested if it's possible to build two teams with equal scores. Your task is to answer that question. Input The single line contains six integers a1, ..., a6 (0 ≤ ai ≤ 1000) — scores of the participants Output Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Examples Input 1 3 2 1 2 1 Output YES Input 1 1 1 1 1 99 Output NO Note In the first sample, first team can be composed of 1st, 2nd and 6th participant, second — of 3rd, 4th and 5th: team scores are 1 + 3 + 1 = 2 + 1 + 2 = 5. In the second sample, score of participant number 6 is too high: his team score will be definitely greater.
instruction
0
11,339
17
22,678
Tags: brute force Correct Solution: ``` l = list(map(int, input().split())) y = False s = sum(l) i = 0 while i < 4 and not y: j = i + 1 while j < 5 and not y: k = j + 1 while k < 6 and not y: t = l[i] + l[j] + l[k] if t == s - t: y = True k += 1 j += 1 i += 1 if y: print("YES") else: print("NO") ```
output
1
11,339
17
22,679
Provide tags and a correct Python 3 solution for this coding contest problem. In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams. After practice competition, participant number i got a score of ai. Team score is defined as sum of scores of its participants. High school management is interested if it's possible to build two teams with equal scores. Your task is to answer that question. Input The single line contains six integers a1, ..., a6 (0 ≤ ai ≤ 1000) — scores of the participants Output Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Examples Input 1 3 2 1 2 1 Output YES Input 1 1 1 1 1 99 Output NO Note In the first sample, first team can be composed of 1st, 2nd and 6th participant, second — of 3rd, 4th and 5th: team scores are 1 + 3 + 1 = 2 + 1 + 2 = 5. In the second sample, score of participant number 6 is too high: his team score will be definitely greater.
instruction
0
11,340
17
22,680
Tags: brute force Correct Solution: ``` A=list(map(int,input().split())) flag=False for i in range(6): pow1=A[i] used1=[i] for j in range(6): if j not in used1: pow2=pow1+A[j] used2=used1+[j] for k in range(6): if k not in used2: pow3=pow2+A[k] used3=used2+[k] for l in range(6): if l not in used3: pow4=pow3-A[l] used4=used3+[l] for m in range(6): if m not in used4: pow5=pow4-A[m] used5=used4+[m] for n in range(6): if n not in used5: pow6=pow5-A[n] if pow6==0: flag=True ans=flag and 'YES' or 'NO' print(ans) ```
output
1
11,340
17
22,681
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams. After practice competition, participant number i got a score of ai. Team score is defined as sum of scores of its participants. High school management is interested if it's possible to build two teams with equal scores. Your task is to answer that question. Input The single line contains six integers a1, ..., a6 (0 ≤ ai ≤ 1000) — scores of the participants Output Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Examples Input 1 3 2 1 2 1 Output YES Input 1 1 1 1 1 99 Output NO Note In the first sample, first team can be composed of 1st, 2nd and 6th participant, second — of 3rd, 4th and 5th: team scores are 1 + 3 + 1 = 2 + 1 + 2 = 5. In the second sample, score of participant number 6 is too high: his team score will be definitely greater. Submitted Solution: ``` def main(): a=list(map(int,input().split())) s=sum(a) if s%2==0: for i in range(6): for j in range(6): if j!=i: for k in range(6): if k!=i and k!=j: if a[i]+a[j]+a[k]==s//2: return 1 return 0 if main(): print('YES') else: print('NO') ```
instruction
0
11,341
17
22,682
Yes
output
1
11,341
17
22,683
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams. After practice competition, participant number i got a score of ai. Team score is defined as sum of scores of its participants. High school management is interested if it's possible to build two teams with equal scores. Your task is to answer that question. Input The single line contains six integers a1, ..., a6 (0 ≤ ai ≤ 1000) — scores of the participants Output Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Examples Input 1 3 2 1 2 1 Output YES Input 1 1 1 1 1 99 Output NO Note In the first sample, first team can be composed of 1st, 2nd and 6th participant, second — of 3rd, 4th and 5th: team scores are 1 + 3 + 1 = 2 + 1 + 2 = 5. In the second sample, score of participant number 6 is too high: his team score will be definitely greater. Submitted Solution: ``` x = list(map(int, input().split())) total_sum = sum(x) if sum(x) % 2 != 0: print('NO') else: for i in range(6): for j in range(6): if j == i and j != 5: break for k in range(6): if k == j and k != 5: break if i != k and i != j and k != j and x[i] + x[j] + x[k] == total_sum // 2: print('YES') exit() if i == j == k == 5: print('NO') ```
instruction
0
11,342
17
22,684
Yes
output
1
11,342
17
22,685
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams. After practice competition, participant number i got a score of ai. Team score is defined as sum of scores of its participants. High school management is interested if it's possible to build two teams with equal scores. Your task is to answer that question. Input The single line contains six integers a1, ..., a6 (0 ≤ ai ≤ 1000) — scores of the participants Output Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Examples Input 1 3 2 1 2 1 Output YES Input 1 1 1 1 1 99 Output NO Note In the first sample, first team can be composed of 1st, 2nd and 6th participant, second — of 3rd, 4th and 5th: team scores are 1 + 3 + 1 = 2 + 1 + 2 = 5. In the second sample, score of participant number 6 is too high: his team score will be definitely greater. Submitted Solution: ``` from sys import stdin a = [int(i) for i in stdin.readline().split(' ')] s = sum(a) possible = False for i in range(1, 5): for j in range(i+1, 6): team = a[0]+a[i]+a[j] if team == s-team: possible = True if possible: print("YES") else: print("NO") ```
instruction
0
11,343
17
22,686
Yes
output
1
11,343
17
22,687
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams. After practice competition, participant number i got a score of ai. Team score is defined as sum of scores of its participants. High school management is interested if it's possible to build two teams with equal scores. Your task is to answer that question. Input The single line contains six integers a1, ..., a6 (0 ≤ ai ≤ 1000) — scores of the participants Output Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Examples Input 1 3 2 1 2 1 Output YES Input 1 1 1 1 1 99 Output NO Note In the first sample, first team can be composed of 1st, 2nd and 6th participant, second — of 3rd, 4th and 5th: team scores are 1 + 3 + 1 = 2 + 1 + 2 = 5. In the second sample, score of participant number 6 is too high: his team score will be definitely greater. Submitted Solution: ``` n = list(map(int, input().split())) Sum = sum(n) r = False for i in range (len(n)): for j in range (i+1, len(n)): for k in range (j+1, len(n)): if (n[i] + n[j] + n[k] == Sum/2): r = True if (r == True): print ('YES') else: print ('NO') ```
instruction
0
11,344
17
22,688
Yes
output
1
11,344
17
22,689
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams. After practice competition, participant number i got a score of ai. Team score is defined as sum of scores of its participants. High school management is interested if it's possible to build two teams with equal scores. Your task is to answer that question. Input The single line contains six integers a1, ..., a6 (0 ≤ ai ≤ 1000) — scores of the participants Output Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Examples Input 1 3 2 1 2 1 Output YES Input 1 1 1 1 1 99 Output NO Note In the first sample, first team can be composed of 1st, 2nd and 6th participant, second — of 3rd, 4th and 5th: team scores are 1 + 3 + 1 = 2 + 1 + 2 = 5. In the second sample, score of participant number 6 is too high: his team score will be definitely greater. Submitted Solution: ``` r = lambda : list(map(int, input().split())) arr = r() arr.sort() f = False for i in range(0 , len(arr)): for j in range(0 , len(arr)): for k in range(0,len(arr)): if i!=j!=k: if arr[i]+arr[j]+arr[k] == sum(arr)//2: f = True break print("YES" if f else "NO") ```
instruction
0
11,345
17
22,690
No
output
1
11,345
17
22,691
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams. After practice competition, participant number i got a score of ai. Team score is defined as sum of scores of its participants. High school management is interested if it's possible to build two teams with equal scores. Your task is to answer that question. Input The single line contains six integers a1, ..., a6 (0 ≤ ai ≤ 1000) — scores of the participants Output Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Examples Input 1 3 2 1 2 1 Output YES Input 1 1 1 1 1 99 Output NO Note In the first sample, first team can be composed of 1st, 2nd and 6th participant, second — of 3rd, 4th and 5th: team scores are 1 + 3 + 1 = 2 + 1 + 2 = 5. In the second sample, score of participant number 6 is too high: his team score will be definitely greater. Submitted Solution: ``` si= input() def opr(grad): l =[] i = 0 l.append(0) grew = [] j=len(grad)-1 while i<len(grad): grew.append(grad[j]) j-=1 i+=1 i=0 m=0 j=0 while i<len(grew): if grew[i]!=' ': l[j]+=int(grew[i])*(10**m) m+=1 if grew[i]==' ': m=0 j+=1 l.append(0) i+=1 g = [] i=len(l)-1 while i>=0: g.append(l[i]) i-=1 return g def ma(l): i =0 m=l[i] im=0 while i<len(l): if l[i]>m: m=l[i] im =i i+=1 return im def su(l): i =0 m = 0 while i<len(l): m+=l[i] i+=1 return m i = 0 l = opr (si) k1 = 0 k2 = 0 ki1 = 0 ki2 = 0 while i<6: if k1 < k2 and ki1<3: k1+= l[ma(l)] l.remove(l[ma(l)]) ki1+=1 if k1>= k2 and ki2<3: k2+= l[ma(l)] l.remove(l[ma(l)]) ki2+=1 i+=1 if k1-k2 == 0: print("YES") else: print("NO") ```
instruction
0
11,346
17
22,692
No
output
1
11,346
17
22,693
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams. After practice competition, participant number i got a score of ai. Team score is defined as sum of scores of its participants. High school management is interested if it's possible to build two teams with equal scores. Your task is to answer that question. Input The single line contains six integers a1, ..., a6 (0 ≤ ai ≤ 1000) — scores of the participants Output Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Examples Input 1 3 2 1 2 1 Output YES Input 1 1 1 1 1 99 Output NO Note In the first sample, first team can be composed of 1st, 2nd and 6th participant, second — of 3rd, 4th and 5th: team scores are 1 + 3 + 1 = 2 + 1 + 2 = 5. In the second sample, score of participant number 6 is too high: his team score will be definitely greater. Submitted Solution: ``` powers = list(int(i) for i in input().split()) powers = list(reversed(sorted(powers))) team = 0; end = False for p in powers: team += p team /= 2 result = "NO" if powers[0] > team: end = True if end == False: for i in range(len(powers)): if powers[0] == team: result = "YES" end = True break if end == False: for i in range(len(powers)): for j in range(len(powers)): if powers[i] + powers[j] == team: result = "YES" end = True break if end == False: for i in range(len(powers)): for j in range(len(powers)): for z in range(len(powers)): if powers[i] + powers[j] + powers[z] == team: result = "YES" end = True break print(result) ```
instruction
0
11,347
17
22,694
No
output
1
11,347
17
22,695
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams. After practice competition, participant number i got a score of ai. Team score is defined as sum of scores of its participants. High school management is interested if it's possible to build two teams with equal scores. Your task is to answer that question. Input The single line contains six integers a1, ..., a6 (0 ≤ ai ≤ 1000) — scores of the participants Output Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Examples Input 1 3 2 1 2 1 Output YES Input 1 1 1 1 1 99 Output NO Note In the first sample, first team can be composed of 1st, 2nd and 6th participant, second — of 3rd, 4th and 5th: team scores are 1 + 3 + 1 = 2 + 1 + 2 = 5. In the second sample, score of participant number 6 is too high: his team score will be definitely greater. Submitted Solution: ``` N = list(map(int,input().split())) a = 0 N.sort() for i in range(6): a += N[i] if a % 2 != 0: print('NO') else: a = a//2 if a - N[5] - N[0] < N[1]: print('NO') elif a - N[5] - N[0] == N[1]: print('YES') else: a -= N[5] if a-N[2]-N[0] < 0: print('NO') elif a-N[2]-N[0] == 0: print('YES') elif a-N[2]-N[1] < 0: print('NO') elif a - N[2] - N[1] == 0: print('YES') elif a - N[3] - N[0] < 0: print('NO') elif a - N[3] - N[0] == 0: print('YES') else: print('NO') ```
instruction
0
11,348
17
22,696
No
output
1
11,348
17
22,697
Provide a correct Python 3 solution for this coding contest problem. There are league games and tournament games in sports competitions. In soccer league games, points are given to each of the wins, losses, and draws, and the rankings are competed based on the points. The points are win (3 points), negative (0 points), and draw (1 point), respectively. Enter the number of teams and the results of the league match, sort them in order of best results (in descending order of points), and create a program that outputs the team name and points. If the points are tied, output in the order of input. Input Given multiple datasets. Each dataset is given in the following format: n name1 w1 l1 d1 name2 w2 l2 d2 :: namen wn ln dn The number of teams n (n ≤ 10) is given on the first line. The next n lines are given the name of team i (alphabet of up to 20 characters), the number of wins wi, the number of negatives li, and the number of draws di (0 ≤ wi, li, di ≤ 9), separated by spaces. .. When the number of teams is 0, the input is completed. The number of datasets does not exceed 50. Output Print a sorted list of teams for each dataset. Print the name of the i-th team and the points on the i-line, separated by commas. Insert one blank line between the datasets. Example Input 4 Japan 1 0 2 Egypt 1 2 0 Canada 0 2 1 Spain 2 0 1 3 India 0 2 0 Poland 1 0 1 Italy 1 0 1 0 Output Spain,7 Japan,5 Egypt,3 Canada,1 Poland,4 Italy,4 India,0
instruction
0
11,539
17
23,078
"Correct Solution: ``` def getScore(d): return int(d[1]) * 3 + int(d[3]) isFirst = True while 1: n = int(input()) if n == 0: break if not isFirst: print() table = [] for i in range(0,n): d = list(_ for _ in input().split()) score = getScore(d) table.append((d[0],score)) table.sort(key=lambda i:i[1],reverse=True) for n,s in table: print(str(n) + ',' + str(s)) isFirst = False ```
output
1
11,539
17
23,079
Provide a correct Python 3 solution for this coding contest problem. There are league games and tournament games in sports competitions. In soccer league games, points are given to each of the wins, losses, and draws, and the rankings are competed based on the points. The points are win (3 points), negative (0 points), and draw (1 point), respectively. Enter the number of teams and the results of the league match, sort them in order of best results (in descending order of points), and create a program that outputs the team name and points. If the points are tied, output in the order of input. Input Given multiple datasets. Each dataset is given in the following format: n name1 w1 l1 d1 name2 w2 l2 d2 :: namen wn ln dn The number of teams n (n ≤ 10) is given on the first line. The next n lines are given the name of team i (alphabet of up to 20 characters), the number of wins wi, the number of negatives li, and the number of draws di (0 ≤ wi, li, di ≤ 9), separated by spaces. .. When the number of teams is 0, the input is completed. The number of datasets does not exceed 50. Output Print a sorted list of teams for each dataset. Print the name of the i-th team and the points on the i-line, separated by commas. Insert one blank line between the datasets. Example Input 4 Japan 1 0 2 Egypt 1 2 0 Canada 0 2 1 Spain 2 0 1 3 India 0 2 0 Poland 1 0 1 Italy 1 0 1 0 Output Spain,7 Japan,5 Egypt,3 Canada,1 Poland,4 Italy,4 India,0
instruction
0
11,540
17
23,080
"Correct Solution: ``` f = True while True: n = int(input()) if n == 0: break T = [] for i in range(n): name,w,l,d = list(input().split()) T.append((name,i,int(w)*3+int(d))) if f: f = False else: print() for i in sorted(T, key = lambda x:(-x[2],x[1])): print(i[0],",",i[2],sep="") ```
output
1
11,540
17
23,081
Provide a correct Python 3 solution for this coding contest problem. There are league games and tournament games in sports competitions. In soccer league games, points are given to each of the wins, losses, and draws, and the rankings are competed based on the points. The points are win (3 points), negative (0 points), and draw (1 point), respectively. Enter the number of teams and the results of the league match, sort them in order of best results (in descending order of points), and create a program that outputs the team name and points. If the points are tied, output in the order of input. Input Given multiple datasets. Each dataset is given in the following format: n name1 w1 l1 d1 name2 w2 l2 d2 :: namen wn ln dn The number of teams n (n ≤ 10) is given on the first line. The next n lines are given the name of team i (alphabet of up to 20 characters), the number of wins wi, the number of negatives li, and the number of draws di (0 ≤ wi, li, di ≤ 9), separated by spaces. .. When the number of teams is 0, the input is completed. The number of datasets does not exceed 50. Output Print a sorted list of teams for each dataset. Print the name of the i-th team and the points on the i-line, separated by commas. Insert one blank line between the datasets. Example Input 4 Japan 1 0 2 Egypt 1 2 0 Canada 0 2 1 Spain 2 0 1 3 India 0 2 0 Poland 1 0 1 Italy 1 0 1 0 Output Spain,7 Japan,5 Egypt,3 Canada,1 Poland,4 Italy,4 India,0
instruction
0
11,541
17
23,082
"Correct Solution: ``` flag = True while True: n = int(input()) if n == 0: break answer = {} for _ in range(n): result = input().split() country = result[0] win, lose, draw = map(int, result[1:]) score = win * 3 + draw answer[country] = score if flag: flag = False else: print() for c, s in sorted(answer.items(), key=lambda x: -x[1]): print(c + ',' + str(s)) ```
output
1
11,541
17
23,083
Provide a correct Python 3 solution for this coding contest problem. There are league games and tournament games in sports competitions. In soccer league games, points are given to each of the wins, losses, and draws, and the rankings are competed based on the points. The points are win (3 points), negative (0 points), and draw (1 point), respectively. Enter the number of teams and the results of the league match, sort them in order of best results (in descending order of points), and create a program that outputs the team name and points. If the points are tied, output in the order of input. Input Given multiple datasets. Each dataset is given in the following format: n name1 w1 l1 d1 name2 w2 l2 d2 :: namen wn ln dn The number of teams n (n ≤ 10) is given on the first line. The next n lines are given the name of team i (alphabet of up to 20 characters), the number of wins wi, the number of negatives li, and the number of draws di (0 ≤ wi, li, di ≤ 9), separated by spaces. .. When the number of teams is 0, the input is completed. The number of datasets does not exceed 50. Output Print a sorted list of teams for each dataset. Print the name of the i-th team and the points on the i-line, separated by commas. Insert one blank line between the datasets. Example Input 4 Japan 1 0 2 Egypt 1 2 0 Canada 0 2 1 Spain 2 0 1 3 India 0 2 0 Poland 1 0 1 Italy 1 0 1 0 Output Spain,7 Japan,5 Egypt,3 Canada,1 Poland,4 Italy,4 India,0
instruction
0
11,542
17
23,084
"Correct Solution: ``` b=False while True: n = int(input()) if n==0:break d=dict() if b:print() b=True for _ in range(n): line = input().split() tmp = int(line[1])*3+int(line[3]*1) if tmp in d: d[tmp].append(line[0]) else: d[tmp] = [] d[tmp].append(line[0]) for k, vs in sorted(d.items(), key=lambda x: x[0])[::-1]: for v in vs: print('{0},{1}'.format(v, k)) ```
output
1
11,542
17
23,085
Provide a correct Python 3 solution for this coding contest problem. There are league games and tournament games in sports competitions. In soccer league games, points are given to each of the wins, losses, and draws, and the rankings are competed based on the points. The points are win (3 points), negative (0 points), and draw (1 point), respectively. Enter the number of teams and the results of the league match, sort them in order of best results (in descending order of points), and create a program that outputs the team name and points. If the points are tied, output in the order of input. Input Given multiple datasets. Each dataset is given in the following format: n name1 w1 l1 d1 name2 w2 l2 d2 :: namen wn ln dn The number of teams n (n ≤ 10) is given on the first line. The next n lines are given the name of team i (alphabet of up to 20 characters), the number of wins wi, the number of negatives li, and the number of draws di (0 ≤ wi, li, di ≤ 9), separated by spaces. .. When the number of teams is 0, the input is completed. The number of datasets does not exceed 50. Output Print a sorted list of teams for each dataset. Print the name of the i-th team and the points on the i-line, separated by commas. Insert one blank line between the datasets. Example Input 4 Japan 1 0 2 Egypt 1 2 0 Canada 0 2 1 Spain 2 0 1 3 India 0 2 0 Poland 1 0 1 Italy 1 0 1 0 Output Spain,7 Japan,5 Egypt,3 Canada,1 Poland,4 Italy,4 India,0
instruction
0
11,543
17
23,086
"Correct Solution: ``` import bisect cnt = 0 while 1: outs = [] outn = [] n = int(input()) if n == 0: break elif cnt > 0: print() names = [] scores = [] for i in range(n): line = list(input().split()) name = line[0] names.append(name) w = line[1] l = line[2] d = line[3] score = 3*int(w) + int(d) tm = bisect.bisect_left(outs, score) outs.insert(tm, score) outn.insert(tm, name) #scores.append(score) #ind = 1 for i,j in zip(reversed(outn),reversed(outs)): print(f'{i},{j}') #if ind != n: print() #ind += 1 #t = zip(scores, names) #t = sorted(t,reverse=True) #names,scores = zip(*t) #ind = 0 #for i,j in t: #print(f'{j},{i}',end='') #if ind != len(t)-1: print() #ind += 1 cnt += 1 ```
output
1
11,543
17
23,087
Provide a correct Python 3 solution for this coding contest problem. There are league games and tournament games in sports competitions. In soccer league games, points are given to each of the wins, losses, and draws, and the rankings are competed based on the points. The points are win (3 points), negative (0 points), and draw (1 point), respectively. Enter the number of teams and the results of the league match, sort them in order of best results (in descending order of points), and create a program that outputs the team name and points. If the points are tied, output in the order of input. Input Given multiple datasets. Each dataset is given in the following format: n name1 w1 l1 d1 name2 w2 l2 d2 :: namen wn ln dn The number of teams n (n ≤ 10) is given on the first line. The next n lines are given the name of team i (alphabet of up to 20 characters), the number of wins wi, the number of negatives li, and the number of draws di (0 ≤ wi, li, di ≤ 9), separated by spaces. .. When the number of teams is 0, the input is completed. The number of datasets does not exceed 50. Output Print a sorted list of teams for each dataset. Print the name of the i-th team and the points on the i-line, separated by commas. Insert one blank line between the datasets. Example Input 4 Japan 1 0 2 Egypt 1 2 0 Canada 0 2 1 Spain 2 0 1 3 India 0 2 0 Poland 1 0 1 Italy 1 0 1 0 Output Spain,7 Japan,5 Egypt,3 Canada,1 Poland,4 Italy,4 India,0
instruction
0
11,544
17
23,088
"Correct Solution: ``` f = True while True: n = int(input()) if n == 0: break tbl = [] for i in range(n): name, w, l, d = list(input().split()) tbl.append([name, i, 3*int(w)+int(d)]) if f: f = False else: print() for i in sorted(tbl, key=lambda x:(-x[2],x[1])): print(i[0], ',', i[2], sep='') ```
output
1
11,544
17
23,089
Provide a correct Python 3 solution for this coding contest problem. There are league games and tournament games in sports competitions. In soccer league games, points are given to each of the wins, losses, and draws, and the rankings are competed based on the points. The points are win (3 points), negative (0 points), and draw (1 point), respectively. Enter the number of teams and the results of the league match, sort them in order of best results (in descending order of points), and create a program that outputs the team name and points. If the points are tied, output in the order of input. Input Given multiple datasets. Each dataset is given in the following format: n name1 w1 l1 d1 name2 w2 l2 d2 :: namen wn ln dn The number of teams n (n ≤ 10) is given on the first line. The next n lines are given the name of team i (alphabet of up to 20 characters), the number of wins wi, the number of negatives li, and the number of draws di (0 ≤ wi, li, di ≤ 9), separated by spaces. .. When the number of teams is 0, the input is completed. The number of datasets does not exceed 50. Output Print a sorted list of teams for each dataset. Print the name of the i-th team and the points on the i-line, separated by commas. Insert one blank line between the datasets. Example Input 4 Japan 1 0 2 Egypt 1 2 0 Canada 0 2 1 Spain 2 0 1 3 India 0 2 0 Poland 1 0 1 Italy 1 0 1 0 Output Spain,7 Japan,5 Egypt,3 Canada,1 Poland,4 Italy,4 India,0
instruction
0
11,545
17
23,090
"Correct Solution: ``` st=True while True: n=int(input()) if n==0: break if st==False: print() team={} for _ in range(n): data=input().split(" ") team[data[0]]=3*int(data[1])+int(data[3]) team=team.items() team=sorted(team,key=lambda w:w[1],reverse=True) for t in team: print("{0},{1}".format(*t)) st=False ```
output
1
11,545
17
23,091
Provide a correct Python 3 solution for this coding contest problem. There are league games and tournament games in sports competitions. In soccer league games, points are given to each of the wins, losses, and draws, and the rankings are competed based on the points. The points are win (3 points), negative (0 points), and draw (1 point), respectively. Enter the number of teams and the results of the league match, sort them in order of best results (in descending order of points), and create a program that outputs the team name and points. If the points are tied, output in the order of input. Input Given multiple datasets. Each dataset is given in the following format: n name1 w1 l1 d1 name2 w2 l2 d2 :: namen wn ln dn The number of teams n (n ≤ 10) is given on the first line. The next n lines are given the name of team i (alphabet of up to 20 characters), the number of wins wi, the number of negatives li, and the number of draws di (0 ≤ wi, li, di ≤ 9), separated by spaces. .. When the number of teams is 0, the input is completed. The number of datasets does not exceed 50. Output Print a sorted list of teams for each dataset. Print the name of the i-th team and the points on the i-line, separated by commas. Insert one blank line between the datasets. Example Input 4 Japan 1 0 2 Egypt 1 2 0 Canada 0 2 1 Spain 2 0 1 3 India 0 2 0 Poland 1 0 1 Italy 1 0 1 0 Output Spain,7 Japan,5 Egypt,3 Canada,1 Poland,4 Italy,4 India,0
instruction
0
11,546
17
23,092
"Correct Solution: ``` # -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0124 """ import sys from operator import itemgetter def solve(data): result = [] for name, win, lose, draw in data: score = win * 3 + draw * 1 result.append([name, score]) result.sort(key=itemgetter(1), reverse=True) return result def main(args): first_data_set = True while True: n = int(input()) if n == 0: break data = [] for _ in range(n): name, win, lose, draw = input().split() data.append([name, int(win), int(lose), int(draw)]) result = solve(data) if first_data_set: first_data_set = False else: print() for r in result: print(','.join(map(str, r))) if __name__ == '__main__': main(sys.argv[1:]) ```
output
1
11,546
17
23,093
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are league games and tournament games in sports competitions. In soccer league games, points are given to each of the wins, losses, and draws, and the rankings are competed based on the points. The points are win (3 points), negative (0 points), and draw (1 point), respectively. Enter the number of teams and the results of the league match, sort them in order of best results (in descending order of points), and create a program that outputs the team name and points. If the points are tied, output in the order of input. Input Given multiple datasets. Each dataset is given in the following format: n name1 w1 l1 d1 name2 w2 l2 d2 :: namen wn ln dn The number of teams n (n ≤ 10) is given on the first line. The next n lines are given the name of team i (alphabet of up to 20 characters), the number of wins wi, the number of negatives li, and the number of draws di (0 ≤ wi, li, di ≤ 9), separated by spaces. .. When the number of teams is 0, the input is completed. The number of datasets does not exceed 50. Output Print a sorted list of teams for each dataset. Print the name of the i-th team and the points on the i-line, separated by commas. Insert one blank line between the datasets. Example Input 4 Japan 1 0 2 Egypt 1 2 0 Canada 0 2 1 Spain 2 0 1 3 India 0 2 0 Poland 1 0 1 Italy 1 0 1 0 Output Spain,7 Japan,5 Egypt,3 Canada,1 Poland,4 Italy,4 India,0 Submitted Solution: ``` a = int(input()) while True: s = [] for i in range(a): b,c,d,e = input().split() c = int(c) e = int(e) s.append([c*3+e,-(i),b]) s.sort(reverse = True) for z in s: print(z[2]+","+str(z[0])) a = int(input()) if a == 0: break else: print() ```
instruction
0
11,547
17
23,094
Yes
output
1
11,547
17
23,095
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are league games and tournament games in sports competitions. In soccer league games, points are given to each of the wins, losses, and draws, and the rankings are competed based on the points. The points are win (3 points), negative (0 points), and draw (1 point), respectively. Enter the number of teams and the results of the league match, sort them in order of best results (in descending order of points), and create a program that outputs the team name and points. If the points are tied, output in the order of input. Input Given multiple datasets. Each dataset is given in the following format: n name1 w1 l1 d1 name2 w2 l2 d2 :: namen wn ln dn The number of teams n (n ≤ 10) is given on the first line. The next n lines are given the name of team i (alphabet of up to 20 characters), the number of wins wi, the number of negatives li, and the number of draws di (0 ≤ wi, li, di ≤ 9), separated by spaces. .. When the number of teams is 0, the input is completed. The number of datasets does not exceed 50. Output Print a sorted list of teams for each dataset. Print the name of the i-th team and the points on the i-line, separated by commas. Insert one blank line between the datasets. Example Input 4 Japan 1 0 2 Egypt 1 2 0 Canada 0 2 1 Spain 2 0 1 3 India 0 2 0 Poland 1 0 1 Italy 1 0 1 0 Output Spain,7 Japan,5 Egypt,3 Canada,1 Poland,4 Italy,4 India,0 Submitted Solution: ``` # -*- coding: utf-8 -*- import sys import os import math for i, s in enumerate(sys.stdin): n = int(s) if n == 0: break elif i != 0: print() A = [] for i in range(n): lst = input().split() name = lst[0] w = int(lst[1]) l = int(lst[2]) d = int(lst[3]) score = 3 * w + d A.append((score, -i, name)) A.sort(reverse=True) for a in A: score = a[0] name = a[2] print('{},{}'.format(name, score)) ```
instruction
0
11,548
17
23,096
Yes
output
1
11,548
17
23,097
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are league games and tournament games in sports competitions. In soccer league games, points are given to each of the wins, losses, and draws, and the rankings are competed based on the points. The points are win (3 points), negative (0 points), and draw (1 point), respectively. Enter the number of teams and the results of the league match, sort them in order of best results (in descending order of points), and create a program that outputs the team name and points. If the points are tied, output in the order of input. Input Given multiple datasets. Each dataset is given in the following format: n name1 w1 l1 d1 name2 w2 l2 d2 :: namen wn ln dn The number of teams n (n ≤ 10) is given on the first line. The next n lines are given the name of team i (alphabet of up to 20 characters), the number of wins wi, the number of negatives li, and the number of draws di (0 ≤ wi, li, di ≤ 9), separated by spaces. .. When the number of teams is 0, the input is completed. The number of datasets does not exceed 50. Output Print a sorted list of teams for each dataset. Print the name of the i-th team and the points on the i-line, separated by commas. Insert one blank line between the datasets. Example Input 4 Japan 1 0 2 Egypt 1 2 0 Canada 0 2 1 Spain 2 0 1 3 India 0 2 0 Poland 1 0 1 Italy 1 0 1 0 Output Spain,7 Japan,5 Egypt,3 Canada,1 Poland,4 Italy,4 India,0 Submitted Solution: ``` c = 0 while True: c +=1 L = [] num = int(input()) if num == 0: break for i in range(num): name, w, l, d = input().split() w = int(w) d = int(d) L.append((i,name,w*3+d)) L.sort(key=lambda x: (-x[2],x[0])) if c > 1: print() for l in L: print("{},{}".format(l[1], l[2])) ```
instruction
0
11,549
17
23,098
Yes
output
1
11,549
17
23,099
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are league games and tournament games in sports competitions. In soccer league games, points are given to each of the wins, losses, and draws, and the rankings are competed based on the points. The points are win (3 points), negative (0 points), and draw (1 point), respectively. Enter the number of teams and the results of the league match, sort them in order of best results (in descending order of points), and create a program that outputs the team name and points. If the points are tied, output in the order of input. Input Given multiple datasets. Each dataset is given in the following format: n name1 w1 l1 d1 name2 w2 l2 d2 :: namen wn ln dn The number of teams n (n ≤ 10) is given on the first line. The next n lines are given the name of team i (alphabet of up to 20 characters), the number of wins wi, the number of negatives li, and the number of draws di (0 ≤ wi, li, di ≤ 9), separated by spaces. .. When the number of teams is 0, the input is completed. The number of datasets does not exceed 50. Output Print a sorted list of teams for each dataset. Print the name of the i-th team and the points on the i-line, separated by commas. Insert one blank line between the datasets. Example Input 4 Japan 1 0 2 Egypt 1 2 0 Canada 0 2 1 Spain 2 0 1 3 India 0 2 0 Poland 1 0 1 Italy 1 0 1 0 Output Spain,7 Japan,5 Egypt,3 Canada,1 Poland,4 Italy,4 India,0 Submitted Solution: ``` b = False while True: n = int(input()) if n == 0: break dataset = [] if b: print() b = True for _ in range(n): name, w, l, d = input().split() dataset.append((name, 3*int(w) + int(d), n-len(dataset))) for name, score, _ in sorted(dataset, key=lambda x: (x[1], x[2]))[::-1]: print(name + "," + str(score)) ```
instruction
0
11,550
17
23,100
Yes
output
1
11,550
17
23,101
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are league games and tournament games in sports competitions. In soccer league games, points are given to each of the wins, losses, and draws, and the rankings are competed based on the points. The points are win (3 points), negative (0 points), and draw (1 point), respectively. Enter the number of teams and the results of the league match, sort them in order of best results (in descending order of points), and create a program that outputs the team name and points. If the points are tied, output in the order of input. Input Given multiple datasets. Each dataset is given in the following format: n name1 w1 l1 d1 name2 w2 l2 d2 :: namen wn ln dn The number of teams n (n ≤ 10) is given on the first line. The next n lines are given the name of team i (alphabet of up to 20 characters), the number of wins wi, the number of negatives li, and the number of draws di (0 ≤ wi, li, di ≤ 9), separated by spaces. .. When the number of teams is 0, the input is completed. The number of datasets does not exceed 50. Output Print a sorted list of teams for each dataset. Print the name of the i-th team and the points on the i-line, separated by commas. Insert one blank line between the datasets. Example Input 4 Japan 1 0 2 Egypt 1 2 0 Canada 0 2 1 Spain 2 0 1 3 India 0 2 0 Poland 1 0 1 Italy 1 0 1 0 Output Spain,7 Japan,5 Egypt,3 Canada,1 Poland,4 Italy,4 India,0 Submitted Solution: ``` def main(): while True: n = int(input()) if n == 0: break teams = [] for i in range(n): row = input().split() teams.append([row[0], 0]) s_t = 0 for e, p in zip(row[1:], [3, 0, 1]): s_t += int(e) * p teams[i][1] = s_t for t in sorted(teams, key=lambda x: x[1], reverse=True): print("{},{}".format(t[0], t[1])) print() if __name__ == "__main__": main() ```
instruction
0
11,551
17
23,102
No
output
1
11,551
17
23,103
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are league games and tournament games in sports competitions. In soccer league games, points are given to each of the wins, losses, and draws, and the rankings are competed based on the points. The points are win (3 points), negative (0 points), and draw (1 point), respectively. Enter the number of teams and the results of the league match, sort them in order of best results (in descending order of points), and create a program that outputs the team name and points. If the points are tied, output in the order of input. Input Given multiple datasets. Each dataset is given in the following format: n name1 w1 l1 d1 name2 w2 l2 d2 :: namen wn ln dn The number of teams n (n ≤ 10) is given on the first line. The next n lines are given the name of team i (alphabet of up to 20 characters), the number of wins wi, the number of negatives li, and the number of draws di (0 ≤ wi, li, di ≤ 9), separated by spaces. .. When the number of teams is 0, the input is completed. The number of datasets does not exceed 50. Output Print a sorted list of teams for each dataset. Print the name of the i-th team and the points on the i-line, separated by commas. Insert one blank line between the datasets. Example Input 4 Japan 1 0 2 Egypt 1 2 0 Canada 0 2 1 Spain 2 0 1 3 India 0 2 0 Poland 1 0 1 Italy 1 0 1 0 Output Spain,7 Japan,5 Egypt,3 Canada,1 Poland,4 Italy,4 India,0 Submitted Solution: ``` def main(): while True: n = int(input()) if n == 0: break teams = [] for i in range(n): row = input().split() teams.append([row[0], 0]) s_t = 0 for e, p in zip(row[1:], [3, 0, 1]): s_t += int(e) * p teams[i][1] = s_t for t in sorted(teams, key=lambda x: x[1], reverse=True): print("{},{}".format(t[0], t[1])) if __name__ == "__main__": main() ```
instruction
0
11,552
17
23,104
No
output
1
11,552
17
23,105
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are league games and tournament games in sports competitions. In soccer league games, points are given to each of the wins, losses, and draws, and the rankings are competed based on the points. The points are win (3 points), negative (0 points), and draw (1 point), respectively. Enter the number of teams and the results of the league match, sort them in order of best results (in descending order of points), and create a program that outputs the team name and points. If the points are tied, output in the order of input. Input Given multiple datasets. Each dataset is given in the following format: n name1 w1 l1 d1 name2 w2 l2 d2 :: namen wn ln dn The number of teams n (n ≤ 10) is given on the first line. The next n lines are given the name of team i (alphabet of up to 20 characters), the number of wins wi, the number of negatives li, and the number of draws di (0 ≤ wi, li, di ≤ 9), separated by spaces. .. When the number of teams is 0, the input is completed. The number of datasets does not exceed 50. Output Print a sorted list of teams for each dataset. Print the name of the i-th team and the points on the i-line, separated by commas. Insert one blank line between the datasets. Example Input 4 Japan 1 0 2 Egypt 1 2 0 Canada 0 2 1 Spain 2 0 1 3 India 0 2 0 Poland 1 0 1 Italy 1 0 1 0 Output Spain,7 Japan,5 Egypt,3 Canada,1 Poland,4 Italy,4 India,0 Submitted Solution: ``` while True: n = int(input()) if n == 0: break dataset = [] for _ in range(n): name, w, l, d = input().split() dataset.append((name, 3*int(w) + int(d), n-len(dataset))) for name, score, _ in sorted(dataset, key=lambda x: (x[1], x[2]))[::-1]: print(name + "," + str(score)) ```
instruction
0
11,553
17
23,106
No
output
1
11,553
17
23,107
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are league games and tournament games in sports competitions. In soccer league games, points are given to each of the wins, losses, and draws, and the rankings are competed based on the points. The points are win (3 points), negative (0 points), and draw (1 point), respectively. Enter the number of teams and the results of the league match, sort them in order of best results (in descending order of points), and create a program that outputs the team name and points. If the points are tied, output in the order of input. Input Given multiple datasets. Each dataset is given in the following format: n name1 w1 l1 d1 name2 w2 l2 d2 :: namen wn ln dn The number of teams n (n ≤ 10) is given on the first line. The next n lines are given the name of team i (alphabet of up to 20 characters), the number of wins wi, the number of negatives li, and the number of draws di (0 ≤ wi, li, di ≤ 9), separated by spaces. .. When the number of teams is 0, the input is completed. The number of datasets does not exceed 50. Output Print a sorted list of teams for each dataset. Print the name of the i-th team and the points on the i-line, separated by commas. Insert one blank line between the datasets. Example Input 4 Japan 1 0 2 Egypt 1 2 0 Canada 0 2 1 Spain 2 0 1 3 India 0 2 0 Poland 1 0 1 Italy 1 0 1 0 Output Spain,7 Japan,5 Egypt,3 Canada,1 Poland,4 Italy,4 India,0 Submitted Solution: ``` b=False while True: n = int(input()) if n==0:break d=dict() if b:print() for _ in range(n): t = input().split() d[t[0]]=int(t[1])*3+int(t[3])*1 b=True for s in sorted(d.items(), key=lambda x:x[1])[::-1]: print('{0},{1}'.format(s[0],s[1])) print() ```
instruction
0
11,554
17
23,108
No
output
1
11,554
17
23,109
Provide a correct Python 3 solution for this coding contest problem. In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ≠ b). That is, w = wb − wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ≠ b). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb − wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Example Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output 1 -1 UNKNOWN 100 200 -50
instruction
0
11,581
17
23,162
"Correct Solution: ``` def solve(): def measurement(a, b, w): a_root = root[a] b_root = root[b] if a_root != b_root: a_member = member[a_root] b_member = member[b_root] offset = w - (weight[b] - weight[a]) if len(a_member) > len(b_member): a_member.extend(b_member) for n in b_member: root[n] = a_root weight[n] += offset else: b_member.extend(a_member) for n in a_member: root[n] = b_root weight[n] -= offset def inquiry(a, b): if root[a] == root[b]: return weight[b] - weight[a] else: return 'UNKNOWN' import sys file_input = sys.stdin.readlines() ans = [] while True: N, M = map(int, file_input[0].split()) if N == 0: break root = [i for i in range(N + 1)] member = [[i] for i in range(N + 1)] weight = [0] * (N + 1) for line in file_input[1:M+1]: if line[0] == '!': a, b, w = map(int, line[2:].split()) measurement(a, b, w) else: a, b= map(int, line[2:].split()) ans.append(inquiry(a, b)) del file_input[:M+1] print(*ans, sep='\n') solve() ```
output
1
11,581
17
23,163
Provide a correct Python 3 solution for this coding contest problem. In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ≠ b). That is, w = wb − wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ≠ b). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb − wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Example Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output 1 -1 UNKNOWN 100 200 -50
instruction
0
11,582
17
23,164
"Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**13 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) class UnionFind: def __init__(self, size): self.table = [-1 for _ in range(size)] def find(self, x): if self.table[x] < 0: return x else: self.table[x] = self.find(self.table[x]) return self.table[x] def union(self, x, y): s1 = self.find(x) s2 = self.find(y) if s1 != s2: if self.table[s1] <= self.table[s2]: self.table[s1] += self.table[s2] self.table[s2] = s1 else: self.table[s2] += self.table[s1] self.table[s1] = s2 return True return False def subsetall(self): a = [] for i in range(len(self.table)): if self.table[i] < 0: a.append((i, -self.table[i])) return a def main(): random.seed(42) rr = [] def f(n, m): qa = [LS() for _ in range(m)] d = collections.defaultdict(lambda: collections.defaultdict(int)) for i in range(n+1): d[i][i] = random.randrange(1,100) r = [] uf = UnionFind(n+1) for q in qa: if q[0] == '!': a,b,w = map(int, q[1:]) fa = uf.find(a) fb = uf.find(b) if fa == fb: continue uf.union(a,b) # print('a,b',a,b) # print('dfa', d[fa]) # print('dfb', d[fb]) if fa == uf.find(a): sa = w + (d[fa][a] - d[fb][b]) for k in d[fb].keys(): d[fa][k] = d[fb][k] + sa else: sa = (d[fa][a] - d[fb][b]) + w for k in d[fa].keys(): d[fb][k] = d[fa][k] - sa # print('sa',sa) # print('dfa', d[fa]) # print('dfb', d[fb]) else: a,b = map(int , q[1:]) fa = uf.find(a) if fa != uf.find(b): r.append('UNKNOWN') else: r.append(d[fa][b] - d[fa][a]) # for k in d.keys(): # print('k,d',k,d[k]) return r while 1: n,m = LI() if n == 0 and m == 0: break rr.extend(f(n,m)) return '\n'.join(map(str,rr)) print(main()) ```
output
1
11,582
17
23,165
Provide a correct Python 3 solution for this coding contest problem. In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ≠ b). That is, w = wb − wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ≠ b). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb − wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Example Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output 1 -1 UNKNOWN 100 200 -50
instruction
0
11,583
17
23,166
"Correct Solution: ``` import sys sys.setrecursionlimit(10000000) MOD = 10 ** 9 + 7 INF = 10 ** 15 class WeightedUnionFind(): def __init__(self,N): self.N = N self.parents = [-1] * self.N self.diff_weight = [0] * self.N def find(self,x): if self.parents[x] < 0: return x else: p = self.find(self.parents[x]) self.diff_weight[x] += self.diff_weight[self.parents[x]] self.parents[x] = p return p def weight(self,x):#xの親からの重みを返す self.find(x) return self.diff_weight[x] def diff(self,x,y):#同じグループにいたらxとyの重みの差を返す if self.find(x) == self.find(y): return self.weight(y) - self.weight(x) else: return 10000000000 def unite(self,x,y,w):#wieght(y) = weight(x) + dとなるように合体 w += self.weight(x) w -= self.weight(y) x = self.find(x) y = self.find(y) if x == y: if self.diff(x,y) == w: return True else: return False if self.parents[x] > self.parents[y]: x,y = y,x w *= -1 self.parents[x] += self.parents[y] self.parents[y] = x self.diff_weight[y] = w return True def same(self,x,y): return self.find(x) == self.find(y) def size(self,x): return -self.parents[self.find(x)] def solve(N,M): uf = WeightedUnionFind(N) for _ in range(M): q = input().split() if q[0] == '!': a,b,w = map(int,q[1:]) a -= 1 b -= 1 uf.unite(a,b,w) else: a,b = map(int,q[1:]) a -= 1 b -= 1 ans = uf.diff(a,b) if ans == 10000000000: print('UNKNOWN') else: print(ans) def main(): while True: N,M = map(int,input().split()) if N == 0 and M == 0: break solve(N,M) if __name__ == '__main__': main() ```
output
1
11,583
17
23,167
Provide a correct Python 3 solution for this coding contest problem. In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ≠ b). That is, w = wb − wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ≠ b). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb − wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Example Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output 1 -1 UNKNOWN 100 200 -50
instruction
0
11,584
17
23,168
"Correct Solution: ``` class WeightedUnionFind(object): __slots__ = ["nodes", "weight"] def __init__(self, n: int) -> None: self.nodes = [-1]*n self.weight = [0]*n def get_root(self, x: int) -> int: if x < 0: raise ValueError("Negative Index") if self.nodes[x] < 0: return x else: root = self.get_root(self.nodes[x]) self.weight[x] += self.weight[self.nodes[x]] self.nodes[x] = root return root def relate(self, smaller: int, bigger: int, diff_weight: int) -> None: if smaller < 0 or bigger < 0: raise ValueError("Negative Index") root_a, root_b = self.get_root(smaller), self.get_root(bigger) new_weight = diff_weight + self.weight[smaller] - self.weight[bigger] if root_a == root_b: # 問題によっては必要かも(情報に矛盾があるなら-1を出力など) if self.weight[smaller] + diff_weight == self.weight[bigger]: return raise ValueError("relateに矛盾あり") if self.nodes[root_a] > self.nodes[root_b]: root_a, root_b, new_weight = root_b, root_a, -new_weight self.nodes[root_a] += self.nodes[root_b] self.nodes[root_b] = root_a self.weight[root_b] = new_weight def diff(self, x: int, y: int) -> int: root_x, root_y = self.get_root(x), self.get_root(y) if root_x != root_y: return None return self.weight[y] - self.weight[x] import sys while True: N, M = map(int, input().split()) if N == 0: break uf = WeightedUnionFind(N+1) for qtype, a, b, *c in (sys.stdin.readline().split() for _ in [0]*M): if qtype == "!": uf.relate(int(a), int(b), int(c[0])) else: diff = uf.diff(int(a), int(b)) print(diff if diff is not None else "UNKNOWN") ```
output
1
11,584
17
23,169
Provide a correct Python 3 solution for this coding contest problem. In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ≠ b). That is, w = wb − wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ≠ b). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb − wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Example Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output 1 -1 UNKNOWN 100 200 -50
instruction
0
11,585
17
23,170
"Correct Solution: ``` class Value_UnionFind(): def __init__(self,n): self.par = [i for i in range(n)] self.differ_weight = [0] * n self.rank = [0] * n def root(self,x): if x == self.par[x]: return x r = self.root(self.par[x]) self.differ_weight[x] += self.differ_weight[self.par[x]] self.par[x] = r return r def weight(self, x): self.root(x) return self.differ_weight[x] def unit(self, x, y, w): w += self.weight(x) w -= self.weight(y) x = self.root(x) y = self.root(y) if x == y: return False if self.rank[x] < self.rank[y]: x, y, w = y, x, -w if self.rank[x] == self.rank[y]: self.rank[x] += 1 self.par[y] = x self.differ_weight[y] = w return True def differ(self, x, y): return self.weight(y) - self.weight(x) def main(n, m): Union = Value_UnionFind(n) for _ in range(m): x = list(map(str, input().split())) q = x[0] if q == "!": a, b, w = map(int, x[1:]) Union.unit(a-1, b-1, w) if q == "?": a, b = map(int,x[1:]) if Union.root(a-1) != Union.root(b-1): print("UNKNOWN") else: print(Union.differ(a-1, b-1)) while 1: n, m = map(int, input().split()) if n == m == 0: break main(n,m) ```
output
1
11,585
17
23,171
Provide a correct Python 3 solution for this coding contest problem. In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ≠ b). That is, w = wb − wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ≠ b). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb − wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Example Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output 1 -1 UNKNOWN 100 200 -50
instruction
0
11,586
17
23,172
"Correct Solution: ``` def solve(): def measurement(a, b, w): a_root = root[a] b_root = root[b] if a_root != b_root: a_member = member[a_root] b_member = member[b_root] offset = w - (weight[b] - weight[a]) if len(a_member) > len(b_member): a_member.extend(b_member) for n in b_member: root[n] = a_root weight[n] += offset else: b_member.extend(a_member) for n in a_member: root[n] = b_root weight[n] -= offset def inquiry(a, b): if root[a] == root[b]: return weight[b] - weight[a] else: return 'UNKNOWN' import sys file_input = sys.stdin while True: N, M = map(int, file_input.readline().split()) if N == 0: break root = [i for i in range(N + 1)] member = [[i] for i in range(N + 1)] weight = [0] * (N + 1) for _ in range(M): line = file_input.readline() if line[0] == '!': a, b, w = map(int, line[2:].split()) measurement(a, b, w) else: a, b= map(int, line[2:].split()) print(inquiry(a, b)) solve() ```
output
1
11,586
17
23,173
Provide a correct Python 3 solution for this coding contest problem. In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ≠ b). That is, w = wb − wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ≠ b). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb − wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Example Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output 1 -1 UNKNOWN 100 200 -50
instruction
0
11,587
17
23,174
"Correct Solution: ``` def solve(): def measurement(a, b, w): a_root = root[a] b_root = root[b] if a_root != b_root: a_member = member[a_root] b_member = member[b_root] offset = w - (weight[b] - weight[a]) if len(a_member) > len(b_member): a_member.extend(b_member) for n in b_member: root[n] = a_root weight[n] += offset else: b_member.extend(a_member) for n in a_member: root[n] = b_root weight[n] -= offset def inquiry(a, b): if root[a] == root[b]: return weight[b] - weight[a] else: return 'UNKNOWN' def operation(line): if line[0] == '!': a, b, w = map(int, line[2:].split()) measurement(a, b, w) else: a, b = map(int, line[2:].split()) return inquiry(a, b) import sys file_input = sys.stdin.readlines() while True: N, M = map(int, file_input[0].split()) if N == 0: break root = [i for i in range(N + 1)] member = [[i] for i in range(N + 1)] weight = [0] * (N + 1) ans = (operation(line) for line in file_input[1:M+1]) ans = filter(lambda x: x != None, ans) print(*ans, sep='\n') del file_input[:M+1] solve() ```
output
1
11,587
17
23,175
Provide a correct Python 3 solution for this coding contest problem. In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ≠ b). That is, w = wb − wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ≠ b). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb − wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Example Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output 1 -1 UNKNOWN 100 200 -50
instruction
0
11,588
17
23,176
"Correct Solution: ``` import sys sys.setrecursionlimit(10 ** 5) # 連結性の確認 class UnionFind(): def __init__(self, n): self.parents = list(range(n)) self.weight = [0] * n self.rank = [0] * n def find(self, a): if self.parents[a] == a: return a p = self.find(self.parents[a]) self.weight[a] += self.weight[self.parents[a]] self.parents[a] = p return p def union(self, a, b, w): self.find(a), self.find(b) w += self.weight[a] w -= self.weight[b] a = self.find(a) b = self.find(b) if a == b: return if self.rank[a] < self.rank[b]: self.parents[a] = b self.weight[a] = -w else: self.parents[b] = a self.weight[b] = w if self.rank[a] == self.rank[b]: self.rank[a] += 1 def weight_check(self, a, b): self.find(a), self.find(b) return - self.weight[a] + self.weight[b] while True: N, M = map(int, input().split()) if not(N | M): break uf = UnionFind(N) for i in range(M): input_str = input().split() if input_str[0] == '!': a, b, w = map(int, input_str[1:]) uf.union(a - 1, b - 1, w) else: a, b = map(lambda x: int(x) - 1, input_str[1:]) if uf.find(a) != uf.find(b): print('UNKNOWN') continue print(uf.weight_check(a, b)) ```
output
1
11,588
17
23,177
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ≠ b). That is, w = wb − wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ≠ b). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb − wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Example Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output 1 -1 UNKNOWN 100 200 -50 Submitted Solution: ``` def solve(): def measurement(a, b, w): a_root = root[a] b_root = root[b] if a_root != b_root: a_member = member[a_root] b_member = member[b_root] offset = w - (weight[b] - weight[a]) if len(a_member) > len(b_member): a_member.extend(b_member) for n in b_member: root[n] = a_root weight[n] += offset else: b_member.extend(a_member) for n in a_member: root[n] = b_root weight[n] -= offset def inquiry(a, b): if root[a] == root[b]: return weight[b] - weight[a] else: return 'UNKNOWN' import sys file_input = sys.stdin.readlines() while True: N, M = map(int, file_input[0].split()) if N == 0: break root = [i for i in range(N + 1)] member = [[i] for i in range(N + 1)] weight = [0] * (N + 1) for line in file_input[1:M+1]: if line[0] == '!': a, b, w = map(int, line[2:].split()) measurement(a, b, w) else: a, b= map(int, line[2:].split()) print(inquiry(a, b)) del file_input[:M+1] solve() ```
instruction
0
11,589
17
23,178
Yes
output
1
11,589
17
23,179
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ≠ b). That is, w = wb − wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ≠ b). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb − wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Example Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output 1 -1 UNKNOWN 100 200 -50 Submitted Solution: ``` import sys sys.setrecursionlimit(10**6) def root(x): if x == parent[x]: return x y = root(parent[x]) relative[x] += relative[parent[x]] parent[x] = y return y def unite(a, b, w): pa = root(a); pb = root(b) pw = relative[a] + w - relative[b] if pa < pb: parent[pb] = pa relative[pb] = pw else: parent[pa] = pb relative[pa] = -pw while 1: N, M = map(int, input().split()) if N == M == 0: break *parent, = range(N) relative = [0]*N for i in range(M): cmd = input().split() if cmd[0] == '!': a, b, w = map(int, cmd[1:]) unite(a-1, b-1, w) else: a, b = map(int, cmd[1:]) if root(a-1) != root(b-1): print("UNKNOWN") else: print(relative[b-1] - relative[a-1]) ```
instruction
0
11,590
17
23,180
Yes
output
1
11,590
17
23,181
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ≠ b). That is, w = wb − wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ≠ b). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb − wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Example Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output 1 -1 UNKNOWN 100 200 -50 Submitted Solution: ``` class WeightedUnionFind: def __init__(self, n): self.par = [i for i in range(n+1)] self.rank = [0] * (n+1) self.weight = [0] * (n+1) # 検索 def find(self, x): if self.par[x] == x: return x else: y = self.find(self.par[x]) self.weight[x] += self.weight[self.par[x]] self.par[x] = y return y # 重さの検索 def weighting(self, x): self.find(x) return self.weight[x] # 併合 def union(self, x, y, w): px = self.find(x) py = self.find(y) if px != py: if self.rank[px] < self.rank[py]: self.par[px] = py self.weight[px] = w - self.weight[x] + self.weight[y] else: self.par[py] = px self.weight[py] = -w - self.weight[y] + self.weight[x] if self.rank[px] == self.rank[py]: self.rank[px] += 1 # 同じ集合に属するか def same(self, x, y): return self.find(x) == self.find(y) # 各頂点間の絶対距離 def diff(self, x, y): return self.weight[x] - self.weight[y] N, M = 1, 1 while True: N, M = map(int, input().split()) if (N == 0) & (M == 0): quit() info = [list(input().split()) for i in range(M)] wuf = WeightedUnionFind(N) for i in range(M): if info[i][0] == "!": wuf.union(int(info[i][1]), int(info[i][2]), int(info[i][3])) #print("parent:", wuf.par) #print("weight:", wuf.weight) #print("rank:", wuf.rank, "\n") else: if wuf.same(int(info[i][1]), int(info[i][2])): print(wuf.diff(int(info[i][1]), int(info[i][2]))) #print("parent:", wuf.par) #print("weight:", wuf.weight) #print("rank:", wuf.rank, "\n") else: print("UNKNOWN") #print("parent:", wuf.par) #print("weight:", wuf.weight) #print("rank:", wuf.rank, "\n") ```
instruction
0
11,591
17
23,182
Yes
output
1
11,591
17
23,183
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ≠ b). That is, w = wb − wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ≠ b). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb − wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Example Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output 1 -1 UNKNOWN 100 200 -50 Submitted Solution: ``` # Author: cr4zjh0bp # Created: Fri Mar 20 22:48:43 UTC 2020 import sys stdin = sys.stdin inf = 1 << 60 mod = 1000000007 ni = lambda: int(ns()) nin = lambda y: [ni() for _ in range(y)] na = lambda: list(map(int, stdin.readline().split())) nan = lambda y: [na() for _ in range(y)] nf = lambda: float(ns()) nfn = lambda y: [nf() for _ in range(y)] nfa = lambda: list(map(float, stdin.readline().split())) nfan = lambda y: [nfa() for _ in range(y)] ns = lambda: stdin.readline().rstrip() nsn = lambda y: [ns() for _ in range(y)] ncl = lambda y: [list(ns()) for _ in range(y)] nas = lambda: stdin.readline().split() class WUnionFind: def __init__(self, n, sum_unity=0): self.n = n self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.diff_weight = [sum_unity for _ in range(n)] self._size = [1 for _ in range(n)] self._edges = 0 def find(self, x): if self.par[x] == x: return x else: r = self.find(self.par[x]) self.diff_weight[x] += self.diff_weight[self.par[x]] self.par[x] = r return r def unite(self, x, y, w): w += self.weight(x) w -= self.weight(y) x = self.find(x) y = self.find(y) if x == y: return if self.rank[x] < self.rank[y]: self.par[x] = y self.diff_weight[x] = -w self._size[y] += self._size[x] self._edges += 1 else: self.par[y] = x self.diff_weight[y] = w if self.rank[x] == self.rank[y]: self.rank[x] += 1 self._size[x] += self._size[y] self._edges += 1 def weight(self, x): self.find(x) return self.diff_weight[x] def diff(self, x, y): return self.weight(y) - self.weight(x) def size(self, x): x = self.find(x) return self._size[x] def trees(self): return self.n - self._edges def same(self, x, y): return self.find(x) == self.find(y) while True: n, m = na() if n == 0 and m == 0: break wuf = WUnionFind(n) for i in range(m): que = nas() if que[0] == '!': a, b, w = list(map(int, que[1:])) a -= 1 b -= 1 wuf.unite(a, b, w) elif que[0] == '?': a, b = list(map(int, que[1:])) a -= 1 b -= 1 if wuf.same(a, b): print(wuf.diff(a, b)) else: print("UNKNOWN") ```
instruction
0
11,592
17
23,184
Yes
output
1
11,592
17
23,185
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ≠ b). That is, w = wb − wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ≠ b). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb − wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Example Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output 1 -1 UNKNOWN 100 200 -50 Submitted Solution: ``` class WeightedUnionFind: def __init__(self, n): self.par = [i for i in range(n+1)] self.rank = [0] * (n+1) self.weight = [0] * (n+1) # 検索 def find(self, x): if self.par[x] == x: return x else: y = self.find(self.par[x]) self.weight[x] += self.weight[self.par[x]] self.par[x] = y return y # 重さの検索 def weighting(self, x): self.find(x) return self.weight[x] # 併合 def union(self, x, y, w): px = self.find(x) py = self.find(y) if px != py: if self.rank[px] < self.rank[py]: self.par[px] = py self.weight[px] = w - self.weighting(x) + self.weighting(y) else: self.par[py] = px self.weight[py] = -w - self.weighting(y) + self.weighting(x) if self.rank[px] == self.rank[py]: self.rank[px] += 1 # 同じ集合に属するか def same(self, x, y): return self.find(x) == self.find(y) # 各頂点間の絶対距離 def diff(self, x, y): return self.weighting(x) - self.weighting(y) N, M = 1, 1 while True: N, M = map(int, input().split()) if (N == 0) & (M == 0): quit() info = [list(input().split()) for i in range(M)] wuf = WeightedUnionFind(N) for i in range(M): if info[i][0] == "!": wuf.union(int(info[i][1]), int(info[i][2]), int(info[i][3])) #print("parent:", wuf.par) #print("weight:", wuf.weight) #print("rank:", wuf.rank, "\n") else: if wuf.same(int(info[i][1]), int(info[i][2])): print(wuf.diff(int(info[i][1]), int(info[i][2]))) #print("parent:", wuf.par) #print("weight:", wuf.weight) #print("rank:", wuf.rank, "\n") else: print("UNKNOWN") #print("parent:", wuf.par) #print("weight:", wuf.weight) #print("rank:", wuf.rank, "\n") ```
instruction
0
11,593
17
23,186
No
output
1
11,593
17
23,187
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ≠ b). That is, w = wb − wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ≠ b). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb − wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Example Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output 1 -1 UNKNOWN 100 200 -50 Submitted Solution: ``` class WeightedUnionFind: def __init__(self, n): self.par = [i for i in range(n+1)] self.rank = [0] * (n + 1) self.weight = [0] * (n+1) # 検索 def find(self, x): if self.par[x] == x: return x else: self.weight[x] += self.weight[self.par[x]] self.par[x] = self.find(self.par[x]) return self.par[x] # 重さの検索 def weighting(self, x): self.find(x) return self.weight[x] # 併合 def union(self, x, y, w): w -= self.weighting(x) w += self.weighting(y) x = self.find(x) y = self.find(y) if self.rank[x] < self.rank[y]: self.weight[y] = w self.par[y] = w else: self.weight[y] = -w self.par[y] = x if self.rank[x] == self.rank[y]: self.rank[x] += 1 # 同じ集合に属するか def same(self, x, y): return self.find(x) == self.find(y) # 各頂点間の絶対距離 def diff(self, x, y): return self.weighting(x) - self.weighting(y) N, M = 1, 1 while True: N, M = map(int, input().split()) if (N == 0) & (M == 0): quit() info = [list(input().split()) for i in range(M)] wuf = WeightedUnionFind(N) for i in range(M): if info[i][0] == "!": wuf.union(int(info[i][1]), int(info[i][2]), int(info[i][3])) else: if wuf.same(int(info[i][1]), int(info[i][2])): print(wuf.diff(int(info[i][1]), int(info[i][2]))) else: print("UNKNOWN") ```
instruction
0
11,594
17
23,188
No
output
1
11,594
17
23,189
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ≠ b). That is, w = wb − wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ≠ b). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb − wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Example Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output 1 -1 UNKNOWN 100 200 -50 Submitted Solution: ``` class WeightedUnionFind: def __init__(self, n): self.par = [i for i in range(n+1)] self.rank = [0] * (n + 1) self.weight = [0] * (n+1) # 検索 def find(self, x): if self.par[x] == x: return x else: self.weight[x] += self.weight[self.par[x]] self.par[x] = self.find(self.par[x]) return self.par[x] # 重さの検索 def weighting(self, x): self.find(x) return self.weight[x] # 併合 def union(self, x, y, w): w -= self.weighting(x) w += self.weighting(y) x = self.find(x) y = self.find(y) if self.rank[x] < self.rank[y]: self.weight[x] = w self.par[x] = y else: w = -w self.weight[y] = w self.par[y] = x if self.rank[x] == self.rank[y]: self.rank[x] += 1 # 同じ集合に属するか def same(self, x, y): return self.find(x) == self.find(y) # 各頂点間の絶対距離 def diff(self, x, y): return self.weighting(x) - self.weighting(y) N, M = 1, 1 while True: N, M = map(int, input().split()) if (N == 0) & (M == 0): quit() info = [list(input().split()) for i in range(M)] wuf = WeightedUnionFind(N) for i in range(M): if info[i][0] == "!": wuf.union(int(info[i][1]), int(info[i][2]), int(info[i][3])) else: if wuf.same(int(info[i][1]), int(info[i][2])): print(wuf.diff(int(info[i][1]), int(info[i][2]))) else: print("UNKNOWN") ```
instruction
0
11,595
17
23,190
No
output
1
11,595
17
23,191
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight. He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained. Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences. Input The input consists of multiple datasets. The first line of a dataset contains two integers N and M. N denotes the number of sample pieces (2 ≤ N ≤ 100,000). Each sample is assigned a unique number from 1 to N as an identifier. The rest of the dataset consists of M lines (1 ≤ M ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order. A measurement result has the format, ! a b w which represents the sample piece numbered b is heavier than one numbered a by w micrograms (a ≠ b). That is, w = wb − wa, where wa and wb are the weights of a and b, respectively. Here, w is a non-negative integer not exceeding 1,000,000. You may assume that all measurements are exact and consistent. An inquiry has the format, ? a b which asks the weight difference between the sample pieces numbered a and b (a ≠ b). The last dataset is followed by a line consisting of two zeros separated by a space. Output For each inquiry, ? a b, print the weight difference in micrograms between the sample pieces numbered a and b, wb − wa, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print UNKNOWN followed by a newline. Example Input 2 2 ! 1 2 1 ? 1 2 2 2 ! 1 2 1 ? 2 1 4 7 ! 1 2 100 ? 2 3 ! 2 3 100 ? 2 3 ? 1 3 ! 4 3 150 ? 4 1 0 0 Output 1 -1 UNKNOWN 100 200 -50 Submitted Solution: ``` import sys sys.setrecursionlimit(10**6) def root(x): if x == parent[x]: return x y = root(parent[x]) relative[x] += relative[parent[x]] parent[x] = y return y def unite(a, b, w): pa = root(a); pb = root(b) if pa < pb: parent[pb] = pa relative[pb] = relative[a] + w else: parent[pa] = pb relative[pa] = relative[b] - w while 1: N, M = map(int, input().split()) if N == M == 0: break *parent, = range(N+1) relative = [0]*(N+1) for i in range(M): cmd = input().split() if cmd[0] == '!': a, b, w = map(int, cmd[1:]) unite(a-1, b-1, w) else: a, b = map(int, cmd[1:]) if root(a-1) != root(b-1): print("UNKNOWN") else: print(relative[b-1] - relative[a-1]) ```
instruction
0
11,596
17
23,192
No
output
1
11,596
17
23,193
Provide tags and a correct Python 3 solution for this coding contest problem. There are n boxers, the weight of the i-th boxer is a_i. Each of them can change the weight by no more than 1 before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number. It is necessary to choose the largest boxing team in terms of the number of people, that all the boxers' weights in the team are different (i.e. unique). Write a program that for given current values ​a_i will find the maximum possible number of boxers in a team. It is possible that after some change the weight of some boxer is 150001 (but no more). Input The first line contains an integer n (1 ≤ n ≤ 150000) — the number of boxers. The next line contains n integers a_1, a_2, ..., a_n, where a_i (1 ≤ a_i ≤ 150000) is the weight of the i-th boxer. Output Print a single integer — the maximum possible number of people in a team. Examples Input 4 3 2 4 1 Output 4 Input 6 1 1 1 4 4 4 Output 5 Note In the first example, boxers should not change their weights — you can just make a team out of all of them. In the second example, one boxer with a weight of 1 can be increased by one (get the weight of 2), one boxer with a weight of 4 can be reduced by one, and the other can be increased by one (resulting the boxers with a weight of 3 and 5, respectively). Thus, you can get a team consisting of boxers with weights of 5, 4, 3, 2, 1.
instruction
0
11,698
17
23,396
Tags: greedy, sortings Correct Solution: ``` def max_team(n, weights): weights.sort(reverse=True) prev_weights = 10000000000 ans = 0 for i in range(n): curr_weights = weights[i] if curr_weights + 1 < prev_weights: ans += 1 prev_weights = curr_weights + 1 else: if curr_weights < prev_weights: ans += 1 prev_weights = curr_weights else: if curr_weights - 1 < prev_weights: if curr_weights - 1 != 0: ans += 1 prev_weights = curr_weights - 1 return ans n = int(input()) weights = list(map(int, input().split())) print(max_team(n, weights)) ```
output
1
11,698
17
23,397
Provide tags and a correct Python 3 solution for this coding contest problem. There are n boxers, the weight of the i-th boxer is a_i. Each of them can change the weight by no more than 1 before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number. It is necessary to choose the largest boxing team in terms of the number of people, that all the boxers' weights in the team are different (i.e. unique). Write a program that for given current values ​a_i will find the maximum possible number of boxers in a team. It is possible that after some change the weight of some boxer is 150001 (but no more). Input The first line contains an integer n (1 ≤ n ≤ 150000) — the number of boxers. The next line contains n integers a_1, a_2, ..., a_n, where a_i (1 ≤ a_i ≤ 150000) is the weight of the i-th boxer. Output Print a single integer — the maximum possible number of people in a team. Examples Input 4 3 2 4 1 Output 4 Input 6 1 1 1 4 4 4 Output 5 Note In the first example, boxers should not change their weights — you can just make a team out of all of them. In the second example, one boxer with a weight of 1 can be increased by one (get the weight of 2), one boxer with a weight of 4 can be reduced by one, and the other can be increased by one (resulting the boxers with a weight of 3 and 5, respectively). Thus, you can get a team consisting of boxers with weights of 5, 4, 3, 2, 1.
instruction
0
11,699
17
23,398
Tags: greedy, sortings Correct Solution: ``` def int_multiple(): return [int(c) for c in input().split()] def int_single(): return int(input()) def str_multiple(): return [c for c in input().split()] def str_single(): return input() # start n = int_single() l = int_multiple() l = sorted(l) team = 0 avail = 1 for i in range(n): if (l[i] - 1 >= avail): team += 1 avail = l[i] elif (l[i] >= avail): team += 1 avail = l[i] + 1 elif (l[i] + 1 >= avail): team += 1 avail = l[i] + 2 else: continue print(team) ```
output
1
11,699
17
23,399
Provide tags and a correct Python 3 solution for this coding contest problem. There are n boxers, the weight of the i-th boxer is a_i. Each of them can change the weight by no more than 1 before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number. It is necessary to choose the largest boxing team in terms of the number of people, that all the boxers' weights in the team are different (i.e. unique). Write a program that for given current values ​a_i will find the maximum possible number of boxers in a team. It is possible that after some change the weight of some boxer is 150001 (but no more). Input The first line contains an integer n (1 ≤ n ≤ 150000) — the number of boxers. The next line contains n integers a_1, a_2, ..., a_n, where a_i (1 ≤ a_i ≤ 150000) is the weight of the i-th boxer. Output Print a single integer — the maximum possible number of people in a team. Examples Input 4 3 2 4 1 Output 4 Input 6 1 1 1 4 4 4 Output 5 Note In the first example, boxers should not change their weights — you can just make a team out of all of them. In the second example, one boxer with a weight of 1 can be increased by one (get the weight of 2), one boxer with a weight of 4 can be reduced by one, and the other can be increased by one (resulting the boxers with a weight of 3 and 5, respectively). Thus, you can get a team consisting of boxers with weights of 5, 4, 3, 2, 1.
instruction
0
11,700
17
23,400
Tags: greedy, sortings Correct Solution: ``` n = int(input()) nums = [int(x) for x in input().split()] nums = sorted(nums, reverse=True) cnt = 1 cur = nums[0]+1 for x in nums[1:]: if x + 1 < cur: cur = x+1 cnt += 1 elif x + 1 == cur: cur = x cnt += 1 elif x == cur and x != 1: cur = x - 1 cnt += 1 print(cnt) ```
output
1
11,700
17
23,401
Provide tags and a correct Python 3 solution for this coding contest problem. There are n boxers, the weight of the i-th boxer is a_i. Each of them can change the weight by no more than 1 before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number. It is necessary to choose the largest boxing team in terms of the number of people, that all the boxers' weights in the team are different (i.e. unique). Write a program that for given current values ​a_i will find the maximum possible number of boxers in a team. It is possible that after some change the weight of some boxer is 150001 (but no more). Input The first line contains an integer n (1 ≤ n ≤ 150000) — the number of boxers. The next line contains n integers a_1, a_2, ..., a_n, where a_i (1 ≤ a_i ≤ 150000) is the weight of the i-th boxer. Output Print a single integer — the maximum possible number of people in a team. Examples Input 4 3 2 4 1 Output 4 Input 6 1 1 1 4 4 4 Output 5 Note In the first example, boxers should not change their weights — you can just make a team out of all of them. In the second example, one boxer with a weight of 1 can be increased by one (get the weight of 2), one boxer with a weight of 4 can be reduced by one, and the other can be increased by one (resulting the boxers with a weight of 3 and 5, respectively). Thus, you can get a team consisting of boxers with weights of 5, 4, 3, 2, 1.
instruction
0
11,701
17
23,402
Tags: greedy, sortings Correct Solution: ``` n=int(input()) arr=list(map(int,input().split(" "))) arr.sort() arr[n-1]+=1 #print(arr) for i in range(n-2,-1,-1): if(arr[i+1]-arr[i]==1): continue if(arr[i+1]-arr[i]>1): arr[i]+=1 elif(i>0 and arr[i-1]!=arr[i]): if(arr[i]!=1): arr[i]-=1 elif(arr[i]>1 and i==0): arr[i]-=1 ans=n #print(arr) for i in range(1,n): if(arr[i]==arr[i-1]): ans-=1 print(ans) ```
output
1
11,701
17
23,403
Provide tags and a correct Python 3 solution for this coding contest problem. There are n boxers, the weight of the i-th boxer is a_i. Each of them can change the weight by no more than 1 before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number. It is necessary to choose the largest boxing team in terms of the number of people, that all the boxers' weights in the team are different (i.e. unique). Write a program that for given current values ​a_i will find the maximum possible number of boxers in a team. It is possible that after some change the weight of some boxer is 150001 (but no more). Input The first line contains an integer n (1 ≤ n ≤ 150000) — the number of boxers. The next line contains n integers a_1, a_2, ..., a_n, where a_i (1 ≤ a_i ≤ 150000) is the weight of the i-th boxer. Output Print a single integer — the maximum possible number of people in a team. Examples Input 4 3 2 4 1 Output 4 Input 6 1 1 1 4 4 4 Output 5 Note In the first example, boxers should not change their weights — you can just make a team out of all of them. In the second example, one boxer with a weight of 1 can be increased by one (get the weight of 2), one boxer with a weight of 4 can be reduced by one, and the other can be increased by one (resulting the boxers with a weight of 3 and 5, respectively). Thus, you can get a team consisting of boxers with weights of 5, 4, 3, 2, 1.
instruction
0
11,702
17
23,404
Tags: greedy, sortings Correct Solution: ``` n = int(input()) A = list(map(int, input().split())) A.sort() MAX_WEIGHT = 150001 k = 0 count = 0 for i in range(1, MAX_WEIGHT+1): if k >= len(A): break boxer = A[k] if i < boxer - 1: i += boxer - 2 continue while i > boxer + 1 and k < len(A) - 1: k += 1 boxer = A[k] if i == boxer or i == boxer - 1 or i == boxer + 1: count += 1 k += 1 print(count) ```
output
1
11,702
17
23,405