message
stringlengths
2
22.8k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
16
109k
cluster
float64
1
1
__index_level_0__
int64
32
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N towns in Takahashi Kingdom. They are conveniently numbered 1 through N. Takahashi the king is planning to go on a tour of inspection for M days. He will determine a sequence of towns c, and visit town c_i on the i-th day. That is, on the i-th day, he will travel from his current location to town c_i. If he is already at town c_i, he will stay at that town. His location just before the beginning of the tour is town 1, the capital. The tour ends at town c_M, without getting back to the capital. The problem is that there is no paved road in this kingdom. He decided to resolve this issue by paving the road himself while traveling. When he travels from town a to town b, there will be a newly paved one-way road from town a to town b. Since he cares for his people, he wants the following condition to be satisfied after his tour is over: "it is possible to travel from any town to any other town by traversing roads paved by him". How many sequences of towns c satisfy this condition? Constraints * 2≦N≦300 * 1≦M≦300 Input The input is given from Standard Input in the following format: N M Output Print the number of sequences of towns satisfying the condition, modulo 1000000007 (=10^9+7). Examples Input 3 3 Output 2 Input 150 300 Output 734286322 Input 300 150 Output 0 Submitted Solution: ``` mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline N, M = map(int, input().split()) dp = [[[0] * N for _ in range(N+1)] for _ in range(M+1)] dp[0][1][0] = 1 for i in range(M): for j in range(N+1): for k in range(N): dp[i+1][j][k] = (dp[i+1][j][k] + dp[i][j][k] * k)%mod if k+1 < N: dp[i+1][j][k+1] = (dp[i+1][j][k+1] + dp[i][j][k] * (N - j - k))%mod if j+k <= N: dp[i+1][j+k][0] = (dp[i+1][j+k][0] + dp[i][j][k] * j)%mod print(dp[-1][N][0]) if __name__ == '__main__': main() ```
instruction
0
8,952
1
17,904
No
output
1
8,952
1
17,905
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N towns in Takahashi Kingdom. They are conveniently numbered 1 through N. Takahashi the king is planning to go on a tour of inspection for M days. He will determine a sequence of towns c, and visit town c_i on the i-th day. That is, on the i-th day, he will travel from his current location to town c_i. If he is already at town c_i, he will stay at that town. His location just before the beginning of the tour is town 1, the capital. The tour ends at town c_M, without getting back to the capital. The problem is that there is no paved road in this kingdom. He decided to resolve this issue by paving the road himself while traveling. When he travels from town a to town b, there will be a newly paved one-way road from town a to town b. Since he cares for his people, he wants the following condition to be satisfied after his tour is over: "it is possible to travel from any town to any other town by traversing roads paved by him". How many sequences of towns c satisfy this condition? Constraints * 2≦N≦300 * 1≦M≦300 Input The input is given from Standard Input in the following format: N M Output Print the number of sequences of towns satisfying the condition, modulo 1000000007 (=10^9+7). Examples Input 3 3 Output 2 Input 150 300 Output 734286322 Input 300 150 Output 0 Submitted Solution: ``` import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np MOD = 10**9+7 N,M = map(int,read().split()) """ ・既に強連結成分として確定している町の個数 ・訪問済だけれど強連結成分には入っていない町の個数 に対する場合の数を持ってdp """ dp = np.zeros((N+1,N+1),np.int64) dp[1,0] = 1 rng = np.arange(N+1,dtype=np.int64) for _ in range(M): prev = dp dp = np.zeros_like(prev) # 強連結成分から出発する場合。 dp[:,0] += prev[:,0] * rng dp[:,1] += prev[:,0] * rng[::-1] # 未確定部分から for m in range(1,N): # 強連結成分に戻る dp[m:,0] += prev[:-m,m] * rng[:-m] # 未確定部分のまま dp[:,m] += prev[:,m] * m # 未確定部分を広げる dp[:,m+1] += prev[:,m] * (N-m-rng) dp %= MOD answer = dp[N,0] print(answer) ```
instruction
0
8,953
1
17,906
No
output
1
8,953
1
17,907
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N towns in Takahashi Kingdom. They are conveniently numbered 1 through N. Takahashi the king is planning to go on a tour of inspection for M days. He will determine a sequence of towns c, and visit town c_i on the i-th day. That is, on the i-th day, he will travel from his current location to town c_i. If he is already at town c_i, he will stay at that town. His location just before the beginning of the tour is town 1, the capital. The tour ends at town c_M, without getting back to the capital. The problem is that there is no paved road in this kingdom. He decided to resolve this issue by paving the road himself while traveling. When he travels from town a to town b, there will be a newly paved one-way road from town a to town b. Since he cares for his people, he wants the following condition to be satisfied after his tour is over: "it is possible to travel from any town to any other town by traversing roads paved by him". How many sequences of towns c satisfy this condition? Constraints * 2≦N≦300 * 1≦M≦300 Input The input is given from Standard Input in the following format: N M Output Print the number of sequences of towns satisfying the condition, modulo 1000000007 (=10^9+7). Examples Input 3 3 Output 2 Input 150 300 Output 734286322 Input 300 150 Output 0 Submitted Solution: ``` import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np MOD = 10**9+7 N,M = map(int,read().split()) """ ・既に強連結成分として確定している町の個数 ・訪問済だけれど強連結成分には入っていない町の個数 に対する場合の数を持ってdp """ dp = np.zeros((N+1,N+1),np.int64) dp[1,0] = 1 rng = np.arange(N+1,dtype=np.int64) for _ in range(M): prev = dp dp = np.zeros_like(prev) # 強連結成分から出発する場合。 dp[:,0] += prev[:,0] * rng dp[:,1] += prev[:,0] * rng[::-1] # 未確定部分から for m in range(1,N): # 強連結成分に戻る dp[m:,0] += prev[:-m,m] * rng[:-m] # 未確定部分のまま dp[:,m] += prev[:,m] * m # 未確定部分を広げる dp[:N-m,m+1] += prev[:N-m,m] * (N-m-rng[:N-m]) dp %= MOD answer = dp[N,0] print(answer) ```
instruction
0
8,954
1
17,908
No
output
1
8,954
1
17,909
Provide a correct Python 3 solution for this coding contest problem. When a boy was cleaning up after his grand father passing, he found an old paper: <image> In addition, other side of the paper says that "go ahead a number of steps equivalent to the first integer, and turn clockwise by degrees equivalent to the second integer". His grand mother says that Sanbonmatsu was standing at the center of town. However, now buildings are crammed side by side and people can not walk along exactly what the paper says in. Your task is to write a program which hunts for the treature on the paper. For simplicity, 1 step is equivalent to 1 meter. Input consists of several pairs of two integers d (the first integer) and t (the second integer) separated by a comma. Input ends with "0, 0". Your program should print the coordinate (x, y) of the end point. There is the treature where x meters to the east and y meters to the north from the center of town. You can assume that d ≤ 100 and -180 ≤ t ≤ 180. Input A sequence of pairs of integers d and t which end with "0,0". Output Print the integer portion of x and y in a line respectively. Example Input 56,65 97,54 64,-4 55,76 42,-27 43,80 87,-86 55,-6 89,34 95,5 0,0 Output 171 -302
instruction
0
8,955
1
17,910
"Correct Solution: ``` import math Tre = [] num = 0 while True: li = list(map(int,input().split(","))) if li == [0,0]: break Tre.append(li) num += 1 x,y = 0,0 deg = 90 for i in range(num): x,y = x + Tre[i][0]*math.cos(math.pi*deg/180),y + Tre[i][0]*math.sin(math.pi*deg/180) deg = deg - Tre[i][1] print(int(x)) print(int(y)) ```
output
1
8,955
1
17,911
Provide a correct Python 3 solution for this coding contest problem. When a boy was cleaning up after his grand father passing, he found an old paper: <image> In addition, other side of the paper says that "go ahead a number of steps equivalent to the first integer, and turn clockwise by degrees equivalent to the second integer". His grand mother says that Sanbonmatsu was standing at the center of town. However, now buildings are crammed side by side and people can not walk along exactly what the paper says in. Your task is to write a program which hunts for the treature on the paper. For simplicity, 1 step is equivalent to 1 meter. Input consists of several pairs of two integers d (the first integer) and t (the second integer) separated by a comma. Input ends with "0, 0". Your program should print the coordinate (x, y) of the end point. There is the treature where x meters to the east and y meters to the north from the center of town. You can assume that d ≤ 100 and -180 ≤ t ≤ 180. Input A sequence of pairs of integers d and t which end with "0,0". Output Print the integer portion of x and y in a line respectively. Example Input 56,65 97,54 64,-4 55,76 42,-27 43,80 87,-86 55,-6 89,34 95,5 0,0 Output 171 -302
instruction
0
8,957
1
17,914
"Correct Solution: ``` import sys import math def sign(x): if x>=0: return 1.0 else: return -1.0 pi = math.pi deg = 0.0 pos_x = 0.0 pos_y = 0.0 lines = sys.stdin.readlines() for line in lines: line = line.split(",") inp = [] for i in line: inp.append(int(i)) if inp[0]>0: dist = inp[0] pos_x += dist*math.sin(deg/180*pi) pos_y += dist*math.cos(deg/180*pi) deg += inp[1] else: print ("%d" % (sign(pos_x)*math.floor(math.fabs(pos_x)))) print ("%d" % (sign(pos_y)*math.floor(math.fabs(pos_y)))) ```
output
1
8,957
1
17,915
Provide a correct Python 3 solution for this coding contest problem. When a boy was cleaning up after his grand father passing, he found an old paper: <image> In addition, other side of the paper says that "go ahead a number of steps equivalent to the first integer, and turn clockwise by degrees equivalent to the second integer". His grand mother says that Sanbonmatsu was standing at the center of town. However, now buildings are crammed side by side and people can not walk along exactly what the paper says in. Your task is to write a program which hunts for the treature on the paper. For simplicity, 1 step is equivalent to 1 meter. Input consists of several pairs of two integers d (the first integer) and t (the second integer) separated by a comma. Input ends with "0, 0". Your program should print the coordinate (x, y) of the end point. There is the treature where x meters to the east and y meters to the north from the center of town. You can assume that d ≤ 100 and -180 ≤ t ≤ 180. Input A sequence of pairs of integers d and t which end with "0,0". Output Print the integer portion of x and y in a line respectively. Example Input 56,65 97,54 64,-4 55,76 42,-27 43,80 87,-86 55,-6 89,34 95,5 0,0 Output 171 -302
instruction
0
8,958
1
17,916
"Correct Solution: ``` from math import sin,cos,pi x = 0 y = 0 ca = 90 while True: d, a = list(map(int, input().split(","))) if d or a: x += d*cos(ca*pi/180) y += d*sin(ca*pi/180) ca -= a else: break print(int(x)) print(int(y)) ```
output
1
8,958
1
17,917
Provide a correct Python 3 solution for this coding contest problem. When a boy was cleaning up after his grand father passing, he found an old paper: <image> In addition, other side of the paper says that "go ahead a number of steps equivalent to the first integer, and turn clockwise by degrees equivalent to the second integer". His grand mother says that Sanbonmatsu was standing at the center of town. However, now buildings are crammed side by side and people can not walk along exactly what the paper says in. Your task is to write a program which hunts for the treature on the paper. For simplicity, 1 step is equivalent to 1 meter. Input consists of several pairs of two integers d (the first integer) and t (the second integer) separated by a comma. Input ends with "0, 0". Your program should print the coordinate (x, y) of the end point. There is the treature where x meters to the east and y meters to the north from the center of town. You can assume that d ≤ 100 and -180 ≤ t ≤ 180. Input A sequence of pairs of integers d and t which end with "0,0". Output Print the integer portion of x and y in a line respectively. Example Input 56,65 97,54 64,-4 55,76 42,-27 43,80 87,-86 55,-6 89,34 95,5 0,0 Output 171 -302
instruction
0
8,959
1
17,918
"Correct Solution: ``` import math import cmath coordinates = 0 ang = 90 while True: line = list(map(float, input().split(","))) if line[0] == 0 and line[1] == 0: break coordinates += cmath.rect(line[0], math.radians(ang)) ang -= line[1] print(int(coordinates.real)) print(int(coordinates.imag)) ```
output
1
8,959
1
17,919
Provide a correct Python 3 solution for this coding contest problem. When a boy was cleaning up after his grand father passing, he found an old paper: <image> In addition, other side of the paper says that "go ahead a number of steps equivalent to the first integer, and turn clockwise by degrees equivalent to the second integer". His grand mother says that Sanbonmatsu was standing at the center of town. However, now buildings are crammed side by side and people can not walk along exactly what the paper says in. Your task is to write a program which hunts for the treature on the paper. For simplicity, 1 step is equivalent to 1 meter. Input consists of several pairs of two integers d (the first integer) and t (the second integer) separated by a comma. Input ends with "0, 0". Your program should print the coordinate (x, y) of the end point. There is the treature where x meters to the east and y meters to the north from the center of town. You can assume that d ≤ 100 and -180 ≤ t ≤ 180. Input A sequence of pairs of integers d and t which end with "0,0". Output Print the integer portion of x and y in a line respectively. Example Input 56,65 97,54 64,-4 55,76 42,-27 43,80 87,-86 55,-6 89,34 95,5 0,0 Output 171 -302
instruction
0
8,960
1
17,920
"Correct Solution: ``` import cmath import math z = 0 + 0j theta = 90 while True: r, phi = map(int, input().split(",")) if r == phi == 0: break z += cmath.rect(r, math.radians(theta)) theta = (theta - phi) % 360 print(math.trunc(z.real)) print(math.trunc(z.imag)) ```
output
1
8,960
1
17,921
Provide a correct Python 3 solution for this coding contest problem. When a boy was cleaning up after his grand father passing, he found an old paper: <image> In addition, other side of the paper says that "go ahead a number of steps equivalent to the first integer, and turn clockwise by degrees equivalent to the second integer". His grand mother says that Sanbonmatsu was standing at the center of town. However, now buildings are crammed side by side and people can not walk along exactly what the paper says in. Your task is to write a program which hunts for the treature on the paper. For simplicity, 1 step is equivalent to 1 meter. Input consists of several pairs of two integers d (the first integer) and t (the second integer) separated by a comma. Input ends with "0, 0". Your program should print the coordinate (x, y) of the end point. There is the treature where x meters to the east and y meters to the north from the center of town. You can assume that d ≤ 100 and -180 ≤ t ≤ 180. Input A sequence of pairs of integers d and t which end with "0,0". Output Print the integer portion of x and y in a line respectively. Example Input 56,65 97,54 64,-4 55,76 42,-27 43,80 87,-86 55,-6 89,34 95,5 0,0 Output 171 -302
instruction
0
8,961
1
17,922
"Correct Solution: ``` # AOJ 0016 Treasure Hunt # Python3 2018.6.15 bal4u import math PI = 3.1415926535897932384626433832795 M = 0.01745329251994329576923690768489 EPS = 1e-8 r, q, f = 0, 90*M, 0 while True: d, a = list(map(int, input().split(','))) if d == 0 and a == 0: break r2 = math.sqrt(r*r + d*d - 2*r*d*math.cos(PI - math.fabs(f))) if math.fabs(r) < EPS or math.fabs(r2) < EPS: q2 = q - f f = a * M else: t = math.acos((r*r + r2*r2 - d*d) / (2 * r*r2)); if f >= 0: q2 = q - t f += a*M - t else: q2 = q + t f += a*M + t r = r2 q = q2 print(int(r * math.cos(q)), int(r * math.sin(q)), sep='\n') ```
output
1
8,961
1
17,923
Provide tags and a correct Python 3 solution for this coding contest problem. Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has n levels, where n is an even number. In the local council you were given an area map, where the granny's house is marked by point H, parts of dense forest are marked grey (see the picture to understand better). After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics: * it starts and ends in the same place — the granny's house; * the route goes along the forest paths only (these are the segments marked black in the picture); * the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway); * the route cannot cross itself; * there shouldn't be any part of dense forest within the part marked out by this route; You should find the amount of such suitable oriented routes modulo 1000000009. <image> The example of the area map for n = 12 is given in the picture. Since the map has a regular structure, you can construct it for other n by analogy using the example. Input The input data contain the only even integer n (2 ≤ n ≤ 106). Output Output the only number — the amount of Peter's routes modulo 1000000009. Examples Input 2 Output 10 Input 4 Output 74
instruction
0
9,331
1
18,662
Tags: combinatorics, dp Correct Solution: ``` __author__ = 'Darren' def solve(): n = int(input()) a, b, c, mod = 2, 2, 4, 1000000009 while n > 2: a = (a << 1) % mod c = c * (a - 3) % mod b = (b + c) % mod n -= 2 print((b * b + 1) * 2 % mod) if __name__ == '__main__': solve() ```
output
1
9,331
1
18,663
Provide tags and a correct Python 3 solution for this coding contest problem. Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has n levels, where n is an even number. In the local council you were given an area map, where the granny's house is marked by point H, parts of dense forest are marked grey (see the picture to understand better). After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics: * it starts and ends in the same place — the granny's house; * the route goes along the forest paths only (these are the segments marked black in the picture); * the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway); * the route cannot cross itself; * there shouldn't be any part of dense forest within the part marked out by this route; You should find the amount of such suitable oriented routes modulo 1000000009. <image> The example of the area map for n = 12 is given in the picture. Since the map has a regular structure, you can construct it for other n by analogy using the example. Input The input data contain the only even integer n (2 ≤ n ≤ 106). Output Output the only number — the amount of Peter's routes modulo 1000000009. Examples Input 2 Output 10 Input 4 Output 74
instruction
0
9,332
1
18,664
Tags: combinatorics, dp Correct Solution: ``` n=int(input()) z=8 y=[] for i in range(n//2-1): y.append((z-3)%1000000009) z*=2 z%=1000000009 if n==2: print(10) else: a=4 for i in range(n//2,2,-1): a*=y[i-3] a+=4 a%=1000000009 a+=2 print(2*(a**2+1)%1000000009) ```
output
1
9,332
1
18,665
Provide tags and a correct Python 3 solution for this coding contest problem. Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has n levels, where n is an even number. In the local council you were given an area map, where the granny's house is marked by point H, parts of dense forest are marked grey (see the picture to understand better). After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics: * it starts and ends in the same place — the granny's house; * the route goes along the forest paths only (these are the segments marked black in the picture); * the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway); * the route cannot cross itself; * there shouldn't be any part of dense forest within the part marked out by this route; You should find the amount of such suitable oriented routes modulo 1000000009. <image> The example of the area map for n = 12 is given in the picture. Since the map has a regular structure, you can construct it for other n by analogy using the example. Input The input data contain the only even integer n (2 ≤ n ≤ 106). Output Output the only number — the amount of Peter's routes modulo 1000000009. Examples Input 2 Output 10 Input 4 Output 74
instruction
0
9,333
1
18,666
Tags: combinatorics, dp Correct Solution: ``` a,b,c,m=1,2,4,10**9+9 n=int(input()) for i in range(1,n//2): c=c*a%m b=(b+c)%m a=(2*a+3)%m print((b*b+1)*2%m) ```
output
1
9,333
1
18,667
Provide tags and a correct Python 3 solution for this coding contest problem. Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has n levels, where n is an even number. In the local council you were given an area map, where the granny's house is marked by point H, parts of dense forest are marked grey (see the picture to understand better). After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics: * it starts and ends in the same place — the granny's house; * the route goes along the forest paths only (these are the segments marked black in the picture); * the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway); * the route cannot cross itself; * there shouldn't be any part of dense forest within the part marked out by this route; You should find the amount of such suitable oriented routes modulo 1000000009. <image> The example of the area map for n = 12 is given in the picture. Since the map has a regular structure, you can construct it for other n by analogy using the example. Input The input data contain the only even integer n (2 ≤ n ≤ 106). Output Output the only number — the amount of Peter's routes modulo 1000000009. Examples Input 2 Output 10 Input 4 Output 74
instruction
0
9,334
1
18,668
Tags: combinatorics, dp Correct Solution: ``` # METO Bot 0.9.9 a,b,c,m=1,2,4,10**9+9 n=int(input()) for i in range(1,n//2): c=c*a%m b=(b+c)%m a=(2*a+3)%m print((b*b+1)*2%m) ```
output
1
9,334
1
18,669
Provide tags and a correct Python 2 solution for this coding contest problem. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5.
instruction
0
9,527
1
19,054
Tags: binary search, math Correct Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations raw_input = stdin.readline from math import ceil pr = stdout.write def in_num(): return int(raw_input()) def in_arr(): return map(int,raw_input().split()) def pr_num(n): stdout.write(str(n)+'\n') def pr_arr(arr): pr(' '.join(map(str,arr))+'\n') # fast read function for total integer input def inp(): # this function returns whole input of # space/line seperated integers # Use Ctrl+D to flush stdin. return map(float,stdin.read().split()) range = xrange # not for python 3.0+ n,l,v1,v2,k=inp() val=ceil(n/k) ans=l/((val*v2)-(((val-1)*v2)*((v2-v1)/(v1+v2)))) ans+=(l-(ans*v2))/v1 pr_num(ans) ```
output
1
9,527
1
19,055
Provide tags and a correct Python 3 solution for this coding contest problem. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5.
instruction
0
9,528
1
19,056
Tags: binary search, math Correct Solution: ``` array = list(map(int,input().split())) n = int(array[0]) l = int(array[1]) v1 = int(array[2]) v2 = int(array[3]) k = int(array[4]) bus = n//k if n%k != 0: bus += 1 V = (v1+v2) + 2*v2*(bus-1) U = (v1+v2) + 2*v1*(bus-1) print (l*V/U/v2) ```
output
1
9,528
1
19,057
Provide tags and a correct Python 3 solution for this coding contest problem. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5.
instruction
0
9,529
1
19,058
Tags: binary search, math Correct Solution: ``` n, l, v1, v2, k = map(int, input().split()) diff = v2 - v1 n = (n + k - 1) // k * 2 p1 = (n * v2 - diff) * l p2 = (n * v1 + diff) * v2 print(p1 / p2) ```
output
1
9,529
1
19,059
Provide tags and a correct Python 3 solution for this coding contest problem. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5.
instruction
0
9,530
1
19,060
Tags: binary search, math Correct Solution: ``` #coding=utf-8 import sys eps = 1e-6 def solve(n, l, v1, v2): t2 = 1.0 * (v1 + v2) * l / (n * (v1 + v2) * v2 - (n - 1) * v2 * (v2 - v1)) l2 = v2 * t2 l1 = l - l2 t1 = l1 / v1 #print(t1, l1, t2, l2) return t1 + t2 #print(solve(3, 6, 1, 2)) #print(solve(1, 10, 1, 2)) n, t, v1, v2, k = map(int, input().split()) n = (n + k - 1) // k print(solve(n, t, v1, v2)) ```
output
1
9,530
1
19,061
Provide tags and a correct Python 3 solution for this coding contest problem. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5.
instruction
0
9,531
1
19,062
Tags: binary search, math Correct Solution: ``` n, L, v1, v2, k = map(int, input().split()) n = (n + k - 1) // k * 2 dif = v2 - v1 p1 = (n * v2 - dif) * L p2 = (n * v1 + dif) * v2 ans = p1 / p2 print(ans) ```
output
1
9,531
1
19,063
Provide tags and a correct Python 3 solution for this coding contest problem. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5.
instruction
0
9,532
1
19,064
Tags: binary search, math Correct Solution: ``` #!/usr/local/bin/python3 # -*- coding:utf-8 -*- import math inputParams = input().split() n = int(inputParams[0]) l = int(inputParams[1]) v1 = int(inputParams[2]) v2 = int(inputParams[3]) k = int(inputParams[4]) # 运送次数 times = math.ceil(n / k) t1 = l / (v2 + (2 * v2 / (v2 + v1)) * v1 * (times - 1)) totalTime = t1 + (2 * v2 / (v1 + v2)) * t1 * (times - 1) print(str(totalTime)) ```
output
1
9,532
1
19,065
Provide tags and a correct Python 3 solution for this coding contest problem. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5.
instruction
0
9,533
1
19,066
Tags: binary search, math Correct Solution: ``` n, l, v1, v2, k = [int(x) for x in input().split()] x = (n+k-1)//k print((l - ((l/v1)/((x/(v2-v1)) + ((x-1)/(v2+v1)) + (1/v1))))/v1) ```
output
1
9,533
1
19,067
Provide tags and a correct Python 3 solution for this coding contest problem. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5.
instruction
0
9,534
1
19,068
Tags: binary search, math Correct Solution: ``` n, l, v1, v2, k = list(map(int, input().split())) n = (n+k-1)//k t = l / ((v1 + (v2 - v1) * v1 / (v1 + v2)) * (n - 1) + v2) print(t * n + t * (v2 - v1) / (v1 + v2) * (n - 1)) ```
output
1
9,534
1
19,069
Provide tags and a correct Python 3 solution for this coding contest problem. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5.
instruction
0
9,535
1
19,070
Tags: binary search, math Correct Solution: ``` n, L, v1, v2, k = map(int, input().split()) n = (n + k - 1) // k * 2 dif = v2 - v1 p1 = (n * v2 - dif) * L p2 = (n * v1 + dif) * v2 ans = p1 / p2 print('%.7f' % ans) ```
output
1
9,535
1
19,071
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5. Submitted Solution: ``` n,l,v1,v2,k=map(int,input().split()) m=(n-1)//k+1 v=v1+v2 x=l*v*v2/(m*v*v2-(m-1)*(v2-v1)*v2) print(x/v2+(l-x)/v1) ```
instruction
0
9,536
1
19,072
Yes
output
1
9,536
1
19,073
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5. Submitted Solution: ``` s=input(); li=s.split(); n=int(li[0]) l=int(li[1]) v1=int(li[2]) v2=int(li[3]) k=int(li[4]) t=n//k if n%k!=0: t+=1 a=(v1+v2)*l/(v1+v2+2*(t-1)*v1) ans=a/v2+(l-a)/v1 print("{0:.10f}".format(ans)) ```
instruction
0
9,537
1
19,074
Yes
output
1
9,537
1
19,075
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5. Submitted Solution: ``` #!/usr/local/bin/python3 # -*- coding:utf-8 -*- import math # inputParams = input().split() # n = int(inputParams[0]) # l = int(inputParams[1]) # v1 = int(inputParams[2]) # v2 = int(inputParams[3]) # k = int(inputParams[4]) n,l,v1,v2,k = map(int,input().split()) # 运送次数 times = math.ceil(n / k) t1 = l / (v2 + (2 * v2 / (v2 + v1)) * v1 * (times - 1)) totalTime = t1 + (2 * v2 / (v1 + v2)) * t1 * (times - 1) print(str(totalTime)) ```
instruction
0
9,538
1
19,076
Yes
output
1
9,538
1
19,077
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5. Submitted Solution: ``` n, l, a, b, k = map(int, input().split()) m = n // k if n % k != 0: m += 1 A = (b + a) + 2 * b * (m - 1) B = (b + a) + 2 * a * (m - 1) print((l * A) / (b * B)) ```
instruction
0
9,539
1
19,078
Yes
output
1
9,539
1
19,079
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5. Submitted Solution: ``` # -*- coding: utf-8 -*- """ Created on Tue Jul 26 15:20:38 2016 @author: pinku """ import sys bkpstdin = sys.stdin #sys.stdin = open('in.txt','r') n,l,v1,v2,k = map(int,input().split(' ')) o = n/k if n%k == 1: o+=1 d = 1.0*o ans = l*( ( (d*2.0) - 1.0)*v2 + v1) ans /= (v2 + ( (d*2.0) - 1.0)*v1) ans /= v2 print(ans) sys.stdin = bkpstdin ```
instruction
0
9,540
1
19,080
No
output
1
9,540
1
19,081
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5. Submitted Solution: ``` n, l, v1, v2, k = list(map(int, input().split())) p = [0]*(l+1) p[1] = n i = l t = (n+k-1)/k low = 0.0 hi = 1e9+10 for i in range(100): t1 = (hi+low)*0.5 len = t1 * t * v2 - (t-1) * v2 * t1 * (v2 - v1) / (v1 + v2) if len>l: hi = t1 else: low = t1 print(t1+(l-t1*v2)/v1) ```
instruction
0
9,541
1
19,082
No
output
1
9,541
1
19,083
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5. Submitted Solution: ``` n,l,v1,v2,k=map(int,input().split()) m=(n-1)//k+1 v=v1+v2 x=l*v/(m*v-(m-1)*v1) print(x/v2+(l-x)/v1) ```
instruction
0
9,542
1
19,084
No
output
1
9,542
1
19,085
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once. Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. Input The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. Output Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6. Examples Input 5 10 1 2 5 Output 5.0000000000 Input 3 6 1 2 1 Output 4.7142857143 Note In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5. Submitted Solution: ``` def binSearch(n, L, v1, v2, k): l = 0 r = L while r-l > 0.000001: d = (l+r)/2 td = d/v1 S = v2*td g = d-S tb = g/(v1+v2) b = v1*tb if n%k == 0: a = n//k else: a = n//k + 1 if a*d-b*(a-1) > L: r = d else: l = d td = l / v1 S = v2 * td g = l - S tb = g / (v1 + v2) b = v1 * tb #return (a*td+(a-1)*tb) return (a * d + (a - 1) * b) / v1 n1, L1, v11, v21, k1 = map(int, input().split()) print('{0:.10f}'.format(binSearch(n1, L1, v21, v11, k1))) ```
instruction
0
9,543
1
19,086
No
output
1
9,543
1
19,087
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and m two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible that there is no way to get from one city to some other city using only these roads. The road minister decided to make a reform in Berland and to orient all roads in the country, i.e. to make each road one-way. The minister wants to maximize the number of cities, for which the number of roads that begins in the city equals to the number of roads that ends in it. Input The first line contains a positive integer t (1 ≤ t ≤ 200) — the number of testsets in the input. Each of the testsets is given in the following way. The first line contains two integers n and m (1 ≤ n ≤ 200, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and the number of roads in Berland. The next m lines contain the description of roads in Berland. Each line contains two integers u and v (1 ≤ u, v ≤ n) — the cities the corresponding road connects. It's guaranteed that there are no self-loops and multiple roads. It is possible that there is no way along roads between a pair of cities. It is guaranteed that the total number of cities in all testset of input data doesn't exceed 200. Pay attention that for hacks, you can only use tests consisting of one testset, so t should be equal to one. Output For each testset print the maximum number of such cities that the number of roads that begins in the city, is equal to the number of roads that ends in it. In the next m lines print oriented roads. First print the number of the city where the road begins and then the number of the city where the road ends. If there are several answers, print any of them. It is allowed to print roads in each test in arbitrary order. Each road should be printed exactly once. Example Input 2 5 5 2 1 4 5 2 3 1 3 3 5 7 2 3 7 4 2 Output 3 1 3 3 5 5 4 3 2 2 1 3 2 4 3 7
instruction
0
9,544
1
19,088
Tags: constructive algorithms, dfs and similar, flows, graphs, greedy Correct Solution: ``` def hierholzer(graph): edges = [] for node in graph: for neighbour in graph[node]: if graph[node][neighbour] is False: continue stack = [node] parents = defaultdict(list) parents[node] = [] while (len(stack) != 0): current = stack.pop() seen = False for neighbour in graph[current]: if graph[current][neighbour] is False: continue stack.append(current) stack.append(neighbour) graph[current][neighbour] = False graph[neighbour][current] = False parents[neighbour].append(current) seen = True break if not seen and len(parents[current])!=0: edges.append((parents[current].pop(), current)) return edges import sys sys.setrecursionlimit(5000) t = int(input()) from collections import defaultdict for test_number in range(t): n, m = map(int, input().split()) graph = defaultdict(dict) for _ in range(m): u, v = map(int, input().split()) graph[u][v] = True graph[v][u] = True if m == 0: print(n) continue odds = [] for u in graph: if len(graph[u]) % 2 == 1: odds.append(u) # now every node has an even degree for odd in odds: graph[odd][n + 1] = True graph[n + 1][odd] = True # we just do a dfs edges = hierholzer(graph) print(n - len(odds)) for edge in edges: if edge[0] != n + 1 and edge[1] != n + 1: print("{0} {1}".format(edge[0], edge[1])) ```
output
1
9,544
1
19,089
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and m two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible that there is no way to get from one city to some other city using only these roads. The road minister decided to make a reform in Berland and to orient all roads in the country, i.e. to make each road one-way. The minister wants to maximize the number of cities, for which the number of roads that begins in the city equals to the number of roads that ends in it. Input The first line contains a positive integer t (1 ≤ t ≤ 200) — the number of testsets in the input. Each of the testsets is given in the following way. The first line contains two integers n and m (1 ≤ n ≤ 200, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and the number of roads in Berland. The next m lines contain the description of roads in Berland. Each line contains two integers u and v (1 ≤ u, v ≤ n) — the cities the corresponding road connects. It's guaranteed that there are no self-loops and multiple roads. It is possible that there is no way along roads between a pair of cities. It is guaranteed that the total number of cities in all testset of input data doesn't exceed 200. Pay attention that for hacks, you can only use tests consisting of one testset, so t should be equal to one. Output For each testset print the maximum number of such cities that the number of roads that begins in the city, is equal to the number of roads that ends in it. In the next m lines print oriented roads. First print the number of the city where the road begins and then the number of the city where the road ends. If there are several answers, print any of them. It is allowed to print roads in each test in arbitrary order. Each road should be printed exactly once. Example Input 2 5 5 2 1 4 5 2 3 1 3 3 5 7 2 3 7 4 2 Output 3 1 3 3 5 5 4 3 2 2 1 3 2 4 3 7
instruction
0
9,545
1
19,090
Tags: constructive algorithms, dfs and similar, flows, graphs, greedy Correct Solution: ``` t = int(input()) from collections import defaultdict for test_number in range(t): n, m = map(int, input().split()) graph = defaultdict(dict) for _ in range(m): u, v = map(int, input().split()) graph[u][v] = True graph[v][u] = True if m == 0: print(n) continue odds = [] for u in graph: if len(graph[u]) % 2 == 1: odds.append(u) # now every node has an even degree for odd in odds: graph[odd][n + 1] = True graph[n + 1][odd] = True # we just do a dfs edges = [] # we don't have a visited dictionary as usual, because we must allow to visit a node several times # but we do delete the edges for node in graph: for neighbour in graph[node]: if graph[node][neighbour] is False: continue stack = [node] while (len(stack) != 0): current = stack.pop() for neighbour in graph[current]: if graph[current][neighbour] is False: continue stack.append(current) stack.append(neighbour) graph[current][neighbour] = False graph[neighbour][current] = False edges.append((current, neighbour)) break print(n - len(odds)) for edge in edges: if edge[0] != n + 1 and edge[1] != n + 1: print("{0} {1}".format(edge[0], edge[1])) ```
output
1
9,545
1
19,091
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and m two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible that there is no way to get from one city to some other city using only these roads. The road minister decided to make a reform in Berland and to orient all roads in the country, i.e. to make each road one-way. The minister wants to maximize the number of cities, for which the number of roads that begins in the city equals to the number of roads that ends in it. Input The first line contains a positive integer t (1 ≤ t ≤ 200) — the number of testsets in the input. Each of the testsets is given in the following way. The first line contains two integers n and m (1 ≤ n ≤ 200, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and the number of roads in Berland. The next m lines contain the description of roads in Berland. Each line contains two integers u and v (1 ≤ u, v ≤ n) — the cities the corresponding road connects. It's guaranteed that there are no self-loops and multiple roads. It is possible that there is no way along roads between a pair of cities. It is guaranteed that the total number of cities in all testset of input data doesn't exceed 200. Pay attention that for hacks, you can only use tests consisting of one testset, so t should be equal to one. Output For each testset print the maximum number of such cities that the number of roads that begins in the city, is equal to the number of roads that ends in it. In the next m lines print oriented roads. First print the number of the city where the road begins and then the number of the city where the road ends. If there are several answers, print any of them. It is allowed to print roads in each test in arbitrary order. Each road should be printed exactly once. Example Input 2 5 5 2 1 4 5 2 3 1 3 3 5 7 2 3 7 4 2 Output 3 1 3 3 5 5 4 3 2 2 1 3 2 4 3 7
instruction
0
9,546
1
19,092
Tags: constructive algorithms, dfs and similar, flows, graphs, greedy Correct Solution: ``` import sys from collections import defaultdict rlines = sys.stdin.readlines() lines = (l.strip() for l in rlines) def eucycle(n,m,adj): dir_edges = [] us = list(adj.keys()) for u in us: while adj[u]: v0 = u v1 = adj[v0].pop() adj[v1].remove(v0) dir_edges.append((v0, v1)) while v1 != u: v0 = v1 v1 = adj[v0].pop() adj[v1].remove(v0) dir_edges.append((v0, v1)) return dir_edges def solve(n,m,edges): adj = defaultdict(set) for u, v in edges: adj[u].add(v) adj[v].add(u) odds = set(u for u in adj if len(adj[u])%2==1) for odd in odds: adj[odd].add(n+1) adj[n+1].add(odd) total = n - len(odds) dir_edges = eucycle(n+1, m, adj) return total, dir_edges t = int(next(lines)) for ti in range(t): n,m = [int(s) for s in next(lines).split()] edges = [] for ei in range(m): u,v = [int(s) for s in next(lines).split()] edges.append((u,v)) total, ans = solve(n,m,edges) print(total) print('\n'.join(str(u)+ ' '+ str(v) for u, v in ans if u != n+1 and v!= n+1)) ```
output
1
9,546
1
19,093
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and m two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible that there is no way to get from one city to some other city using only these roads. The road minister decided to make a reform in Berland and to orient all roads in the country, i.e. to make each road one-way. The minister wants to maximize the number of cities, for which the number of roads that begins in the city equals to the number of roads that ends in it. Input The first line contains a positive integer t (1 ≤ t ≤ 200) — the number of testsets in the input. Each of the testsets is given in the following way. The first line contains two integers n and m (1 ≤ n ≤ 200, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and the number of roads in Berland. The next m lines contain the description of roads in Berland. Each line contains two integers u and v (1 ≤ u, v ≤ n) — the cities the corresponding road connects. It's guaranteed that there are no self-loops and multiple roads. It is possible that there is no way along roads between a pair of cities. It is guaranteed that the total number of cities in all testset of input data doesn't exceed 200. Pay attention that for hacks, you can only use tests consisting of one testset, so t should be equal to one. Output For each testset print the maximum number of such cities that the number of roads that begins in the city, is equal to the number of roads that ends in it. In the next m lines print oriented roads. First print the number of the city where the road begins and then the number of the city where the road ends. If there are several answers, print any of them. It is allowed to print roads in each test in arbitrary order. Each road should be printed exactly once. Example Input 2 5 5 2 1 4 5 2 3 1 3 3 5 7 2 3 7 4 2 Output 3 1 3 3 5 5 4 3 2 2 1 3 2 4 3 7
instruction
0
9,547
1
19,094
Tags: constructive algorithms, dfs and similar, flows, graphs, greedy Correct Solution: ``` import sys import collections rlines = sys.stdin.readlines() lines = (l.strip() for l in rlines) def eucycle(n,m,adj): diredges = [] us = list(adj.keys()) for u in us: while adj[u]: v0 = u v1 = adj[v0].pop() adj[v1].remove(v0) diredges.append((v0,v1)) while v1 != u: v0 = v1 v1 = adj[v0].pop() adj[v1].remove(v0) diredges.append((v0,v1)) return diredges def solve(n,m,edges): adj = collections.defaultdict(set) diredges = [] for u,v in edges: adj[u].add(v) adj[v].add(u) odds = set(u for u in adj if len(adj[u])%2 == 1) ans = n - len(odds) assert(len(odds)%2 == 0) for o in odds: adj[n+1].add(o) adj[o].add(n+1) diredges = eucycle(n+1,m,adj) return str(ans) + '\n' + '\n'.join(str(u) + ' ' + str(v) for (u,v) in diredges\ if u != n+1 and v != n+1) t = int(next(lines)) for ti in range(t): n,m = [int(s) for s in next(lines).split()] edges = [] for ei in range(m): u,v = [int(s) for s in next(lines).split()] edges.append((u,v)) #print(edges) print(solve(n,m,edges)) ```
output
1
9,547
1
19,095
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and m two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible that there is no way to get from one city to some other city using only these roads. The road minister decided to make a reform in Berland and to orient all roads in the country, i.e. to make each road one-way. The minister wants to maximize the number of cities, for which the number of roads that begins in the city equals to the number of roads that ends in it. Input The first line contains a positive integer t (1 ≤ t ≤ 200) — the number of testsets in the input. Each of the testsets is given in the following way. The first line contains two integers n and m (1 ≤ n ≤ 200, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and the number of roads in Berland. The next m lines contain the description of roads in Berland. Each line contains two integers u and v (1 ≤ u, v ≤ n) — the cities the corresponding road connects. It's guaranteed that there are no self-loops and multiple roads. It is possible that there is no way along roads between a pair of cities. It is guaranteed that the total number of cities in all testset of input data doesn't exceed 200. Pay attention that for hacks, you can only use tests consisting of one testset, so t should be equal to one. Output For each testset print the maximum number of such cities that the number of roads that begins in the city, is equal to the number of roads that ends in it. In the next m lines print oriented roads. First print the number of the city where the road begins and then the number of the city where the road ends. If there are several answers, print any of them. It is allowed to print roads in each test in arbitrary order. Each road should be printed exactly once. Example Input 2 5 5 2 1 4 5 2 3 1 3 3 5 7 2 3 7 4 2 Output 3 1 3 3 5 5 4 3 2 2 1 3 2 4 3 7
instruction
0
9,548
1
19,096
Tags: constructive algorithms, dfs and similar, flows, graphs, greedy Correct Solution: ``` from collections import defaultdict, Counter T = int(input()) for _ in range(T): global lst N, M = map(int, input().split()) visit = set() oddNodes = [] directed = [] G = defaultdict(list) oriG = defaultdict(list) Gra = [[0] * (N+1) for _ in range(N+1)] deg = [0] * (N+1) for k in range(M): u, v = map(int, input().split()) Gra[u][v] += 1 Gra[v][u] += 1 deg[u] += 1 deg[v] += 1 # G[u].append(v) # G[v].append(u) # oriG[u].append(v) # oriG[v].append(u) ans = 0 for i in range(1, N+1): if deg[i] % 2 == 0: ans += 1 else: oddNodes.append(i) for i in range(0, len(oddNodes), 2): # G[oddNodes[i]].append(oddNodes[i+1]) # G[oddNodes[i+1]].append(oddNodes[i]) Gra[oddNodes[i]][oddNodes[i+1]] += 1 Gra[oddNodes[i+1]][oddNodes[i]] += 1 deg[oddNodes[i]] += 1 deg[oddNodes[i+1]] += 1 def eulerPath(u): stk = [u] while stk: u = stk.pop() for i in range(1, N+1): if Gra[u][i]: Gra[u][i] -= 1 Gra[i][u] -= 1 directed.append((u, i)) stk.append(i) break for i in range(1, N+1): eulerPath(i) for i in range(0, len(oddNodes), 2): # G[oddNodes[i]].append(oddNodes[i+1]) # G[oddNodes[i+1]].append(oddNodes[i]) Gra[oddNodes[i]][oddNodes[i+1]] += 1 Gra[oddNodes[i+1]][oddNodes[i]] += 1 # for i in oddNodes: print(ans) for u, v in directed: if Gra[u][v] != 0: Gra[u][v] -= 1 Gra[v][u] -= 1 else: print(str(u) + " " + str(v)) ```
output
1
9,548
1
19,097
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and m two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible that there is no way to get from one city to some other city using only these roads. The road minister decided to make a reform in Berland and to orient all roads in the country, i.e. to make each road one-way. The minister wants to maximize the number of cities, for which the number of roads that begins in the city equals to the number of roads that ends in it. Input The first line contains a positive integer t (1 ≤ t ≤ 200) — the number of testsets in the input. Each of the testsets is given in the following way. The first line contains two integers n and m (1 ≤ n ≤ 200, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and the number of roads in Berland. The next m lines contain the description of roads in Berland. Each line contains two integers u and v (1 ≤ u, v ≤ n) — the cities the corresponding road connects. It's guaranteed that there are no self-loops and multiple roads. It is possible that there is no way along roads between a pair of cities. It is guaranteed that the total number of cities in all testset of input data doesn't exceed 200. Pay attention that for hacks, you can only use tests consisting of one testset, so t should be equal to one. Output For each testset print the maximum number of such cities that the number of roads that begins in the city, is equal to the number of roads that ends in it. In the next m lines print oriented roads. First print the number of the city where the road begins and then the number of the city where the road ends. If there are several answers, print any of them. It is allowed to print roads in each test in arbitrary order. Each road should be printed exactly once. Example Input 2 5 5 2 1 4 5 2 3 1 3 3 5 7 2 3 7 4 2 Output 3 1 3 3 5 5 4 3 2 2 1 3 2 4 3 7 Submitted Solution: ``` def walk(v,reverse): for u in road[v]: if not used[v][u]: ## print(v,u,reverse) used[v][u] = True used[u][v] = True if not reverse: out[v] += 1 into[u] += 1 res.append((v+1,u+1)) else: into[v] += 1 out[u] += 1 res.append((u+1,v+1)) walk(u,reverse) reverse = not reverse return t = int(input()) for test in range(t): n, m = [int(i) for i in input().split()] road = [[] for i in range(n)] for i in range(m): a, b = [int(j)-1 for j in input().split()] road[a].append(b) road[b].append(a) used = [[False for i in range(n)] for j in range(n)] into = [0] * n out = [0] * n res = [] for v in range(n): if len(road[v]) % 2 == 1: walk(v,False) for v in range(n): walk(v,False) cnt = 0 for i in range(n): if into[i] == out[i]: cnt += 1 print(cnt) for i in range(len(res)): print(res[i][0],res[i][1]) ```
instruction
0
9,549
1
19,098
No
output
1
9,549
1
19,099
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and m two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible that there is no way to get from one city to some other city using only these roads. The road minister decided to make a reform in Berland and to orient all roads in the country, i.e. to make each road one-way. The minister wants to maximize the number of cities, for which the number of roads that begins in the city equals to the number of roads that ends in it. Input The first line contains a positive integer t (1 ≤ t ≤ 200) — the number of testsets in the input. Each of the testsets is given in the following way. The first line contains two integers n and m (1 ≤ n ≤ 200, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and the number of roads in Berland. The next m lines contain the description of roads in Berland. Each line contains two integers u and v (1 ≤ u, v ≤ n) — the cities the corresponding road connects. It's guaranteed that there are no self-loops and multiple roads. It is possible that there is no way along roads between a pair of cities. It is guaranteed that the total number of cities in all testset of input data doesn't exceed 200. Pay attention that for hacks, you can only use tests consisting of one testset, so t should be equal to one. Output For each testset print the maximum number of such cities that the number of roads that begins in the city, is equal to the number of roads that ends in it. In the next m lines print oriented roads. First print the number of the city where the road begins and then the number of the city where the road ends. If there are several answers, print any of them. It is allowed to print roads in each test in arbitrary order. Each road should be printed exactly once. Example Input 2 5 5 2 1 4 5 2 3 1 3 3 5 7 2 3 7 4 2 Output 3 1 3 3 5 5 4 3 2 2 1 3 2 4 3 7 Submitted Solution: ``` t = int(input()) from collections import defaultdict for _ in range(t): n, m = map(int, input().split()) graph = defaultdict(dict) for _ in range(m): u, v = map(int, input().split()) graph[u][v] = True graph[v][u] = True if m==0: print(n) continue odds = [] for u in graph: if len(graph[u]) % 2 == 1: odds.append(u) # now every node has an even degree for odd in odds: graph[odd][n + 1] = True graph[n + 1][odd] = True # we just do a dfs edges = [] stack = [list(graph.keys())[0]] # we don't have a visited dictionary as usual, because we must allow to visit a node several times # but we do delete the edges while (len(stack) != 0): current = stack.pop() for neighbour in graph[current]: if graph[current][neighbour] is False: continue stack.append(current) stack.append(neighbour) graph[current][neighbour] = False graph[neighbour][current] = False edges.append((current, neighbour)) break print(n - len(odds)) for edge in edges: if edge[0] != n + 1 and edge[1] != n + 1: print("{0} {1}".format(edge[0], edge[1])) ```
instruction
0
9,550
1
19,100
No
output
1
9,550
1
19,101
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and m two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible that there is no way to get from one city to some other city using only these roads. The road minister decided to make a reform in Berland and to orient all roads in the country, i.e. to make each road one-way. The minister wants to maximize the number of cities, for which the number of roads that begins in the city equals to the number of roads that ends in it. Input The first line contains a positive integer t (1 ≤ t ≤ 200) — the number of testsets in the input. Each of the testsets is given in the following way. The first line contains two integers n and m (1 ≤ n ≤ 200, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and the number of roads in Berland. The next m lines contain the description of roads in Berland. Each line contains two integers u and v (1 ≤ u, v ≤ n) — the cities the corresponding road connects. It's guaranteed that there are no self-loops and multiple roads. It is possible that there is no way along roads between a pair of cities. It is guaranteed that the total number of cities in all testset of input data doesn't exceed 200. Pay attention that for hacks, you can only use tests consisting of one testset, so t should be equal to one. Output For each testset print the maximum number of such cities that the number of roads that begins in the city, is equal to the number of roads that ends in it. In the next m lines print oriented roads. First print the number of the city where the road begins and then the number of the city where the road ends. If there are several answers, print any of them. It is allowed to print roads in each test in arbitrary order. Each road should be printed exactly once. Example Input 2 5 5 2 1 4 5 2 3 1 3 3 5 7 2 3 7 4 2 Output 3 1 3 3 5 5 4 3 2 2 1 3 2 4 3 7 Submitted Solution: ``` import random import copy import math #globals k_max = 10000 class State: def __init__(self, edges, n, degree=None, score=None): self.edges = edges self.n = n if degree is None: self.degree = [0 for _ in range(n+1)] for a, b in edges: self.degree[a] -= 1 self.degree[b] += 1 self.score = 0 for v in self.degree[1:]: if v == 0: self.score += 1 else: self.degree = degree self.score = score def score(state): return state.score def get_neighbour(state): rindex = random.randint(0, len(state.edges)-1) a, b = state.edges[rindex] cp = State(state.edges[::], state.n, degree=state.degree[::], score=state.score) if cp.degree[a] == 0: cp.score -= 1 if cp.degree[b] == 0: cp.score -= 1 cp.degree[a] += 2 cp.degree[b] -= 2 if cp.degree[a] == 0: cp.score += 1 if cp.degree[b] == 0: cp.score += 1 cp.edges[rindex] = (b, a) return cp def probability(diff, temperature): # prob func if diff >= 0: return 1 else: return math.exp(2*diff/temperature) def adjust_state(state, temperature): #cur = score(state) while True: n = get_neighbour(state) #print(score(n)) #print(probability(score(n)-score(state), temperature)) if probability(score(n)-score(state), temperature) >= random.random(): return n def temperature_schedule(v): # temp sched global k_max c = 100 return c*(0.99)**v t = int (input()) for _ in range(t): n, m = map(int, input().split(' ')) edges = [] for __ in range(m): edges.append(tuple(map(int, input().split(' ')))) state = State(edges, n) #print(state.score) #print('-----------') i = 0 temp = temperature_schedule(i) while temp > 1: state = adjust_state(state, temp) i += 1 temp = temperature_schedule(i) #print(state.degree) #print(state.score) #rint('----------') #print(temp) print(state.score) for edge in state.edges: print(edge[0], edge[1]) #print(state.edges) #print(State(state.edges, n).score) ```
instruction
0
9,551
1
19,102
No
output
1
9,551
1
19,103
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and m two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible that there is no way to get from one city to some other city using only these roads. The road minister decided to make a reform in Berland and to orient all roads in the country, i.e. to make each road one-way. The minister wants to maximize the number of cities, for which the number of roads that begins in the city equals to the number of roads that ends in it. Input The first line contains a positive integer t (1 ≤ t ≤ 200) — the number of testsets in the input. Each of the testsets is given in the following way. The first line contains two integers n and m (1 ≤ n ≤ 200, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and the number of roads in Berland. The next m lines contain the description of roads in Berland. Each line contains two integers u and v (1 ≤ u, v ≤ n) — the cities the corresponding road connects. It's guaranteed that there are no self-loops and multiple roads. It is possible that there is no way along roads between a pair of cities. It is guaranteed that the total number of cities in all testset of input data doesn't exceed 200. Pay attention that for hacks, you can only use tests consisting of one testset, so t should be equal to one. Output For each testset print the maximum number of such cities that the number of roads that begins in the city, is equal to the number of roads that ends in it. In the next m lines print oriented roads. First print the number of the city where the road begins and then the number of the city where the road ends. If there are several answers, print any of them. It is allowed to print roads in each test in arbitrary order. Each road should be printed exactly once. Example Input 2 5 5 2 1 4 5 2 3 1 3 3 5 7 2 3 7 4 2 Output 3 1 3 3 5 5 4 3 2 2 1 3 2 4 3 7 Submitted Solution: ``` import sys from collections import defaultdict, Counter class Graph: def __init__(self, vertices): self.vertices = vertices self.graph = defaultdict(list) def add_edge(self, a, b): self.graph[a].append(b) self.graph[b].append(a) def eulerPath(self): g = self.graph odd = [v for v in g if len(g[v])&1] if not odd: odd = [list(graph.keys())[0]] start = odd[-1] path = [start] circuit = [] while path: u = path[-1] if g[u]: v = g[u][0] del g[u][0] del g[v][g[v].index(u)] path.append(v) else: circuit.append(path.pop()) circuit.reverse() return circuit t = int(sys.stdin.readline()) for _ in range(t): n, m = map(int, sys.stdin.readline().split()) g = Graph(n) u = [] v = [] for i in range(m): a, b = map(int, sys.stdin.readline().split()) g.add_edge(a, b) u.append(a) v.append(b) total = 0 for i in range(1, n+1): if len(g.graph[i])%2==0: total += 1 ans = g.eulerPath() temp = defaultdict(defaultdict) for i in range(len(ans)-1): temp[ans[i]][ans[i+1]] = True print(total) for i in range(len(u)): a, b = u[i], v[i] if a in temp and b in temp[a]: print(a, b) else: print(b, a) ```
instruction
0
9,552
1
19,104
No
output
1
9,552
1
19,105
Provide tags and a correct Python 3 solution for this coding contest problem. The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: 1. For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi = i; 2. For each station i there exists exactly one station j such that pj = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of pi for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! Input The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p1, p2, ..., pn (1 ≤ pi ≤ n) — the current structure of the subway. All these numbers are distinct. Output Print one number — the maximum possible value of convenience. Examples Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 Note In the first example the mayor can change p2 to 3 and p3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p2 to 4 and p3 to 5.
instruction
0
9,616
1
19,232
Tags: dfs and similar, greedy, math Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) if(n==1):print('1'),exit() if(n==2):print('4'),exit() b=[i for i in range(0,n+1)] r=[1 for i in range(0,n+1)] def find(x): if b[x]==x: return x else: b[x]=find(b[x]) return b[x] for i in range(1,n+1): x,y=find(i),find(a[i-1]) if x==y:continue if(r[x]>=r[y]): r[x]+=r[y] r[y]=0 b[y]=x else: r[y] += r[x] r[x]=0 b[x] = y k=0 for i in range(1,n+1): k+=r[i]**2 mx1=max(r[2],r[1]) mx2=min(r[2],r[1]) for i in range(3,n+1): if r[i]>mx1: mx2=mx1 mx1=r[i] elif r[i]>mx2: mx2=r[i] #print(k,mx1,mx2) k-=mx1**2+mx2**2 k+=(mx1+mx2)**2 print(k) ```
output
1
9,616
1
19,233
Provide tags and a correct Python 3 solution for this coding contest problem. The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: 1. For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi = i; 2. For each station i there exists exactly one station j such that pj = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of pi for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! Input The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p1, p2, ..., pn (1 ≤ pi ≤ n) — the current structure of the subway. All these numbers are distinct. Output Print one number — the maximum possible value of convenience. Examples Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 Note In the first example the mayor can change p2 to 3 and p3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p2 to 4 and p3 to 5.
instruction
0
9,617
1
19,234
Tags: dfs and similar, greedy, math Correct Solution: ``` input() l = [[int(x)-1,False] for x in input().split()] loop = [] for begin in l: if begin[1]: continue count = 0; nextI = begin[0]; while not l[nextI][1]: l[nextI][1]=True nextI = l[nextI][0] count += 1 loop.append(count) s = sorted(loop,reverse=True) total = sum(map(lambda x:x*x,s)) + (2*s[0]*s[1] if len(s)>=2 else 0) print(total) ```
output
1
9,617
1
19,235
Provide tags and a correct Python 3 solution for this coding contest problem. The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: 1. For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi = i; 2. For each station i there exists exactly one station j such that pj = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of pi for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! Input The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p1, p2, ..., pn (1 ≤ pi ≤ n) — the current structure of the subway. All these numbers are distinct. Output Print one number — the maximum possible value of convenience. Examples Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 Note In the first example the mayor can change p2 to 3 and p3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p2 to 4 and p3 to 5.
instruction
0
9,618
1
19,236
Tags: dfs and similar, greedy, math Correct Solution: ``` n=int(input()) p=[0]+list(map(int,input().split())) vis=[0]*(n+1) part=[] for i in range(1,n+1): if not vis[i]: tot=0 x=i while not vis[x]: tot+=1 vis[x]=1 x=p[x] part.append(tot) part.sort(reverse=True) if len(part)==1: print(n*n) else: ans=(part[0]+part[1])**2 for x in part[2:]: ans+=x*x print(ans) ```
output
1
9,618
1
19,237
Provide tags and a correct Python 3 solution for this coding contest problem. The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: 1. For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi = i; 2. For each station i there exists exactly one station j such that pj = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of pi for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! Input The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p1, p2, ..., pn (1 ≤ pi ≤ n) — the current structure of the subway. All these numbers are distinct. Output Print one number — the maximum possible value of convenience. Examples Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 Note In the first example the mayor can change p2 to 3 and p3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p2 to 4 and p3 to 5.
instruction
0
9,619
1
19,238
Tags: dfs and similar, greedy, math Correct Solution: ``` from itertools import product def solve(): stations = int(input()) arr = [int(x) - 1 for x in input().split(" ")] cycles = [] discovered = {} # All the nodes already traversed for i in arr: # Already part of a cycle found if i in discovered: continue count = 1 dest = arr[i] path = [dest] discovered[dest] = 1 # While still have nodes that are reachable while dest != i: count += 1 dest = arr[dest] path.append(dest) discovered[dest] = 1 cycles.append(path) # The whole graph is reachable if len(cycles) == 1: print(stations * stations) return # Swap destination stations for two points in two chains longest = sorted(cycles, key=len)[len(cycles) - 2:] joined = longest[0] + longest[1] cycles = sorted(cycles, key=len)[:len(cycles) - 2] cycles.append(joined) # Find total amount of combinations total = 0 for cycle in cycles: total += len(cycle) ** 2 print(total) solve() ```
output
1
9,619
1
19,239
Provide tags and a correct Python 3 solution for this coding contest problem. The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: 1. For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi = i; 2. For each station i there exists exactly one station j such that pj = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of pi for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! Input The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p1, p2, ..., pn (1 ≤ pi ≤ n) — the current structure of the subway. All these numbers are distinct. Output Print one number — the maximum possible value of convenience. Examples Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 Note In the first example the mayor can change p2 to 3 and p3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p2 to 4 and p3 to 5.
instruction
0
9,620
1
19,240
Tags: dfs and similar, greedy, math Correct Solution: ``` from collections import defaultdict as dd g=dd(list) def addE(u,v): g[u].append(v) g[v].append(u) n=int(input()) l=[int(x) for x in input().split()] for i in range(n): addE(i+1,l[i]) visited=[False]*(n+1) def dfs(v,count): visited[v]=True stack=[v] while len(stack)!=0: cur=stack.pop() for ch in g[cur]: if visited[ch]: continue visited[ch]=True count+=1 stack.append(ch) return count ans=[] for i in range(1,n+1): if not visited[i]: ans.append(dfs(i,1)) ans=sorted(ans,reverse=True) if len(ans) ==1: print(ans[0]*ans[0]) else: ans[1]+=ans[0] ans.pop(0) print(sum(x*x for x in ans)) ```
output
1
9,620
1
19,241
Provide tags and a correct Python 3 solution for this coding contest problem. The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: 1. For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi = i; 2. For each station i there exists exactly one station j such that pj = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of pi for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! Input The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p1, p2, ..., pn (1 ≤ pi ≤ n) — the current structure of the subway. All these numbers are distinct. Output Print one number — the maximum possible value of convenience. Examples Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 Note In the first example the mayor can change p2 to 3 and p3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p2 to 4 and p3 to 5.
instruction
0
9,621
1
19,242
Tags: dfs and similar, greedy, math Correct Solution: ``` n = int(input()) p = [int(i)-1 for i in input().split()] visited = [0 for _ in range(n)] cycle = [0,0] for i in range(n): j = i count = 0 while not visited[j]: visited[j] = 1 count += 1 j = p[j] if count > 0: cycle.append(count) cycle.sort() print(sum([i*i for i in cycle]) + 2 * cycle[-1] * cycle[-2]) ```
output
1
9,621
1
19,243
Provide tags and a correct Python 3 solution for this coding contest problem. The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: 1. For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi = i; 2. For each station i there exists exactly one station j such that pj = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of pi for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! Input The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p1, p2, ..., pn (1 ≤ pi ≤ n) — the current structure of the subway. All these numbers are distinct. Output Print one number — the maximum possible value of convenience. Examples Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 Note In the first example the mayor can change p2 to 3 and p3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p2 to 4 and p3 to 5.
instruction
0
9,622
1
19,244
Tags: dfs and similar, greedy, math Correct Solution: ``` import sys input = sys.stdin.readline class Unionfind: def __init__(self, n): self.par = [-1]*n self.rank = [1]*n def root(self, x): p = x while not self.par[p]<0: p = self.par[p] while x!=p: tmp = x x = self.par[x] self.par[tmp] = p return p def unite(self, x, y): rx, ry = self.root(x), self.root(y) if rx==ry: return False if self.rank[rx]<self.rank[ry]: rx, ry = ry, rx self.par[rx] += self.par[ry] self.par[ry] = rx if self.rank[rx]==self.rank[ry]: self.rank[rx] += 1 def is_same(self, x, y): return self.root(x)==self.root(y) def count(self, x): return -self.par[self.root(x)] n = int(input()) p = list(map(int, input().split())) uf = Unionfind(n) for i in range(n): uf.unite(i, p[i]-1) rs = set(uf.root(i) for i in range(n)) l = [] for r in rs: l.append(uf.count(r)) l.sort(reverse=True) if len(l)==1: print(n*n) exit() ans = (l[0]+l[1])**2 for i in range(2, len(l)): ans += l[i]**2 print(ans) ```
output
1
9,622
1
19,245
Provide tags and a correct Python 3 solution for this coding contest problem. The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: 1. For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi = i; 2. For each station i there exists exactly one station j such that pj = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of pi for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! Input The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p1, p2, ..., pn (1 ≤ pi ≤ n) — the current structure of the subway. All these numbers are distinct. Output Print one number — the maximum possible value of convenience. Examples Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 Note In the first example the mayor can change p2 to 3 and p3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p2 to 4 and p3 to 5.
instruction
0
9,623
1
19,246
Tags: dfs and similar, greedy, math Correct Solution: ``` def dfs(i, Colour, P): Stack = [i] size = 0 while Stack: v = Stack[-1] if Colour[v] == 0: Colour[v] = 1 size += 1 if Colour[P[v] - 1] == 0: Stack.append(P[v] - 1) else: Colour[v] = 2 Stack.pop() else: Colour[v] = 2 Stack.pop() return size n = int(input()) P = list(map(int, input().split())) Colour = [0] * n Len = [] for i in range(n): if Colour[i] == 0: Len.append(dfs(i, Colour, P)) c1 = 0 c2 = 0 count = 0 for i in Len: if c1 <= i: c2 = c1 c1 = i elif i > c2: c2 = i for i in Len: count += i*i count += c1*c2*2 print(count) ```
output
1
9,623
1
19,247
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: 1. For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi = i; 2. For each station i there exists exactly one station j such that pj = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of pi for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! Input The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p1, p2, ..., pn (1 ≤ pi ≤ n) — the current structure of the subway. All these numbers are distinct. Output Print one number — the maximum possible value of convenience. Examples Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 Note In the first example the mayor can change p2 to 3 and p3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p2 to 4 and p3 to 5. Submitted Solution: ``` import sys sys.setrecursionlimit(10000) def dfs(root,t): time[root]=t vis[root]=1 stack=[root] while (len(stack)!=0): element = stack.pop() time[element] = t vis[element] = 1 for i in graph[element]: if vis[i]==0: stack.append(i) t+=1 else: c.append(t-time[i]+1) # for i in graph[root]: # if vis[i]==0: # dfs(i,t+1) # else: # c.append(t-time[i]+1) n=int(input()) l=list(map(int,input().split())) graph=[[] for i in range(n)] for i in range(n): graph[i].append(l[i]-1) # print (graph) vis=[0]*n c=[] time=[0]*n for i in range(n): if vis[i]==0: dfs(i,1) # print (time) # print (c) c.sort() ans=0 for i in c: ans+=i**2 if len(c)>=2: print (ans+c[-1]*c[-2]*2) else: print (ans) ```
instruction
0
9,624
1
19,248
Yes
output
1
9,624
1
19,249
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: 1. For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi = i; 2. For each station i there exists exactly one station j such that pj = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of pi for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! Input The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p1, p2, ..., pn (1 ≤ pi ≤ n) — the current structure of the subway. All these numbers are distinct. Output Print one number — the maximum possible value of convenience. Examples Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 Note In the first example the mayor can change p2 to 3 and p3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p2 to 4 and p3 to 5. Submitted Solution: ``` n = int(input()) graph = {} a = input().split() for i in range(n): graph[i] = int(a[i]) - 1 vis = [False] * n cycles = [] for i in graph.keys(): if not vis[i]: cv = graph[i] vis[i] = True size = 1 while cv != i: vis[cv] = True cv = graph[cv] size += 1 cycles.append(size) cycles.sort(reverse = True) v = 0 if len(cycles) >= 2: v += (cycles[0] + cycles[1]) ** 2 for i in cycles[2:]: v += i ** 2 else: v = cycles[0] ** 2 print(v) ```
instruction
0
9,625
1
19,250
Yes
output
1
9,625
1
19,251
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself. There are n stations in the subway. It was built according to the Bertown Transport Law: 1. For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi = i; 2. For each station i there exists exactly one station j such that pj = i. The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y (1 ≤ x, y ≤ n). The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of pi for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes. The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! Input The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations. The second line contains n integer numbers p1, p2, ..., pn (1 ≤ pi ≤ n) — the current structure of the subway. All these numbers are distinct. Output Print one number — the maximum possible value of convenience. Examples Input 3 2 1 3 Output 9 Input 5 1 5 4 3 2 Output 17 Note In the first example the mayor can change p2 to 3 and p3 to 1, so there will be 9 pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3). In the second example the mayor can change p2 to 4 and p3 to 5. Submitted Solution: ``` n=int(input()) p=[int(x) for x in input().split()] p.insert(0,-3) max1,max2=0,0 used=[0]*(n+1) count=0 for i in range (1, n+1): m=0 v=i while used[p[v]]==0: used[p[v]]=1 v=p[v] m+=1 if m>max2: max2=m if max2>max1: max2,max1=max1,max2 count+=m**2 count=count-max1**2-max2**2+(max1+max2)**2 print(count) ```
instruction
0
9,626
1
19,252
Yes
output
1
9,626
1
19,253