text
stringlengths
198
433k
conversation_id
int64
0
109k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vitya loves programming and problem solving, but sometimes, to distract himself a little, he plays computer games. Once he found a new interesting game about tanks, and he liked it so much that he went through almost all levels in one day. Remained only the last level, which was too tricky. Then Vitya remembered that he is a programmer, and wrote a program that helped him to pass this difficult level. Try do the same. The game is organized as follows. There is a long road, two cells wide and n cells long. Some cells have obstacles. You control a tank that occupies one cell. Initially, the tank is located before the start of the road, in a cell with coordinates (0, 1). Your task is to move the tank to the end of the road, to the cell (n + 1, 1) or (n + 1, 2). <image> Every second the tank moves one cell to the right: the coordinate x is increased by one. When you press the up or down arrow keys, the tank instantly changes the lane, that is, the y coordinate. When you press the spacebar, the tank shoots, and the nearest obstacle along the lane in which the tank rides is instantly destroyed. In order to load a gun, the tank needs t seconds. Initially, the gun is not loaded, that means, the first shot can be made only after t seconds after the tank starts to move. If at some point the tank is in the same cell with an obstacle not yet destroyed, it burns out. If you press the arrow exactly at the moment when the tank moves forward, the tank will first move forward, and then change the lane, so it will not be possible to move diagonally. Your task is to find out whether it is possible to pass the level, and if possible, to find the order of actions the player need to make. Input The first line contains four integers n, m1, m2 and t, the length of the field, the number of obstacles in the first lane, the number of obstacles in the second lane and the number of tank steps before reloading, respectively (1 ≤ n ≤ 109; 0 ≤ m1, m2 ≤ n; 0 ≤ m1 + m2 ≤ 106; 1 ≤ t ≤ n). The next two lines contain a description of the obstacles. The first of these lines contains m1 numbers xi — the obstacle coordinates in the first lane (1 ≤ xi ≤ n; xi < xi + 1). The y coordinate for all these obstacles will be 1. The second line contains m2 numbers describing the obstacles of the second lane in the same format. The y coordinate of all these obstacles will be 2. Output In the first line print «Yes», if it is possible to pass the level, or «No», otherwise. If it is possible, then in the second line print the number of times the tank moves from one lane to another, and in the next line print the coordinates of the transitions, one number per transition: the coordinate x (0 ≤ x ≤ n + 1). All transition coordinates coordinates must be distinct and should be output in strictly increasing order.The number of transitions should not exceed 2·106. If the tank can pass the level, then it can do it using no more than 2·106 transitions. In the fourth line print the number of shots that the tank makes during the movement, in the following lines print two numbers, x and y coordinates of the point (1 ≤ x ≤ n, 1 ≤ y ≤ 2), from which the tank fired a shot, the number of shots must not exceed m1 + m2. Shots must be output in the order in which they are fired. If there are several solutions, output any one. Examples Input 6 2 3 2 2 6 3 5 6 Output Yes 2 0 3 2 2 2 4 1 Input 1 1 1 1 1 1 Output No Input 9 5 2 5 1 2 7 8 9 4 6 Output Yes 4 0 3 5 10 1 5 2 Note Picture for the first sample test. <image> Submitted Solution: ``` #!/usr/bin/env python # -*- coding:utf-8 -*- block1 = [] block2 = [] n, m1, m2, t = map(int, input().split()) block1 = input().split() block2 = input().split() for i in range(len(block2)): block2[i] = int(block2[i]) for i in range(len(block1)): block1[i] = int(block1[i]) # print(n, m1, m2, t, block1, block2) # 建立分析区 vision = [[],[]] # setup for i in range(n): if i+1 in block2: vision[1].append(1) else: vision[1].append(0) if i+1 in block1: vision[0].append(1) else: vision[0].append(0) # print(vision) fire_ready=False y=1 x=0 expect=[y,x] transition=[] shoots=[] fire_spot=[] last_shot=0 failed=False vision_boundary=t+1 while x!=n: if vision[y-1][x]==0: expect=[y,x+1] x=x+1 elif y==1: if vision[1][x-1]==0 and vision[1][x]==0: expect = [y+1 , x] transition.append(str(x)) y = y + 1 elif vision[1][x-1]==0 and vision[1][x]==1: if vision[0][x+1]==0 or (vision[0][x+1]==1 and vision[1][x+1]==1): if x - last_shot >= t: fire_spot = [y, x] expect = [y, x + 1] x = x + 1 shoots.append([y, x - 1]) last_shot = x else: failed = True break elif vision[0][x+1]==1 and vision[1][x+1]==0: expect = [y + 1, x] transition.append(str(x)) y = y + 1 elif vision[y][x-1]==1: if x-last_shot>=t: fire_spot = [y, x ] expect=[y,x+1] x=x+1 shoots.append([y,x-1]) last_shot=x else: failed=True break elif y==2: if vision[0][x-1]==0: expect = [y-1 , x] transition.append(str(x)) y = y - 1 elif vision[0][x-1]==1: if x-last_shot>=t: fire_spot = [y, x ] expect=[y,x+1] x=x+1 shoots.append([y,x-1]) last_shot=x else: failed=True break # while x!=n: # if y==2: # if vision[1][x]==1: # if vision[0][x-1]==0: # expect=[y-1,x] # transition.append(x) # y=y-1 # elif vision[0][x-1]==1: # if x - last_shot >= t: # fire_spot = [y, x] # expect = [y, x + 1] # x = x + 1 # shoots.append([y, x]) # last_shot = x # else: # failed = True # break if failed==True: print("No") else: print("Yes") print(len(transition)) print(" ".join(transition)) print(len(shoots)) for i in shoots: print(i[1],i[0]) ``` No
2,200
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vitya loves programming and problem solving, but sometimes, to distract himself a little, he plays computer games. Once he found a new interesting game about tanks, and he liked it so much that he went through almost all levels in one day. Remained only the last level, which was too tricky. Then Vitya remembered that he is a programmer, and wrote a program that helped him to pass this difficult level. Try do the same. The game is organized as follows. There is a long road, two cells wide and n cells long. Some cells have obstacles. You control a tank that occupies one cell. Initially, the tank is located before the start of the road, in a cell with coordinates (0, 1). Your task is to move the tank to the end of the road, to the cell (n + 1, 1) or (n + 1, 2). <image> Every second the tank moves one cell to the right: the coordinate x is increased by one. When you press the up or down arrow keys, the tank instantly changes the lane, that is, the y coordinate. When you press the spacebar, the tank shoots, and the nearest obstacle along the lane in which the tank rides is instantly destroyed. In order to load a gun, the tank needs t seconds. Initially, the gun is not loaded, that means, the first shot can be made only after t seconds after the tank starts to move. If at some point the tank is in the same cell with an obstacle not yet destroyed, it burns out. If you press the arrow exactly at the moment when the tank moves forward, the tank will first move forward, and then change the lane, so it will not be possible to move diagonally. Your task is to find out whether it is possible to pass the level, and if possible, to find the order of actions the player need to make. Input The first line contains four integers n, m1, m2 and t, the length of the field, the number of obstacles in the first lane, the number of obstacles in the second lane and the number of tank steps before reloading, respectively (1 ≤ n ≤ 109; 0 ≤ m1, m2 ≤ n; 0 ≤ m1 + m2 ≤ 106; 1 ≤ t ≤ n). The next two lines contain a description of the obstacles. The first of these lines contains m1 numbers xi — the obstacle coordinates in the first lane (1 ≤ xi ≤ n; xi < xi + 1). The y coordinate for all these obstacles will be 1. The second line contains m2 numbers describing the obstacles of the second lane in the same format. The y coordinate of all these obstacles will be 2. Output In the first line print «Yes», if it is possible to pass the level, or «No», otherwise. If it is possible, then in the second line print the number of times the tank moves from one lane to another, and in the next line print the coordinates of the transitions, one number per transition: the coordinate x (0 ≤ x ≤ n + 1). All transition coordinates coordinates must be distinct and should be output in strictly increasing order.The number of transitions should not exceed 2·106. If the tank can pass the level, then it can do it using no more than 2·106 transitions. In the fourth line print the number of shots that the tank makes during the movement, in the following lines print two numbers, x and y coordinates of the point (1 ≤ x ≤ n, 1 ≤ y ≤ 2), from which the tank fired a shot, the number of shots must not exceed m1 + m2. Shots must be output in the order in which they are fired. If there are several solutions, output any one. Examples Input 6 2 3 2 2 6 3 5 6 Output Yes 2 0 3 2 2 2 4 1 Input 1 1 1 1 1 1 Output No Input 9 5 2 5 1 2 7 8 9 4 6 Output Yes 4 0 3 5 10 1 5 2 Note Picture for the first sample test. <image> Submitted Solution: ``` def shoot(o): if len(o) == 1: return [] else: return o[1:] n, m1, m2, t = list(map(int, input().split())) o1 = list(map(int, input().split())) o2 = list(map(int, input().split())) result_move = [] result_shoot = [] x, y = 0, 1 load = 0 result = "YES" while x <= n: # print('x=',x,',y=',y) # print(o1,o2) if len(o1) == 0: if y==2: result_move.append(o2[0]-1) break elif len(o2) == 0: if y==1: result_move.append(o1[0]-1) break if o1[0] >= o2[0]: if y==2: result_move.append(o2[0]-1) y=1 x+=1 if x == o1[0]: result = 'NO' break if x == o2[0]: o2 = shoot(o2) load+=1 if load == t: o1 = shoot(o1) result_shoot.append((x,y)) load = 0 else: if y==1: result_move.append(o1[0]-1) y=2 x+=1 if x == o2[0]: result = 'NO' break if x == o1[0]: o1 = shoot(o1) load+=1 if load == t: o2 = shoot(o2) result_shoot.append((x,y)) load = 0 print(result) if result == 'YES': print(len(result_move)) print(' '.join(list(map(str, result_move)))) print(len(result_shoot)) for item in result_shoot: print(' '.join(list(map(str, item)))) ``` No
2,201
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vitya loves programming and problem solving, but sometimes, to distract himself a little, he plays computer games. Once he found a new interesting game about tanks, and he liked it so much that he went through almost all levels in one day. Remained only the last level, which was too tricky. Then Vitya remembered that he is a programmer, and wrote a program that helped him to pass this difficult level. Try do the same. The game is organized as follows. There is a long road, two cells wide and n cells long. Some cells have obstacles. You control a tank that occupies one cell. Initially, the tank is located before the start of the road, in a cell with coordinates (0, 1). Your task is to move the tank to the end of the road, to the cell (n + 1, 1) or (n + 1, 2). <image> Every second the tank moves one cell to the right: the coordinate x is increased by one. When you press the up or down arrow keys, the tank instantly changes the lane, that is, the y coordinate. When you press the spacebar, the tank shoots, and the nearest obstacle along the lane in which the tank rides is instantly destroyed. In order to load a gun, the tank needs t seconds. Initially, the gun is not loaded, that means, the first shot can be made only after t seconds after the tank starts to move. If at some point the tank is in the same cell with an obstacle not yet destroyed, it burns out. If you press the arrow exactly at the moment when the tank moves forward, the tank will first move forward, and then change the lane, so it will not be possible to move diagonally. Your task is to find out whether it is possible to pass the level, and if possible, to find the order of actions the player need to make. Input The first line contains four integers n, m1, m2 and t, the length of the field, the number of obstacles in the first lane, the number of obstacles in the second lane and the number of tank steps before reloading, respectively (1 ≤ n ≤ 109; 0 ≤ m1, m2 ≤ n; 0 ≤ m1 + m2 ≤ 106; 1 ≤ t ≤ n). The next two lines contain a description of the obstacles. The first of these lines contains m1 numbers xi — the obstacle coordinates in the first lane (1 ≤ xi ≤ n; xi < xi + 1). The y coordinate for all these obstacles will be 1. The second line contains m2 numbers describing the obstacles of the second lane in the same format. The y coordinate of all these obstacles will be 2. Output In the first line print «Yes», if it is possible to pass the level, or «No», otherwise. If it is possible, then in the second line print the number of times the tank moves from one lane to another, and in the next line print the coordinates of the transitions, one number per transition: the coordinate x (0 ≤ x ≤ n + 1). All transition coordinates coordinates must be distinct and should be output in strictly increasing order.The number of transitions should not exceed 2·106. If the tank can pass the level, then it can do it using no more than 2·106 transitions. In the fourth line print the number of shots that the tank makes during the movement, in the following lines print two numbers, x and y coordinates of the point (1 ≤ x ≤ n, 1 ≤ y ≤ 2), from which the tank fired a shot, the number of shots must not exceed m1 + m2. Shots must be output in the order in which they are fired. If there are several solutions, output any one. Examples Input 6 2 3 2 2 6 3 5 6 Output Yes 2 0 3 2 2 2 4 1 Input 1 1 1 1 1 1 Output No Input 9 5 2 5 1 2 7 8 9 4 6 Output Yes 4 0 3 5 10 1 5 2 Note Picture for the first sample test. <image> Submitted Solution: ``` def solve(): n, m1, m2, shotSecond = map(int, input().split(" ")) block = [] block.append(input().split(" ")) block.append(input().split(" ")) for i in range(m1): block[0][i] = int(block[0][i]) for i in range(m2): block[1][i] = int(block[1][i]) posStatus = 0 # 位置 shotStatus = 0 # 射击 change = [] shot = [] line = 0 win = True while posStatus < n: temp = checkStatus(block, posStatus, line) if temp == 1: line = (line + 1) % 2 change.append(str(posStatus)) elif temp == 2: if checkShot(shotSecond, shotStatus): shotStatus = 0 shot.append("{} {}".format(posStatus, line + 1)) # shot(block[line], posStatus) for i in range(len(block[line])): if block[line][i] > posStatus: block[line].pop(i) break else: win = False elif temp == 3: line = (line + 1) % 2 if checkShot(shotSecond, shotStatus): shotStatus = 0 shot.append("{} {}".format(posStatus, line + 1)) # shot(block[line], posStatus) for i in range(len(block[line])): if block[line][i] > posStatus: block[line].pop(i) break else: win = False change.append(str(posStatus)) elif temp == 4: win = False elif temp == 5: if checkShot(shotSecond, shotStatus): shotStatus = 0 shot.append("{} {}".format(posStatus, line + 1)) # shot(block[line], posStatus) for i in range(len(block[line])): if block[line][i] > posStatus: block[line].pop(i) break elif temp==6: line = (line + 1) % 2 if checkShot(shotSecond, shotStatus): shotStatus = 0 shot.append("{} {}".format(posStatus, line + 1)) # shot(block[line], posStatus) for i in range(len(block[line])): if block[line][i] > posStatus: block[line].pop(i) break change.append(str(posStatus)) posStatus += 1 shotStatus += 1 if win: print("Yes") print(len(change)) print(" ".join(change)) print(len(shot)) for i in range(len(shot)): print(shot[i]) else: print("No") # 0 不动 1 换行 2 打 3 换行且打 4 die 5 可容错打 6 可容错打 def checkStatus(block, posStatus, line): another = (line + 1) % 2 if (posStatus + 1) in block[line]: if posStatus in block[another]: return 2 elif (posStatus + 1) in block[another]: if not (posStatus + 2) in block[line]: return 2 elif not (posStatus + 2) in block[another]: return 3 else: return 4 elif not (posStatus + 1) in block[another]: if (posStatus+2) in block[another]: return 6 else: return 1 elif (posStatus + 2) in block[line]: if not (posStatus + 1) in block[another] and not (posStatus + 2) in block[another]: if posStatus in block[another]: return 0 else: return 1 elif (posStatus + 1) in block[another]: return 5 else: return 0 def checkShot(shotSecond, shotStatus): return shotStatus >= shotSecond solve() ``` No
2,202
Provide tags and a correct Python 3 solution for this coding contest problem. There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that: * no student-programmer is sitting next to the student-programmer; * and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). Input The first line contain three integers n, a and b (1 ≤ n ≤ 2⋅10^{5}, 0 ≤ a, b ≤ 2⋅10^{5}, a + b > 0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length n, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. Output Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. Examples Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 Note In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete. Tags: constructive algorithms, greedy, implementation Correct Solution: ``` def solve(carriage, n, a, b): odd=0; total=0 subsum=0; for ch in carriage: if ch=='.': subsum +=1 elif ch=='*': total += subsum if subsum%2==1: odd += 1 subsum=0 total += subsum if subsum%2==1: odd += 1 even=(total-odd)//2 ans=0 if a<= even: ans += a a=0 else: ans+=even a -= even if b<= even: ans += b b=0 else: ans+=even b -= even #print(even) #print(odd) #print(a+b) return ans+min(a+b, odd) def test(): print(solve("*...*", 5, 1, 1)==2) print(solve("*...*.", 6, 2, 3)==4) print(solve(".*....**.*.", 11, 3, 10)==7) print(solve("***", 3, 2, 3)==0) def nia(): s=input() while len(s)==0: s=input() s=s.split() iVal=[]; for i in range (len(s)): iVal.append(int(s[i])) return iVal n=nia() arr=input() print(solve(arr, n[0], n[1], n[2])) ```
2,203
Provide tags and a correct Python 3 solution for this coding contest problem. There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that: * no student-programmer is sitting next to the student-programmer; * and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). Input The first line contain three integers n, a and b (1 ≤ n ≤ 2⋅10^{5}, 0 ≤ a, b ≤ 2⋅10^{5}, a + b > 0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length n, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. Output Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. Examples Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 Note In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete. Tags: constructive algorithms, greedy, implementation Correct Solution: ``` n, a, b = [int(x) for x in input().split()] a, b = sorted([a, b]) total = a + b from itertools import groupby s = input().strip() for val, g in groupby(s): if val == '*': continue length = len(list(g)) b -= (length + 1) // 2 a -= length // 2 if a > b: a, b = b, a a = max(a, 0) b = max(b, 0) print(total - a - b) ```
2,204
Provide tags and a correct Python 3 solution for this coding contest problem. There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that: * no student-programmer is sitting next to the student-programmer; * and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). Input The first line contain three integers n, a and b (1 ≤ n ≤ 2⋅10^{5}, 0 ≤ a, b ≤ 2⋅10^{5}, a + b > 0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length n, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. Output Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. Examples Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 Note In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete. Tags: constructive algorithms, greedy, implementation Correct Solution: ``` n, v, t = input().split() n = int(n) a = int(v) b = int(t) s = [len(k) for k in input().split('*')] a,b = max(a,b),min(a,b) for i in range(len(s)): a -= (s[i]+1)//2 b -= s[i]//2 if a < 0: a, b = 0,0 break elif b < 0: b = 0 a, b = max(a,b), min(a,b) print(int(v) + int(t) - a - b) ```
2,205
Provide tags and a correct Python 3 solution for this coding contest problem. There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that: * no student-programmer is sitting next to the student-programmer; * and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). Input The first line contain three integers n, a and b (1 ≤ n ≤ 2⋅10^{5}, 0 ≤ a, b ≤ 2⋅10^{5}, a + b > 0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length n, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. Output Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. Examples Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 Note In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete. Tags: constructive algorithms, greedy, implementation Correct Solution: ``` _, m, n = map(int, input().split()) t = m+n a = [len(s) for s in input().split('*')] for x in a: m -= x // 2 n -= x // 2 if m > n: m -= x % 2 else: n -= x % 2 m = max(m, 0) n = max(n, 0) print(t - (m+n)) ```
2,206
Provide tags and a correct Python 3 solution for this coding contest problem. There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that: * no student-programmer is sitting next to the student-programmer; * and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). Input The first line contain three integers n, a and b (1 ≤ n ≤ 2⋅10^{5}, 0 ≤ a, b ≤ 2⋅10^{5}, a + b > 0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length n, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. Output Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. Examples Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 Note In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete. Tags: constructive algorithms, greedy, implementation Correct Solution: ``` n,a,b=map(int,input().split()) s=list(input()) ans=0 for i in range(n): if s[i]=='.': if i==0 or (s[i-1]!='a' and s[i-1]!='b'): if a>b and a>0: s[i]='a' a-=1 ans+=1 elif b>0: s[i]='b' b-=1 ans+=1 elif s[i-1]=='a': if b>0: s[i]='b' b-=1 ans+=1 elif s[i-1]=='b': if a>0: s[i]='a' a-=1 ans+=1 print(ans) ```
2,207
Provide tags and a correct Python 3 solution for this coding contest problem. There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that: * no student-programmer is sitting next to the student-programmer; * and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). Input The first line contain three integers n, a and b (1 ≤ n ≤ 2⋅10^{5}, 0 ≤ a, b ≤ 2⋅10^{5}, a + b > 0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length n, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. Output Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. Examples Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 Note In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete. Tags: constructive algorithms, greedy, implementation Correct Solution: ``` n, a, b = map(int, input().split()) s, ans, flag, cur = input(), 0, 1, 0 for i, j in enumerate(s): if j == '.': if flag: cur = 0 if a >= b else 1 flag = 0 if cur and b: ans += 1 b -= 1 elif cur == 0 and a: ans += 1 a -= 1 cur ^= 1 else: flag = 1 print(ans) ```
2,208
Provide tags and a correct Python 3 solution for this coding contest problem. There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that: * no student-programmer is sitting next to the student-programmer; * and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). Input The first line contain three integers n, a and b (1 ≤ n ≤ 2⋅10^{5}, 0 ≤ a, b ≤ 2⋅10^{5}, a + b > 0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length n, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. Output Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. Examples Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 Note In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete. Tags: constructive algorithms, greedy, implementation Correct Solution: ``` n, a, b =map(int,input().split(" ")) s=input().split("*") count=0; for i in range(len(s)): if(len(s[i])!=0): if(a==0 and b==0): break; t=len(s[i]) count+=t; if(t%2==0): a-=int(t / 2); b-=int(t / 2); if(a<0): count+=a; a=0; if (b < 0): count += b; b=0; else: if(a>=b): a -= int(t / 2)+1; b -= int(t / 2); else: a -= int(t / 2); b -= int(t / 2)+1; if (a < 0): count += a; a = 0; if (b < 0): count += b; b = 0; print(count) ```
2,209
Provide tags and a correct Python 3 solution for this coding contest problem. There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that: * no student-programmer is sitting next to the student-programmer; * and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). Input The first line contain three integers n, a and b (1 ≤ n ≤ 2⋅10^{5}, 0 ≤ a, b ≤ 2⋅10^{5}, a + b > 0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length n, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. Output Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. Examples Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 Note In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete. Tags: constructive algorithms, greedy, implementation Correct Solution: ``` n, a, b = map(int, input().split()) s = list(input()) count = 0 for i in range(n): if s[i] == ".": if i - 1 >= 0: if s[i - 1] == "A" and b > 0: count += 1 s[i] = "B" b -= 1 elif s[i - 1] == "B" and a > 0: count += 1 s[i] = "A" a -= 1 elif s[i - 1] == "*" or s[i - 1] == ".": if a >= b and a > 0: count += 1 s[i] = "A" a -= 1 elif b > a and b > 0: count += 1 s[i] = "B" b -= 1 else: if a >= b and a > 0: count += 1 s[i] = "A" a -= 1 elif b > a and b > 0: count += 1 s[i] = "B" b -= 1 print(count) ```
2,210
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that: * no student-programmer is sitting next to the student-programmer; * and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). Input The first line contain three integers n, a and b (1 ≤ n ≤ 2⋅10^{5}, 0 ≤ a, b ≤ 2⋅10^{5}, a + b > 0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length n, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. Output Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. Examples Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 Note In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete. Submitted Solution: ``` n,a,b = [int(i) for i in input().split()] ss = a + b st = input() for ez in st.split("*"): if(a > b): a,b = b,a a -= min(a , len(ez)//2) b -= min(b , len(ez) - len(ez)//2) print(ss - a - b) ``` Yes
2,211
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that: * no student-programmer is sitting next to the student-programmer; * and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). Input The first line contain three integers n, a and b (1 ≤ n ≤ 2⋅10^{5}, 0 ≤ a, b ≤ 2⋅10^{5}, a + b > 0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length n, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. Output Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. Examples Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 Note In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete. Submitted Solution: ``` def main(): n, a, b = map(int, input().split()) ab = a + b for v in map(len, filter(None, input().split('*'))): if v & 1: v //= 2 if a > b: a -= v + 1 b -= v else: b -= v + 1 a -= v else: v //= 2 a -= v b -= v print(ab - max(a, 0) - max(b, 0)) if __name__ == '__main__': main() ``` Yes
2,212
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that: * no student-programmer is sitting next to the student-programmer; * and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). Input The first line contain three integers n, a and b (1 ≤ n ≤ 2⋅10^{5}, 0 ≤ a, b ≤ 2⋅10^{5}, a + b > 0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length n, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. Output Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. Examples Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 Note In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete. Submitted Solution: ``` n, a, b = map(int, input().split()) number = 0 string = input() s = [t for t in string] for i in range(0, n): if s[i] == '.': if i != 0: if s[i - 1] == '*': if a > b: if a != 0: number += 1 a -= 1 s[i] = 'a' else: if b != 0: b -= 1 number += 1 s[i] = 'b' else: if b != 0: number += 1 b -= 1 s[i] = 'b' else: if a != 0: number += 1 a -= 1 s[i] = 'a' else: if s[i - 1] == 'a': if b != 0: b -= 1 number += 1 s[i] = 'b' if s[i - 1] == 'b': if a != 0: a -= 1 number += 1 s[i] = 'a' if s[i - 1] == '.': if a > b: if a != 0: number += 1 a -= 1 s[i] = 'a' else: if b != 0: number += 1 b -= 1 s[i] = 'b' else: if b != 0: number += 1 b -= 1 s[i] = 'b' else: if a != 0: number += 1 a -= 1 s[i] = 'a' else: if a > b: if a != 0: number += 1 a -= 1 s[i] = 'a' else: if b != 0: number += 1 b -= 1 s[i] = 'b' else: if b != 0: number += 1 b -= 1 s[i] = 'b' else: if a != 0: number += 1 a -= 1 s[i] = 'a' if a == 0 and b == 0: break print(number) ``` Yes
2,213
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that: * no student-programmer is sitting next to the student-programmer; * and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). Input The first line contain three integers n, a and b (1 ≤ n ≤ 2⋅10^{5}, 0 ≤ a, b ≤ 2⋅10^{5}, a + b > 0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length n, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. Output Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. Examples Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 Note In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete. Submitted Solution: ``` n, a, b = map(int, input().split()) t = a + b s = input() curr = 0 arr = [] for c in s: if c == ".": curr += 1 else: arr.append(curr) curr = 0 if curr > 0: arr.append(curr) arr.sort() arr.reverse() if a < b: a, b = b, a for e in arr: if e % 2 == 1: b -= e//2 a -= e//2+1 if a < b: a, b = b, a else: b -= e//2 a -= e//2 if b > 0: t -= b if a > 0: t -= a print(t) ``` Yes
2,214
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that: * no student-programmer is sitting next to the student-programmer; * and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). Input The first line contain three integers n, a and b (1 ≤ n ≤ 2⋅10^{5}, 0 ≤ a, b ≤ 2⋅10^{5}, a + b > 0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length n, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. Output Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. Examples Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 Note In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete. Submitted Solution: ``` z = [int(s) for s in input().split()] n = z[0] a = z[1] b = z[2] st = 0 # print(n, a, b) x = input() f = [] for i in range(len(x)): f.append(x[i]) for i in range(len(f)): if i == 0 and f[i] == ".": if a > b: f[i] = "a" a -= 1 if a < b: f[i] = "b" b -= 1 if a == b: f[i] = "a" a -= 1 elif f[i] == ".": if f[i-1] == "a": if b > 0: f[i] = "b" b -= 1 if f[i-1] == "b": if a > 0: f[i] = "a" a -= 1 if f[i-1] == "*": if a > b and a > 0: f[i] = "a" a -= 1 if a < b and b > 0: f[i] = "b" b -= 1 if a == b and a > 0: f[i] = "a" a -= 1 for i in range(len(f)): if f[i] == "a" or f[i] == "b": st += 1 print(st) ``` No
2,215
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that: * no student-programmer is sitting next to the student-programmer; * and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). Input The first line contain three integers n, a and b (1 ≤ n ≤ 2⋅10^{5}, 0 ≤ a, b ≤ 2⋅10^{5}, a + b > 0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length n, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. Output Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. Examples Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 Note In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete. Submitted Solution: ``` n,a,b=map(int,input().split()) s=input() s=list(s) c=0 a=max(a,b) b=min(a,b) #1 #0 for i in range(0,n): if(s[i]!='*'): if(i!=0): if(s[i-1]!=1): if(a>0): s[i]=1 a=a-1 c=c+1 else: if(s[i-1]=='*' and b>0): s[i]=0 c=c+1 b=b-1 else: if(b>0): s[i]=0 b=b-1 c=c+1 else: if(s[i-1]=='*' and a>0): s[i]=1 c=c+1 a=a-1 else: s[i]=1 a=a-1 c=c+1 a=max(a,b) b=min(a,b) else: continue print(c) ``` No
2,216
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that: * no student-programmer is sitting next to the student-programmer; * and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). Input The first line contain three integers n, a and b (1 ≤ n ≤ 2⋅10^{5}, 0 ≤ a, b ≤ 2⋅10^{5}, a + b > 0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length n, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. Output Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. Examples Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 Note In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete. Submitted Solution: ``` n,a,b = map(int,input().split()) s = input() res,e = 0,'' if a==0 or b==0: print(1) from sys import exit exit() r=0 for i,x in enumerate(s): if a==0 and b==0: break if x=='.': if e=='': if a>b: if b==0: r=0 a-=1 else: a-=1 e='a' else: if a==0: r=0 b-=1 else: b-=1 e='b' res+=1 elif e=='a': if b==0: if r==0: r=1 else: a-=1 r=0 res+=1 else: b-=1 e='b' res+=1 else: if a==0: if r==0: r=1 else: b-=1 r=0 res+=1 else: a-=1 e='a' res+=1 else: e='' print(res) ``` No
2,217
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger. The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that: * no student-programmer is sitting next to the student-programmer; * and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting. Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). Input The first line contain three integers n, a and b (1 ≤ n ≤ 2⋅10^{5}, 0 ≤ a, b ≤ 2⋅10^{5}, a + b > 0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length n, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. Output Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. Examples Input 5 1 1 *...* Output 2 Input 6 2 3 *...*. Output 4 Input 11 3 10 .*....**.*. Output 7 Input 3 2 3 *** Output 0 Note In the first example you can put all student, for example, in the following way: *.AB* In the second example you can put four students, for example, in the following way: *BAB*B In the third example you can put seven students, for example, in the following way: B*ABAB**A*B The letter A means a student-programmer, and the letter B — student-athlete. Submitted Solution: ``` import math n,a,b=[int(n) for n in input().split()] s=input() c=a d=b l=list(s) if(l[0]=="."): if(c>d): l[0]="0" c=c-1 else: l[0]="1" d=d-1 for i in range(1,n): if(l[i]=="*"): continue else: if(l[i-1]=="*"): if(c>d): l[i]="0" c=c-1 else: l[i]="1" d=d-1 if(l[i-1]=="0"): if(d>0): l[i]="1" d=d-1 if(l[i-1]=="1"): if(c>0): l[i]="0" c=c-1 print((a+b)-(c+d)) ``` No
2,218
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story. The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help. <image> You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor. Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him... Input The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104). Output Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes). It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition. Examples Input 2 2 1 Output 1.0000000 Input 2 2 2 Output 2.0000000 Input 2 2 3 Output 1.3284271 Input 2 2 6 Output My poor head =( Note In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length). In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). Submitted Solution: ``` a, b, l = map(int, input().split(' ')) d = (a+b)*(2**.5) if l > d: ans = "My poor head =(" else: f = 0 e = (d-l)/2 if l < max(a, b): f = max(a, b) ans = str(float(min(l, max(f, min(l, e))))) ans1 = ans.split('.') if len(ans1[1]) == 7: ans = str(float(ans)) elif len(ans1[1]) < 7: while len(ans1[1]) != 7: ans1[1] += '0' elif len(ans1[1]) > 7: print(ans1) ans1[1] = ans1[1][0] + ans1[1][1] + ans1[1][2]+ ans1[1][3]+ ans1[1][4]+ ans1[1][5]+ans1[1][6] ans = ans1[0] + '.' + ans1[1] print(ans) ``` No
2,219
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story. The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help. <image> You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor. Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him... Input The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104). Output Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes). It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition. Examples Input 2 2 1 Output 1.0000000 Input 2 2 2 Output 2.0000000 Input 2 2 3 Output 1.3284271 Input 2 2 6 Output My poor head =( Note In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length). In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). Submitted Solution: ``` a, b, l = map(int, input().split(' ')) d = (a+b)*(2**.5) if l > d: ans = "My poor head =(" else: f = 0 e = (d-l)/2 if l <= max(a, b): f = max(a, b) ans = str(float(min(l, max(f, min(l, e))))) ans1 = ans.split('.') if len(ans1[1]) == 7: ans = str(float(ans)) elif len(ans1[1]) < 7: while len(ans1[1]) != 7: ans1[1] += '0' elif len(ans1[1]) > 7: ans1[1] = ans1[1][0] + ans1[1][1] + ans1[1][2]+ ans1[1][3]+ ans1[1][4]+ ans1[1][5]+ans1[1][6] ans = ans1[0] + '.' + ans1[1] print(ans) ``` No
2,220
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story. The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help. <image> You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor. Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him... Input The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104). Output Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes). It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition. Examples Input 2 2 1 Output 1.0000000 Input 2 2 2 Output 2.0000000 Input 2 2 3 Output 1.3284271 Input 2 2 6 Output My poor head =( Note In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length). In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). Submitted Solution: ``` a, b, l = map(int, input().split(' ')) d = (a+b)*(2**.5) if l > d: ans = "My poor head =(" else: f = 0 e = (d-l)/2 if l < max(a, b): f = max(a, b) ans = str(float(max(f, min(l, e)))) ans1 = ans.split('.') if len(ans1[1]) == 7: ans = str(float(ans)) elif len(ans1[1]) < 7: while len(ans1[1]) != 7: ans1[1] += '0' elif len(ans1[1]) > 7: print(ans1) ans1[1] = ans1[1][0] + ans1[1][1] + ans1[1][2]+ ans1[1][3]+ ans1[1][4]+ ans1[1][5]+ans1[1][6] ans = ans1[0] + '.' + ans1[1] print(ans) ``` No
2,221
Provide a correct Python 3 solution for this coding contest problem. How many multiples of d are there among the integers between L and R (inclusive)? Constraints * All values in input are integers. * 1 \leq L \leq R \leq 100 * 1 \leq d \leq 100 Input Input is given from Standard Input in the following format: L R d Output Print the number of multiples of d among the integers between L and R (inclusive). Examples Input 5 10 2 Output 3 Input 6 20 7 Output 2 Input 1 100 1 Output 100 "Correct Solution: ``` l,r,d = map(int,input().split()) a = (l-1) // d b = r // d s = b - a print(s) ```
2,222
Provide a correct Python 3 solution for this coding contest problem. How many multiples of d are there among the integers between L and R (inclusive)? Constraints * All values in input are integers. * 1 \leq L \leq R \leq 100 * 1 \leq d \leq 100 Input Input is given from Standard Input in the following format: L R d Output Print the number of multiples of d among the integers between L and R (inclusive). Examples Input 5 10 2 Output 3 Input 6 20 7 Output 2 Input 1 100 1 Output 100 "Correct Solution: ``` a = list(map(int,input().split())) x = (a[1] // a[2]) - ((a[0] - 1) // a[2]) print (x) ```
2,223
Provide a correct Python 3 solution for this coding contest problem. How many multiples of d are there among the integers between L and R (inclusive)? Constraints * All values in input are integers. * 1 \leq L \leq R \leq 100 * 1 \leq d \leq 100 Input Input is given from Standard Input in the following format: L R d Output Print the number of multiples of d among the integers between L and R (inclusive). Examples Input 5 10 2 Output 3 Input 6 20 7 Output 2 Input 1 100 1 Output 100 "Correct Solution: ``` L, R, d = map(int, input().split()) print(R // d - L // d + (L % d == 0)) ```
2,224
Provide a correct Python 3 solution for this coding contest problem. How many multiples of d are there among the integers between L and R (inclusive)? Constraints * All values in input are integers. * 1 \leq L \leq R \leq 100 * 1 \leq d \leq 100 Input Input is given from Standard Input in the following format: L R d Output Print the number of multiples of d among the integers between L and R (inclusive). Examples Input 5 10 2 Output 3 Input 6 20 7 Output 2 Input 1 100 1 Output 100 "Correct Solution: ``` l, r, d = map(int, input().split(' ')) a = (r - l) // d if r % d == 0: a += 1 print(a) ```
2,225
Provide a correct Python 3 solution for this coding contest problem. How many multiples of d are there among the integers between L and R (inclusive)? Constraints * All values in input are integers. * 1 \leq L \leq R \leq 100 * 1 \leq d \leq 100 Input Input is given from Standard Input in the following format: L R d Output Print the number of multiples of d among the integers between L and R (inclusive). Examples Input 5 10 2 Output 3 Input 6 20 7 Output 2 Input 1 100 1 Output 100 "Correct Solution: ``` l,r,n = map(int, input().split()) L = l//n if l%n!=0 else (l//n)-1 R = r//n print(R-L) ```
2,226
Provide a correct Python 3 solution for this coding contest problem. How many multiples of d are there among the integers between L and R (inclusive)? Constraints * All values in input are integers. * 1 \leq L \leq R \leq 100 * 1 \leq d \leq 100 Input Input is given from Standard Input in the following format: L R d Output Print the number of multiples of d among the integers between L and R (inclusive). Examples Input 5 10 2 Output 3 Input 6 20 7 Output 2 Input 1 100 1 Output 100 "Correct Solution: ``` L, R, d = map(int, input().split()) print(len([x for x in range(L, R+1) if x%d==0])) ```
2,227
Provide a correct Python 3 solution for this coding contest problem. How many multiples of d are there among the integers between L and R (inclusive)? Constraints * All values in input are integers. * 1 \leq L \leq R \leq 100 * 1 \leq d \leq 100 Input Input is given from Standard Input in the following format: L R d Output Print the number of multiples of d among the integers between L and R (inclusive). Examples Input 5 10 2 Output 3 Input 6 20 7 Output 2 Input 1 100 1 Output 100 "Correct Solution: ``` l,r,d = map(int, input().split()) sr= r //d sl= (l-1) //d print(sr-sl) ```
2,228
Provide a correct Python 3 solution for this coding contest problem. How many multiples of d are there among the integers between L and R (inclusive)? Constraints * All values in input are integers. * 1 \leq L \leq R \leq 100 * 1 \leq d \leq 100 Input Input is given from Standard Input in the following format: L R d Output Print the number of multiples of d among the integers between L and R (inclusive). Examples Input 5 10 2 Output 3 Input 6 20 7 Output 2 Input 1 100 1 Output 100 "Correct Solution: ``` l, r, d = [int(x) for x in input().rstrip().split(" ")] print(r//d-l//d + (l%d==0)) ```
2,229
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. How many multiples of d are there among the integers between L and R (inclusive)? Constraints * All values in input are integers. * 1 \leq L \leq R \leq 100 * 1 \leq d \leq 100 Input Input is given from Standard Input in the following format: L R d Output Print the number of multiples of d among the integers between L and R (inclusive). Examples Input 5 10 2 Output 3 Input 6 20 7 Output 2 Input 1 100 1 Output 100 Submitted Solution: ``` a,b,c=map(int,input().split()) print(b//c-(a-1)//c) ``` Yes
2,230
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. How many multiples of d are there among the integers between L and R (inclusive)? Constraints * All values in input are integers. * 1 \leq L \leq R \leq 100 * 1 \leq d \leq 100 Input Input is given from Standard Input in the following format: L R d Output Print the number of multiples of d among the integers between L and R (inclusive). Examples Input 5 10 2 Output 3 Input 6 20 7 Output 2 Input 1 100 1 Output 100 Submitted Solution: ``` L, R, d = list(map(int, input().split())) ans = R // d - (L -1) // d print(ans) ``` Yes
2,231
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. How many multiples of d are there among the integers between L and R (inclusive)? Constraints * All values in input are integers. * 1 \leq L \leq R \leq 100 * 1 \leq d \leq 100 Input Input is given from Standard Input in the following format: L R d Output Print the number of multiples of d among the integers between L and R (inclusive). Examples Input 5 10 2 Output 3 Input 6 20 7 Output 2 Input 1 100 1 Output 100 Submitted Solution: ``` l,r,n=map(int,input().split()) a=(r//n)-(l//n if l!=n else 0) if l!=r:print(a) else: print(1) ``` Yes
2,232
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. How many multiples of d are there among the integers between L and R (inclusive)? Constraints * All values in input are integers. * 1 \leq L \leq R \leq 100 * 1 \leq d \leq 100 Input Input is given from Standard Input in the following format: L R d Output Print the number of multiples of d among the integers between L and R (inclusive). Examples Input 5 10 2 Output 3 Input 6 20 7 Output 2 Input 1 100 1 Output 100 Submitted Solution: ``` N, M ,L = map(int, input().split()) print(M//L-(N-1)//L) ``` Yes
2,233
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. How many multiples of d are there among the integers between L and R (inclusive)? Constraints * All values in input are integers. * 1 \leq L \leq R \leq 100 * 1 \leq d \leq 100 Input Input is given from Standard Input in the following format: L R d Output Print the number of multiples of d among the integers between L and R (inclusive). Examples Input 5 10 2 Output 3 Input 6 20 7 Output 2 Input 1 100 1 Output 100 Submitted Solution: ``` a,b,c=(int(x) for x in input().split()) if a != b: if b !=c: if 1 <= a: if b <= 100: if a < b: if 1 <= c <= 100: print((b-a)//c) ``` No
2,234
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. How many multiples of d are there among the integers between L and R (inclusive)? Constraints * All values in input are integers. * 1 \leq L \leq R \leq 100 * 1 \leq d \leq 100 Input Input is given from Standard Input in the following format: L R d Output Print the number of multiples of d among the integers between L and R (inclusive). Examples Input 5 10 2 Output 3 Input 6 20 7 Output 2 Input 1 100 1 Output 100 Submitted Solution: ``` l,r,d = map(int,input.split()) print((l-r+1) // d) ``` No
2,235
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. How many multiples of d are there among the integers between L and R (inclusive)? Constraints * All values in input are integers. * 1 \leq L \leq R \leq 100 * 1 \leq d \leq 100 Input Input is given from Standard Input in the following format: L R d Output Print the number of multiples of d among the integers between L and R (inclusive). Examples Input 5 10 2 Output 3 Input 6 20 7 Output 2 Input 1 100 1 Output 100 Submitted Solution: ``` s = input().split(" ") L = int(s[0]) R = int(s[1]) d = int(s[2]) cnt = 0 for i in range(L:R+1): if i%d == 0: cnt += 1 print(cnt) ``` No
2,236
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. How many multiples of d are there among the integers between L and R (inclusive)? Constraints * All values in input are integers. * 1 \leq L \leq R \leq 100 * 1 \leq d \leq 100 Input Input is given from Standard Input in the following format: L R d Output Print the number of multiples of d among the integers between L and R (inclusive). Examples Input 5 10 2 Output 3 Input 6 20 7 Output 2 Input 1 100 1 Output 100 Submitted Solution: ``` l,r,d=map(int,input().split()) ans=0 for i in range(l,r+1): if i%2==0: ans+=1 print(ans) ``` No
2,237
Provide a correct Python 3 solution for this coding contest problem. Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}). Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k. We will span edges in W as follows: * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l). * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l). * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v). Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an independent set in W, and print that total weight modulo 998,244,353. Constraints * 2 \leq N \leq 100,000 * 1 \leq M_1, M_2, M_3 \leq 100,000 * 1 \leq a_i, b_i, c_i, d_i, e_i, f_i \leq N * X, Y, and Z are simple, that is, they have no self-loops and no multiple edges. Input Input is given from Standard Input in the following format: N M_1 a_1 b_1 a_2 b_2 \vdots a_{M_1} b_{M_1} M_2 c_1 d_1 c_2 d_2 \vdots c_{M_2} d_{M_2} M_3 e_1 f_1 e_2 f_2 \vdots e_{M_3} f_{M_3} Output Print the maximum possible total weight of an independent set in W, modulo 998,244,353. Examples Input 2 1 1 2 1 1 2 1 1 2 Output 46494701 Input 3 3 1 3 1 2 3 2 2 2 1 2 3 1 2 1 Output 883188316 Input 100000 1 1 2 1 99999 100000 1 1 100000 Output 318525248 "Correct Solution: ``` import sys input = lambda: sys.stdin.readline().rstrip() def calc(): def mex(i): D = {} for j in X[i]: D[G[j]] = 1 for g in range(N + 1): if g not in D: return g M = int(input()) X = [[] for _ in range(N)] for _ in range(M): a, b = map(int, input().split()) X[N - min(a, b)].append(N - max(a, b)) G = [0] * N for i in range(N): G[i] = mex(i) H = [0] * 1024 a = 1 for i in range(N)[::-1]: a = a * A % P H[G[i]] = (H[G[i]] + a) % P return H P = 998244353 A = pow(10, 18, P) N = int(input()) H1, H2, H3 = calc(), calc(), calc() ans = 0 for i in range(1024): if H1[i] == 0: continue for j in range(1024): if H2[j] == 0: continue ans = (ans + H1[i] * H2[j] * H3[i^j]) % P print(ans) ```
2,238
Provide a correct Python 3 solution for this coding contest problem. Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}). Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k. We will span edges in W as follows: * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l). * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l). * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v). Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an independent set in W, and print that total weight modulo 998,244,353. Constraints * 2 \leq N \leq 100,000 * 1 \leq M_1, M_2, M_3 \leq 100,000 * 1 \leq a_i, b_i, c_i, d_i, e_i, f_i \leq N * X, Y, and Z are simple, that is, they have no self-loops and no multiple edges. Input Input is given from Standard Input in the following format: N M_1 a_1 b_1 a_2 b_2 \vdots a_{M_1} b_{M_1} M_2 c_1 d_1 c_2 d_2 \vdots c_{M_2} d_{M_2} M_3 e_1 f_1 e_2 f_2 \vdots e_{M_3} f_{M_3} Output Print the maximum possible total weight of an independent set in W, modulo 998,244,353. Examples Input 2 1 1 2 1 1 2 1 1 2 Output 46494701 Input 3 3 1 3 1 2 3 2 2 2 1 2 3 1 2 1 Output 883188316 Input 100000 1 1 2 1 99999 100000 1 1 100000 Output 318525248 "Correct Solution: ``` from collections import defaultdict M = 998244353 B = 10**18 % M def mex(s): for i in range(N+1): if i not in s: return i def ext_euc(a, b): x1, y1, z1 = 1, 0, a x2, y2, z2 = 0, 1, b while z1 != 1: d, m = divmod(z2,z1) x1, x2 = x2-d*x1, x1 y1, y2 = y2-d*y1, y1 z1, z2 = m, z1 return x1, y1 def inv_mod(a, b, m): x, y = ext_euc(a, m) return (x * b % m) def calc_grundy(e): g = {} sum_g = defaultdict(int) sum_g[0] = inv_mod(B-1, pow(B, N+1, M)-B, M) for i in sorted(e.keys(), reverse=True): m = mex({g.get(j, 0) for j in e[i]}) if m: g[i] = m x = pow(B, i, M) sum_g[m] += x sum_g[0] -= x return sum_g def read_edge(): M = int(input()) e = defaultdict(set) for i in range(M): a, b = sorted(map(int, input().split())) e[a].add(b) return e def solve(N, edge): sum_g = list(map(calc_grundy, edge)) ret = 0 for gx, sx in sum_g[0].items(): for gy, sy in sum_g[1].items(): gz = gx^gy sz = sum_g[2][gz] if sz: ret = (ret + sx*sy*sz)%M return ret N = int(input()) edge = [read_edge() for i in range(3)] print(solve(N, edge)) ```
2,239
Provide a correct Python 3 solution for this coding contest problem. Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}). Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k. We will span edges in W as follows: * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l). * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l). * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v). Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an independent set in W, and print that total weight modulo 998,244,353. Constraints * 2 \leq N \leq 100,000 * 1 \leq M_1, M_2, M_3 \leq 100,000 * 1 \leq a_i, b_i, c_i, d_i, e_i, f_i \leq N * X, Y, and Z are simple, that is, they have no self-loops and no multiple edges. Input Input is given from Standard Input in the following format: N M_1 a_1 b_1 a_2 b_2 \vdots a_{M_1} b_{M_1} M_2 c_1 d_1 c_2 d_2 \vdots c_{M_2} d_{M_2} M_3 e_1 f_1 e_2 f_2 \vdots e_{M_3} f_{M_3} Output Print the maximum possible total weight of an independent set in W, modulo 998,244,353. Examples Input 2 1 1 2 1 1 2 1 1 2 Output 46494701 Input 3 3 1 3 1 2 3 2 2 2 1 2 3 1 2 1 Output 883188316 Input 100000 1 1 2 1 99999 100000 1 1 100000 Output 318525248 "Correct Solution: ``` from collections import defaultdict import sys input = lambda: sys.stdin.readline().rstrip() M = 998244353 B = pow(10, 18, M) N = int(input()) def geometric_mod(a, r, m, n): x = a for i in range(n): yield x x = (x*r)%m BB = list(geometric_mod(1, B, M, N+2)) def ext_euc(a, b): x1, y1, z1 = 1, 0, a x2, y2, z2 = 0, 1, b while z1 != 1: d, m = divmod(z2,z1) x1, x2 = x2-d*x1, x1 y1, y2 = y2-d*y1, y1 z1, z2 = m, z1 return x1, y1 def inv_mod(a, b, m): x, y = ext_euc(a, m) return (x * b % m) def mex(s): for i in range(N+1): if i not in s: return i def calc_grundy(e): g = {} sum_g = defaultdict(int) sum_g[0] = inv_mod(B-1, pow(B, N+1, M)-B, M) for i in range(N, 0, -1): if i not in e: continue m = mex({g.get(j, 0) for j in set(e[i])}) if m: g[i] = m sum_g[g[i]] = (sum_g[g[i]] + BB[i]) % M sum_g[0] = (sum_g[0] - BB[i]) % M return sum_g def read_edge(): M = int(input()) e = defaultdict(list) for i in range(M): a, b = sorted(map(int, input().split())) e[a].append(b) return e def solve(N, edge): sum_g = list(map(calc_grundy, edge)) ret = 0 for gx, sx in sum_g[0].items(): for gy, sy in sum_g[1].items(): gz = gx^gy sz = sum_g[2][gz] if sz: ret = (ret + sx*sy*sz) % M return ret edge = [read_edge() for i in range(3)] print(solve(N, edge)) ```
2,240
Provide a correct Python 3 solution for this coding contest problem. Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}). Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k. We will span edges in W as follows: * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l). * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l). * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v). Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an independent set in W, and print that total weight modulo 998,244,353. Constraints * 2 \leq N \leq 100,000 * 1 \leq M_1, M_2, M_3 \leq 100,000 * 1 \leq a_i, b_i, c_i, d_i, e_i, f_i \leq N * X, Y, and Z are simple, that is, they have no self-loops and no multiple edges. Input Input is given from Standard Input in the following format: N M_1 a_1 b_1 a_2 b_2 \vdots a_{M_1} b_{M_1} M_2 c_1 d_1 c_2 d_2 \vdots c_{M_2} d_{M_2} M_3 e_1 f_1 e_2 f_2 \vdots e_{M_3} f_{M_3} Output Print the maximum possible total weight of an independent set in W, modulo 998,244,353. Examples Input 2 1 1 2 1 1 2 1 1 2 Output 46494701 Input 3 3 1 3 1 2 3 2 2 2 1 2 3 1 2 1 Output 883188316 Input 100000 1 1 2 1 99999 100000 1 1 100000 Output 318525248 "Correct Solution: ``` import sys from collections import defaultdict n, *mabs = map(int, sys.stdin.buffer.read().split()) MOD = 998244353 base = 10 ** 18 % MOD base_costs = [base] for i in range(n - 1): base_costs.append(base_costs[-1] * base % MOD) graphs = [] i = 0 for _ in range(3): m = mabs[i] i += 1 j = i + 2 * m parents = [set() for _ in range(n)] for a, b in zip(mabs[i:j:2], mabs[i + 1:j:2]): a -= 1 b -= 1 if a > b: a, b = b, a parents[a].add(b) grundy = [0] * n weights = defaultdict(int) for v in range(n - 1, -1, -1): pgs = {grundy[p] for p in parents[v]} for g in range(m + 1): if g not in pgs: grundy[v] = g weights[g] += base_costs[v] break for g in weights: weights[g] %= MOD graphs.append(dict(weights)) i = j ans = 0 graph_x, graph_y, graph_z = graphs for gx, cx in graph_x.items(): for gy, cy in graph_y.items(): gz = gx ^ gy if gz in graph_z: ans = (ans + cx * cy * graph_z[gz]) % MOD print(ans) ```
2,241
Provide a correct Python 3 solution for this coding contest problem. Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}). Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k. We will span edges in W as follows: * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l). * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l). * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v). Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an independent set in W, and print that total weight modulo 998,244,353. Constraints * 2 \leq N \leq 100,000 * 1 \leq M_1, M_2, M_3 \leq 100,000 * 1 \leq a_i, b_i, c_i, d_i, e_i, f_i \leq N * X, Y, and Z are simple, that is, they have no self-loops and no multiple edges. Input Input is given from Standard Input in the following format: N M_1 a_1 b_1 a_2 b_2 \vdots a_{M_1} b_{M_1} M_2 c_1 d_1 c_2 d_2 \vdots c_{M_2} d_{M_2} M_3 e_1 f_1 e_2 f_2 \vdots e_{M_3} f_{M_3} Output Print the maximum possible total weight of an independent set in W, modulo 998,244,353. Examples Input 2 1 1 2 1 1 2 1 1 2 Output 46494701 Input 3 3 1 3 1 2 3 2 2 2 1 2 3 1 2 1 Output 883188316 Input 100000 1 1 2 1 99999 100000 1 1 100000 Output 318525248 "Correct Solution: ``` from collections import defaultdict import sys input = lambda: sys.stdin.readline().rstrip() M = 998244353 B = pow(10, 18, M) N = int(input()) def geometric_mod(a, r, m, n): x = a for i in range(n): yield x x = (x*r)%m BB = list(geometric_mod(1, B, M, N+2)) def ext_euc(a, b): x1, y1, z1 = 1, 0, a x2, y2, z2 = 0, 1, b while z1 != 1: d, m = divmod(z2,z1) x1, x2 = x2-d*x1, x1 y1, y2 = y2-d*y1, y1 z1, z2 = m, z1 return x1, y1 def inv_mod(a, b, m): x, y = ext_euc(a, m) return (x * b % m) def mex(s): for i in range(N+1): if i not in s: return i def calc_grundy(e): g = {} sum_g = defaultdict(int) for i in range(N, 0, -1): if i not in e: continue m = mex({g.get(j, 0) for j in e[i]}) if m: g[i] = m sum_g[g[i]] = (sum_g[g[i]] + BB[i]) % M sum_g[0] = (sum_g[0] + BB[i]) % M sum_g[0] = (inv_mod(B-1, pow(B, N+1, M)-B, M) - sum_g[0]) % M return sum_g def read_edge(): M = int(input()) e = defaultdict(set) for i in range(M): a, b = sorted(map(int, input().split())) e[a].add(b) return e def solve(N, edge): sum_g = list(map(calc_grundy, edge)) ret = 0 for gx, sx in sum_g[0].items(): for gy, sy in sum_g[1].items(): gz = gx^gy sz = sum_g[2][gz] if sz: ret = (ret + sx*sy*sz) % M return ret edge = [read_edge() for i in range(3)] print(solve(N, edge)) ```
2,242
Provide a correct Python 3 solution for this coding contest problem. Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}). Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k. We will span edges in W as follows: * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l). * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l). * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v). Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an independent set in W, and print that total weight modulo 998,244,353. Constraints * 2 \leq N \leq 100,000 * 1 \leq M_1, M_2, M_3 \leq 100,000 * 1 \leq a_i, b_i, c_i, d_i, e_i, f_i \leq N * X, Y, and Z are simple, that is, they have no self-loops and no multiple edges. Input Input is given from Standard Input in the following format: N M_1 a_1 b_1 a_2 b_2 \vdots a_{M_1} b_{M_1} M_2 c_1 d_1 c_2 d_2 \vdots c_{M_2} d_{M_2} M_3 e_1 f_1 e_2 f_2 \vdots e_{M_3} f_{M_3} Output Print the maximum possible total weight of an independent set in W, modulo 998,244,353. Examples Input 2 1 1 2 1 1 2 1 1 2 Output 46494701 Input 3 3 1 3 1 2 3 2 2 2 1 2 3 1 2 1 Output 883188316 Input 100000 1 1 2 1 99999 100000 1 1 100000 Output 318525248 "Correct Solution: ``` from collections import defaultdict M = 998244353 B = pow(10, 18, M) def ext_euc(a, b): x1, y1, z1 = 1, 0, a x2, y2, z2 = 0, 1, b while z1 != 1: d, m = divmod(z2,z1) x1, x2 = x2-d*x1, x1 y1, y2 = y2-d*y1, y1 z1, z2 = m, z1 return x1, y1 def inv_mod(a, b, m): x, y = ext_euc(a, m) return (x * b % m) def mex(s): for i in range(N+1): if i not in s: return i def calc_grundy(e): g = {} sum_g = defaultdict(int) sum_g[0] = inv_mod(B-1, pow(B, N+1, M)-B, M) for i in range(N, 0, -1): if i not in e: continue m = mex({g.get(j, 0) for j in e[i]}) if m: g[i] = m x = pow(B, i, M) sum_g[m] = (sum_g[m] + x) % M sum_g[0] = (sum_g[0] - x) % M return sum_g def read_edge(): M = int(input()) e = defaultdict(set) for i in range(M): a, b = sorted(map(int, input().split())) e[a].add(b) return e def solve(N, edge): sum_g = list(map(calc_grundy, edge)) ret = 0 for gx, sx in sum_g[0].items(): for gy, sy in sum_g[1].items(): gz = gx^gy sz = sum_g[2][gz] if sz: ret = (ret + sx*sy*sz)%M return ret N = int(input()) edge = [read_edge() for i in range(3)] print(solve(N, edge)) ```
2,243
Provide a correct Python 3 solution for this coding contest problem. Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}). Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k. We will span edges in W as follows: * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l). * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l). * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v). Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an independent set in W, and print that total weight modulo 998,244,353. Constraints * 2 \leq N \leq 100,000 * 1 \leq M_1, M_2, M_3 \leq 100,000 * 1 \leq a_i, b_i, c_i, d_i, e_i, f_i \leq N * X, Y, and Z are simple, that is, they have no self-loops and no multiple edges. Input Input is given from Standard Input in the following format: N M_1 a_1 b_1 a_2 b_2 \vdots a_{M_1} b_{M_1} M_2 c_1 d_1 c_2 d_2 \vdots c_{M_2} d_{M_2} M_3 e_1 f_1 e_2 f_2 \vdots e_{M_3} f_{M_3} Output Print the maximum possible total weight of an independent set in W, modulo 998,244,353. Examples Input 2 1 1 2 1 1 2 1 1 2 Output 46494701 Input 3 3 1 3 1 2 3 2 2 2 1 2 3 1 2 1 Output 883188316 Input 100000 1 1 2 1 99999 100000 1 1 100000 Output 318525248 "Correct Solution: ``` from collections import defaultdict M = 998244353 B = 10**18 % M def mex(s): for i in range(max(s)+2): if i not in s: return i def ext_euc(a, b): x1, y1, z1 = 1, 0, a x2, y2, z2 = 0, 1, b while z1 != 1: d, m = divmod(z2,z1) x1, x2 = x2-d*x1, x1 y1, y2 = y2-d*y1, y1 z1, z2 = m, z1 return x1, y1 def inv_mod(a, b, m): x, y = ext_euc(a, m) return (x * b % m) def calc_grundy(e): g = {} sum_g = defaultdict(int) sum_g[0] = inv_mod(B-1, pow(B, N+1, M)-B, M) for i in sorted(e.keys(), reverse=True): m = mex({g.get(j, 0) for j in e[i]}) if m: g[i] = m x = pow(B, i, M) sum_g[m] += x sum_g[0] -= x return sum_g def read_edge(): M = int(input()) e = defaultdict(set) for i in range(M): a, b = sorted(map(int, input().split())) e[a].add(b) return e def solve(N, edge): sum_g = list(map(calc_grundy, edge)) ret = 0 for gx, sx in sum_g[0].items(): for gy, sy in sum_g[1].items(): gz = gx^gy sz = sum_g[2][gz] if sz: ret = (ret + sx*sy*sz)%M return ret N = int(input()) edge = [read_edge() for i in range(3)] print(solve(N, edge)) ```
2,244
Provide a correct Python 3 solution for this coding contest problem. Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}). Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k. We will span edges in W as follows: * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l). * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l). * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v). Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an independent set in W, and print that total weight modulo 998,244,353. Constraints * 2 \leq N \leq 100,000 * 1 \leq M_1, M_2, M_3 \leq 100,000 * 1 \leq a_i, b_i, c_i, d_i, e_i, f_i \leq N * X, Y, and Z are simple, that is, they have no self-loops and no multiple edges. Input Input is given from Standard Input in the following format: N M_1 a_1 b_1 a_2 b_2 \vdots a_{M_1} b_{M_1} M_2 c_1 d_1 c_2 d_2 \vdots c_{M_2} d_{M_2} M_3 e_1 f_1 e_2 f_2 \vdots e_{M_3} f_{M_3} Output Print the maximum possible total weight of an independent set in W, modulo 998,244,353. Examples Input 2 1 1 2 1 1 2 1 1 2 Output 46494701 Input 3 3 1 3 1 2 3 2 2 2 1 2 3 1 2 1 Output 883188316 Input 100000 1 1 2 1 99999 100000 1 1 100000 Output 318525248 "Correct Solution: ``` from collections import defaultdict M = 998244353 B = pow(10, 18, M) N = int(input()) def ext_euc(a, b): x1, y1, z1 = 1, 0, a x2, y2, z2 = 0, 1, b while z1 != 1: d, m = divmod(z2,z1) x1, x2 = x2-d*x1, x1 y1, y2 = y2-d*y1, y1 z1, z2 = m, z1 return x1, y1 def inv_mod(a, b, m): x, y = ext_euc(a, m) return (x * b % m) def mex(s): for i in range(N+1): if i not in s: return i def calc_grundy(e): g = {} sum_g = defaultdict(int) sum_g[0] = inv_mod(B-1, pow(B, N+1, M)-B, M) for i in range(N, 0, -1): if i not in e: continue m = mex({g.get(j, 0) for j in e[i]}) if m: g[i] = m x = pow(B, i, M) sum_g[m] += x sum_g[0] -= x return sum_g def read_edge(): M = int(input()) e = defaultdict(set) for i in range(M): a, b = sorted(map(int, input().split())) e[a].add(b) return e def solve(N, edge): sum_g = list(map(calc_grundy, edge)) ret = 0 for gx, sx in sum_g[0].items(): for gy, sy in sum_g[1].items(): gz = gx^gy sz = sum_g[2][gz] if sz: ret += (sx*sy*sz) % M return ret%M edge = [read_edge() for i in range(3)] print(solve(N, edge)) ```
2,245
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}). Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k. We will span edges in W as follows: * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l). * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l). * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v). Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an independent set in W, and print that total weight modulo 998,244,353. Constraints * 2 \leq N \leq 100,000 * 1 \leq M_1, M_2, M_3 \leq 100,000 * 1 \leq a_i, b_i, c_i, d_i, e_i, f_i \leq N * X, Y, and Z are simple, that is, they have no self-loops and no multiple edges. Input Input is given from Standard Input in the following format: N M_1 a_1 b_1 a_2 b_2 \vdots a_{M_1} b_{M_1} M_2 c_1 d_1 c_2 d_2 \vdots c_{M_2} d_{M_2} M_3 e_1 f_1 e_2 f_2 \vdots e_{M_3} f_{M_3} Output Print the maximum possible total weight of an independent set in W, modulo 998,244,353. Examples Input 2 1 1 2 1 1 2 1 1 2 Output 46494701 Input 3 3 1 3 1 2 3 2 2 2 1 2 3 1 2 1 Output 883188316 Input 100000 1 1 2 1 99999 100000 1 1 100000 Output 318525248 Submitted Solution: ``` import itertools import os import sys from collections import defaultdict if os.getenv("LOCAL"): sys.stdin = open("_in.txt", "r") sys.setrecursionlimit(10 ** 9) INF = float("inf") IINF = 10 ** 18 # MOD = 10 ** 9 + 7 MOD = 998244353 # 解説 # 大きい方から貪欲に取るのの見方を変えるとゲームになる N = int(sys.stdin.buffer.readline()) M = [] m = int(sys.stdin.buffer.readline()) M.append(m) XVU = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(m)] m = int(sys.stdin.buffer.readline()) M.append(m) YVU = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(m)] m = int(sys.stdin.buffer.readline()) M.append(m) ZVU = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(m)] X = [[] for _ in range(N)] Y = [[] for _ in range(N)] Z = [[] for _ in range(N)] for G, VU in zip([X, Y, Z], (XVU, YVU, ZVU)): for v, u in VU: v -= 1 u -= 1 if v > u: v, u = u, v G[v].append(u) def grundy(graph): ret = [-1] * N for v in reversed(range(N)): gs = set() for u in graph[v]: gs.add(ret[u]) g = 0 while g in gs: g += 1 ret[v] = g return ret grundy_x = defaultdict(list) grundy_y = defaultdict(list) grundy_z = defaultdict(list) for x, g in enumerate(grundy(X), 1): grundy_x[g].append(x) for y, g in enumerate(grundy(Y), 1): grundy_y[g].append(y) for z, g in enumerate(grundy(Z), 1): grundy_z[g].append(z) # base[i]: (10^18)^i base = [1] for _ in range(N * 3 + 10): base.append(base[-1] * pow(10, 18, MOD) % MOD) ans = 0 for xg, yg in itertools.product(grundy_x.keys(), grundy_y.keys()): zg = xg ^ yg # 10^(x1+y1+z1) + 10^(x1+y1+z2) + ... + 10^(x1+y2+z1) + ... # = (10^x1 + 10^x2 + ...) * (10^y1 + ...) * (10^z1 + ...) xs = 0 ys = 0 zs = 0 for x in grundy_x[xg]: xs += base[x] for y in grundy_y[yg]: ys += base[y] for z in grundy_z[zg]: zs += base[z] ans += xs % MOD * ys % MOD * zs % MOD ans %= MOD print(ans) ``` Yes
2,246
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}). Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k. We will span edges in W as follows: * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l). * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l). * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v). Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an independent set in W, and print that total weight modulo 998,244,353. Constraints * 2 \leq N \leq 100,000 * 1 \leq M_1, M_2, M_3 \leq 100,000 * 1 \leq a_i, b_i, c_i, d_i, e_i, f_i \leq N * X, Y, and Z are simple, that is, they have no self-loops and no multiple edges. Input Input is given from Standard Input in the following format: N M_1 a_1 b_1 a_2 b_2 \vdots a_{M_1} b_{M_1} M_2 c_1 d_1 c_2 d_2 \vdots c_{M_2} d_{M_2} M_3 e_1 f_1 e_2 f_2 \vdots e_{M_3} f_{M_3} Output Print the maximum possible total weight of an independent set in W, modulo 998,244,353. Examples Input 2 1 1 2 1 1 2 1 1 2 Output 46494701 Input 3 3 1 3 1 2 3 2 2 2 1 2 3 1 2 1 Output 883188316 Input 100000 1 1 2 1 99999 100000 1 1 100000 Output 318525248 Submitted Solution: ``` import sys input = lambda: sys.stdin.readline().rstrip() def calc(): def mex(i): t = 0 for j in X[i]: t |= 1 << G[j] t = ~t return (t & -t).bit_length() - 1 M = int(input()) X = [[] for _ in range(N)] for _ in range(M): a, b = map(int, input().split()) X[N - min(a, b)].append(N - max(a, b)) G = [0] * N for i in range(N): G[i] = mex(i) H = [0] * 1024 a = 1 for i in range(N)[::-1]: a = a * A % P H[G[i]] = (H[G[i]] + a) % P return H P = 998244353 A = pow(10, 18, P) N = int(input()) H1, H2, H3 = calc(), calc(), calc() ans = 0 for i in range(1024): if H1[i] == 0: continue for j in range(1024): if H2[j] == 0: continue ans = (ans + H1[i] * H2[j] * H3[i^j]) % P print(ans) ``` Yes
2,247
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}). Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k. We will span edges in W as follows: * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l). * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l). * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v). Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an independent set in W, and print that total weight modulo 998,244,353. Constraints * 2 \leq N \leq 100,000 * 1 \leq M_1, M_2, M_3 \leq 100,000 * 1 \leq a_i, b_i, c_i, d_i, e_i, f_i \leq N * X, Y, and Z are simple, that is, they have no self-loops and no multiple edges. Input Input is given from Standard Input in the following format: N M_1 a_1 b_1 a_2 b_2 \vdots a_{M_1} b_{M_1} M_2 c_1 d_1 c_2 d_2 \vdots c_{M_2} d_{M_2} M_3 e_1 f_1 e_2 f_2 \vdots e_{M_3} f_{M_3} Output Print the maximum possible total weight of an independent set in W, modulo 998,244,353. Examples Input 2 1 1 2 1 1 2 1 1 2 Output 46494701 Input 3 3 1 3 1 2 3 2 2 2 1 2 3 1 2 1 Output 883188316 Input 100000 1 1 2 1 99999 100000 1 1 100000 Output 318525248 Submitted Solution: ``` mod=998244353 inv=pow(2,mod-2,mod) def xorconv(n,X,Y): if n==0: res=[(X[0]*Y[0])%mod] return res x=[X[i]+X[i+2**(n-1)] for i in range(2**(n-1))] y=[Y[i]+Y[i+2**(n-1)] for i in range(2**(n-1))] z=[X[i]-X[i+2**(n-1)] for i in range(2**(n-1))] w=[Y[i]-Y[i+2**(n-1)] for i in range(2**(n-1))] res1=xorconv(n-1,x,y) res2=xorconv(n-1,z,w) former=[(res1[i]+res2[i])*inv for i in range(2**(n-1))] latter=[(res1[i]-res2[i])*inv for i in range(2**(n-1))] former=list(map(lambda x:x%mod,former)) latter=list(map(lambda x:x%mod,latter)) return former+latter import sys input=sys.stdin.buffer.readline N=int(input()) def data(): edge=[[] for i in range(N)] for i in range(int(input())): a,b=map(int,input().split()) if a>b: a,b=b,a edge[a-1].append(b-1) mex=[0 for i in range(N)] for i in range(N-1,-1,-1): used=[False]*(len(edge[i])+1) for pv in edge[i]: if mex[pv]<=len(edge[i]): used[mex[pv]]=True for j in range(len(used)): if not used[j]: mex[i]=j break res=[0]*(max(mex)+1) for i in range(N): res[mex[i]]+=pow(10,18*(i+1),mod) res[mex[i]]%=mod return res Xdata=data() Ydata=data() Zdata=data() n=0 m=max(len(Xdata),len(Ydata),len(Zdata)) while 2**n<m: n+=1 X=[0 for i in range(2**n)] Y=[0 for i in range(2**n)] Z=[0 for i in range(2**n)] for i in range(len(Xdata)): X[i]=Xdata[i] for i in range(len(Ydata)): Y[i]=Ydata[i] for i in range(len(Zdata)): Z[i]=Zdata[i] YZ=xorconv(n,Y,Z) ans=0 for i in range(2**n): ans+=X[i]*YZ[i] ans%=mod print(ans) ``` Yes
2,248
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}). Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k. We will span edges in W as follows: * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l). * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l). * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v). Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an independent set in W, and print that total weight modulo 998,244,353. Constraints * 2 \leq N \leq 100,000 * 1 \leq M_1, M_2, M_3 \leq 100,000 * 1 \leq a_i, b_i, c_i, d_i, e_i, f_i \leq N * X, Y, and Z are simple, that is, they have no self-loops and no multiple edges. Input Input is given from Standard Input in the following format: N M_1 a_1 b_1 a_2 b_2 \vdots a_{M_1} b_{M_1} M_2 c_1 d_1 c_2 d_2 \vdots c_{M_2} d_{M_2} M_3 e_1 f_1 e_2 f_2 \vdots e_{M_3} f_{M_3} Output Print the maximum possible total weight of an independent set in W, modulo 998,244,353. Examples Input 2 1 1 2 1 1 2 1 1 2 Output 46494701 Input 3 3 1 3 1 2 3 2 2 2 1 2 3 1 2 1 Output 883188316 Input 100000 1 1 2 1 99999 100000 1 1 100000 Output 318525248 Submitted Solution: ``` from collections import defaultdict M = 998244353 B = pow(10, 18, M) N = int(input()) def geometric_mod(a, r, m, n): x = a for i in range(n): yield x x = (x*r)%m BB = list(geometric_mod(1, B, M, N+2)) def ext_euc(a, b): x1, y1, z1 = 1, 0, a x2, y2, z2 = 0, 1, b while z1 != 1: d, m = divmod(z2,z1) x1, x2 = x2-d*x1, x1 y1, y2 = y2-d*y1, y1 z1, z2 = m, z1 return x1, y1 def inv_mod(a, b, m): x, y = ext_euc(a, m) return (x * b % m) def mex(s): for i in range(N+1): if i not in s: return i def calc_grundy(e): g = {} sum_g = defaultdict(int) sum_g[0] = inv_mod(B-1, pow(B, N+1, M)-B, M) for i in range(N, 0, -1): if i not in e: continue m = mex({g.get(j, 0) for j in set(e[i])}) if m: g[i] = m sum_g[g[i]] = (sum_g[g[i]] + BB[i]) % M sum_g[0] = (sum_g[0] - BB[i]) % M return sum_g def read_edge(): M = int(input()) e = defaultdict(list) for i in range(M): a, b = sorted(map(int, input().split())) e[a].append(b) return e def solve(N, edge): sum_g = list(map(calc_grundy, edge)) ret = 0 for gx, sx in sum_g[0].items(): for gy, sy in sum_g[1].items(): gz = gx^gy sz = sum_g[2][gz] if sz: ret = (ret + sx*sy*sz) % M return ret edge = [read_edge() for i in range(3)] print(solve(N, edge)) ``` Yes
2,249
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}). Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k. We will span edges in W as follows: * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l). * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l). * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v). Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an independent set in W, and print that total weight modulo 998,244,353. Constraints * 2 \leq N \leq 100,000 * 1 \leq M_1, M_2, M_3 \leq 100,000 * 1 \leq a_i, b_i, c_i, d_i, e_i, f_i \leq N * X, Y, and Z are simple, that is, they have no self-loops and no multiple edges. Input Input is given from Standard Input in the following format: N M_1 a_1 b_1 a_2 b_2 \vdots a_{M_1} b_{M_1} M_2 c_1 d_1 c_2 d_2 \vdots c_{M_2} d_{M_2} M_3 e_1 f_1 e_2 f_2 \vdots e_{M_3} f_{M_3} Output Print the maximum possible total weight of an independent set in W, modulo 998,244,353. Examples Input 2 1 1 2 1 1 2 1 1 2 Output 46494701 Input 3 3 1 3 1 2 3 2 2 2 1 2 3 1 2 1 Output 883188316 Input 100000 1 1 2 1 99999 100000 1 1 100000 Output 318525248 Submitted Solution: ``` import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines n = int(readline()) data = tuple(map(int,read().split())) m1 = data[0] ab = data[1:1+2*m1] m2 = data[1+2*m1] cd = data[2+2*m1:2+2*(m1+m2)] m3 = data[2+2*(m1+m2)] ef = data[3+2*(m1+m2):] mod = 998244353 mods = [1] * (n+10) base = pow(10,18,mod) for i in range(1,n+1): mods[i] = (mods[i-1] * base)%mod def calc_node(xy): links = [[] for _ in range(n+1)] it = iter(xy) for x,y in zip(it,it): links[x].append(y) links[y].append(x) group = [-1] * (n+1) group[-1] = 0 num_max = 1 for i in range(n,0,-1): remain = set(range(num_max+2)) for j in links[i]: if(i < j): remain.discard(group[j]) num = min(remain) group[i] = num num_max = max(num_max,num) res = [0] * (num_max+1) for i,num in enumerate(group[1:],1): res[num] += mods[i] res[num] %= mod return res x = calc_node(ab) y = calc_node(cd) z = calc_node(ef) if(len(x) < len(y)): x,y = y,x if(len(y) < len(z)): y,z = z,y if(len(x) < len(y)): x,y = y,x len_2 = 2**(len(y)-1).bit_length() yz = [0] * (len_2) for j in range(len(y)): for k in range(len(z)): yz[j^k] += y[j]*z[k] yz[j^k] %= mod ans = 0 for i,j in zip(x,yz): ans += i*j ans %= mod print(ans) ``` No
2,250
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}). Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k. We will span edges in W as follows: * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l). * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l). * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v). Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an independent set in W, and print that total weight modulo 998,244,353. Constraints * 2 \leq N \leq 100,000 * 1 \leq M_1, M_2, M_3 \leq 100,000 * 1 \leq a_i, b_i, c_i, d_i, e_i, f_i \leq N * X, Y, and Z are simple, that is, they have no self-loops and no multiple edges. Input Input is given from Standard Input in the following format: N M_1 a_1 b_1 a_2 b_2 \vdots a_{M_1} b_{M_1} M_2 c_1 d_1 c_2 d_2 \vdots c_{M_2} d_{M_2} M_3 e_1 f_1 e_2 f_2 \vdots e_{M_3} f_{M_3} Output Print the maximum possible total weight of an independent set in W, modulo 998,244,353. Examples Input 2 1 1 2 1 1 2 1 1 2 Output 46494701 Input 3 3 1 3 1 2 3 2 2 2 1 2 3 1 2 1 Output 883188316 Input 100000 1 1 2 1 99999 100000 1 1 100000 Output 318525248 Submitted Solution: ``` def main(): """"ここに今までのコード""" import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines n = int(readline()) data = tuple(map(int,read().split())) m1 = data[0] ab = data[1:1+2*m1] m2 = data[1+2*m1] cd = data[2+2*m1:2+2*(m1+m2)] m3 = data[2+2*(m1+m2)] ef = data[3+2*(m1+m2):] mod = 998244353 mods = [1] * (n+10) base = pow(10,18,mod) for i in range(1,n+1): mods[i] = (mods[i-1] * base)%mod def calc_node(xy): links = [[] for _ in range(n+1)] it = iter(xy) for x,y in zip(it,it): if(x < y): x,y = y,x links[y].append(x) group = [-1] * (n+1) group[-1] = 0 num_max = 1 for i in range(n,0,-1): remain = set(range(num_max+2)) for j in links[i]: remain.discard(group[j]) num = min(remain) group[i] = num num_max = max(num_max,num) res = [0] * (num_max+1) for i,num in enumerate(group[1:],1): res[num] += mods[i] res[num] %= mod return res x = calc_node(ab) y = calc_node(cd) z = calc_node(ef) if(len(x) < len(y)): x,y = y,x if(len(y) < len(z)): y,z = z,y if(len(x) < len(y)): x,y = y,x len_2 = 2**(len(y)-1).bit_length() yz = [0] * (len_2) for j in range(len(y)): for k in range(len(z)): yz[j^k] += y[j]*z[k] yz[j^k] %= mod ans = 0 for i,j in zip(x,yz): ans += i*j ans %= mod print(ans) if __name__ == '__main__': main() ``` No
2,251
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}). Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k. We will span edges in W as follows: * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l). * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l). * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v). Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an independent set in W, and print that total weight modulo 998,244,353. Constraints * 2 \leq N \leq 100,000 * 1 \leq M_1, M_2, M_3 \leq 100,000 * 1 \leq a_i, b_i, c_i, d_i, e_i, f_i \leq N * X, Y, and Z are simple, that is, they have no self-loops and no multiple edges. Input Input is given from Standard Input in the following format: N M_1 a_1 b_1 a_2 b_2 \vdots a_{M_1} b_{M_1} M_2 c_1 d_1 c_2 d_2 \vdots c_{M_2} d_{M_2} M_3 e_1 f_1 e_2 f_2 \vdots e_{M_3} f_{M_3} Output Print the maximum possible total weight of an independent set in W, modulo 998,244,353. Examples Input 2 1 1 2 1 1 2 1 1 2 Output 46494701 Input 3 3 1 3 1 2 3 2 2 2 1 2 3 1 2 1 Output 883188316 Input 100000 1 1 2 1 99999 100000 1 1 100000 Output 318525248 Submitted Solution: ``` from collections import defaultdict M = 998244353 B = pow(10, 18, M) N = int(input()) def geometric_mod(a, r, m, n): x = a for i in range(n): yield x x = (x*r)%m BB = list(geometric_mod(1, B, M, N+2)) def ext_euc(a, b): x1, y1, z1 = 1, 0, a x2, y2, z2 = 0, 1, b while z1 != 1: d, m = divmod(z2,z1) x1, x2 = x2-d*x1, x1 y1, y2 = y2-d*y1, y1 z1, z2 = m, z1 return x1, y1 def inv_mod(a, b, m): x, y = ext_euc(a, m) return (x * b % m) def mex(s): for i in range(N+1): if i not in s: return i def calc_grundy(e): e = set(e) g = {} sum_g = defaultdict(int) sum_g[0] = inv_mod(B-1, pow(B, N+1, M)-B, M) for i in range(N, 0, -1): if i not in e: continue m = mex({g.get(j, 0) for j in e[i]}) if m: g[i] = m sum_g[g[i]] = (sum_g[g[i]] + BB[i]) % M sum_g[0] = (sum_g[0] - BB[i]) % M return sum_g def read_edge(): M = int(input()) e = defaultdict(list) for i in range(M): a, b = sorted(map(int, input().split())) e[a].append(b) return e def solve(N, edge): sum_g = list(map(calc_grundy, edge)) ret = 0 for gx, sx in sum_g[0].items(): for gy, sy in sum_g[1].items(): gz = gx^gy sz = sum_g[2][gz] if sz: ret = (ret + sx*sy*sz) % M return ret edge = [read_edge() for i in range(3)] print(solve(N, edge)) ``` No
2,252
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}). Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k. We will span edges in W as follows: * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l). * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l). * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v). Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an independent set in W, and print that total weight modulo 998,244,353. Constraints * 2 \leq N \leq 100,000 * 1 \leq M_1, M_2, M_3 \leq 100,000 * 1 \leq a_i, b_i, c_i, d_i, e_i, f_i \leq N * X, Y, and Z are simple, that is, they have no self-loops and no multiple edges. Input Input is given from Standard Input in the following format: N M_1 a_1 b_1 a_2 b_2 \vdots a_{M_1} b_{M_1} M_2 c_1 d_1 c_2 d_2 \vdots c_{M_2} d_{M_2} M_3 e_1 f_1 e_2 f_2 \vdots e_{M_3} f_{M_3} Output Print the maximum possible total weight of an independent set in W, modulo 998,244,353. Examples Input 2 1 1 2 1 1 2 1 1 2 Output 46494701 Input 3 3 1 3 1 2 3 2 2 2 1 2 3 1 2 1 Output 883188316 Input 100000 1 1 2 1 99999 100000 1 1 100000 Output 318525248 Submitted Solution: ``` from collections import defaultdict M = 998244353 B = 10**18 % M def mex(s): for i in range(max(s)+2): if i not in s: return i def calc_grandy(e): g = {} rev_g = defaultdict(set) rev_g[0] = set(range(1, N+1)) for i in sorted(e.keys(), reverse=True): m = mex({g.get(j,0) for j in e[i]}) if m: g[i] = m rev_g[m].add(i) rev_g[0].remove(i) return defaultdict(int, ((k,sum(map(lambda x:pow(B, x, M), v))%M) for k, v in rev_g.items())) def get_edge(): M = int(input()) e = defaultdict(set) for i in range(M): a, b = sorted(map(int, input().split())) e[a].add(b) return e def solve(N, edge): grandy = list(map(calc_grandy, edge)) ret = 0 for gx, x in grandy[0].items(): for gy, y in grandy[1].items(): gz = gx^gy z = grandy[2][gz] ret = (ret + x*y*z)%M return ret N = int(input()) edge = [get_edge() for i in range(3)] print(solve(N, edge)) ``` No
2,253
Provide a correct Python 3 solution for this coding contest problem. Requirements * All inputs are of non-negative integer value. * T_{\text{max}} = 10000 * 200 \leq |V| \leq 400 * 1.5 |V| \leq |E| \leq 2 |V| * 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) * 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) * The given graph has no self-loops / multiple edges and is guaranteed to be connected. * 0 \leq N_{\text{new}} \leq 1 * 1 \leq \mathrm{new\\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}). Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1. Therefore, the possible range of \mathrm{new\\_id}_{i} should be from 1 to T_{\text{last}}+1. * 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) * The order IDs \mathrm{new\\_{id}}_i are unique. Input Format Input is provided in the following form: |V| |E| u_{1} v_{1} d_{u_{1}, v_{1}} u_{2} v_{2} d_{u_{2}, v_{2}} : u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} T_{\text{max}} \mathrm{info}_{0} \mathrm{info}_{1} : \mathrm{info}_{T_{\text{max}}-1} * In the first line |V| denotes the number of vertices, while |E| denotes the number of edges. * The following |E| lines denote the edges of the graph. In particular, in the ith line u_{i} and v_{i} denote the edge connecting u_{i} and v_{i} and d_{u_{i}, v_{i}} the corresponding distance. * The following line denotes the number of steps T_{\text{max}}. In the following line, \mathrm{info}_t is information about the order from the customer that occurs at time t. \mathrm{info}_t is given in the form: N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} * N_{\text{new}} represents the number of new orders which appear at time t. * The next N_{\text{new}} lines give the newly generated order information. The i-th order information indicates that the order ID \mathrm{new\\_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. * Note: If N_{\text{new}}=0, there are no new lines. Output Format The Output expects T_{\text{max}} integers in the format specified below. \mathrm{command}_{0} \mathrm{command}_{1} : \mathrm{command}_{T_{\text{max}}-1} In particular, \mathrm{command}_{i} shall specify the movement of the delivery car by using one of the following two options: 1) `stay`, if the car shall not move: -1 2) `move w`, if the car shall be moved one step towards the neighboring vertex w w Note that in case of 2) w has to satisfy the following conditions: * w \in V * If the car is at vertex u: \left\\{ u, w \right\\} \in E . * If the car is on the edge \left\\{ u, v \right\\}, w must either satisfy u = w or v = w. * * * Examples Input Output Input 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 4 1 1 2 1 2 5 1 3 4 0 Output 2 -1 1 5 "Correct Solution: ``` import sys printn = lambda x: sys.stdout.write(x) inn = lambda : int(input()) inl = lambda: list(map(int, input().split())) inm = lambda: map(int, input().split()) DBG = True and False PROBL = 1 MAXVISIT = 400 def ddprint(x): if DBG: print(x) sys.stdout.flush() def trigger(): return (len(pendreq) > 0) def nxtnode(): global lastnd, workreq, nxtndary tgt = workreq[0] if len(workreq)>0 else 1 #ddprint("in nxtnode lastnd {} wr0 {} tgt {} ret {}".format(\ # lastnd, workreq[0] if len(workreq)>0 else -1, tgt, nxtndary[lastnd][tgt])) return nxtndary[lastnd][tgt] def atdepot(): return (lastnd==1 and distfromlast==0) def stay(): #print(-1, flush=True) print(-1) sys.stdout.flush() def readput(): n = inn() for i in range(n): inn() def readverd(): v = input().strip() if v != 'OK': exit() readput() def move(): global distfromlast, lastnd, workreq nxtnd = nxtnode() #print(nxtnd, flush=True) print(nxtnd) sys.stdout.flush() distfromlast += 1 if distfromlast == dist[lastnd][nxtnd]: distfromlast = 0 if len(workreq)>0 and nxtnd == workreq[0]: del workreq[0] lastnd = nxtnd def ufinit(n): global ufary, ufrank ufary = [i for i in range(n)] ufrank = [0] * n def ufroot(i): p = ufary[i] if p==i: return p q = ufroot(p) ufary[i] = q return q def ufmerge(i,j): global ufary, ufrank ri = ufroot(i) rj = ufroot(j) if ri==rj: return if ufrank[ri]>ufrank[rj]: ufary[rj] = ri else: ufary[ri] = rj if ufrank[ri]==ufrank[rj]: ufrank[rj] += 1 def setwork(): global pendreq, workreq vislist = set([x[1] for x in pendreq]) | set(workreq) vv = len(vislist) pendreq = [] if vv==1: workreq = list(vislist) return # Clark & Wright e = {} f = {} for x in vislist: e[x] = [] ufinit(V+1) #ddprint("e pre") #ddprint(e) for _ in range(vv-1): mx = -1 for i in e: for j in e: if ufroot(i)==ufroot(j): continue df = dist[i][1] + dist[1][j] - dist[i][j] if df>mx: mx = df mxi = i mxj = j i = mxi j = mxj e[i].append(j) e[j].append(i) ufmerge(i,j) if len(e[i])==2: f[i] = e[i] del e[i] if len(e[j])==2: f[j] = e[j] del e[j] # now len(e) is 2. find the first #ddprint("e {}".format(e)) #ddprint("f {}".format(f)) ek = list(e.keys()) p = ek[0] #q = ek[1] prev = p cur = e[p][0] workreq = [p,cur] for i in range(vv-2): p = cur cur = f[p][0] if cur==prev: cur = f[p][1] prev = p workreq.append(cur) #workreq.append(q) #ddprint("w {}".format(workreq)) def setwork1(): global pendreq, workreq # sort pend reqs by distance from depot/oldest oldest = pendreq[0][1] pendset = set([x[1] for x in pendreq]) sortq = [] for i in [0,1]: q = [] base = 1 if i==1 else oldest # 0:oldest 1:depot for x in pendset: q.append((dist[base][x], x)) q.sort(reverse=True) # shortest at tail sortq.append(q) # create visit node list - closest to depot/oldest vislist = [] while len(vislist) < MAXVISIT and \ (len(sortq[0])>0 or len(sortq[1])>0): for j in [0,1]: if len(sortq[j])==0: continue d,x = sortq[j].pop() if x in vislist or x in workreq: continue vislist.append(x) #ddprint("vislist") #ddprint(vislist) # create route prev = 1 while len(vislist)>0: mn = 9999 for i,x in enumerate(vislist): d = dist[prev][x] if d<mn: mn = d mni = i mnx = vislist[mni] workreq.append(mnx) del vislist[mni] prev = mnx # update pendreq pendreq = [x for x in pendreq if x[1] not in workreq] # # # # main start # # # # ddprint("start") prob = PROBL # int(sys.argv[1]) V, E = inm() es = [[] for i in range(V+1)] dist = [[9999]*(V+1) for i in range(V+1)] for i in range(V+1): dist[i][i] = 0 for i in range(E): a, b, c = inm() #a, b = a-1, b-1 es[a].append((b,c)) es[b].append((a,c)) dist[a][b] = dist[b][a] = c if DBG: for i,z in enumerate(es): print(str(i)+' ' + str(z)) # Warshal-Floyd for k in range(1,V+1): for i in range(1,V+1): for j in range(1,V+1): dist[i][j] = min(dist[i][j], \ dist[i][k]+dist[k][j]) ddprint("WS done") nxtndary = [[0]*(V+1) for i in range(V+1)] for i in range(1,V+1): for j in range(1,V+1): if j==i: continue found = False for z in es[i]: k = z[0] if dist[i][j] == z[1]+dist[k][j]: nxtndary[i][j] = k found = True break if not found: print("nxtndary ERR") exit() if prob==2: F = inl() tt = inn() pendreq = [] workreq = [] lastnd = 1 distfromlast = 0 for t in range(tt): #ddprint("t {} lastnd {} dist {}".format( \ # t,lastnd,distfromlast)) #ddprint(pendreq) #ddprint(workreq) if DBG and t>3400: exit() # read a new req if any n = inn() if n==1: new_id, dst = inm() #ddprint("new req id {} dst {}".format(new_id, dst)) pendreq.append((new_id, dst)) if prob==2: readput() # determine action if atdepot(): if trigger(): setwork() if len(workreq)>0: move() else: stay() else: stay() else: move() if prob==2: readverd() ```
2,254
Provide a correct Python 3 solution for this coding contest problem. Requirements * All inputs are of non-negative integer value. * T_{\text{max}} = 10000 * 200 \leq |V| \leq 400 * 1.5 |V| \leq |E| \leq 2 |V| * 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) * 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) * The given graph has no self-loops / multiple edges and is guaranteed to be connected. * 0 \leq N_{\text{new}} \leq 1 * 1 \leq \mathrm{new\\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}). Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1. Therefore, the possible range of \mathrm{new\\_id}_{i} should be from 1 to T_{\text{last}}+1. * 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) * The order IDs \mathrm{new\\_{id}}_i are unique. Input Format Input is provided in the following form: |V| |E| u_{1} v_{1} d_{u_{1}, v_{1}} u_{2} v_{2} d_{u_{2}, v_{2}} : u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} T_{\text{max}} \mathrm{info}_{0} \mathrm{info}_{1} : \mathrm{info}_{T_{\text{max}}-1} * In the first line |V| denotes the number of vertices, while |E| denotes the number of edges. * The following |E| lines denote the edges of the graph. In particular, in the ith line u_{i} and v_{i} denote the edge connecting u_{i} and v_{i} and d_{u_{i}, v_{i}} the corresponding distance. * The following line denotes the number of steps T_{\text{max}}. In the following line, \mathrm{info}_t is information about the order from the customer that occurs at time t. \mathrm{info}_t is given in the form: N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} * N_{\text{new}} represents the number of new orders which appear at time t. * The next N_{\text{new}} lines give the newly generated order information. The i-th order information indicates that the order ID \mathrm{new\\_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. * Note: If N_{\text{new}}=0, there are no new lines. Output Format The Output expects T_{\text{max}} integers in the format specified below. \mathrm{command}_{0} \mathrm{command}_{1} : \mathrm{command}_{T_{\text{max}}-1} In particular, \mathrm{command}_{i} shall specify the movement of the delivery car by using one of the following two options: 1) `stay`, if the car shall not move: -1 2) `move w`, if the car shall be moved one step towards the neighboring vertex w w Note that in case of 2) w has to satisfy the following conditions: * w \in V * If the car is at vertex u: \left\\{ u, w \right\\} \in E . * If the car is on the edge \left\\{ u, v \right\\}, w must either satisfy u = w or v = w. * * * Examples Input Output Input 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 4 1 1 2 1 2 5 1 3 4 0 Output 2 -1 1 5 "Correct Solution: ``` import sys from collections import deque from heapq import heapify, heappop, heappush from math import sqrt from itertools import permutations def root_search(order, cost, wf, V): #店舗から出るときの探索 type_num = len(order) hold = [] #holdで上位n個のみ保持し、最後に残ったものをpre_holdに移す heapify(hold) pre_hold = [] #一段階前の確定した道順 pre_hold.append((0, "0", set() | order, 0)) #cost, pass, left, id hold_size = 0 for i in range(type_num): for pre_cost, pre_pass, to_search, pre_id in pre_hold: for key in to_search: heappush(hold, (pre_cost + wf[pre_id][key] / sqrt(cost[key][1]), " ".join([pre_pass, str(key)]), to_search - {key}, key)) hold_size += 1 pre_hold = [] for _ in range(min(hold_size, 3)): pre_hold.append(heappop(hold)) hold = [] hold_size = 0 ans_cost, ans_pass, ans_search, ans_id = pre_hold[0] pass_list = [int(v) for v in ans_pass.split()] pass_list.append(0) return pass_list def decide_next_dst(cost, order, current_id, final_dist, store_hold, wf): c = store_hold * 1000 / wf[current_id][0] for key in order: if cost[key][1] * 10000 / wf[current_id][key] > c: return final_dist return 0 def solve(): inf = 10000000000 input = sys.stdin.readline #fin = open("sampleinput.txt", "r") #input = fin.readline V, E = map(int, input().split()) edge = dict() wf = dict() next_id = dict() #iからjに行くときにiの次に訪れる地点 for i in range(V): edge[i] = dict() wf[i] = dict() next_id[i] = dict() for j in range(V): edge[i][j] = (0 if i == j else inf) next_id[i][j] = j wf[i][j] = (0 if i == j else inf) for _ in range(E): u, v, d = map(int, input().split()) edge[u - 1][v - 1] = d edge[v - 1][u - 1] = d wf[u - 1][v - 1] = d wf[v - 1][u - 1] = d #全頂点最短経路と目的の頂点に向かうとき次に行くべき頂点の復元 for k in range(V): for i, j in permutations(range(V), 2): if wf[i][j] > wf[i][k] + wf[k][j]: wf[i][j] = wf[i][k] + wf[k][j] next_id[i][j] = next_id[i][k] T = int(input()) order = set() stuck_order = set() command = dict() for t in range(T): command[t] = -1 heading = 0 #次に向かう地点 dist_left = 0 #次に向かう地点までの残り距離 final_dist = 0 driver_hold = 0 root_pointer = 0 store_list = [0] * V root_list = [] cost = dict() for i in range(V): cost[i] = [0, 0] store_hold = 0 for t in range(T): N = int(input()) #注文の発生 if N == 1: new_id, dst = map(int, input().split()) stuck_order |= {dst - 1} store_hold += 1 store_list[dst - 1] += 1 if dist_left > 0: #移動中の場合そのまま移動を続ける command[t] = heading + 1 dist_left -= 1 else: if heading == 0: #店にいるときの処理 if store_hold == driver_hold == 0: continue else: order |= stuck_order driver_hold += store_hold for key in order: cost[key][1] += 0.5 * cost[key][0] + store_list[key] cost[key][0] += store_list[key] store_list[key] = 0 stuck_order = set() store_hold = 0 root_list = root_search(order, cost, wf, V) current_id = heading = final_dist = 0 root_pointer = 0 root_size = len(root_list) if heading in order: order.remove(heading) driver_hold -= cost[heading][0] cost[heading] = [0, 0] current_id = heading #現在地の更新 if current_id == final_dist: for j in range(root_pointer, root_size - 1): if cost[root_list[j + 1]][0] > 0: final_dist = root_list[j + 1] root_pointer = j + 1 if current_id > 0 and store_hold > 0: final_dist = decide_next_dst(cost, order, current_id, final_dist, store_hold, wf) break else: final_dist = 0 heading = next_id[current_id][final_dist] dist_left = edge[current_id][heading] - 1 command[t] = heading + 1 for i in range(T): print(command[i]) #out = open(str(V) + "-" + str(E) + "-out.txt", "w") # i in range(T): #print(command[i], file = out, end ="\n") #out.close return 0 if __name__ == "__main__": solve() ```
2,255
Provide a correct Python 3 solution for this coding contest problem. Requirements * All inputs are of non-negative integer value. * T_{\text{max}} = 10000 * 200 \leq |V| \leq 400 * 1.5 |V| \leq |E| \leq 2 |V| * 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) * 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) * The given graph has no self-loops / multiple edges and is guaranteed to be connected. * 0 \leq N_{\text{new}} \leq 1 * 1 \leq \mathrm{new\\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}). Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1. Therefore, the possible range of \mathrm{new\\_id}_{i} should be from 1 to T_{\text{last}}+1. * 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) * The order IDs \mathrm{new\\_{id}}_i are unique. Input Format Input is provided in the following form: |V| |E| u_{1} v_{1} d_{u_{1}, v_{1}} u_{2} v_{2} d_{u_{2}, v_{2}} : u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} T_{\text{max}} \mathrm{info}_{0} \mathrm{info}_{1} : \mathrm{info}_{T_{\text{max}}-1} * In the first line |V| denotes the number of vertices, while |E| denotes the number of edges. * The following |E| lines denote the edges of the graph. In particular, in the ith line u_{i} and v_{i} denote the edge connecting u_{i} and v_{i} and d_{u_{i}, v_{i}} the corresponding distance. * The following line denotes the number of steps T_{\text{max}}. In the following line, \mathrm{info}_t is information about the order from the customer that occurs at time t. \mathrm{info}_t is given in the form: N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} * N_{\text{new}} represents the number of new orders which appear at time t. * The next N_{\text{new}} lines give the newly generated order information. The i-th order information indicates that the order ID \mathrm{new\\_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. * Note: If N_{\text{new}}=0, there are no new lines. Output Format The Output expects T_{\text{max}} integers in the format specified below. \mathrm{command}_{0} \mathrm{command}_{1} : \mathrm{command}_{T_{\text{max}}-1} In particular, \mathrm{command}_{i} shall specify the movement of the delivery car by using one of the following two options: 1) `stay`, if the car shall not move: -1 2) `move w`, if the car shall be moved one step towards the neighboring vertex w w Note that in case of 2) w has to satisfy the following conditions: * w \in V * If the car is at vertex u: \left\\{ u, w \right\\} \in E . * If the car is on the edge \left\\{ u, v \right\\}, w must either satisfy u = w or v = w. * * * Examples Input Output Input 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 4 1 1 2 1 2 5 1 3 4 0 Output 2 -1 1 5 "Correct Solution: ``` """https://atcoder.jp/contests/hokudai-hitachi2019-1/tasks/hokudai_hitachi2019_1_a Time Limit: 30 sec / Memory Limit: 1024 MB # Score score := time_max**2 * n_deleverd - sum((deleverd_time[i] - ordered_time[i])**2 for i in deleverd) # Requirements - time_max = 10_000 - 200 <= n_vertices <= 400 - 1.5*n_vertices <= n_edges <= 2*n_vertices - 1 <= d <= ceil(4*sqrt(2*n_vertices)) <= 114 # Output - stay ==> -1 - move x ==> x """ import collections import sys STAY = -1 BASE_POS = 0 class Order: def __init__(self, order_id, ordered_time, destination): self.order_id = order_id self.ordered_time = ordered_time self.destination = destination def __str__(self): return 'Order' + str((self.order_id, self.ordered_time, self.destination)) class Graph: def __init__(self, n_vertices, n_edges): self.n_vertices = n_vertices self.n_edges = n_edges self.adjacency_list = [[] for i in range(n_vertices)] self.distances = [[float('inf')] * (n_vertices) for _ in range(n_vertices)] self.transits = [[j for j in range(n_vertices)] for i in range(n_vertices)] def add_edge(self, u, v, d): self.adjacency_list[u].append((v, d)) self.adjacency_list[v].append((u, d)) self.distances[u][v] = d self.distances[v][u] = d self.transits[u][v] = v self.transits[v][u] = u def floyd_warshall(self): for i in range(self.n_vertices): self.distances[i][i] = 0 self.transits[i][i] = i for k in range(self.n_vertices): for i in range(self.n_vertices): for j in range(self.n_vertices): if self.distances[i][j] > self.distances[i][k] + self.distances[k][j]: self.distances[i][j] = self.distances[i][k] + \ self.distances[k][j] self.transits[i][j] = self.transits[i][k] def get_orders(ordered_time): n_new_order = int(input()) # 0 <= n_new_order <= 1 orders = [] for _ in range(n_new_order): # 1 <= order_id <= time_max+1 # 2 <= destination <= n_vertices order_id, destination = map(int, input().split()) orders.append(Order(order_id, ordered_time, destination-1)) return orders def get_input(): n_vertices, n_edges = map(int, input().split()) graph = Graph(n_vertices, n_edges) for _ in range(n_edges): u, v, d = map(int, input().split()) graph.add_edge(u-1, v-1, d) graph.floyd_warshall() # Floyd–Warshall algorithm time_max = int(input()) orders = [] for time_index in range(time_max): new_orders = get_orders(time_index) for order in new_orders: orders.append(order) return graph, time_max, orders def solve(graph, time_max, orders): car_on_vertex = True car_pos = BASE_POS # edge_car_driving = (0, 0) next_pickup = 0 commands = [-1] * time_max cargo = [] t = 0 while t < time_max: # pick up cargos if car_on_vertex and car_pos == BASE_POS: while next_pickup < len(orders) and orders[next_pickup].ordered_time <= t: cargo.append(orders[next_pickup]) next_pickup += 1 if cargo: cargo.sort(key=lambda x: (t-x.ordered_time + graph.distances[car_pos][x.destination]) ** 2) print(','.join([str(c) for c in cargo]), file=sys.stderr) dist = cargo[0].destination if time_max <= t + graph.distances[car_pos][dist]: cargo = cargo[1:] # TODO: 高速化 continue transit_point = graph.transits[car_pos][dist] need_time_for_move = graph.distances[car_pos][transit_point] for i in range(need_time_for_move): commands[t + i] = transit_point t += need_time_for_move car_pos = transit_point cargo = cargo[1:] # TODO: 高速化 continue else: if car_on_vertex: if car_pos == BASE_POS: commands[t] = STAY else: transit_point = graph.transits[car_pos][BASE_POS] need_time_for_move = graph.distances[car_pos][transit_point] if time_max <= t + need_time_for_move: t = time_max break for i in range(need_time_for_move): commands[t + i] = transit_point t += need_time_for_move car_pos = transit_point continue else: pass commands[t] = STAY t += 1 return commands def simulate(graph, time_max, orders, commands): car_on_vertex = True car_pos = BASE_POS return 0 def main(): graph, time_max, orders = get_input() commands = solve(graph, time_max, orders) for command in commands: if command == STAY: print(STAY) else: print(command+1) if __name__ == "__main__": main() ```
2,256
Provide a correct Python 3 solution for this coding contest problem. Requirements * All inputs are of non-negative integer value. * T_{\text{max}} = 10000 * 200 \leq |V| \leq 400 * 1.5 |V| \leq |E| \leq 2 |V| * 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) * 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) * The given graph has no self-loops / multiple edges and is guaranteed to be connected. * 0 \leq N_{\text{new}} \leq 1 * 1 \leq \mathrm{new\\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}). Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1. Therefore, the possible range of \mathrm{new\\_id}_{i} should be from 1 to T_{\text{last}}+1. * 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) * The order IDs \mathrm{new\\_{id}}_i are unique. Input Format Input is provided in the following form: |V| |E| u_{1} v_{1} d_{u_{1}, v_{1}} u_{2} v_{2} d_{u_{2}, v_{2}} : u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} T_{\text{max}} \mathrm{info}_{0} \mathrm{info}_{1} : \mathrm{info}_{T_{\text{max}}-1} * In the first line |V| denotes the number of vertices, while |E| denotes the number of edges. * The following |E| lines denote the edges of the graph. In particular, in the ith line u_{i} and v_{i} denote the edge connecting u_{i} and v_{i} and d_{u_{i}, v_{i}} the corresponding distance. * The following line denotes the number of steps T_{\text{max}}. In the following line, \mathrm{info}_t is information about the order from the customer that occurs at time t. \mathrm{info}_t is given in the form: N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} * N_{\text{new}} represents the number of new orders which appear at time t. * The next N_{\text{new}} lines give the newly generated order information. The i-th order information indicates that the order ID \mathrm{new\\_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. * Note: If N_{\text{new}}=0, there are no new lines. Output Format The Output expects T_{\text{max}} integers in the format specified below. \mathrm{command}_{0} \mathrm{command}_{1} : \mathrm{command}_{T_{\text{max}}-1} In particular, \mathrm{command}_{i} shall specify the movement of the delivery car by using one of the following two options: 1) `stay`, if the car shall not move: -1 2) `move w`, if the car shall be moved one step towards the neighboring vertex w w Note that in case of 2) w has to satisfy the following conditions: * w \in V * If the car is at vertex u: \left\\{ u, w \right\\} \in E . * If the car is on the edge \left\\{ u, v \right\\}, w must either satisfy u = w or v = w. * * * Examples Input Output Input 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 4 1 1 2 1 2 5 1 3 4 0 Output 2 -1 1 5 "Correct Solution: ``` def empty(t_max): return [-1] * t_max def main(): nv, ne = map(int, input().split()) # graph graph_dict = dict() for i in range(nv): graph_dict[i + 1] = dict() for _ in range(ne): u, v, d = map(int, input().split()) graph_dict[u][v] = d graph_dict[v][u] = d # orders t_max = int(input()) order_list = [[] for _ in range(t_max)] for t in range(t_max): n_new = int(input()) for _ in range(n_new): new_id, dst = map(int, input().split()) order_list[t].append([new_id, dst]) res = empty(t_max) for r in res: print(r) if __name__ == "__main__": main() ```
2,257
Provide a correct Python 3 solution for this coding contest problem. Requirements * All inputs are of non-negative integer value. * T_{\text{max}} = 10000 * 200 \leq |V| \leq 400 * 1.5 |V| \leq |E| \leq 2 |V| * 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) * 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) * The given graph has no self-loops / multiple edges and is guaranteed to be connected. * 0 \leq N_{\text{new}} \leq 1 * 1 \leq \mathrm{new\\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}). Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1. Therefore, the possible range of \mathrm{new\\_id}_{i} should be from 1 to T_{\text{last}}+1. * 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) * The order IDs \mathrm{new\\_{id}}_i are unique. Input Format Input is provided in the following form: |V| |E| u_{1} v_{1} d_{u_{1}, v_{1}} u_{2} v_{2} d_{u_{2}, v_{2}} : u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} T_{\text{max}} \mathrm{info}_{0} \mathrm{info}_{1} : \mathrm{info}_{T_{\text{max}}-1} * In the first line |V| denotes the number of vertices, while |E| denotes the number of edges. * The following |E| lines denote the edges of the graph. In particular, in the ith line u_{i} and v_{i} denote the edge connecting u_{i} and v_{i} and d_{u_{i}, v_{i}} the corresponding distance. * The following line denotes the number of steps T_{\text{max}}. In the following line, \mathrm{info}_t is information about the order from the customer that occurs at time t. \mathrm{info}_t is given in the form: N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} * N_{\text{new}} represents the number of new orders which appear at time t. * The next N_{\text{new}} lines give the newly generated order information. The i-th order information indicates that the order ID \mathrm{new\\_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. * Note: If N_{\text{new}}=0, there are no new lines. Output Format The Output expects T_{\text{max}} integers in the format specified below. \mathrm{command}_{0} \mathrm{command}_{1} : \mathrm{command}_{T_{\text{max}}-1} In particular, \mathrm{command}_{i} shall specify the movement of the delivery car by using one of the following two options: 1) `stay`, if the car shall not move: -1 2) `move w`, if the car shall be moved one step towards the neighboring vertex w w Note that in case of 2) w has to satisfy the following conditions: * w \in V * If the car is at vertex u: \left\\{ u, w \right\\} \in E . * If the car is on the edge \left\\{ u, v \right\\}, w must either satisfy u = w or v = w. * * * Examples Input Output Input 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 4 1 1 2 1 2 5 1 3 4 0 Output 2 -1 1 5 "Correct Solution: ``` import copy ############################## # 指定した商品データを削除する処理 ############################## def deleteProduct(nowPos): global product for i in reversed(range(len(product))): #削除したので、ループ回数上限を再チェック # if i >= len(product): # break if nowPos == product[i][2]: del product[i] # product = [ x for x in product if product[x][2] != nowPos ] ############################## # 指定の場所までの距離を取得する処理 ############################## def getDistance(x, y): global route global temp_node temp_node.clear() temp_node = [[] for i in range(V)] # ([経路情報]、確定ノードフラグ、コストi、ノード変更元) NODE_ROUTE = 0 #経路情報 NODE_DEFINED = 1 #確定ノードフラグ NODE_COST = 2 #コスト NODE_CHG_FROM= 3 #どのノードに変更されたか distance = 100 for i in range(V): temp_node[i].append(copy.deepcopy(es[i]))#経路情報 temp_node[i].append(0) #確定ノードフラグ temp_node[i].append(-1) #コスト temp_node[i].append(0) #ノード変更元 #スタートノードのコストは0 temp_node[y][NODE_COST] = 0 while 1: #確定ノードを探索 for i in range(V): if temp_node[i][NODE_DEFINED] == 1 or temp_node[i][NODE_COST] < 0: continue else: break #全て確定ノードになったら完了 loop_end_flag = 1 for k in range(len(temp_node)): if temp_node[k][NODE_DEFINED] == 0: loop_end_flag = 0 if loop_end_flag == 1: break #ノードを確定させる temp_node[i][NODE_DEFINED] = 1 #行先のノード情報を更新する temp_node_to = 0 temp_node_cost = 0 for j in range(len(temp_node[i][NODE_ROUTE])): temp_node_to = temp_node[i][NODE_ROUTE][j][0] temp_node_cost = temp_node[i][NODE_COST] + temp_node[i][NODE_ROUTE][j][1] if temp_node[temp_node_to][NODE_COST] < 0: temp_node[temp_node_to][NODE_COST] = temp_node_cost temp_node[temp_node_to][NODE_CHG_FROM] = i elif temp_node[temp_node_to][NODE_COST] > temp_node_cost: temp_node[temp_node_to][NODE_COST] = temp_node_cost temp_node[temp_node_to][NODE_CHG_FROM] = i #探索結果から距離とルートを確定 cost=0 route_num = 0 i=x route.clear() distance=0 while 1: cost = cost + temp_node[i][NODE_COST] if y == i: distande = cost break moto_node = i i = temp_node[i][NODE_CHG_FROM]#次の行先を取得 route.append([i,0])#次の行先をルートに追加 #行先ルートのコストを取得する for j in range(len(temp_node[moto_node][0])):#持ってるルート内の if temp_node[moto_node][0][j][0] == route[route_num][0]:#行先を探索 route[route_num][1]=temp_node[moto_node][0][j][1] distance = distance + route[route_num][1] route_num = route_num + 1 return distance ############################## # 商品リストから目的地を決定する処理 ############################## route = [] def getTargetPos(now_pos): global product global pos global route #route = [None for i in range(200)] # pos = -1 # distance = 201 # for i in range(len(product)): # if distance > getDistance(now_pos,product[i][2]): # distance = getDistance(now_pos,product[i][1]) # pos = product[i][2] # for i in range(len(product)): # break #とりあえず上から順番に配達する pos = (product[0][2]) return pos ############################## # お店で商品を受け取る処理 ############################## product=[] def getProduct(currentTime): for i in range(len(order)): if time_step_befor <= order[i][0] <= time_step: #if order[i][0] == currentTime: product.append ( copy.deepcopy(order[i]) ) ############################## # 指定の場所まで車を進める処理 ############################## def gotoVertex(x,y): global nowPos global route global total_move_count #コマンド出力 i=0 while 1: if x==y: print (-1)#移動なし total_move_count = total_move_count + 1 # 指定時間オーバー if total_move_count >= T: return break if route[i][0] == y:#ルート先が終着点 for j in range(route[i][1]): print(route[i][0]+1) if len(product) != 0: deleteProduct(route[i][0]) # 通過した場合は荷物を配達する total_move_count = total_move_count + 1 # 指定時間オーバー if total_move_count >= T: return break else:#ルート途中 for j in range(route[i][1]): print (route[i][0]+1) if len(product) != 0: deleteProduct(route[i][0]) # 通過した場合は荷物を配達する total_move_count = total_move_count + 1 # 指定時間オーバー if total_move_count >= T: return i = i + 1 #nowPosを目的地点まで進める nowPos = y def initProduct(nowPos): global temp_node global product # プロダクト毎に、ダイクストラをかける #for i in range(len(product)): # product[i][3] = getDistance(nowPos, product[i][2]) getDistance(nowPos,V-2) for i in range(len(product)): for j in range(len(temp_node)): if product[i][2] == j:#プロダクトと一致するノードがあったら product[i][3] = temp_node[j][2] break # 出来上がったproductをソート product = sorted(product,key=lambda x:x[3]) # product = sorted(product,key=lambda x:x[3]) # recieve |V| and |E| V, E = map(int, input().split()) es = [[] for i in range(V)] for i in range(E): # recieve edges a, b, c = map(int, input().split()) a, b = a-1, b-1 es[a].append([b,c])#(行先、経路コスト) es[b].append([a,c])#(行先、経路コスト) T = int(input()) # recieve info order=[] temp_node = [[] for i in range(V)] # ([経路情報]、確定ノードフラグ、コストi、ノード変更元) total_move_count = 0 for i in range(T): Nnew = int(input()) for j in range(Nnew): new_id, dst = map(int, input().split()) order.append([i, new_id, (dst-1), 0])#(注文時間,注文ID,配達先,処理フラグ) # insert your code here to get more meaningful output # all stay nowPos = 0 # 現在地 targetPos = -1 time_step=0 time_step_befor=0 for i in range(T) : if total_move_count >= T: break #現在地がお店なら注文の商品を受け取る if nowPos == 0: getProduct(time_step) time_step_befor = time_step # if i == 0:#初回のみ if len(product) != 0: initProduct(nowPos) else: print (-1) time_step = time_step + 1 total_move_count = total_move_count + 1 if total_move_count >= T: break continue #商品が尽きたら終了 #if len(product) == 0: # while 1: # gotoVertex(nowPos, 0) # print (-1) # if total_move_count>=T: # break #一番近いところに向かう if len(product) != 0: targetPos = getTargetPos(nowPos); time_step = time_step + getDistance(nowPos,targetPos) gotoVertex(nowPos,targetPos) continue #持ってる商品情報を削除 # if nowPos == targetPos: # deleteProduct(nowPos) #次のところへ向かう 以下ループ #持っている商品が無くなったらお店へ向かう if len(product) == 0 and nowPos != 0: time_step = time_step + getDistance(nowPos,0) #getDistance(nowPos,0) gotoVertex(nowPos,0) continue # ```
2,258
Provide a correct Python 3 solution for this coding contest problem. Requirements * All inputs are of non-negative integer value. * T_{\text{max}} = 10000 * 200 \leq |V| \leq 400 * 1.5 |V| \leq |E| \leq 2 |V| * 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) * 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) * The given graph has no self-loops / multiple edges and is guaranteed to be connected. * 0 \leq N_{\text{new}} \leq 1 * 1 \leq \mathrm{new\\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}). Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1. Therefore, the possible range of \mathrm{new\\_id}_{i} should be from 1 to T_{\text{last}}+1. * 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) * The order IDs \mathrm{new\\_{id}}_i are unique. Input Format Input is provided in the following form: |V| |E| u_{1} v_{1} d_{u_{1}, v_{1}} u_{2} v_{2} d_{u_{2}, v_{2}} : u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} T_{\text{max}} \mathrm{info}_{0} \mathrm{info}_{1} : \mathrm{info}_{T_{\text{max}}-1} * In the first line |V| denotes the number of vertices, while |E| denotes the number of edges. * The following |E| lines denote the edges of the graph. In particular, in the ith line u_{i} and v_{i} denote the edge connecting u_{i} and v_{i} and d_{u_{i}, v_{i}} the corresponding distance. * The following line denotes the number of steps T_{\text{max}}. In the following line, \mathrm{info}_t is information about the order from the customer that occurs at time t. \mathrm{info}_t is given in the form: N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} * N_{\text{new}} represents the number of new orders which appear at time t. * The next N_{\text{new}} lines give the newly generated order information. The i-th order information indicates that the order ID \mathrm{new\\_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. * Note: If N_{\text{new}}=0, there are no new lines. Output Format The Output expects T_{\text{max}} integers in the format specified below. \mathrm{command}_{0} \mathrm{command}_{1} : \mathrm{command}_{T_{\text{max}}-1} In particular, \mathrm{command}_{i} shall specify the movement of the delivery car by using one of the following two options: 1) `stay`, if the car shall not move: -1 2) `move w`, if the car shall be moved one step towards the neighboring vertex w w Note that in case of 2) w has to satisfy the following conditions: * w \in V * If the car is at vertex u: \left\\{ u, w \right\\} \in E . * If the car is on the edge \left\\{ u, v \right\\}, w must either satisfy u = w or v = w. * * * Examples Input Output Input 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 4 1 1 2 1 2 5 1 3 4 0 Output 2 -1 1 5 "Correct Solution: ``` import sys from collections import deque from heapq import heapify, heappop, heappush def decide_next_dst(cost, order): d = 0 c = 0 for key in order: if cost[key][0] > c: d = key c = cost[key][0] return (d, c) def solve(): inf = 10000000000 input = sys.stdin.readline V, E = map(int, input().split()) edge = dict() wf = dict() next_id = dict() #iからjに行くときにiの次に訪れる地点 far = dict() for i in range(V): edge[i] = dict() wf[i] = dict() next_id[i] = dict() for j in range(V): edge[i][j] = (0 if i == j else inf) next_id[i][j] = j wf[i][j] = (0 if i == j else inf) for _ in range(E): u, v, d = map(int, input().split()) edge[u - 1][v - 1] = d edge[v - 1][u - 1] = d wf[u - 1][v - 1] = d wf[v - 1][u - 1] = d #全頂点最短経路と目的の頂点に向かうとき次に行くべき頂点の復元 for k in range(V): for i in range(V): for j in range(V): if wf[i][j] > wf[i][k] + wf[k][j]: wf[i][j] = wf[i][k] + wf[k][j] next_id[i][j] = next_id[i][k] T = int(input()) order = set() stuck_order = set() command = [None] * T heading = 0 #次に向かう地点 dist_left = 0 #次に向かう地点までの残り距離 final_dist = 0 stuck_cost = [[0, 0] for _ in range(V)] cost = [[0, 0] for _ in range(V)] driver_hold = 0 store_hold = 0 for t in range(T): N = int(input()) #注文の発生 if N == 1: new_id, dst = map(int, input().split()) stuck_order |= {dst - 1} stuck_cost[dst - 1][0] += (5000 - t) ** 2 stuck_cost[dst - 1][1] += 1 store_hold += 1 if dist_left > 0: #移動中の場合そのまま移動を続ける command[t] = heading + 1 dist_left -= 1 else: if heading == 0: #店にいるときの処理 if store_hold == driver_hold == 0: command[t] = -1 continue else: order |= stuck_order for key in order: cost[key][0] += stuck_cost[key][0] cost[key][1] += stuck_cost[key][1] driver_hold += stuck_cost[key][1] stuck_cost[key] = [0, 0] stuck_order = set() store_hold = 0 if heading in order and heading > 0: #顧客のいる場所で荷物を積み下ろすとき order -= {heading} driver_hold -= cost[heading][1] cost[heading] = [0, 0] current_id = heading #現在地の更新 if len(order) > 0: #まだ配達すべき荷物があるとき if current_id == final_dist: #目的地に到着したときは残りの荷物で先に運ぶべき荷物を選ぶ final_dist, max_hold = decide_next_dst(cost, order) if driver_hold < store_hold and current_id > 0: final_dist = 0 else: final_dist = 0 #荷物が無いので店に戻る heading = next_id[current_id][final_dist] dist_left = edge[current_id][heading] - 1 command[t] = heading + 1 for i in range(T): print(command[i]) return 0 if __name__ == "__main__": solve() ```
2,259
Provide a correct Python 3 solution for this coding contest problem. Requirements * All inputs are of non-negative integer value. * T_{\text{max}} = 10000 * 200 \leq |V| \leq 400 * 1.5 |V| \leq |E| \leq 2 |V| * 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) * 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) * The given graph has no self-loops / multiple edges and is guaranteed to be connected. * 0 \leq N_{\text{new}} \leq 1 * 1 \leq \mathrm{new\\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}). Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1. Therefore, the possible range of \mathrm{new\\_id}_{i} should be from 1 to T_{\text{last}}+1. * 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) * The order IDs \mathrm{new\\_{id}}_i are unique. Input Format Input is provided in the following form: |V| |E| u_{1} v_{1} d_{u_{1}, v_{1}} u_{2} v_{2} d_{u_{2}, v_{2}} : u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} T_{\text{max}} \mathrm{info}_{0} \mathrm{info}_{1} : \mathrm{info}_{T_{\text{max}}-1} * In the first line |V| denotes the number of vertices, while |E| denotes the number of edges. * The following |E| lines denote the edges of the graph. In particular, in the ith line u_{i} and v_{i} denote the edge connecting u_{i} and v_{i} and d_{u_{i}, v_{i}} the corresponding distance. * The following line denotes the number of steps T_{\text{max}}. In the following line, \mathrm{info}_t is information about the order from the customer that occurs at time t. \mathrm{info}_t is given in the form: N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} * N_{\text{new}} represents the number of new orders which appear at time t. * The next N_{\text{new}} lines give the newly generated order information. The i-th order information indicates that the order ID \mathrm{new\\_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. * Note: If N_{\text{new}}=0, there are no new lines. Output Format The Output expects T_{\text{max}} integers in the format specified below. \mathrm{command}_{0} \mathrm{command}_{1} : \mathrm{command}_{T_{\text{max}}-1} In particular, \mathrm{command}_{i} shall specify the movement of the delivery car by using one of the following two options: 1) `stay`, if the car shall not move: -1 2) `move w`, if the car shall be moved one step towards the neighboring vertex w w Note that in case of 2) w has to satisfy the following conditions: * w \in V * If the car is at vertex u: \left\\{ u, w \right\\} \in E . * If the car is on the edge \left\\{ u, v \right\\}, w must either satisfy u = w or v = w. * * * Examples Input Output Input 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 4 1 1 2 1 2 5 1 3 4 0 Output 2 -1 1 5 "Correct Solution: ``` import sys from collections import deque from heapq import heapify, heappop, heappush def decide_final_dst(cost, order, current_id, store_cost, wf, dmax): d = 0 c = (store_cost * (dmax / wf[current_id][0]) ** 2 if current_id > 0 else 0) * 0.1 for key in order: if cost[key] * (dmax / wf[current_id][key]) ** 2 > c: d = key c = cost[key] * (dmax / wf[current_id][key]) ** 2 return d def decide_next_dst(cost, order, current_id, pre_id, heading, edge, wf, passed, V, dmax): c = cost[heading] * (dmax / wf[current_id][heading]) ** 2 if heading in passed: for e in range(V): if e != current_id and e != pre_id and wf[current_id][e] < 10000000000: if cost[e] * (dmax / wf[current_id][e]) ** 2 > c: heading = e c = cost[e] * (dmax / wf[current_id][e]) ** 2 return def solve(): inf = 10000000000 input = sys.stdin.readline V, E = map(int, input().split()) edge = dict() wf = dict() next_id = dict() #iからjに行くときにiの次に訪れる地点 for i in range(V): edge[i] = dict() wf[i] = dict() next_id[i] = dict() for j in range(V): edge[i][j] = (0 if i == j else inf) next_id[i][j] = j wf[i][j] = (0 if i == j else inf) for _ in range(E): u, v, d = map(int, input().split()) edge[u - 1][v - 1] = d edge[v - 1][u - 1] = d wf[u - 1][v - 1] = d wf[v - 1][u - 1] = d #全頂点最短経路と目的の頂点に向かうとき次に行くべき頂点の復元 for k in range(V): for i in range(V): for j in range(V): if wf[i][j] > wf[i][k] + wf[k][j]: wf[i][j] = wf[i][k] + wf[k][j] next_id[i][j] = next_id[i][k] #グラフの直径を求める dmax = 0 for i in range(V): dmax = max(dmax, max(wf[i])) T = int(input()) order = set() stuck_order = set() command = [None] * T heading = 0 #次に向かう地点 pre_id = 0 #前の地点 current_id = 0 dist_left = 0 #次に向かう地点までの残り距離 final_dist = 0 stuck_cost = [0 for _ in range(V)] cost = [0 for _ in range(V)] driver_hold = 0 store_hold = 0 passed = set() for t in range(T): N = int(input()) #注文の発生 if N == 1: new_id, dst = map(int, input().split()) stuck_order |= {dst - 1} stuck_cost[dst - 1] += 1 store_hold += 1 if dist_left > 0: #移動中の場合そのまま移動を続ける command[t] = heading + 1 dist_left -= 1 else: if heading == 0: #店にいるときの処理 passed = set() if store_hold == driver_hold == 0: #運ぶべき荷物がなければ待機 command[t] = -1 continue else: order |= stuck_order for key in order: cost[key] += stuck_cost[key] driver_hold += stuck_cost[key] stuck_cost[key] = 0 stuck_order = set() store_hold = 0 if heading in order and heading > 0: #顧客のいる場所で荷物を積み下ろすとき order -= {heading} driver_hold -= cost[heading] cost[heading] = 0 pre_id = current_id #前の地点の更新 current_id = heading #現在地の更新 passed |= {heading} if len(order) > 0: #まだ配達すべき荷物があるとき if current_id == final_dist: #目的地に到着したときは残りの荷物で先に運ぶべき荷物を選ぶ final_dist = decide_final_dst(cost, order, current_id, store_hold, wf, dmax) else: final_dist = 0 #荷物が無いので店に戻る heading = next_id[current_id][final_dist] decide_next_dst(cost, order, current_id, pre_id, heading, edge, wf, passed, V, dmax) dist_left = edge[current_id][heading] - 1 command[t] = heading + 1 for i in range(T): print(command[i]) return 0 if __name__ == "__main__": solve() ```
2,260
Provide a correct Python 3 solution for this coding contest problem. Requirements * All inputs are of non-negative integer value. * T_{\text{max}} = 10000 * 200 \leq |V| \leq 400 * 1.5 |V| \leq |E| \leq 2 |V| * 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) * 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) * The given graph has no self-loops / multiple edges and is guaranteed to be connected. * 0 \leq N_{\text{new}} \leq 1 * 1 \leq \mathrm{new\\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}). Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1. Therefore, the possible range of \mathrm{new\\_id}_{i} should be from 1 to T_{\text{last}}+1. * 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) * The order IDs \mathrm{new\\_{id}}_i are unique. Input Format Input is provided in the following form: |V| |E| u_{1} v_{1} d_{u_{1}, v_{1}} u_{2} v_{2} d_{u_{2}, v_{2}} : u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} T_{\text{max}} \mathrm{info}_{0} \mathrm{info}_{1} : \mathrm{info}_{T_{\text{max}}-1} * In the first line |V| denotes the number of vertices, while |E| denotes the number of edges. * The following |E| lines denote the edges of the graph. In particular, in the ith line u_{i} and v_{i} denote the edge connecting u_{i} and v_{i} and d_{u_{i}, v_{i}} the corresponding distance. * The following line denotes the number of steps T_{\text{max}}. In the following line, \mathrm{info}_t is information about the order from the customer that occurs at time t. \mathrm{info}_t is given in the form: N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} * N_{\text{new}} represents the number of new orders which appear at time t. * The next N_{\text{new}} lines give the newly generated order information. The i-th order information indicates that the order ID \mathrm{new\\_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. * Note: If N_{\text{new}}=0, there are no new lines. Output Format The Output expects T_{\text{max}} integers in the format specified below. \mathrm{command}_{0} \mathrm{command}_{1} : \mathrm{command}_{T_{\text{max}}-1} In particular, \mathrm{command}_{i} shall specify the movement of the delivery car by using one of the following two options: 1) `stay`, if the car shall not move: -1 2) `move w`, if the car shall be moved one step towards the neighboring vertex w w Note that in case of 2) w has to satisfy the following conditions: * w \in V * If the car is at vertex u: \left\\{ u, w \right\\} \in E . * If the car is on the edge \left\\{ u, v \right\\}, w must either satisfy u = w or v = w. * * * Examples Input Output Input 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 4 1 1 2 1 2 5 1 3 4 0 Output 2 -1 1 5 "Correct Solution: ``` import sys from collections import deque from heapq import heapify, heappop, heappush def decide_next_dst(cost, order): d = 0 c = 0 for key in order: if cost[key][0] > c: d = key c = cost[key][0] return (d, c) def solve(): inf = 10000000000 input = sys.stdin.readline V, E = map(int, input().split()) edge = dict() wf = dict() next_id = dict() #iからjに行くときにiの次に訪れる地点 for i in range(V): edge[i] = dict() wf[i] = dict() next_id[i] = dict() for j in range(V): edge[i][j] = (0 if i == j else inf) next_id[i][j] = j wf[i][j] = (0 if i == j else inf) for _ in range(E): u, v, d = map(int, input().split()) edge[u - 1][v - 1] = d edge[v - 1][u - 1] = d wf[u - 1][v - 1] = d wf[v - 1][u - 1] = d #全頂点最短経路と目的の頂点に向かうとき次に行くべき頂点の復元 for k in range(V): for i in range(V): for j in range(V): if wf[i][j] > wf[i][k] + wf[k][j]: wf[i][j] = wf[i][k] + wf[k][j] next_id[i][j] = next_id[i][k] T = int(input()) order = set() stuck_order = set() command = [None] * T heading = 0 #次に向かう地点 dist_left = 0 #次に向かう地点までの残り距離 final_dist = 0 stuck_cost = [[0, 0] for _ in range(V)] cost = [[0, 0] for _ in range(V)] driver_hold = 0 store_hold = 0 for t in range(T): N = int(input()) #注文の発生 if N == 1: new_id, dst = map(int, input().split()) stuck_order |= {dst - 1} stuck_cost[dst - 1][0] += (10000 - t) ** 2 stuck_cost[dst - 1][1] += 1 store_hold += 1 if dist_left > 0: #移動中の場合そのまま移動を続ける command[t] = heading + 1 dist_left -= 1 else: if heading == 0: #店にいるときの処理 if store_hold == driver_hold == 0: command[t] = -1 continue else: order |= stuck_order for key in order: cost[key][0] += stuck_cost[key][0] cost[key][1] += stuck_cost[key][1] driver_hold += stuck_cost[key][1] stuck_cost[key] = [0, 0] stuck_order = set() store_hold = 0 if heading in order and heading > 0: #顧客のいる場所で荷物を積み下ろすとき order -= {heading} driver_hold -= cost[heading][1] cost[heading] = [0, 0] current_id = heading #現在地の更新 if len(order) > 0: #まだ配達すべき荷物があるとき if current_id == final_dist: #目的地に到着したときは残りの荷物で先に運ぶべき荷物を選ぶ final_dist, max_hold = decide_next_dst(cost, order) if driver_hold < store_hold and current_id > 0: final_dist = 0 else: final_dist = 0 #荷物が無いので店に戻る heading = next_id[current_id][final_dist] dist_left = edge[current_id][heading] - 1 command[t] = heading + 1 for i in range(T): print(command[i]) return 0 if __name__ == "__main__": solve() ```
2,261
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Requirements * All inputs are of non-negative integer value. * T_{\text{max}} = 10000 * 200 \leq |V| \leq 400 * 1.5 |V| \leq |E| \leq 2 |V| * 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) * 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) * The given graph has no self-loops / multiple edges and is guaranteed to be connected. * 0 \leq N_{\text{new}} \leq 1 * 1 \leq \mathrm{new\\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}). Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1. Therefore, the possible range of \mathrm{new\\_id}_{i} should be from 1 to T_{\text{last}}+1. * 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) * The order IDs \mathrm{new\\_{id}}_i are unique. Input Format Input is provided in the following form: |V| |E| u_{1} v_{1} d_{u_{1}, v_{1}} u_{2} v_{2} d_{u_{2}, v_{2}} : u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} T_{\text{max}} \mathrm{info}_{0} \mathrm{info}_{1} : \mathrm{info}_{T_{\text{max}}-1} * In the first line |V| denotes the number of vertices, while |E| denotes the number of edges. * The following |E| lines denote the edges of the graph. In particular, in the ith line u_{i} and v_{i} denote the edge connecting u_{i} and v_{i} and d_{u_{i}, v_{i}} the corresponding distance. * The following line denotes the number of steps T_{\text{max}}. In the following line, \mathrm{info}_t is information about the order from the customer that occurs at time t. \mathrm{info}_t is given in the form: N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} * N_{\text{new}} represents the number of new orders which appear at time t. * The next N_{\text{new}} lines give the newly generated order information. The i-th order information indicates that the order ID \mathrm{new\\_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. * Note: If N_{\text{new}}=0, there are no new lines. Output Format The Output expects T_{\text{max}} integers in the format specified below. \mathrm{command}_{0} \mathrm{command}_{1} : \mathrm{command}_{T_{\text{max}}-1} In particular, \mathrm{command}_{i} shall specify the movement of the delivery car by using one of the following two options: 1) `stay`, if the car shall not move: -1 2) `move w`, if the car shall be moved one step towards the neighboring vertex w w Note that in case of 2) w has to satisfy the following conditions: * w \in V * If the car is at vertex u: \left\\{ u, w \right\\} \in E . * If the car is on the edge \left\\{ u, v \right\\}, w must either satisfy u = w or v = w. * * * Examples Input Output Input 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 4 1 1 2 1 2 5 1 3 4 0 Output 2 -1 1 5 Submitted Solution: ``` import sys from collections import deque from heapq import heapify, heappop, heappush def decide_next_dst(cost, order, current_id, store_cost, wf): d = 0 c = (store_cost * (10000/wf[current_id][0]) if current_id > 0 else 0) * 0.1 for key in order: if cost[key][1] * (10000 / wf[current_id][key])> c: d = key c = cost[key][1] * (10000 / wf[current_id][key]) return d def solve(): inf = 10000000000 input = sys.stdin.readline #fin = open("sampleinput.txt", "r") #input = fin.readline V, E = map(int, input().split()) edge = dict() wf = dict() next_id = dict() #iからjに行くときにiの次に訪れる地点 for i in range(V): edge[i] = dict() wf[i] = dict() next_id[i] = dict() for j in range(V): edge[i][j] = (0 if i == j else inf) next_id[i][j] = j wf[i][j] = (0 if i == j else inf) for _ in range(E): u, v, d = map(int, input().split()) edge[u - 1][v - 1] = d edge[v - 1][u - 1] = d wf[u - 1][v - 1] = d wf[v - 1][u - 1] = d #全頂点最短経路と目的の頂点に向かうとき次に行くべき頂点の復元 for k in range(V): for i in range(V): for j in range(V): if wf[i][j] > wf[i][k] + wf[k][j]: wf[i][j] = wf[i][k] + wf[k][j] next_id[i][j] = next_id[i][k] T = int(input()) order = set() stuck_order = set() command = [None] * T heading = 0 #次に向かう地点 dist_left = 0 #次に向かう地点までの残り距離 final_dist = 0 stuck_cost = [0 for _ in range(V)] cost = [[0, 0] for _ in range(V)] driver_hold = 0 store_hold = 0 for t in range(T): N = int(input()) #注文の発生 if N == 1: new_id, dst = map(int, input().split()) stuck_order |= {dst - 1} stuck_cost[dst - 1] += 1 store_hold += 1 if dist_left > 0: #移動中の場合そのまま移動を続ける command[t] = heading + 1 dist_left -= 1 else: if heading == 0: #店にいるときの処理 if store_hold == driver_hold == 0: command[t] = -1 continue else: order |= stuck_order for key in order: cost[key][1] += cost[key][0] * 0.5 cost[key][0] += stuck_cost[key] cost[key][1] += stuck_cost[key] driver_hold += stuck_cost[key] stuck_cost[key] = 0 stuck_order = set() store_hold = 0 if heading in order and heading > 0: #顧客のいる場所で荷物を積み下ろすとき order -= {heading} driver_hold -= cost[heading][0] cost[heading] = [0, 0] current_id = heading #現在地の更新 if len(order) > 0: #まだ配達すべき荷物があるとき if current_id == final_dist: #目的地に到着したときは残りの荷物で先に運ぶべき荷物を選ぶ final_dist = decide_next_dst(cost, order, current_id, store_hold, wf) else: final_dist = 0 #荷物が無いので店に戻る heading = next_id[current_id][final_dist] dist_left = edge[current_id][heading] - 1 command[t] = heading + 1 for i in range(T): print(command[i]) #out = open(str(V) + "-" + str(E) + "-out.txt", "w") #for i in range(T): #print(command[i], file = out, end ="\n") #out.close return 0 if __name__ == "__main__": solve() ``` Yes
2,262
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Requirements * All inputs are of non-negative integer value. * T_{\text{max}} = 10000 * 200 \leq |V| \leq 400 * 1.5 |V| \leq |E| \leq 2 |V| * 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) * 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) * The given graph has no self-loops / multiple edges and is guaranteed to be connected. * 0 \leq N_{\text{new}} \leq 1 * 1 \leq \mathrm{new\\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}). Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1. Therefore, the possible range of \mathrm{new\\_id}_{i} should be from 1 to T_{\text{last}}+1. * 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) * The order IDs \mathrm{new\\_{id}}_i are unique. Input Format Input is provided in the following form: |V| |E| u_{1} v_{1} d_{u_{1}, v_{1}} u_{2} v_{2} d_{u_{2}, v_{2}} : u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} T_{\text{max}} \mathrm{info}_{0} \mathrm{info}_{1} : \mathrm{info}_{T_{\text{max}}-1} * In the first line |V| denotes the number of vertices, while |E| denotes the number of edges. * The following |E| lines denote the edges of the graph. In particular, in the ith line u_{i} and v_{i} denote the edge connecting u_{i} and v_{i} and d_{u_{i}, v_{i}} the corresponding distance. * The following line denotes the number of steps T_{\text{max}}. In the following line, \mathrm{info}_t is information about the order from the customer that occurs at time t. \mathrm{info}_t is given in the form: N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} * N_{\text{new}} represents the number of new orders which appear at time t. * The next N_{\text{new}} lines give the newly generated order information. The i-th order information indicates that the order ID \mathrm{new\\_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. * Note: If N_{\text{new}}=0, there are no new lines. Output Format The Output expects T_{\text{max}} integers in the format specified below. \mathrm{command}_{0} \mathrm{command}_{1} : \mathrm{command}_{T_{\text{max}}-1} In particular, \mathrm{command}_{i} shall specify the movement of the delivery car by using one of the following two options: 1) `stay`, if the car shall not move: -1 2) `move w`, if the car shall be moved one step towards the neighboring vertex w w Note that in case of 2) w has to satisfy the following conditions: * w \in V * If the car is at vertex u: \left\\{ u, w \right\\} \in E . * If the car is on the edge \left\\{ u, v \right\\}, w must either satisfy u = w or v = w. * * * Examples Input Output Input 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 4 1 1 2 1 2 5 1 3 4 0 Output 2 -1 1 5 Submitted Solution: ``` import copy ############################## # 指定した商品データを削除する処理 ############################## def deleteProduct(nowPos): global product for i in reversed(range(len(product))): #削除したので、ループ回数上限を再チェック # if i >= len(product): # break if nowPos == product[i][2]: del product[i] # product = [ x for x in product if product[x][2] != nowPos ] ############################## # 指定の場所までの距離を取得する処理 ############################## NODE_ROUTE = 0 # 経路情報 NODE_DEFINED = 1 # 確定ノードフラグ NODE_COST = 2 # コスト NODE_CHG_FROM = 3 # どのノードに変更されたか def getDistance(x, y): global route global temp_node temp_node.clear() temp_node = [[] for i in range(V)] # ([経路情報]、確定ノードフラグ、コストi、ノード変更元) distance = 100 for i in range(V): temp_node[i].append(copy.deepcopy(es[i]))#経路情報 temp_node[i].append(0) #確定ノードフラグ temp_node[i].append(-1) #コスト temp_node[i].append(0) #ノード変更元 #スタートノードのコストは0 temp_node[y][NODE_COST] = 0 while 1: #確定ノードを探索 for i in range(V): if temp_node[i][NODE_DEFINED] == 1 or temp_node[i][NODE_COST] < 0: continue else: break #全て確定ノードになったら完了 loop_end_flag = 1 for k in range(len(temp_node)): if temp_node[k][NODE_DEFINED] == 0: loop_end_flag = 0 if loop_end_flag == 1: break #ノードを確定させる temp_node[i][NODE_DEFINED] = 1 #行先のノード情報を更新する temp_node_to = 0 temp_node_cost = 0 for j in range(len(temp_node[i][NODE_ROUTE])): temp_node_to = temp_node[i][NODE_ROUTE][j][0] temp_node_cost = temp_node[i][NODE_COST] + temp_node[i][NODE_ROUTE][j][1] if temp_node[temp_node_to][NODE_COST] < 0: temp_node[temp_node_to][NODE_COST] = temp_node_cost temp_node[temp_node_to][NODE_CHG_FROM] = i elif temp_node[temp_node_to][NODE_COST] > temp_node_cost: temp_node[temp_node_to][NODE_COST] = temp_node_cost temp_node[temp_node_to][NODE_CHG_FROM] = i if temp_node_to == y: loop_end_flag = 1 #探索結果から距離とルートを確定 distance = decide_route(x,y) return distance ############################## # 探索結果から距離とルートを確定 ############################## def decide_route(x,y): cost=0 route_num = 0 i=x route.clear() distance=0 while 1: cost = cost + temp_node[i][NODE_COST] if y == i: distande = cost break moto_node = i i = temp_node[i][NODE_CHG_FROM]#次の行先を取得 route.append([i,0])#次の行先をルートに追加 #行先ルートのコストを取得する for j in range(len(temp_node[moto_node][0])):#持ってるルート内の if temp_node[moto_node][0][j][0] == route[route_num][0]:#行先を探索 route[route_num][1]=temp_node[moto_node][0][j][1] distance = distance + route[route_num][1] route_num = route_num + 1 return distance ############################## # 商品リストから目的地を決定する処理 ############################## route = [] def getTargetPos(now_pos): global product global pos global route global prePos #route = [None for i in range(200)] # pos = -1 # distance = 201 # for i in range(len(product)): # if distance > getDistance(now_pos,product[i][2]): # distance = getDistance(now_pos,product[i][1]) # pos = product[i][2] # for i in range(len(product)): # break #注文が滞ったら count = 0 for i in range(len(order)): #if order[i][0] == currentTime: if time_step_before < order[i][0] <= time_step: count = count + 1 #とりあえず上から順番に配達する if count > len(product)/2: pos = 0 else: pos = (product[-1][2]) return pos ############################## # お店で商品を受け取る処理 ############################## product=[] def getProduct(currentTime): for i in range(len(order)): #if order[i][0] == currentTime: if time_step_before < order[i][0] <= currentTime: product.append ( copy.deepcopy(order[i]) ) ############################## # 指定の場所まで車を進める処理 ############################## prePos=0 def gotoVertex(x,y): global nowPos global route global total_move_count #コマンド出力 prePos = nowPos nowPos = -1 print (route[0][0]+1) route[0][1] = route[0][1] - 1 if route[0][1] == 0: deleteProduct(route[0][0]) # 通過した場合は荷物を配達する nowPos = route[0][0] # nowPosを目的地点まで進める del route[0] ############################## # その場で待機するか、配達に行くか決定する ############################## def selectWait(): global wait_cost global go_cost global result_cost #次の注文までの待ち時間 for i in range(len(order)): if time_step < order[i][0]: wait_cost = order[i][0]-time_step break # 行先までのコスト if len(product) <= 2:#二週目以降は計算しない go_cost = getDistance(nowPos,product[0][2]) if wait_cost - go_cost > 0: result_cost = wait_cost - go_cost else: result_cost = go_cost - wait_cost #コスト比較 # if wait_cost < go_cost: if len(product) > 50: return 0 elif wait_cost <= 10: #elif wait_cost < go_cost*2: return 1 else: return 0 def initProduct(nowPos): global temp_node global product # プロダクト毎に、ダイクストラをかける # for i in range(len(product)): # #product[i][3] = getDistance(nowPos, product[i][2]) # product[i][3] = decide_route(nowPos, product[i][2]) #product[0][3] = getDistance(nowPos,product[0][2]) #for i in range(len(product)-1): # product[i][3] = getDistance(product[i][2],product[i+1][2]) #product = sorted(product,key=lambda x:x[3]) getDistance(nowPos,product[0][2]) for i in range(len(product)): for j in range(len(temp_node)): if product[i][2] == j:#プロダクトと一致するノードがあったら product[i][3] = temp_node[j][2] # 出来上がったproductをソート product = sorted(product,key=lambda x:x[3]) #product.reverse() # product = sorted(product,key=lambda x:x[3]) # recieve |V| and |E| V, E = map(int, input().split()) es = [[] for i in range(V)] for i in range(E): # recieve edges a, b, c = map(int, input().split()) a, b = a-1, b-1 es[a].append([b,c])#(行先、経路コスト) es[b].append([a,c])#(行先、経路コスト) T = int(input()) # recieve info order=[] temp_node = [[] for i in range(V)] # ([経路情報]、確定ノードフラグ、コストi、ノード変更元) total_move_count = 0 for i in range(T): Nnew = int(input()) for j in range(Nnew): new_id, dst = map(int, input().split()) order.append([i, new_id, (dst-1), 0])#(注文時間,注文ID,配達先,処理フラグ) # insert your code here to get more meaningful output # all stay nowPos = 0 # 現在地 targetPos = -1 time_step=0 time_step_before=0 for i in range(T) : if time_step >= T: break #現在地がお店なら注文の商品を受け取る if nowPos == 0: getProduct(time_step) time_step_before = time_step # 以前訪れた時間 # if i == 0:#初回のみ if len(product) != 0: if selectWait() == 1: print(-1) time_step = time_step + 1 total_move_count = total_move_count + 1 if time_step >= T: break continue else: time_step = time_step initProduct(nowPos) else: print (-1) time_step = time_step + 1 total_move_count = total_move_count + 1 if time_step >= T: break continue #商品が尽きたら終了 #if len(product) == 0: # while 1: # gotoVertex(nowPos, 0) # print (-1) # if total_move_count>=T: # break #一番近いところに向かう if len(product) != 0: targetPos = getTargetPos(nowPos); if len(route) == 0: getDistance(nowPos,targetPos) gotoVertex(nowPos,targetPos) time_step = time_step + 1 continue #持ってる商品情報を削除 # if nowPos == targetPos: # deleteProduct(nowPos) #次のところへ向かう 以下ループ #持っている商品が無くなったらお店へ向かう if len(product) == 0 and nowPos != 0: if len(route) == 0: getDistance(nowPos,0) gotoVertex(nowPos,0) time_step = time_step + 1 continue # vardict = input() # if vardict == 'NG': # sys.exit() # the number of orders that have been delivered at time t. # Nachive = int(input()) # for j in range(Nachive): # achive_id = int(input()) ``` Yes
2,263
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Requirements * All inputs are of non-negative integer value. * T_{\text{max}} = 10000 * 200 \leq |V| \leq 400 * 1.5 |V| \leq |E| \leq 2 |V| * 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) * 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) * The given graph has no self-loops / multiple edges and is guaranteed to be connected. * 0 \leq N_{\text{new}} \leq 1 * 1 \leq \mathrm{new\\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}). Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1. Therefore, the possible range of \mathrm{new\\_id}_{i} should be from 1 to T_{\text{last}}+1. * 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) * The order IDs \mathrm{new\\_{id}}_i are unique. Input Format Input is provided in the following form: |V| |E| u_{1} v_{1} d_{u_{1}, v_{1}} u_{2} v_{2} d_{u_{2}, v_{2}} : u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} T_{\text{max}} \mathrm{info}_{0} \mathrm{info}_{1} : \mathrm{info}_{T_{\text{max}}-1} * In the first line |V| denotes the number of vertices, while |E| denotes the number of edges. * The following |E| lines denote the edges of the graph. In particular, in the ith line u_{i} and v_{i} denote the edge connecting u_{i} and v_{i} and d_{u_{i}, v_{i}} the corresponding distance. * The following line denotes the number of steps T_{\text{max}}. In the following line, \mathrm{info}_t is information about the order from the customer that occurs at time t. \mathrm{info}_t is given in the form: N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} * N_{\text{new}} represents the number of new orders which appear at time t. * The next N_{\text{new}} lines give the newly generated order information. The i-th order information indicates that the order ID \mathrm{new\\_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. * Note: If N_{\text{new}}=0, there are no new lines. Output Format The Output expects T_{\text{max}} integers in the format specified below. \mathrm{command}_{0} \mathrm{command}_{1} : \mathrm{command}_{T_{\text{max}}-1} In particular, \mathrm{command}_{i} shall specify the movement of the delivery car by using one of the following two options: 1) `stay`, if the car shall not move: -1 2) `move w`, if the car shall be moved one step towards the neighboring vertex w w Note that in case of 2) w has to satisfy the following conditions: * w \in V * If the car is at vertex u: \left\\{ u, w \right\\} \in E . * If the car is on the edge \left\\{ u, v \right\\}, w must either satisfy u = w or v = w. * * * Examples Input Output Input 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 4 1 1 2 1 2 5 1 3 4 0 Output 2 -1 1 5 Submitted Solution: ``` import sys from collections import deque from heapq import heapify, heappop, heappush def decide_final_dst(cost, order, current_id, store_cost, wf, dav): d = 0 c = (store_cost * (10000 / wf[current_id][0]) if current_id > 0 else 0) * (1 / dav) for key in order: if cost[key] * (10000 / wf[current_id][key]) > c: d = key c = cost[key] * (10000 / wf[current_id][key]) return d def decide_next_dst(cost, order, current_id, pre_id, heading, edge, wf, passed, V): c = cost[heading] * (10000 / wf[current_id][heading]) if heading in passed or c == 0: for e in range(V): if e != current_id and wf[current_id][e] < 10000000000: if cost[e] * (10000 / wf[current_id][e]) > c: heading = e c = cost[e]* (10000 / wf[current_id][e]) return def solve(): inf = 10000000000 input = sys.stdin.readline V, E = map(int, input().split()) edge = dict() wf = dict() next_id = dict() #iからjに行くときにiの次に訪れる地点 for i in range(V): edge[i] = dict() wf[i] = dict() next_id[i] = dict() for j in range(V): edge[i][j] = (0 if i == j else inf) next_id[i][j] = j wf[i][j] = (0 if i == j else inf) for _ in range(E): u, v, d = map(int, input().split()) edge[u - 1][v - 1] = d edge[v - 1][u - 1] = d wf[u - 1][v - 1] = d wf[v - 1][u - 1] = d #全頂点最短経路と目的の頂点に向かうとき次に行くべき頂点の復元 for k in range(V): for i in range(V): for j in range(V): if wf[i][j] > wf[i][k] + wf[k][j]: wf[i][j] = wf[i][k] + wf[k][j] next_id[i][j] = next_id[i][k] #店からの平均距離 daverage = sum(wf[0]) / (V - 1) T = int(input()) order = set() stuck_order = set() command = [None] * T heading = 0 #次に向かう地点 pre_id = 0 #前の地点 current_id = 0 dist_left = 0 #次に向かう地点までの残り距離 final_dist = 0 stuck_cost = [0 for _ in range(V)] cost = [0 for _ in range(V)] driver_hold = 0 store_hold = 0 passed = set() for t in range(T): N = int(input()) #注文の発生 if N == 1: new_id, dst = map(int, input().split()) stuck_order |= {dst - 1} stuck_cost[dst - 1] += 1 store_hold += 1 if dist_left > 0: #移動中の場合そのまま移動を続ける command[t] = heading + 1 dist_left -= 1 else: if heading == 0: #店にいるときの処理 passed = set() if store_hold == driver_hold == 0: #運ぶべき荷物がなければ待機 command[t] = -1 continue else: order |= stuck_order for key in order: cost[key] += stuck_cost[key] driver_hold += stuck_cost[key] stuck_cost[key] = 0 stuck_order = set() store_hold = 0 if heading in order and heading > 0: #顧客のいる場所で荷物を積み下ろすとき order -= {heading} driver_hold -= cost[heading] cost[heading] = 0 pre_id = current_id #前の地点の更新 current_id = heading #現在地の更新 passed |= {heading} if len(order) > 0: #まだ配達すべき荷物があるとき if current_id == final_dist: #目的地に到着したときは残りの荷物で先に運ぶべき荷物を選ぶ final_dist = decide_final_dst(cost, order, current_id, store_hold, wf, daverage) else: final_dist = 0 #荷物が無いので店に戻る heading = next_id[current_id][final_dist] decide_next_dst(cost, order, current_id, pre_id, heading, edge, wf, passed, V) dist_left = edge[current_id][heading] - 1 command[t] = heading + 1 for i in range(T): print(command[i]) return 0 if __name__ == "__main__": solve() ``` Yes
2,264
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Requirements * All inputs are of non-negative integer value. * T_{\text{max}} = 10000 * 200 \leq |V| \leq 400 * 1.5 |V| \leq |E| \leq 2 |V| * 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) * 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) * The given graph has no self-loops / multiple edges and is guaranteed to be connected. * 0 \leq N_{\text{new}} \leq 1 * 1 \leq \mathrm{new\\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}). Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1. Therefore, the possible range of \mathrm{new\\_id}_{i} should be from 1 to T_{\text{last}}+1. * 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) * The order IDs \mathrm{new\\_{id}}_i are unique. Input Format Input is provided in the following form: |V| |E| u_{1} v_{1} d_{u_{1}, v_{1}} u_{2} v_{2} d_{u_{2}, v_{2}} : u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} T_{\text{max}} \mathrm{info}_{0} \mathrm{info}_{1} : \mathrm{info}_{T_{\text{max}}-1} * In the first line |V| denotes the number of vertices, while |E| denotes the number of edges. * The following |E| lines denote the edges of the graph. In particular, in the ith line u_{i} and v_{i} denote the edge connecting u_{i} and v_{i} and d_{u_{i}, v_{i}} the corresponding distance. * The following line denotes the number of steps T_{\text{max}}. In the following line, \mathrm{info}_t is information about the order from the customer that occurs at time t. \mathrm{info}_t is given in the form: N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} * N_{\text{new}} represents the number of new orders which appear at time t. * The next N_{\text{new}} lines give the newly generated order information. The i-th order information indicates that the order ID \mathrm{new\\_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. * Note: If N_{\text{new}}=0, there are no new lines. Output Format The Output expects T_{\text{max}} integers in the format specified below. \mathrm{command}_{0} \mathrm{command}_{1} : \mathrm{command}_{T_{\text{max}}-1} In particular, \mathrm{command}_{i} shall specify the movement of the delivery car by using one of the following two options: 1) `stay`, if the car shall not move: -1 2) `move w`, if the car shall be moved one step towards the neighboring vertex w w Note that in case of 2) w has to satisfy the following conditions: * w \in V * If the car is at vertex u: \left\\{ u, w \right\\} \in E . * If the car is on the edge \left\\{ u, v \right\\}, w must either satisfy u = w or v = w. * * * Examples Input Output Input 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 4 1 1 2 1 2 5 1 3 4 0 Output 2 -1 1 5 Submitted Solution: ``` #!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): def dijkstra(s): d = [float("inf")]*n pre = [None]*n d[s] = 0 q = [(0,s)] while q: dx,x = heappop(q) for y,w in v[x]: nd = dx+w if nd < d[y]: d[y] = nd pre[y] = x heappush(q,(nd,y)) path = [[i] for i in range(n)] for i in range(n): while path[i][-1] != s: path[i].append(pre[path[i][-1]]) path[i] = path[i][::-1][1:] return d,path n,m = LI() v = [[] for i in range(n)] for i in range(m): a,b,d = LI() a -= 1 b -= 1 v[a].append((b,d)) v[b].append((a,d)) d = [None]*n path = [None]*n for s in range(n): d[s],path[s] = dijkstra(s) t = I() q = deque() for i in range(t): N = I() if N == 0: continue a,y = LI() y -= 1 q.append((i,y)) x = 0 dx = 0 dy = 0 query = deque() dst = 0 go = [] f = 0 time = [None]*n fl = [0]*n for i in range(t): while go and not fl[go[0]]: go.pop(0) while q and q[0][0] == i: k,l = q.popleft() query.append(l) if time[l] == None: time[l] = k if not f: if x == 0: if not query: print(-1) else: f = 1 while query: dst = query.popleft() go.append(dst) fl[dst] = 1 go = list(set(go)) go.sort(key = lambda a:time[a]) time = [None]*n dst = go.pop(0) p = [i for i in path[x][dst]] y = p.pop(0) print(y+1) dx = 1 dy = d[x][y] if dx == dy: fl[y] = 0 x = y dx = 0 if x == dst: if go: dst = go.pop(0) p = [i for i in path[x][dst]] y = p.pop(0) dy = d[x][y] else: f = 0 dst = 0 p = [i for i in path[x][dst]] y = p.pop(0) dy = d[x][y] else: y = p.pop(0) dy = d[x][y] else: print(y+1) dx += 1 if dx == dy: fl[y] = 0 x = y dx = 0 if p: y = p.pop(0) dy = d[x][y] else: print(y+1) dx += 1 if dx == dy: fl[y] = 0 x = y dx = 0 if x == dst: if go: dst = go.pop(0) p = [i for i in path[x][dst]] y = p.pop(0) dy = d[x][y] else: f = 0 dst = 0 p = [i for i in path[x][dst]] y = p.pop(0) dy = d[x][y] else: y = p.pop(0) dy = d[x][y] return #Solve if __name__ == "__main__": solve() ``` Yes
2,265
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Requirements * All inputs are of non-negative integer value. * T_{\text{max}} = 10000 * 200 \leq |V| \leq 400 * 1.5 |V| \leq |E| \leq 2 |V| * 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) * 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) * The given graph has no self-loops / multiple edges and is guaranteed to be connected. * 0 \leq N_{\text{new}} \leq 1 * 1 \leq \mathrm{new\\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}). Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1. Therefore, the possible range of \mathrm{new\\_id}_{i} should be from 1 to T_{\text{last}}+1. * 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) * The order IDs \mathrm{new\\_{id}}_i are unique. Input Format Input is provided in the following form: |V| |E| u_{1} v_{1} d_{u_{1}, v_{1}} u_{2} v_{2} d_{u_{2}, v_{2}} : u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} T_{\text{max}} \mathrm{info}_{0} \mathrm{info}_{1} : \mathrm{info}_{T_{\text{max}}-1} * In the first line |V| denotes the number of vertices, while |E| denotes the number of edges. * The following |E| lines denote the edges of the graph. In particular, in the ith line u_{i} and v_{i} denote the edge connecting u_{i} and v_{i} and d_{u_{i}, v_{i}} the corresponding distance. * The following line denotes the number of steps T_{\text{max}}. In the following line, \mathrm{info}_t is information about the order from the customer that occurs at time t. \mathrm{info}_t is given in the form: N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} * N_{\text{new}} represents the number of new orders which appear at time t. * The next N_{\text{new}} lines give the newly generated order information. The i-th order information indicates that the order ID \mathrm{new\\_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. * Note: If N_{\text{new}}=0, there are no new lines. Output Format The Output expects T_{\text{max}} integers in the format specified below. \mathrm{command}_{0} \mathrm{command}_{1} : \mathrm{command}_{T_{\text{max}}-1} In particular, \mathrm{command}_{i} shall specify the movement of the delivery car by using one of the following two options: 1) `stay`, if the car shall not move: -1 2) `move w`, if the car shall be moved one step towards the neighboring vertex w w Note that in case of 2) w has to satisfy the following conditions: * w \in V * If the car is at vertex u: \left\\{ u, w \right\\} \in E . * If the car is on the edge \left\\{ u, v \right\\}, w must either satisfy u = w or v = w. * * * Examples Input Output Input 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 4 1 1 2 1 2 5 1 3 4 0 Output 2 -1 1 5 Submitted Solution: ``` import sys import copy # recieve |V| and |E| V, E = map(int, input().split()) es = [[] for i in range(V)] for i in range(E): # recieve edges #es[point][edgenumber]: 頂点pointにedgenumber本の辺がある #値はつながっている頂点とその距離 a, b, c = map(int, input().split()) a, b = a-1, b-1 es[a].append((b,c)) es[b].append((a,c)) #頂点iから頂点j(0 <= i <= V-1, 0 <= j <= V-1)の最短時間をリストアップする #shortest_time[i][j]: 頂点iから頂点jまでの最短時間が記録されている shortest_time = [[sys.maxsize for j in range(V)] for i in range(V)] #頂点iから頂点j(0 <= i <= V-1, 0 <= j <= V-1)の最短経路をリストアップする #shortest_route[i][j]: 頂点iから頂点jまでの経路のリストが取得できる shortest_route = [[[] for j in range(V)] for i in range(V)] #最短時間と最短経路をリストアップする関数を作る def make_shortest_time_and_route(current, point, t, ic): #既に追加されている時間以上であればバックする if shortest_time[current][point] < t: return #行き先が自分の頂点であるときの距離は0にする if current == point: shortest_time[current][point] = 0 for edge_tuple in es[point]: if shortest_time[current][edge_tuple[0]] > t+edge_tuple[1]: #既に追加されている時間よりも小さかったら代入する shortest_time[current][edge_tuple[0]] = t+edge_tuple[1] #途中経路を記録していく ic.append(edge_tuple[0]) #最短時間でいける経路を記録していく #新しく最短経路が見つかれば上書きされる shortest_route[current][edge_tuple[0]] = copy.copy(ic) #再帰呼び出し make_shortest_time_and_route(current, edge_tuple[0], t+edge_tuple[1], ic) #新しい経路のために古いものを削除しておく del ic[-1] for i in range(V): interchange = [] make_shortest_time_and_route(i, i, 0, interchange) T = int(input()) # recieve info #受け取った情報をリストアップしていく #oder_time: key: 注文id, value: 注文時間t oder_time = {} #oder_list[t][i]: 時間tに発生したi番目の注文情報 oder_list = [[] for i in range(T)] for i in range(T): Nnew = int(input()) for j in range(Nnew): new_id, dst = map(int, input().split()) oder_time[new_id] = i oder_list[i].append((new_id, dst-1)) #配達に向かう目的地をリストに入れる def get_destination(d, l): for i in range(V): if l[i]: d.append(i) #時間s+1からtまでに注文された荷物を受け取る関数 def get_luggage(l, s, t): for i in range(s+1, t+1): for oder in oder_list[i]: l[oder[1]].append(oder[0]) return t #配達場所までの最短経路になるようにソートする関数 def get_route(start, start_index, d): if start_index >= len(d)-1: return min = sys.maxsize v = -1 for i in range(start_index, len(d)): if min > shortest_time[start][d[i]]: min = shortest_time[start][d[i]] v = i w = d[start_index] d[start_index] = d[v] d[v] = w get_route(d[start_index], start_index+1, d) LEVEL = 4 #読みきる深さ #最適解を探索する #t: 時間, level: 読んでいる深さ, vehicle: 車の位置, score: 得点, shipping: 最後にお店に立ち寄った時間, dst_list: 配達に向かう順番, luggage: 荷物 def search(t, level, vehicle, score, shipping, dst_list, luggage): #t >= Tmax のときscoreを返す if t >= T: return score #levelが読みきる深さになったとき if level >= LEVEL: return score luggage_copy = copy.deepcopy(luggage) dst_list_copy = copy.copy(dst_list) #車がお店にいるとき if vehicle == 0: #時間tまでに受けた注文idを受け取る shipping = get_luggage(luggage_copy, shipping, t) #配達場所(複数の目的地)までの最短経路を計算する #もしdst_listが空ではなかったら空にする if dst_list_copy != None: dst_list_copy.clear() #配達に向かう場所を記憶する get_destination(dst_list_copy, luggage_copy) #dst_listが最短経路でソートされる get_route(0, 0, dst_list_copy) #comp = search(配達に行く場合) max_score = sys.maxsize if dst_list_copy: max_score = search(t+shortest_time[vehicle][dst_list_copy[0]], level+1, dst_list_copy[0], score, shipping, dst_list_copy, luggage_copy) #comp = search(店にとどまる場合) comp = search(t+1, level+1, vehicle, score, shipping, dst_list_copy, luggage_copy) #comp > max のとき now_score = comp if comp > max_score: max_score = comp #return max return max_score #車が今積んでいるすべての荷物を配達完了したとき elif dst_list_copy.index(vehicle) == len(dst_list_copy) - 1: for i in luggage_copy[vehicle]: #得点計算 waitingTime = t - oder_time[i] score += T*T - waitingTime*waitingTime luggage_copy[vehicle].clear() #return search(店に戻る, 計算した得点を引数に渡す) return search(t+shortest_time[vehicle][0], level+1, 0, score, shipping, dst_list_copy, luggage_copy) #車が途中の配達まで完了したとき else: for i in luggage_copy[vehicle]: #得点計算 waitingTime = t - oder_time[i] score += T*T - waitingTime*waitingTime luggage_copy[vehicle].clear() #comp = search(次の配達場所に向かう, 計算した得点を引数に渡す) max_score = search(t+shortest_time[vehicle][dst_list_copy[dst_list_copy.index(vehicle)+1]], level+1, dst_list_copy[dst_list_copy.index(vehicle)+1], score, shipping, dst_list_copy, luggage_copy) #comp = search(店に戻る, 計算した得点を引数に渡す) comp = search(t+shortest_time[vehicle][0], level+1, 0, score, shipping, dst_list_copy, luggage_copy) #comp > max のとき max = comp if comp > max_score: max_score = comp #return max return max_score #車に積んである荷物を記録していく #key: 目的地, value: 注文idのリスト real_l = {key: [] for key in range(V)} #車の目的地を記録していく real_d = [] #index index = 0 #次の行動 next_move = -1 #次の目的地 next_dst = 0 #車の現在地 real_v = 0 #最後に店に訪れた時間 shipping_time = -1 #次の頂点につくまでのカウンター count = 0 # insert your code here to get more meaningful output # all stay #with open('result.out', 'w') as f: for i in range(T) : #次の目的地と車の現在地が等しいとき if real_v == next_dst: count = 0 #車が店にいるとき if real_v == 0: index = 0 #荷物を受け取る関数 shipping_time = get_luggage(real_l, shipping_time, i) #新しく目的地のリストを作る if real_d != None: real_d.clear() get_destination(real_d, real_l) #複数の目的地のルートを受け取る関数 get_route(0, 0, real_d) #とどまる場合 max_score = search(i+1, 0, real_v, 0, shipping_time, real_d, real_l) next_dst = 0 next_move = -2 #いく場合 comp = -1 if real_d: comp = search(i+shortest_time[real_v][real_d[index]], 0, real_d[index], 0, shipping_time, real_d, real_l) if comp > max_score: next_dst = real_d[index] next_move = shortest_route[real_v][next_dst][0] index += 1 #車が今ある荷物をすべて配達したとき elif index >= len(real_d) - 1: real_d.clear() real_l[real_v].clear() next_dst = 0 next_move = shortest_route[real_v][next_dst][0] #車が途中の配達場所まで配達完了したとき else: real_l[real_v].clear() #店にもどる max_score = search(i+shortest_time[real_v][0], 0, 0, 0, shipping_time, real_d, real_l) next_dst = 0 next_move = shortest_route[real_v][next_dst][0] #次の目的地に行く comp = search(i+shortest_time[real_v][real_d[index]], 0, real_d[index], 0, shipping_time, real_d, real_l) if comp > max_score: next_dst = real_d[index] next_move = shortest_route[real_v][next_dst][0] index += 1 #次の行動を出力する print(next_move+1) #次の頂点まで移動中のとき else: if count >= shortest_time[real_v][next_move]-1: if next_move == next_dst: real_v = next_dst count += 1 print(next_move+1) continue count = 0 real_v = next_move next_move = shortest_route[real_v][next_dst][0] count += 1 print(next_move+1) ``` No
2,266
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Requirements * All inputs are of non-negative integer value. * T_{\text{max}} = 10000 * 200 \leq |V| \leq 400 * 1.5 |V| \leq |E| \leq 2 |V| * 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) * 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) * The given graph has no self-loops / multiple edges and is guaranteed to be connected. * 0 \leq N_{\text{new}} \leq 1 * 1 \leq \mathrm{new\\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}). Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1. Therefore, the possible range of \mathrm{new\\_id}_{i} should be from 1 to T_{\text{last}}+1. * 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) * The order IDs \mathrm{new\\_{id}}_i are unique. Input Format Input is provided in the following form: |V| |E| u_{1} v_{1} d_{u_{1}, v_{1}} u_{2} v_{2} d_{u_{2}, v_{2}} : u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} T_{\text{max}} \mathrm{info}_{0} \mathrm{info}_{1} : \mathrm{info}_{T_{\text{max}}-1} * In the first line |V| denotes the number of vertices, while |E| denotes the number of edges. * The following |E| lines denote the edges of the graph. In particular, in the ith line u_{i} and v_{i} denote the edge connecting u_{i} and v_{i} and d_{u_{i}, v_{i}} the corresponding distance. * The following line denotes the number of steps T_{\text{max}}. In the following line, \mathrm{info}_t is information about the order from the customer that occurs at time t. \mathrm{info}_t is given in the form: N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} * N_{\text{new}} represents the number of new orders which appear at time t. * The next N_{\text{new}} lines give the newly generated order information. The i-th order information indicates that the order ID \mathrm{new\\_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. * Note: If N_{\text{new}}=0, there are no new lines. Output Format The Output expects T_{\text{max}} integers in the format specified below. \mathrm{command}_{0} \mathrm{command}_{1} : \mathrm{command}_{T_{\text{max}}-1} In particular, \mathrm{command}_{i} shall specify the movement of the delivery car by using one of the following two options: 1) `stay`, if the car shall not move: -1 2) `move w`, if the car shall be moved one step towards the neighboring vertex w w Note that in case of 2) w has to satisfy the following conditions: * w \in V * If the car is at vertex u: \left\\{ u, w \right\\} \in E . * If the car is on the edge \left\\{ u, v \right\\}, w must either satisfy u = w or v = w. * * * Examples Input Output Input 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 4 1 1 2 1 2 5 1 3 4 0 Output 2 -1 1 5 Submitted Solution: ``` def warshall_floid(graph): """warshall-floid法: 各頂点から各頂点への最短距離を求める 重み付きグラフgraphは隣接リストで与えられる 計算量: O(|N|^3) """ n = len(graph) INF = 10**18 dp = [[INF]*n for i in range(n)] for i in range(n): dp[i][i] = 0 for i in range(n): for j, cost in graph[i]: dp[i][j] = cost dp[j][i] = cost for k in range(n): for i in range(n): for j in range(n): dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]) return dp def trace_route(start, goal): """warshall-floid法で求めた最短距離を通るための経路を復元する 計算量: O(|N|^2) """ pos = start ans = [] while pos != goal: ans.append(pos) for next_pos, _ in graph[pos]: if dp[pos][next_pos] + dp[next_pos][goal] == dp[pos][goal]: pos = next_pos break ans.append(goal) return ans # 値を入力する n, m = map(int, input().split()) info = [list(map(int, input().split())) for i in range(m)] t_max = int(input()) query = [None] * t_max for i in range(t_max): tmp = int(input()) query[i] = [list(map(int, input().split())) for i in range(tmp)] # グラフを構築 graph = [[] for i in range(n)] for a, b, cost in info: a -= 1 b -= 1 graph[a].append((b, cost)) graph[b].append((a, cost)) # 最短距離を求める dp = warshall_floid(graph) visited = [False] * n visited[0] = True route = [0] pos = 0 total_cost = 0 while True: kyori = 10**9 next_pos = -1 for next_p, cost in enumerate(dp[pos]): if visited[next_p]: continue if cost < kyori: kyori = cost next_pos = next_p if next_pos == -1: break else: visited[next_pos] = True route.append(next_pos) pos = next_pos total_cost += kyori route.append(0) # ans: スタートから全部の頂点を通ってスタートに帰るときのコマンド ans = [] for i in range(n): pos = route[i] next_pos = route[i+1] tmp = trace_route(pos, next_pos) for j in range(len(tmp)-1): for _ in range(dp[tmp[j]][tmp[j+1]]): ans.append(tmp[j+1] + 1) #path_w = './output.txt' #f = open(path_w, mode='w') cnt = 0 while True: for i in ans: cnt += 1 print(i) #f.write(str(i) + "\n") if cnt == t_max: exit() ``` No
2,267
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Requirements * All inputs are of non-negative integer value. * T_{\text{max}} = 10000 * 200 \leq |V| \leq 400 * 1.5 |V| \leq |E| \leq 2 |V| * 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) * 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) * The given graph has no self-loops / multiple edges and is guaranteed to be connected. * 0 \leq N_{\text{new}} \leq 1 * 1 \leq \mathrm{new\\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}). Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1. Therefore, the possible range of \mathrm{new\\_id}_{i} should be from 1 to T_{\text{last}}+1. * 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) * The order IDs \mathrm{new\\_{id}}_i are unique. Input Format Input is provided in the following form: |V| |E| u_{1} v_{1} d_{u_{1}, v_{1}} u_{2} v_{2} d_{u_{2}, v_{2}} : u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} T_{\text{max}} \mathrm{info}_{0} \mathrm{info}_{1} : \mathrm{info}_{T_{\text{max}}-1} * In the first line |V| denotes the number of vertices, while |E| denotes the number of edges. * The following |E| lines denote the edges of the graph. In particular, in the ith line u_{i} and v_{i} denote the edge connecting u_{i} and v_{i} and d_{u_{i}, v_{i}} the corresponding distance. * The following line denotes the number of steps T_{\text{max}}. In the following line, \mathrm{info}_t is information about the order from the customer that occurs at time t. \mathrm{info}_t is given in the form: N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} * N_{\text{new}} represents the number of new orders which appear at time t. * The next N_{\text{new}} lines give the newly generated order information. The i-th order information indicates that the order ID \mathrm{new\\_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. * Note: If N_{\text{new}}=0, there are no new lines. Output Format The Output expects T_{\text{max}} integers in the format specified below. \mathrm{command}_{0} \mathrm{command}_{1} : \mathrm{command}_{T_{\text{max}}-1} In particular, \mathrm{command}_{i} shall specify the movement of the delivery car by using one of the following two options: 1) `stay`, if the car shall not move: -1 2) `move w`, if the car shall be moved one step towards the neighboring vertex w w Note that in case of 2) w has to satisfy the following conditions: * w \in V * If the car is at vertex u: \left\\{ u, w \right\\} \in E . * If the car is on the edge \left\\{ u, v \right\\}, w must either satisfy u = w or v = w. * * * Examples Input Output Input 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 4 1 1 2 1 2 5 1 3 4 0 Output 2 -1 1 5 Submitted Solution: ``` from collections import defaultdict import random from logging import getLogger, StreamHandler, DEBUG, ERROR from functools import lru_cache logger = getLogger(__name__) handler = StreamHandler() handler.setLevel(DEBUG) logger.setLevel(ERROR) logger.addHandler(handler) logger.propagate = False logger.debug("HEADER") class Maps(object): def __init__(self): self.readinputs() # recieve |V| and |E| def readinputs(self): self.V, self.E = map(int, input().split()) self.es = [[] for i in range(self.V)] self.Mes = [[0]*self.V for i in range(self.V)] self.graph = Graph() self.car = Car() self.here = ('v', 0) self.plan = [] for _ in range(self.E): # recieve edges a, b, c = map(int, input().split()) a, b = a-1, b-1 self.es[a].append((b,c)) self.es[b].append((a,c)) self.Mes[a][b] = c self.Mes[b][a] = c self.graph.add_edge(a,b,c) self.T = int(input()) # recieve info self.info = [[] for i in range(self.T)] for t in range(self.T): Nnew = int(input()) for _ in range(Nnew): new_id, dst = map(int, input().split()) dst = dst-1 self.info[t].append((new_id, dst)) logger.debug("Set status") logger.debug("Info :") logger.debug(self.info) def get_plan(self): return self.plan def has_plan(self): if self.plan == []: return False else: return True def get_here(self): return self.here def set_here(self, here): self.here = here def next_place(self, here, command): if command == -1: return here else: to = command if here[0] == 'v': dist = self.Mes[here[1]][to] if dist == 0: raise ValueError('Dist has not 0') elif dist == 1: return ('v', to) else: return ('e', here[1], to, 1) # You can move 1m per time elif here[0] == 'e': if here[2] == to: if here[3] + 1 < self.Mes[here[1]][here[2]]: return ('e', here[1], here[2], here[3] + 1) elif here[3] + 1 == self.Mes[here[1]][here[2]]: return ('v', here[2]) else: raise ValueError('Distance error!') elif here[1] == to: if here[3] - 1 > 0: return ('e', here[1], here[2], here[3] - 1) elif here[3] - 1 == 0: return ('v', here[1]) else: raise ValueError('Distance error!') else: raise ValueError('Here has vertex or edge status') def get_command(self, here, plan): if here[0] == 'v': # you are in vertex if plan == []: # stay at 0 return -1 elif here[1] == plan[-1]: # arrive goal return -1 else: return plan[plan.index(here[1]) + 1] elif here[0] == 'e': # you are in edge return here[2] else: raise ValueError('Here has vertex or edge status') def move(self): where = [('v', 0)] # ('v', here) or ('e', from, to, dist) self.path = [0]*self.T last_stay_0 = 0 for t in range(self.T): now_where = where[-1] logger.debug("----------Time----------") logger.debug(t) logger.debug("Here is :") logger.debug(now_where) logger.debug("parcel :") logger.debug(self.info[t]) if now_where[0] == 'v': # 荷物を拾ったりおろしたり if now_where[1] == 0: logger.debug("Car Parameter :") logger.debug("Last stay at 0 :") logger.debug(last_stay_0) logger.debug("Picking up parcel is :") for parcel in self.info[last_stay_0: t + 1]: logger.debug(parcel) self.car.pickup(parcel) last_stay_0 = t + 1 else: self.car.unload(now_where[1]) logger.debug("Car Box :") logger.debug(self.car.box) if self.car.box == []: logger.debug("Car Box is vacant") if now_where[0] == 'v' and now_where[1] == 0: self.path[t] = -1 where.append(('v', 0)) logger.debug("Time " + str(t) + " command is :") logger.debug(self.path[t]) logger.debug("No parcel and stay at 1. Continue") continue if not self.has_plan(): logger.debug("--Set plan--") logger.debug("-Now-") logger.debug(now_where) logger.debug("-Dests-") logger.debug(self.car.show_dest()) if now_where[1] != 0: goal = 0 else: goal = self.select_goal(now_where[1], self.car.show_dest()) logger.debug("Set Goal is :") logger.debug(goal) self.plan = wrap_dijsktra(self.graph, now_where[1], goal) logger.debug("Time " + str(t) + " plan is :") logger.debug(self.plan) if self.plan == []: logger.debug("Time " + str(t) + " has no plan") self.path[t] = -1 continue command = self.get_command(now_where, self.plan) self.path[t] = command logger.debug("Time " + str(t) + " command is :") logger.debug(command) where.append(self.next_place(now_where, command)) logger.debug("Time " + str(t) + " next place is :") logger.debug(where[-1]) if where[-1][0] == 'v' and where[-1][1] == self.plan[-1]: logger.debug("Time " + str(t) + " we arrive at goal !") self.plan = [] def select_goal(self, here, dests): # logger.debug('--select goal--') # logger.debug('Here is : ') # logger.debug(here) dist = dict() for dest in dests: # logger.debug("Dest is : ") # logger.debug(dest) path = wrap_dijsktra(self.graph, here, dest) # logger.debug("path is : ") # logger.debug(path) dist[dest] = self.dist_path(path) nearby = min(dist, key=dist.get) return nearby def dist_path(self, path): distance = 0 for n in range(len(path)-1): distance += self.Mes[path[n]][path[n+1]] return distance def output(self): for t in range(self.T) : print(self.path[t] + 1) class Graph(): def __init__(self): """ self.edges is a dict of all possible next nodes e.g. {'X': ['A', 'B', 'C', 'E'], ...} self.weights has all the weights between two nodes, with the two nodes as a tuple as the key e.g. {('X', 'A'): 7, ('X', 'B'): 2, ...} """ self.edges = defaultdict(list) self.weights = {} def add_edge(self, from_node, to_node, weight): # Note: assumes edges are bi-directional self.edges[from_node].append(to_node) self.edges[to_node].append(from_node) self.weights[(from_node, to_node)] = weight self.weights[(to_node, from_node)] = weight class Car(object): def __init__(self): self.box = [] # self.dest = set() def pickup(self, parcel): # parcel [] or [(new_id, dst)] if parcel == []: pass else: self.box.append(parcel[0]) # self.dest.add(parcel[0]) def unload(self, vertex): self.box = [x for x in self.box if x[1] != vertex] # self.dest = {x for x in self.box if x[1] != vertex} def show_dest(self): dest = set() for x in self.box: # show dst only. dest.add(x[1]) if len(dest) == 0: dest.add(0) return list(dest) def wrap_dijsktra(graph, initial, end): if initial == end: return [] elif initial < end: return dijsktra(graph, initial, end) else: return dijsktra(graph, end, initial)[::-1] @lru_cache(maxsize=80000) def dijsktra(graph, initial, end): # shortest paths is a dict of nodes # whose value is a tuple of (previous node, weight) shortest_paths = {initial: (None, 0)} current_node = initial visited = set() while current_node != end: visited.add(current_node) destinations = graph.edges[current_node] weight_to_current_node = shortest_paths[current_node][1] for next_node in destinations: weight = graph.weights[(current_node, next_node)] + weight_to_current_node if next_node not in shortest_paths: shortest_paths[next_node] = (current_node, weight) else: current_shortest_weight = shortest_paths[next_node][1] if current_shortest_weight > weight: shortest_paths[next_node] = (current_node, weight) next_destinations = {node: shortest_paths[node] for node in shortest_paths if node not in visited} if not next_destinations: return "Route Not Possible" # next node is the destination with the lowest weight current_node = min(next_destinations, key=lambda k: next_destinations[k][1]) # Work back through destinations in shortest path path = [] while current_node is not None: path.append(current_node) next_node = shortest_paths[current_node][0] current_node = next_node # Reverse path path = path[::-1] return path if __name__ == "__main__": def main(): M = Maps() M.move() M.output() main() ``` No
2,268
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Requirements * All inputs are of non-negative integer value. * T_{\text{max}} = 10000 * 200 \leq |V| \leq 400 * 1.5 |V| \leq |E| \leq 2 |V| * 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) * 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) * The given graph has no self-loops / multiple edges and is guaranteed to be connected. * 0 \leq N_{\text{new}} \leq 1 * 1 \leq \mathrm{new\\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}). Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1. Therefore, the possible range of \mathrm{new\\_id}_{i} should be from 1 to T_{\text{last}}+1. * 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) * The order IDs \mathrm{new\\_{id}}_i are unique. Input Format Input is provided in the following form: |V| |E| u_{1} v_{1} d_{u_{1}, v_{1}} u_{2} v_{2} d_{u_{2}, v_{2}} : u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} T_{\text{max}} \mathrm{info}_{0} \mathrm{info}_{1} : \mathrm{info}_{T_{\text{max}}-1} * In the first line |V| denotes the number of vertices, while |E| denotes the number of edges. * The following |E| lines denote the edges of the graph. In particular, in the ith line u_{i} and v_{i} denote the edge connecting u_{i} and v_{i} and d_{u_{i}, v_{i}} the corresponding distance. * The following line denotes the number of steps T_{\text{max}}. In the following line, \mathrm{info}_t is information about the order from the customer that occurs at time t. \mathrm{info}_t is given in the form: N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} * N_{\text{new}} represents the number of new orders which appear at time t. * The next N_{\text{new}} lines give the newly generated order information. The i-th order information indicates that the order ID \mathrm{new\\_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. * Note: If N_{\text{new}}=0, there are no new lines. Output Format The Output expects T_{\text{max}} integers in the format specified below. \mathrm{command}_{0} \mathrm{command}_{1} : \mathrm{command}_{T_{\text{max}}-1} In particular, \mathrm{command}_{i} shall specify the movement of the delivery car by using one of the following two options: 1) `stay`, if the car shall not move: -1 2) `move w`, if the car shall be moved one step towards the neighboring vertex w w Note that in case of 2) w has to satisfy the following conditions: * w \in V * If the car is at vertex u: \left\\{ u, w \right\\} \in E . * If the car is on the edge \left\\{ u, v \right\\}, w must either satisfy u = w or v = w. * * * Examples Input Output Input 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 4 1 1 2 1 2 5 1 3 4 0 Output 2 -1 1 5 Submitted Solution: ``` for i in range(10**18): b=[0,1,2] a=b.pop() x=a**2+a a=1/0 ``` No
2,269
Provide a correct Python 3 solution for this coding contest problem. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2 "Correct Solution: ``` from collections import Counter import itertools as it N=int(input()) l=[] for i in range(N): l.append(tuple(map(int,input().split()))) l.sort() if N==1: ans=1 else: d=[(c[1][0]-c[0][0],c[1][1]-c[0][1]) for c in it.combinations(l,2)] ans=N-max(dict(Counter(d)).values()) print(ans) ```
2,270
Provide a correct Python 3 solution for this coding contest problem. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2 "Correct Solution: ``` N=int(input()) xy=[list(map(int,input().split())) for _ in [0]*N] pq = {0:0} for i in range(N): for j in range(N): if i==j:continue p = xy[i][0] - xy[j][0] q = xy[i][1] - xy[j][1] pq[(p,q)] = pq.get((p,q),0)+1 print(N - max(pq.values())) ```
2,271
Provide a correct Python 3 solution for this coding contest problem. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2 "Correct Solution: ``` from collections import Counter n = int(input()) ball = sorted([list(map(int,input().split())) for i in range(n)]) diff = [] for k in range(1,n): for i in range(1,n): if i-k>=0: p,q = ball[i][0]-ball[i-k][0],ball[i][1]-ball[i-k][1] diff.append((p,q)) D = Counter(diff) p = 0 for v in D.values(): p = max(p,v) print(n-p) ```
2,272
Provide a correct Python 3 solution for this coding contest problem. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2 "Correct Solution: ``` n=int(input()) w={} d=[] for i in range(n): x,y=map(int, input().split()) d.append((x,y)) chk=0 for i in range(n): a=d[i][0] b=d[i][1] for j in range(n): if i==j: continue p=d[j][0] q=d[j][1] if (a-p,b-q) in w: w[(a-p,b-q)]+=1 else: w[(a-p,b-q)]=1 chk=max(chk,w[(a-p,b-q)]) print(n-chk) ```
2,273
Provide a correct Python 3 solution for this coding contest problem. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2 "Correct Solution: ``` from collections import Counter N = int(input()) xys = [] for _ in range(N): xys.append(tuple(map(int, input().split()))) sub = [] for x1, y1 in xys: for x2, y2 in xys: if x1 != x2 or y1 != y2: sub.append((x1 - x2, y1 - y2)) if not sub: print(1) exit(0) c = Counter(sub) m = max(c.values()) print(N - m) ```
2,274
Provide a correct Python 3 solution for this coding contest problem. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2 "Correct Solution: ``` #https://atcoder.jp/contests/diverta2019-2/submissions/11229318 n = int(input()) t = [tuple(map(int, input().split())) for _ in range(n)] s = set(t) cnt = 0 for i in range(n-1): for j in range(i+1,n): u,v = t[i] x,y = t[j] p = u-x; q = v-y c = sum((x-p, y-q) in s for x,y in t) if cnt < c: cnt = c print(n-cnt) ```
2,275
Provide a correct Python 3 solution for this coding contest problem. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2 "Correct Solution: ``` n = int(input()) p = [tuple(map(int,input().split())) for _ in range(n)] d = {} m = 0 for i in range(n): for j in range(n): if i == j: continue fx = p[i][0] - p[j][0] fy = p[i][1] - p[j][1] f = (fx, fy) if f not in d: d[f] = 0 d[f] += 1 m = max((m, d[f])) print(n - m) ```
2,276
Provide a correct Python 3 solution for this coding contest problem. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2 "Correct Solution: ``` import itertools from collections import Counter N = int(input()) point = [tuple(map(int, input().split())) for _ in range(N)] ans = Counter() if N == 1: print(1) exit() for first, second in list(itertools.permutations(point, 2)): fx, fy = first sx, sy = second ans[(sx-fx, sy-fy)] += 1 print(max(1, N - ans.most_common()[0][1])) ```
2,277
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2 Submitted Solution: ``` from collections import Counter from itertools import combinations N = int(input()) R = [[int(i) for i in input().split()] for _ in range(N)] if N == 1: print(1) quit() L = [] for (x1, y1), (x2, y2) in combinations(R, 2): L.append((x1 - x2, y1 - y2)) L.append((-x1 + x2, -y1 + y2)) C = Counter(L) print(N - C.most_common(1)[0][1]) ``` Yes
2,278
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2 Submitted Solution: ``` from collections import Counter n = int(input()) XY = [list(map(int,input().split())) for _ in range(n)] dXY = Counter([]) for i in range(n): for j in range(n): if i!=j: dXY += Counter([str(XY[i][0]-XY[j][0])+'_'+str(XY[i][1]-XY[j][1])]) if n==1: print(1) else: print(n-dXY.most_common()[0][1]) ``` Yes
2,279
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2 Submitted Solution: ``` n=int(input()) A=[] B=[] for i in range(n): a,b=map(int,input().split()) A.append([a,b]) for i in range(n): for j in range(i): B.append([A[i][0]-A[j][0],A[i][1]-A[j][1]]) for j in range(i+1,n): B.append([A[i][0]-A[j][0],A[i][1]-A[j][1]]) l=0 for i in B: l=max(l,B.count(i)) print(n-l) ``` Yes
2,280
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2 Submitted Solution: ``` from itertools import combinations from collections import Counter N = int(input()) xy = [list(map(int, input().split())) for _ in range(N)] count = [] for ((x1,x2),(x3,x4)) in combinations(xy, 2): count.append((x1-x3,x2-x4)) count.append((x3-x1,x4-x2)) c = Counter(count) m = 0 for i in c.values(): m = max(m, i) if N == 1: print(1) else: print(N-m) ``` Yes
2,281
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2 Submitted Solution: ``` N = int(input()) cost = 1 xy_set = [] for i in range(N): x, y = map(int, input().split()) xy_set.append((x, y)) xy_set_sorted = sorted(xy_set, key=lambda a: a[0]) xy_sub_set = {} for i in range(1, N): xs = xy_set_sorted[i][0] - xy_set_sorted[i-1][0] ys = xy_set_sorted[i][1] - xy_set_sorted[i - 1][1] if xy_sub_set.get((xs, ys)) is None: xy_sub_set[(xs, ys)] = 1 else: xy_sub_set[(xs, ys)] += 1 xy_sub_set_sorted = sorted(xy_sub_set.items(), key=lambda a: a[1], reverse=True) print(cost + (N-1) - xy_sub_set_sorted[0][1]) ``` No
2,282
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2 Submitted Solution: ``` N = int(input()) p = [] dic = {} sameX = {} allNum = [] for i in range(N): x,y = map(int,input().split()) p.append([x,y]) allNum.append(i) for i in range(N): for j in range(i): ji = [j,i] if p[j][0] - p[i][0] != 0: a = ( p[j][1] - p[i][1] ) / ( p[j][0] - p[i][0] ) b = p[j][1] - ( a * p[j][1] ) if a == -0.0: a = 0.0 ab = [a,b] if ( str (ab) ) in dic: dic[ str (ab) ].append(ji) else: dic[ str(ab) ] = [ji] elif p[j][0] - p[i][0] == 0: if p[j][0] in sameX: sameX[ p[j][0] ].append(ji) else: sameX[ p[j][0] ] = [ji] #print (dic,sameX) DorS = 0 maxi = 0 keys = "" ans = 0 while len(allNum) > 0: DorS = 0 maxi = 0 keys = "" for i in dic: eable = {} for j in dic[i]: if j[0] in allNum: eable[j[0]] = 1 if j[1] in allNum: eable[j[1]] = 1 if len(eable) > maxi: DorS = 0 maxi = len(dic[i]) keys = i for i in sameX: eable = {} for j in sameX[i]: if j[0] in allNum: eable[j[0]] = 1 if j[1] in allNum: eable[j[1]] = 1 if len(eable) > maxi: DorS = 1 maxi = len(sameX[i]) keys = i if DorS == 0: delPs = dic[keys] del dic[keys] else: delPs = sameX[keys] del sameX[keys] #print (delPs) rmFlag = False for i in delPs: if i[0] in allNum : allNum.remove(i[0]) rmFlag = True if i[1] in allNum : allNum.remove(i[1]) rmFlag = True if rmFlag : ans += 1 #print (allNum) print (ans) ``` No
2,283
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2 Submitted Solution: ``` n = int(input()) xy = [[int(i) for i in input().split()] for i in range(n)] #print(n) print(xy) #print(xy[0][1]) vec = [] vec2 = [] for i in range(n): for j in range(i + 1, n): x = xy[i][0] - xy[j][0] y = xy[i][1] - xy[j][1] z = [x, y] if z not in vec: vec.append(z) vec2.append(z) #print(vec) #print(vec2) #vec2 = list(set(vec)) cnt = 0 idx = 0 for i, x in enumerate(vec): if cnt < vec2.count(x): cnt = vec2.count(x) idx = i #print(cnt) #print(idx) pq = vec[idx] print(pq) #print(pq[0]) #print(pq[1]) cst = 1 for i in range(1, n): pq_add_x = xy[i][0] + pq[0] pq_add_y = xy[i][1] + pq[1] pq_add = [pq_add_x, pq_add_y] #pq_sub_x = xy[i][0] - pq[0] #pq_sub_y = xy[i][1] - pq[1] #pq_sub = [pq_sub_x, pq_sub_y] if pq_add not in xy: #if pq_sub not in xy: cst += 1 print(cst) ``` No
2,284
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i). We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation: * Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1. Find the minimum total cost required to collect all the balls when we optimally choose p and q. Constraints * 1 \leq N \leq 50 * |x_i|, |y_i| \leq 10^9 * If i \neq j, x_i \neq x_j or y_i \neq y_j. * All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the minimum total cost required to collect all the balls. Examples Input 2 1 1 2 2 Output 1 Input 3 1 4 4 6 7 8 Output 1 Input 4 1 1 1 2 2 1 2 2 Output 2 Submitted Solution: ``` import logging import unittest from collections import ( Counter, ) debug = logging.getLogger(__name__).debug def input_ints(): return list(map(int, input().strip().split())) def _main(): n, = input_ints() xy = [input_ints() for _ in range(n)] (_, count), = Counter( (x1 - x2, y1 - y2) for (x1, y1) in xy for (x2, y2) in xy if not (x1 == x2 and y1 == y2) ).most_common(1) print(len(xy) - count) class Test(unittest.TestCase): def setUp(self): import run self._test = run def test_main(self): self._test.test_files(self, _main) if __name__ == '__main__': _main() ``` No
2,285
Provide a correct Python 3 solution for this coding contest problem. A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n). Constraints * 1 \leq s \leq 100 * All values in input are integers. * It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000. Input Input is given from Standard Input in the following format: s Output Print the minimum integer m that satisfies the condition. Examples Input 8 Output 5 Input 7 Output 18 Input 54 Output 114 "Correct Solution: ``` a = [int(input())] s = a[0] while s not in a[0:len(a)-1]: if s%2 == 0: a.append(s//2) else:a.append(3*s+1) s = a[len(a)-1] print(len(a)) ```
2,286
Provide a correct Python 3 solution for this coding contest problem. A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n). Constraints * 1 \leq s \leq 100 * All values in input are integers. * It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000. Input Input is given from Standard Input in the following format: s Output Print the minimum integer m that satisfies the condition. Examples Input 8 Output 5 Input 7 Output 18 Input 54 Output 114 "Correct Solution: ``` s = int(input()) a = [] n=1 while not s in a: a.append(s) n += 1 if s%2==0: s/=2 else: s = 3*s+1 print(n) ```
2,287
Provide a correct Python 3 solution for this coding contest problem. A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n). Constraints * 1 \leq s \leq 100 * All values in input are integers. * It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000. Input Input is given from Standard Input in the following format: s Output Print the minimum integer m that satisfies the condition. Examples Input 8 Output 5 Input 7 Output 18 Input 54 Output 114 "Correct Solution: ``` n = int(input()) l = [0] * 1000001 count = 1 while not l[n]: l[n] = 1 n = 3 * n + 1 if n & 1 else n >> 1 count += 1 print(count) ```
2,288
Provide a correct Python 3 solution for this coding contest problem. A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n). Constraints * 1 \leq s \leq 100 * All values in input are integers. * It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000. Input Input is given from Standard Input in the following format: s Output Print the minimum integer m that satisfies the condition. Examples Input 8 Output 5 Input 7 Output 18 Input 54 Output 114 "Correct Solution: ``` s,cnt = int(input()),1 while s!=1 and s!=2 and s!=4: if s%2==0: s//=2 else: s=s*3+1 cnt+=1 print(cnt+3) ```
2,289
Provide a correct Python 3 solution for this coding contest problem. A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n). Constraints * 1 \leq s \leq 100 * All values in input are integers. * It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000. Input Input is given from Standard Input in the following format: s Output Print the minimum integer m that satisfies the condition. Examples Input 8 Output 5 Input 7 Output 18 Input 54 Output 114 "Correct Solution: ``` s=int(input()) li=[] cnt=1 while s not in li: li.append(s) if s%2==0: s/=2 else: s=(s*3)+1 cnt+=1 print(cnt) ```
2,290
Provide a correct Python 3 solution for this coding contest problem. A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n). Constraints * 1 \leq s \leq 100 * All values in input are integers. * It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000. Input Input is given from Standard Input in the following format: s Output Print the minimum integer m that satisfies the condition. Examples Input 8 Output 5 Input 7 Output 18 Input 54 Output 114 "Correct Solution: ``` s = int(input()) cnt = 1 box = set() while not s in box: box.add(s) if s % 2 == 0: s = s / 2 else: s = 3 * s + 1 cnt += 1 print(cnt) ```
2,291
Provide a correct Python 3 solution for this coding contest problem. A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n). Constraints * 1 \leq s \leq 100 * All values in input are integers. * It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000. Input Input is given from Standard Input in the following format: s Output Print the minimum integer m that satisfies the condition. Examples Input 8 Output 5 Input 7 Output 18 Input 54 Output 114 "Correct Solution: ``` s=int(input()) l=[] l.append(s) for i in range(10**7): if s%2==0:s=int(s/2) else:s=3*s+1 if s in l:break else:l.append(s) print(i+2) ```
2,292
Provide a correct Python 3 solution for this coding contest problem. A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n). Constraints * 1 \leq s \leq 100 * All values in input are integers. * It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000. Input Input is given from Standard Input in the following format: s Output Print the minimum integer m that satisfies the condition. Examples Input 8 Output 5 Input 7 Output 18 Input 54 Output 114 "Correct Solution: ``` s = int(input()) ans = 4 while (s>4 or s==3): ans += 1 if (s%2==0): s //= 2 else: s = 3*s+1 print(ans) ```
2,293
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n). Constraints * 1 \leq s \leq 100 * All values in input are integers. * It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000. Input Input is given from Standard Input in the following format: s Output Print the minimum integer m that satisfies the condition. Examples Input 8 Output 5 Input 7 Output 18 Input 54 Output 114 Submitted Solution: ``` s = int(input()) a = [] ans = 1 while s not in a: a.append(s) if s%2 == 0: s = s//2 else: s = 3*s+1 ans +=1 print(ans) ``` Yes
2,294
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n). Constraints * 1 \leq s \leq 100 * All values in input are integers. * It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000. Input Input is given from Standard Input in the following format: s Output Print the minimum integer m that satisfies the condition. Examples Input 8 Output 5 Input 7 Output 18 Input 54 Output 114 Submitted Solution: ``` s = int(input()) a = [] while s not in a: a.append(s) if s % 2 == 0: s = s / 2 else: s = s * 3 + 1 print(len(a) + 1) ``` Yes
2,295
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n). Constraints * 1 \leq s \leq 100 * All values in input are integers. * It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000. Input Input is given from Standard Input in the following format: s Output Print the minimum integer m that satisfies the condition. Examples Input 8 Output 5 Input 7 Output 18 Input 54 Output 114 Submitted Solution: ``` x=int(input()) a=[] while x not in a: a.append(x) if x%2==0: x=x//2 else: x=3*x+1 print(len(a)+1) ``` Yes
2,296
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n). Constraints * 1 \leq s \leq 100 * All values in input are integers. * It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000. Input Input is given from Standard Input in the following format: s Output Print the minimum integer m that satisfies the condition. Examples Input 8 Output 5 Input 7 Output 18 Input 54 Output 114 Submitted Solution: ``` s = int(input()) e = set() m = 1 while s not in e: e.add(s) s = s // 2 if s % 2 == 0 else s * 3 + 1 m += 1 print(m) ``` Yes
2,297
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n). Constraints * 1 \leq s \leq 100 * All values in input are integers. * It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000. Input Input is given from Standard Input in the following format: s Output Print the minimum integer m that satisfies the condition. Examples Input 8 Output 5 Input 7 Output 18 Input 54 Output 114 Submitted Solution: ``` s = int(input()) a = [] a.append(s) while a[0] != s: if s % 2 ==0: s = s//2 a.append(s) else: s = 3*s +1 a.append(s) print(len(a)) ``` No
2,298
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A sequence a=\\{a_1,a_2,a_3,......\\} is determined as follows: * The first term s is given as input. * Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd. * a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1. Find the minimum integer m that satisfies the following condition: * There exists an integer n such that a_m = a_n (m > n). Constraints * 1 \leq s \leq 100 * All values in input are integers. * It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000. Input Input is given from Standard Input in the following format: s Output Print the minimum integer m that satisfies the condition. Examples Input 8 Output 5 Input 7 Output 18 Input 54 Output 114 Submitted Solution: ``` s = int(input()) a = s lis = list() while True: if (s % 2) == 0: s = s / 2 if s in lis: break lis.append(int(s)) else: s = 3*s + 1 if s in lis: break lis.append(int(s)) if (a ==1) or (a == 2) or (a == 4): print(3) else: print(len(lis)+2) ``` No
2,299