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.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train.
The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train.
Output
Print a single integer — the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train.
Submitted Solution:
```
n = int(input())
a = list(map(int,input().split()))
l = 0
i = 0
o = 0
while(i != n-1):
if(a[i] != l+1):
o+=1
i+=1
else:
l+=1
i+=1
print(o)
``` | instruction | 0 | 24,294 | 1 | 48,588 |
No | output | 1 | 24,294 | 1 | 48,589 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train.
The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train.
Output
Print a single integer — the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train.
Submitted Solution:
```
n=int(input())
k=1
l=list(map(int,input().split()))
for x in l :
if x==k :
k+=1
print(n-k+1)
``` | instruction | 0 | 24,295 | 1 | 48,590 |
No | output | 1 | 24,295 | 1 | 48,591 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
Input
The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train.
The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train.
Output
Print a single integer — the minimum number of actions needed to sort the railway cars.
Examples
Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2
Note
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train.
Submitted Solution:
```
n = input()
arr = list(map(int, input().split()))
def min(a, b):
if a < b:
return a
else: return b
count = 0
for i in range(1, len(arr)):
if arr[i] < arr[i - 1]:
count += 1
print(count)
``` | instruction | 0 | 24,296 | 1 | 48,592 |
No | output | 1 | 24,296 | 1 | 48,593 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.
Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.
Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.
Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression <image>. Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, <image>. Since the required sum can be quite large Noora asks to find it modulo 109 + 7.
Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.
Input
The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.
Output
Print a single integer — the required sum modulo 109 + 7.
Examples
Input
2
4 7
Output
3
Input
3
4 3 1
Output
9
Note
There are three non-empty subsets in the first sample test:<image>, <image> and <image>. The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.
There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: <image>, <image>, <image>, <image>. In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9. | instruction | 0 | 24,396 | 1 | 48,792 |
Tags: implementation, math, sortings
Correct Solution:
```
M = 1000000007
n = int(input())
x = list(map(int, input().split(' ')))
x = sorted(x)
res = 0
pow2 = 0
for i in range(n):
res += pow2 * x[i]
res -= pow2 * x[n-1-i]
pow2 = (pow2*2 + 1) % M
res = res % M
print(res)
``` | output | 1 | 24,396 | 1 | 48,793 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.
Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.
Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.
Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression <image>. Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, <image>. Since the required sum can be quite large Noora asks to find it modulo 109 + 7.
Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.
Input
The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.
Output
Print a single integer — the required sum modulo 109 + 7.
Examples
Input
2
4 7
Output
3
Input
3
4 3 1
Output
9
Note
There are three non-empty subsets in the first sample test:<image>, <image> and <image>. The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.
There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: <image>, <image>, <image>, <image>. In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9. | instruction | 0 | 24,397 | 1 | 48,794 |
Tags: implementation, math, sortings
Correct Solution:
```
import sys
read=lambda:sys.stdin.readline().rstrip()
readi=lambda:int(sys.stdin.readline())
writeln=lambda x:sys.stdout.write(str(x)+"\n")
write=lambda x:sys.stdout.write(x)
MOD = (10**9)+7
N = readi()
ns = list(map(int, read().split()))
ns.sort()
s = 0
powmod = 1
ps = [0]*N
ps[0] = 1
for i in range(1,N):
ps[i] = 2* ps[i-1] % MOD
for i in range(N):
s += ns[i] * ps[i]
s -= ns[i] * ps[-1-i]
s %= MOD
writeln(s)
``` | output | 1 | 24,397 | 1 | 48,795 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.
Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.
Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.
Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression <image>. Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, <image>. Since the required sum can be quite large Noora asks to find it modulo 109 + 7.
Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.
Input
The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.
Output
Print a single integer — the required sum modulo 109 + 7.
Examples
Input
2
4 7
Output
3
Input
3
4 3 1
Output
9
Note
There are three non-empty subsets in the first sample test:<image>, <image> and <image>. The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.
There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: <image>, <image>, <image>, <image>. In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9. | instruction | 0 | 24,398 | 1 | 48,796 |
Tags: implementation, math, sortings
Correct Solution:
```
def main():
largemodulus = 1000000007
maxofn = 300001
n = 0
answer = 0
powersoftwo = []
multiplier = 1
for _ in range(maxofn):
powersoftwo.append(multiplier)
if multiplier >= largemodulus:
multiplier = multiplier % largemodulus
multiplier *= 2
n = int(input())
hacked = [int(i) for i in input().split()]
hacked.sort(reverse = True)
for x in range(n):
answer += (hacked[x] * ((powersoftwo[n-1-x]-1)-(powersoftwo[x]-1))) % largemodulus
if answer >= largemodulus:
answer = answer % largemodulus
print(answer)
return
main()
``` | output | 1 | 24,398 | 1 | 48,797 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.
Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.
Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.
Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression <image>. Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, <image>. Since the required sum can be quite large Noora asks to find it modulo 109 + 7.
Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.
Input
The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.
Output
Print a single integer — the required sum modulo 109 + 7.
Examples
Input
2
4 7
Output
3
Input
3
4 3 1
Output
9
Note
There are three non-empty subsets in the first sample test:<image>, <image> and <image>. The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.
There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: <image>, <image>, <image>, <image>. In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9. | instruction | 0 | 24,399 | 1 | 48,798 |
Tags: implementation, math, sortings
Correct Solution:
```
n = int(input())
a = sorted(list(map(int, input().split())))
modu = 10**9 + 7
if n < 2:
print(0)
else:
ans = 0
p2 = [1]
for i in range(n):
p2.append(2*p2[-1] % modu)
for i in range(n - 1):
ans += (a[i+1] - a[i]) * (p2[i + 1] - 1) * (p2[n - i - 1] - 1)
ans %= modu
print(ans)
``` | output | 1 | 24,399 | 1 | 48,799 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.
Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.
Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.
Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression <image>. Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, <image>. Since the required sum can be quite large Noora asks to find it modulo 109 + 7.
Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.
Input
The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.
Output
Print a single integer — the required sum modulo 109 + 7.
Examples
Input
2
4 7
Output
3
Input
3
4 3 1
Output
9
Note
There are three non-empty subsets in the first sample test:<image>, <image> and <image>. The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.
There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: <image>, <image>, <image>, <image>. In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9. | instruction | 0 | 24,400 | 1 | 48,800 |
Tags: implementation, math, sortings
Correct Solution:
```
MOD = 10 ** 9 + 7
n = int(input())
xs = sorted(map(int, input().split()))
pows = [1]
for i in range(n):
pows.append(2 * pows[-1] % MOD)
ans = 0
for ind, (lx, rx) in enumerate(zip(xs, xs[1:])):
cntl = ind + 1
cntr = n - cntl
ans = (ans + (rx - lx) * (pows[cntl] - 1) * (pows[cntr] - 1)) % MOD
print(ans)
``` | output | 1 | 24,400 | 1 | 48,801 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.
Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.
Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.
Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression <image>. Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, <image>. Since the required sum can be quite large Noora asks to find it modulo 109 + 7.
Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.
Input
The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.
Output
Print a single integer — the required sum modulo 109 + 7.
Examples
Input
2
4 7
Output
3
Input
3
4 3 1
Output
9
Note
There are three non-empty subsets in the first sample test:<image>, <image> and <image>. The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.
There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: <image>, <image>, <image>, <image>. In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9. | instruction | 0 | 24,401 | 1 | 48,802 |
Tags: implementation, math, sortings
Correct Solution:
```
N = int(input())
X = list(map(int, input().split()))
X.sort(reverse=True)
MOD = int(1e9 + 7)
pows_mod = [1]
for i in range(int(3e5) + 10):
pows_mod.append((pows_mod[-1] % MOD) * 2 % MOD)
ans = 0
for i in range(N):
ans += ((pows_mod[N - i - 1] - 1) - (pows_mod[i] - 1)) * X[i]
ans %= MOD
print(ans)
``` | output | 1 | 24,401 | 1 | 48,803 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.
Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.
Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.
Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression <image>. Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, <image>. Since the required sum can be quite large Noora asks to find it modulo 109 + 7.
Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.
Input
The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.
Output
Print a single integer — the required sum modulo 109 + 7.
Examples
Input
2
4 7
Output
3
Input
3
4 3 1
Output
9
Note
There are three non-empty subsets in the first sample test:<image>, <image> and <image>. The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.
There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: <image>, <image>, <image>, <image>. In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9. | instruction | 0 | 24,402 | 1 | 48,804 |
Tags: implementation, math, sortings
Correct Solution:
```
n = int(input())
a = list(map(int, input().split(' ')))
a.sort()
p = 10**9 + 7
ans = 0
two = 1
for x in a:
ans += two * x
two = (two*2) % p
a = reversed(a)
two = 1
for x in a:
ans -= two * x
two = (two*2) % p
ans = ans%p
print(ans)
``` | output | 1 | 24,402 | 1 | 48,805 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.
Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.
Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.
Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression <image>. Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, <image>. Since the required sum can be quite large Noora asks to find it modulo 109 + 7.
Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.
Input
The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.
Output
Print a single integer — the required sum modulo 109 + 7.
Examples
Input
2
4 7
Output
3
Input
3
4 3 1
Output
9
Note
There are three non-empty subsets in the first sample test:<image>, <image> and <image>. The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.
There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: <image>, <image>, <image>, <image>. In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9. | instruction | 0 | 24,403 | 1 | 48,806 |
Tags: implementation, math, sortings
Correct Solution:
```
n = int(input())
coord = [int(x) for x in input().split()]
coord = sorted(coord)
modul = 1000000007
G = [0]
P = [0]
D = [0]
for i in range(1, n + 1):
gi = (G[i - 1] * 2 + coord[i - 1]) % modul
pi = (2 * P[i - 1] + 1) % modul
G.append(gi)
P.append(pi)
for i in range(1, n):
di = (D[i - 1] + P[i] * coord[i] - G[i]) % modul
D.append(di)
# print(G, P, D)
print(D[n - 1])
``` | output | 1 | 24,403 | 1 | 48,807 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In the Kingdom K., there are n towns numbered with integers from 1 to n. The towns are connected by n bi-directional roads numbered with integers from 1 to n. The i-th road connects the towns ui and vi and its length is li. There is no more than one road between two towns. Also, there are no roads that connect the towns with itself.
Let's call the inconvenience of the roads the maximum of the shortest distances between all pairs of towns.
Because of lack of money, it was decided to close down one of the roads so that after its removal it is still possible to reach any town from any other. You have to find the minimum possible inconvenience of the roads after closing down one of the roads.
Input
The first line contains the integer n (3 ≤ n ≤ 2·105) — the number of towns and roads.
The next n lines contain the roads description. The i-th from these lines contains three integers ui, vi, li (1 ≤ ui, vi ≤ n, 1 ≤ li ≤ 109) — the numbers of towns connected by the i-th road and the length of the i-th road. No road connects a town to itself, no two roads connect the same towns.
It's guaranteed that it's always possible to close down one of the roads so that all the towns are still reachable from each other.
Output
Print a single integer — the minimum possible inconvenience of the roads after the refusal from one of the roads.
Examples
Input
3
1 2 4
2 3 5
1 3 1
Output
5
Input
5
2 3 7
3 1 9
4 1 8
3 5 4
4 5 5
Output
18
Submitted Solution:
```
n=int(input())
arr=[]
for i in range(0,n):
arr1=[int(x) for x in input().split()]
arr.append(arr1)
#最小生成树算法
#S[i]=0表示未选入集合,S[i]=1表示选入了集合
S=[0 for x in range(0,n+1)]
minium=arr[0][2]
index=0
for i in range(1,len(arr)):
if arr[i][2]<minium:
minium=arr[i][2]
index=i
S[arr[index][0]]=1
S[arr[index][1]]=1
``` | instruction | 0 | 24,412 | 1 | 48,824 |
No | output | 1 | 24,412 | 1 | 48,825 |
Provide a correct Python 3 solution for this coding contest problem.
The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.
Constraints
* 2 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N
* 1 \leq K \leq 10^{18}
Input
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N
Output
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Examples
Input
4 5
3 2 4 1
Output
4
Input
6 727202214173249351
6 5 2 5 3 2
Output
2 | instruction | 0 | 24,493 | 1 | 48,986 |
"Correct Solution:
```
n,k=map(int,input().split())
a=list(map(int,input().split()))
visited=[]
o=(n+1)*[-1]
cycle,l=1,0
v=1
while o[v]==-1:
o[v]=len(visited)
visited.append(v)
v=a[v-1]
cycle=len(visited)-o[v]
l=o[v]
if k<l:
print(visited[k])
else:
k-=l
k%=cycle
print(visited[l+k])
``` | output | 1 | 24,493 | 1 | 48,987 |
Provide a correct Python 3 solution for this coding contest problem.
The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.
Constraints
* 2 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N
* 1 \leq K \leq 10^{18}
Input
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N
Output
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Examples
Input
4 5
3 2 4 1
Output
4
Input
6 727202214173249351
6 5 2 5 3 2
Output
2 | instruction | 0 | 24,494 | 1 | 48,988 |
"Correct Solution:
```
n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]
ans = 0
while k:
if k & 1:
ans = a[ans]
c = [a[a[i]] for i in range(n)]
a = c[:]
k >>= 1
print(ans + 1)
``` | output | 1 | 24,494 | 1 | 48,989 |
Provide a correct Python 3 solution for this coding contest problem.
The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.
Constraints
* 2 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N
* 1 \leq K \leq 10^{18}
Input
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N
Output
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Examples
Input
4 5
3 2 4 1
Output
4
Input
6 727202214173249351
6 5 2 5 3 2
Output
2 | instruction | 0 | 24,495 | 1 | 48,990 |
"Correct Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = 0
if k<=n:
for i in range(k):
b = a[b]-1
print(b+1)
exit(0)
c = [0]
for i in range(n):
b = a[b]-1
c.append(b)
d = c[:-1].index(c[-1])
c = c[d:-1]
k = k-d
print(c[k%len(c)]+1)
``` | output | 1 | 24,495 | 1 | 48,991 |
Provide a correct Python 3 solution for this coding contest problem.
The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.
Constraints
* 2 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N
* 1 \leq K \leq 10^{18}
Input
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N
Output
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Examples
Input
4 5
3 2 4 1
Output
4
Input
6 727202214173249351
6 5 2 5 3 2
Output
2 | instruction | 0 | 24,496 | 1 | 48,992 |
"Correct Solution:
```
N,K = map(int,input().split())
cc = list(map(int,input().split()))
memo = []
aa = ['.']*N
a = 1
while aa[a-1] == '.':
memo.append(a)
aa[a-1] = 'v'
a = cc[a-1]
if len(memo)>K:
print(memo[K])
else:
b = memo.index(a)
m = len(memo)-b
mm = (K-b) % m
del memo[:b]
print(memo[mm])
``` | output | 1 | 24,496 | 1 | 48,993 |
Provide a correct Python 3 solution for this coding contest problem.
The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.
Constraints
* 2 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N
* 1 \leq K \leq 10^{18}
Input
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N
Output
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Examples
Input
4 5
3 2 4 1
Output
4
Input
6 727202214173249351
6 5 2 5 3 2
Output
2 | instruction | 0 | 24,497 | 1 | 48,994 |
"Correct Solution:
```
N, K = map(int, input().split())
A = [-1] + list(map(int, input().split()))
id = [-1] * (N + 1)
x = 1
l = 0
a = []
while id[x] == -1:
a.append(x)
id[x] = l
l += 1
x = A[x]
c = l - id[x]
if K < l:
print(a[K])
else:
K -= l
print(a[id[x] + K % c])
``` | output | 1 | 24,497 | 1 | 48,995 |
Provide a correct Python 3 solution for this coding contest problem.
The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.
Constraints
* 2 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N
* 1 \leq K \leq 10^{18}
Input
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N
Output
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Examples
Input
4 5
3 2 4 1
Output
4
Input
6 727202214173249351
6 5 2 5 3 2
Output
2 | instruction | 0 | 24,498 | 1 | 48,996 |
"Correct Solution:
```
N, K = map(int, input().split())
A = list(map(int, input().split()))
l=[1]
seen={}
k=1
for i in range (K):
if not k in seen.keys():
seen[k]=i
k=A[k-1]
l.append(k)
else:
r_start=seen[k]
roop=i-r_start
K=(K-r_start)%roop+r_start
break
ans=l[K]
print(ans)
``` | output | 1 | 24,498 | 1 | 48,997 |
Provide a correct Python 3 solution for this coding contest problem.
The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.
Constraints
* 2 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N
* 1 \leq K \leq 10^{18}
Input
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N
Output
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Examples
Input
4 5
3 2 4 1
Output
4
Input
6 727202214173249351
6 5 2 5 3 2
Output
2 | instruction | 0 | 24,499 | 1 | 48,998 |
"Correct Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = [-1]*n
c = []
ans = 0
for i in range(k):
s[ans] = i
c.append(ans)
ans = a[ans] - 1
if s[ans] >= 0:
t = i - s[ans] + 1
ans = c[(k-i-1) % t + s[ans]]
break
print(ans+1)
``` | output | 1 | 24,499 | 1 | 48,999 |
Provide a correct Python 3 solution for this coding contest problem.
The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.
Constraints
* 2 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N
* 1 \leq K \leq 10^{18}
Input
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N
Output
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Examples
Input
4 5
3 2 4 1
Output
4
Input
6 727202214173249351
6 5 2 5 3 2
Output
2 | instruction | 0 | 24,500 | 1 | 49,000 |
"Correct Solution:
```
n,k,*a=map(int,open(0).read().split())
d=[2*k]*n
while k:n=-a[-n]+1;d[n]=k=~-k%(d[n]+1-k)
print(1-n)
``` | output | 1 | 24,500 | 1 | 49,001 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.
Constraints
* 2 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N
* 1 \leq K \leq 10^{18}
Input
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N
Output
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Examples
Input
4 5
3 2 4 1
Output
4
Input
6 727202214173249351
6 5 2 5 3 2
Output
2
Submitted Solution:
```
N,K=map(int,input().split())
A=[int(x)-1 for x in input().split()]
t,i,dist=0,0,{0:0}
while K>0:
K,i,t=K-1,i+1,A[t]
if t in dist:
K%=i-dist[t]
else:
dist[t]=i
print(t+1)
``` | instruction | 0 | 24,501 | 1 | 49,002 |
Yes | output | 1 | 24,501 | 1 | 49,003 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.
Constraints
* 2 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N
* 1 \leq K \leq 10^{18}
Input
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N
Output
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Examples
Input
4 5
3 2 4 1
Output
4
Input
6 727202214173249351
6 5 2 5 3 2
Output
2
Submitted Solution:
```
n,k=map(int,input().split())
A=list(map(int,input().split()))
a=[0]*n
a[0]=1
nxt=A[0]-1
i=2
while 1:
a[nxt]=i
nxt=A[nxt]-1
i+=1
if a[nxt]!=0:
g=i-a[nxt]
t=a[nxt]-1
break
if t>=k:
print(a.index(k+1)+1)
else:
k-=t
u=k%g
print(a.index(t+u+1)+1)
``` | instruction | 0 | 24,502 | 1 | 49,004 |
Yes | output | 1 | 24,502 | 1 | 49,005 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.
Constraints
* 2 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N
* 1 \leq K \leq 10^{18}
Input
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N
Output
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Examples
Input
4 5
3 2 4 1
Output
4
Input
6 727202214173249351
6 5 2 5 3 2
Output
2
Submitted Solution:
```
n,k=map(int,input().split())
a=list(map(int,input().split()))
v=[0]*n
v[0]=1
i=1
l=[1]
while True:
i=a[i-1]
l.append(i)
if v[i-1]:
t=i
break
else:
v[i-1]=1
pre=l.index(t)
loop=len(l)-pre-1
if pre>=k:
print(l[k])
else:
k=k-pre
mod=k%loop
print(l[pre+mod])
``` | instruction | 0 | 24,503 | 1 | 49,006 |
Yes | output | 1 | 24,503 | 1 | 49,007 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.
Constraints
* 2 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N
* 1 \leq K \leq 10^{18}
Input
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N
Output
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Examples
Input
4 5
3 2 4 1
Output
4
Input
6 727202214173249351
6 5 2 5 3 2
Output
2
Submitted Solution:
```
N, K = map(int, input().split())
A = list(map(lambda a: int(a) - 1, input().split()))
T = [A[:]]
for _ in range(64):
prev = T[-1]
T.append([prev[a] for a in prev])
now = 0
for d, P in enumerate(T):
if ((1 << d) & K) > 0:
now = P[now]
print(now + 1)
``` | instruction | 0 | 24,504 | 1 | 49,008 |
Yes | output | 1 | 24,504 | 1 | 49,009 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.
Constraints
* 2 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N
* 1 \leq K \leq 10^{18}
Input
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N
Output
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Examples
Input
4 5
3 2 4 1
Output
4
Input
6 727202214173249351
6 5 2 5 3 2
Output
2
Submitted Solution:
```
def main():
n, k = map(int, input().split())
a = list(map(int, input().split()))
num = 1
l = []
s = [1]*n
while s[num-1]:
l.append(num)
s[num-1] = 0
num = a[num-1]
if l.index(num) <= k:
l2 = l[l.index(num):]
kk = (k-len(set(l)-set(l2)))%len(l2)
print(l2[kk])
else:
l2 = l[l.index(num):]
print(l2[k])
main()
``` | instruction | 0 | 24,505 | 1 | 49,010 |
No | output | 1 | 24,505 | 1 | 49,011 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.
Constraints
* 2 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N
* 1 \leq K \leq 10^{18}
Input
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N
Output
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Examples
Input
4 5
3 2 4 1
Output
4
Input
6 727202214173249351
6 5 2 5 3 2
Output
2
Submitted Solution:
```
from collections import defaultdict as d
n, k = map(int, input().split())
arr = list(map(int, input().split()))
s = '1'
i = 1
while True:
if str(arr[i - 1]) in s:
s += str(arr[i - 1])
break
s += str(arr[i - 1])
i = arr[i - 1]
if k <= s.index(s[-1]):
print(s[k])
else:
k -= (s.index(s[-1]))
x = len(s) - s.index(s[-1]) - 1
print(s[s.index(s[-1]):-1][k%x])
``` | instruction | 0 | 24,506 | 1 | 49,012 |
No | output | 1 | 24,506 | 1 | 49,013 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.
Constraints
* 2 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N
* 1 \leq K \leq 10^{18}
Input
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N
Output
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Examples
Input
4 5
3 2 4 1
Output
4
Input
6 727202214173249351
6 5 2 5 3 2
Output
2
Submitted Solution:
```
n,k = map(int, input().split())
A = list(map(int, input().split()))
d=k%n
print(A[A[d-1]-1])
``` | instruction | 0 | 24,507 | 1 | 49,014 |
No | output | 1 | 24,507 | 1 | 49,015 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Kingdom of Takahashi has N towns, numbered 1 through N.
There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.
Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Help the king by writing a program that answers this question.
Constraints
* 2 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N
* 1 \leq K \leq 10^{18}
Input
Input is given from Standard Input in the following format:
N K
A_1 A_2 \dots A_N
Output
Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.
Examples
Input
4 5
3 2 4 1
Output
4
Input
6 727202214173249351
6 5 2 5 3 2
Output
2
Submitted Solution:
```
from collections import defaultdict
N,K = map(int, input().split())
A = [int(i) for i in input().split()]
used_dict = defaultdict(int)
used_dict[0] += 1
now_town = 0
for i in range(N):
now_town = A[now_town]-1
if used_dict[now_town]:
used_dict[now_town] += 1
break
used_dict[now_town] += 1
now_town2 = 0
cnt = 0
while 1:
if now_town2 == now_town:
for _ in range((K-cnt)%(i-cnt+1)):
now_town = A[now_town]-1
break
now_town2 = A[now_town2]-1
cnt += 1
print(now_town+1)
``` | instruction | 0 | 24,508 | 1 | 49,016 |
No | output | 1 | 24,508 | 1 | 49,017 |
Provide a correct Python 3 solution for this coding contest problem.
There are two cameras which observe the up line and the down line respectively on the double lane (please see the following figure). These cameras are located on a line perpendicular to the lane, and we call the line 'monitoring line.' (the red line in the figure)
<image>
Monitoring systems are connected to the cameras respectively. When a car passes through the monitoring line, the corresponding monitoring system records elapsed time (sec) from the start of monitoring.
Your task is to write a program which reads records of the two monitoring systems and prints the maximum time interval where cars did not pass through the monitoring line.
The two monitoring system start monitoring simultaneously. The end of monitoring is indicated by the latest time in the records.
Input
The input consists of multiple datasets. Each dataset consists of:
n m
tl1 tl2 ... tln
tr1 tr2 ... trm
n, m are integers which represent the number of cars passed through monitoring line on the up line and the down line respectively. tli, tri are integers which denote the elapsed time when i-th car passed through the monitoring line for the up line and the down line respectively. You can assume that tl1 < tl2 < ... < tln、 tr1 < tr2 < ... < trm.
You can also assume that n, m ≤ 10000, and 1 ≤ tli, tri ≤ 1,000,000.
The end of input is indicated by a line including two zero.
Output
For each dataset, print the maximum value in a line.
Example
Input
4 5
20 35 60 70
15 30 40 80 90
3 2
10 20 30
42 60
0 1
100
1 1
10
50
0 0
Output
20
18
100
40 | instruction | 0 | 24,683 | 1 | 49,366 |
"Correct Solution:
```
# AOJ 1029: Traffic Analysis
# Python3 2018.7.6 bal4u
import sys
from sys import stdin
input = stdin.readline
while True:
n, m = map(int, input().split())
if n == 0 and m == 0: break
if n > 0: a = list(map(int, input().split()))
if m > 0: b = list(map(int, input().split()))
ans = i = j = 0
s, t = 0, -1
while i < n or j < m:
if i < n:
if s == a[i]: i += 1
if i < n: t = a[i]
if j < m:
if s == b[j]: j += 1
if j < m and (t < 0 or b[j] < t): t = b[j]
if t >= 0:
if i < n and t == a[i]: i += 1
if j < m and t == b[j]: j += 1
ans = max(ans, t-s)
s, t = t, -1
if t >= 0: ans = max(ans, t-s)
print(ans)
``` | output | 1 | 24,683 | 1 | 49,367 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0.
One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.
Input
The first line contains three integers a, b and c (-10^9≤ a, b, c≤ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue.
The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≤ x_1, y_1, x_2, y_2≤ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2).
Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
<image> | instruction | 0 | 24,747 | 1 | 49,494 |
Tags: geometry, implementation
Correct Solution:
```
a, b, c = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
if a != 0 and b != 0:
x1_ = ((-1) * (b * y1 + c)) / a
y1_ = ((-1) * (a * x1 + c)) / b
x2_ = ((-1) * (b * y2 + c)) / a
y2_ = ((-1) * (a * x2 + c)) / b
p1 = abs(x1 - x1_) + abs(y2 - y2_) + ((x1_ - x2) ** 2 + (y1 - y2_) ** 2) ** 0.5
p2 = abs(x1 - x1_) + abs(x2 - x2_) + ((x1_ - x2_) ** 2 + (y1 - y2) ** 2) ** 0.5
p3 = abs(y1 - y1_) + abs(y2 - y2_) + ((x1 - x2) ** 2 + (y1_ - y2_) ** 2) ** 0.5
p4 = abs(y1 - y1_) + abs(x2 - x2_) + ((x1 - x2_) ** 2 + (y1_ - y2) ** 2) ** 0.5
p5 = abs(y1 - y2) + abs(x2 - x1)
sp = [p1, p2, p3, p4, p5]
print(min(sp))
else:
print(abs(y1 - y2) + abs(x2 - x1))
``` | output | 1 | 24,747 | 1 | 49,495 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0.
One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.
Input
The first line contains three integers a, b and c (-10^9≤ a, b, c≤ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue.
The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≤ x_1, y_1, x_2, y_2≤ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2).
Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
<image> | instruction | 0 | 24,748 | 1 | 49,496 |
Tags: geometry, implementation
Correct Solution:
```
from fractions import Fraction
a, b, c = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x3, y3, x4, y4 = x1, y2, x2, y1
if (y2 - y1) * (x2 - x1) * a * b >= 0 or (a*x1 + b*y1 + c) * (a*x3 + b*y3 + c) > 0 and (a*x1 + b*y1 + c) * (a*x4 + b*y4 + c) > 0:
print(abs(y2 - y1) + abs(x2 - x1))
else:
y5 = Fraction(-(a*x1 + c), b)
if (y5 - y1) * (y5 - y2) <= 0:
x5 = x1
else:
x5 = Fraction(-(b*y1 + c), a)
y5 = y1
y6 = Fraction(-(a*x2 + c), b)
if (y6 - y2) * (y6 - y1) <= 0:
x6 = x2
else:
x6 = Fraction(-(b*y2 + c), a)
y6 = y2
print(abs(y2 - y1) + abs(x2 - x1) - abs(y6 - y5) - abs(x6 - x5) + ((y6 - y5)**2 + (x6 - x5)**2)**0.5)
``` | output | 1 | 24,748 | 1 | 49,497 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0.
One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.
Input
The first line contains three integers a, b and c (-10^9≤ a, b, c≤ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue.
The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≤ x_1, y_1, x_2, y_2≤ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2).
Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
<image> | instruction | 0 | 24,749 | 1 | 49,498 |
Tags: geometry, implementation
Correct Solution:
```
a,b,c=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
bRange=abs(y2-y1)+abs(x2-x1)
if a==0 or b==0:
print(bRange)
else:
py1=(-b*y1-c)/a
py2=(-b*y2-c)/a
px1=(-a*x1-c)/b
px2=(-a*x2-c)/b
pRange1=((x2-py1)**2+(px2-y1)**2)**0.5+abs(y2-px2)+abs(py1-x1)
pRange2=((py2-py1)**2+(y2-y1)**2)**0.5+abs(py1-x1)+abs(x2-py2)
pRange3=((x2-x1)**2+(px2-px1)**2)**0.5+abs(px1-y1)+abs(y2-px2)
pRange4=((py2-x1)**2+(y2-px1)**2)**0.5+abs(px1-y1)+abs(x2-py2)
print(min(bRange,pRange1,pRange2,pRange3,pRange4))
``` | output | 1 | 24,749 | 1 | 49,499 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0.
One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.
Input
The first line contains three integers a, b and c (-10^9≤ a, b, c≤ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue.
The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≤ x_1, y_1, x_2, y_2≤ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2).
Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
<image> | instruction | 0 | 24,750 | 1 | 49,500 |
Tags: geometry, implementation
Correct Solution:
```
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
#sys.setrecursionlimit(300000)
#threading.stack_size(10**8)
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#-------------------------------------------------------------------------
#mod = 9223372036854775807
class SegmentTree:
def __init__(self, data, default=-10**6, func=lambda a, b: max(a,b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
class SegmentTree1:
def __init__(self, data, default=10**6, func=lambda a, b: min(a,b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
MOD=10**9+7
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
mod=10**9+7
omod=998244353
#-------------------------------------------------------------------------
prime = [True for i in range(200001)]
pp=[0]*200001
def SieveOfEratosthenes(n=200000):
# Create a boolean array "prime[0..n]" and initialize
# all entries it as true. A value in prime[i] will
# finally be false if i is Not a prime, else true.
p = 2
while (p * p <= n):
# If prime[p] is not changed, then it is a prime
if (prime[p] == True):
# Update all multiples of p
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
#---------------------------------running code------------------------------------------
a, b, c = map(int, input().split())
ax, ay, bx, by = map(int, input().split())
if (a==0 or b==0):
print(abs(ax - bx) + abs(ay - by))
exit()
oy = -(a*ax+c)/b
ty = -(a*bx+c)/b
ox = -(b*ay+c)/a
tx = -(b*by+c)/a
ans = abs(ax - bx) + abs(ay - by)
ans1 = abs(ay - oy) + abs(by - ty) + math.sqrt(abs(ax - bx)**2 + abs(oy - ty)**2)
ans2 = abs(ax - ox) + abs(bx - tx) + math.sqrt(abs(ay - by)**2 + abs(ox - tx)**2)
ans3 = abs(ax - ox) + abs(by - ty) + math.sqrt(abs(bx - ox)**2 + abs(ay - ty)**2)
ans4 = abs(ay - oy) + abs(bx - tx) + math.sqrt(abs(by - oy)**2 + abs(ax - tx)**2)
print(min(ans, ans1, ans2, ans3, ans4))
``` | output | 1 | 24,750 | 1 | 49,501 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0.
One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.
Input
The first line contains three integers a, b and c (-10^9≤ a, b, c≤ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue.
The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≤ x_1, y_1, x_2, y_2≤ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2).
Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
<image> | instruction | 0 | 24,751 | 1 | 49,502 |
Tags: geometry, implementation
Correct Solution:
```
a, b, c = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
if x1 >= x2 and y1 >= y2:
x1, y1, x2, y2 = x2, y2, x1, y1
if x1 <= x2 and y1 >= y2:
x2 = x1 - (x2 - x1)
c += 2 * x1 * a
a = -a
x1, y1, x2, y2 = x2, y2, x1, y1
if x1 >= x2 and y1 <= y2:
x1 = x2 - (x1 - x2)
c += 2 * x2 * a
a = -a
ans = x2 - x1 + y2 - y1
if a == 0 or b == 0:
print(ans)
else:
yab = -(c + a * x1) * abs(a) * (b // abs(b))
ybb = -(c + a * x2) * abs(a) * (b // abs(b))
xaa = -(c + b * y1) * abs(b) * (a // abs(a))
xba = -(c + b * y2) * abs(b) * (a // abs(a))
#xa = -(c + b * y1) / a
#ya = -(c + a * x1) / b
#xb = -(c + b * y2) / a
#yb = -(c + a * x2) / b
#print(ya, yab)
y1 *= abs(a * b)
y2 *= abs(a * b)
x1 *= abs(a * b)
x2 *= abs(a * b)
if y1 <= yab <= y2 and yab <= ybb:
if ybb <= y2:
ans = (yab - y1 + y2 - ybb + ((ybb - yab) * (ybb - yab) + (x2 - x1) * (x2 - x1)) ** 0.5) / abs(a * b)
else:
ans = (yab - y1 + x2 - xba + ((y2 - yab) * (y2 - yab) + (xba - x1) * (xba - x1)) ** 0.5) / abs(a * b)
print(ans)
elif x1 <= xaa <= x2 and xaa <= xba:
if xba <= x2:
ans = (xaa - x1 + x2 - xba + ((xba - xaa) * (xba - xaa) + (y2 - y1) * (y2 - y1)) ** 0.5) / abs(a * b)
else:
ans = (xaa - x1 + y2 - ybb + ((x2 - xaa) * (x2 - xaa) + (ybb - y1) * (ybb - y1)) ** 0.5) / abs(a * b)
print(ans)
else:
print(ans)
``` | output | 1 | 24,751 | 1 | 49,503 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0.
One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.
Input
The first line contains three integers a, b and c (-10^9≤ a, b, c≤ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue.
The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≤ x_1, y_1, x_2, y_2≤ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2).
Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
<image> | instruction | 0 | 24,752 | 1 | 49,504 |
Tags: geometry, implementation
Correct Solution:
```
import math
l1 = 0
l2 = 0
l3 = 0
l4 = 0
l5 = 0
a,b,c=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
l = 0
if (a!=0 and b!=0):
if (x1 == x2):
r = max(y1,y2) - min(y1,y2)
elif (y1 == y2):
r = max(x1,x2) - min(x1,x2)
else:
x0 = -1*(b*y1+c)/a
y0 = y1
l1 += math.fabs(-1*(b*y1+c)/a - x1)
x3 = x1
y3 = -1*(a*x1+c)/b
l2 += math.fabs(-1*(a*x1+c)/b - y1)
xk = -1*(b*y2+c)/a
yk = y2
l3 += math.fabs(-1*(b*y2+c)/a - x2)
xk1 = x2
yk1 = -1*(a*x2+c)/b
l4 += math.fabs(-1*(a*x2+c)/b - y2)
m1=l1+l3+((x0-xk)**2+(y0-yk)**2)**0.5
m2=l2+l3+((x3-xk)**2+(y3-yk)**2)**0.5
m3=l1+l4+((x0-xk1)**2+(y0-yk1)**2)**0.5
m4 = l2 + l4 +((x3-xk1)**2+(y3-yk1)**2)**0.5
l5 = math.fabs(x1-x2)+math.fabs(y1-y2)
l = min(m1,min(m2,min(m3,min(m4,l5))))
else:
l = math.fabs(x1-x2)+math.fabs(y1-y2)
print(l)
``` | output | 1 | 24,752 | 1 | 49,505 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0.
One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.
Input
The first line contains three integers a, b and c (-10^9≤ a, b, c≤ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue.
The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≤ x_1, y_1, x_2, y_2≤ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2).
Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
<image> | instruction | 0 | 24,753 | 1 | 49,506 |
Tags: geometry, implementation
Correct Solution:
```
a, b, c = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
l = 0
xz1 = x1
if b != 0:
yz1 = (-c - x1 * a) / b
else:
yz1 = 9999999999999999999
if a != 0:
xz2 = (-c - y1 * b) / a
else:
xz2 = 9999999999999999999
yz2 = y1
xf1 = x2
if b != 0:
yf1 = (-c - x2 * a) / b
else:
yf1 = 9999999999999999999
if a != 0:
xf2 = (-c - y2 * b) / a
else:
xf2 = 9999999999999999999
yf2 = y2
v1 = abs(x1 - x2) + abs(y1 - y2)
v2 = abs(xz1 - x1) + abs(yz1 - y1) + abs(xf1 - x2) + abs(yf1 - y2) + ((xz1 - xf1) ** 2 + (yz1 - yf1) ** 2) ** 0.5
v3 = abs(xz1 - x1) + abs(yz1 - y1) + abs(xf2 - x2) + abs(yf2 - y2) + ((xz1 - xf2) ** 2 + (yz1 - yf2) ** 2) ** 0.5
v4 = abs(xz2 - x1) + abs(yz2 - y1) + abs(xf1 - x2) + abs(yf1 - y2) + ((xz2 - xf1) ** 2 + (yz2 - yf1) ** 2) ** 0.5
v5 = abs(xz2 - x1) + abs(yz2 - y1) + abs(xf2 - x2) + abs(yf2 - y2) + ((xz2 - xf2) ** 2 + (yz2 - yf2) ** 2) ** 0.5
print(min(v1, v2, v3, v4, v5))
``` | output | 1 | 24,753 | 1 | 49,507 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0.
One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.
Input
The first line contains three integers a, b and c (-10^9≤ a, b, c≤ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue.
The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≤ x_1, y_1, x_2, y_2≤ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2).
Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
<image> | instruction | 0 | 24,754 | 1 | 49,508 |
Tags: geometry, implementation
Correct Solution:
```
import math
class vector:
def __init__(self, x, y):
self.x = x
self.y = y
def sum(self, vector): # а это просто функция, возвращающая адын(1)
return 1
def len(self):
return math.sqrt(self.x * self.x + self.y * self.y)
def __str__(self):
return str(self.x) + ' ' + str(self.y)
def __add__(self, other): # сумма векторов; a + b
return vector(self.x + other.x, self.y + other.y)
def __sub__(self, other): # разность векторов; a - b
return vector(self.x - other.x, self.y - other.y)
def __pow__(self, other): # скалярное произведение; a ** b
return self.x * other.x + self.y * other.y
def __mod__(self, other): # векторное произведение; a % b
return self.x * other.y - self.y * other.x
def __mul__(self, other): # умножение на число; a * k
return vector(self.x * other, self.y * other)
def manhattanLen(self):
return abs(self.x) + abs(self.y)
def rotate(self): # поворот вектора вправо на 90 градусов
return vector(self.y, -self.x)
def normalize(self): # приведение вектора к единичному
if self.x == 0 and self.y == 0:
return self
else:
return vector(self.x / self.len(), self.y / self.len())
a, b, c = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
A = vector(x1, y1)
B = vector(x2, y2)
if a == 0 or b == 0:
print((A-B).manhattanLen())
exit()
Ah = vector((-c-b*A.y) / a, A.y)
Av = vector(A.x, (-c-a*A.x)/b)
Bh = vector((-c-b*B.y) / a, B.y)
Bv = vector(B.x, (-c-a*B.x)/b)
print(min(
(A-B).manhattanLen(),
(A-Ah).manhattanLen() + (Ah - Bh).len() + (Bh - B).manhattanLen(),
(A-Ah).manhattanLen() + (Ah - Bv).len() + (Bv - B).manhattanLen(),
(A-Av).manhattanLen() + (Av - Bh).len() + (Bh - B).manhattanLen(),
(A-Av).manhattanLen() + (Av - Bv).len() + (Bv - B).manhattanLen(),
))
``` | output | 1 | 24,754 | 1 | 49,509 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0.
One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.
Input
The first line contains three integers a, b and c (-10^9≤ a, b, c≤ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue.
The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≤ x_1, y_1, x_2, y_2≤ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2).
Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
<image>
Submitted Solution:
```
import math
def lll(a, b):
return math.sqrt((b[0] - a[0])**2 + (b[1] - a[1])**2)
def III(a, b):
return abs(b[0] - a[0]) + abs(b[1] - a[1])
a, b, c = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
g = [[]] * 8
for i in range(8):
g[i] = [float('inf')] * 8
p = [
(x1, y1),
(x1, y2),
(x2, y2),
(x2, y1),
]
l = []
if a != 0 and b != 0:
l = [
(x1, (-a*x1 - c) / b),
((-b*y1 - c) / a, y1),
(x2, (-a*x2 - c) / b),
((-b*y2 - c) / a, y2),
]
else:
l = [
(x1, y1),
(x1, y1),
(x1, y1),
(x1, y1),
]
for i in range(4):
for j in range(4):
g[i][4 + j] = g[4 + j][i] = III(p[i], l[j])
if (i != j):
g[i][j] = g[j][i] = III(p[i], p[j])
g[4 + i][4 + j] = g[4 + j][4 + i] = lll(l[i], l[j])
ls = [float('inf')] * 8
ls[0] = 0
qu = [0]
while len(qu) > 0:
i = qu[0]
qu = qu[1:]
for j in range(8):
if ls[i] + g[i][j] < ls[j]:
ls[j] = ls[i] + g[i][j]
if not j in qu:
qu += [j]
print(ls[2])
``` | instruction | 0 | 24,755 | 1 | 49,510 |
Yes | output | 1 | 24,755 | 1 | 49,511 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0.
One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.
Input
The first line contains three integers a, b and c (-10^9≤ a, b, c≤ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue.
The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≤ x_1, y_1, x_2, y_2≤ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2).
Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
<image>
Submitted Solution:
```
def length(x1, y1, x2, y2):
return ((x1 - x2) ** 2 +(y1 - y2) ** 2) ** 0.5
def lengthhelp(xa, ya, xb, yb, xc, yc, xd, yd):
return length(xa, ya, xb, yb) + length(xc, yc, xb, yb) + length(xc, yc, xd, yd)
a, b, c = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
if a == 0 or b == 0:
print(abs(x1 - x2) + abs(y1 - y2))
exit()
p1x, p1y = (-c - b * y1) / a, y1
p2x, p2y = x1, (-c - a * x1) / b
p3x, p3y = (-c - b * y2) / a, y2
p4x, p4y = x2, (-c - a * x2) / b
minim = min(lengthhelp(x1, y1, p1x, p1y, p3x, p3y, x2, y2), lengthhelp(x1,y1,p1x, p1y, p4x, p4y, x2, y2))
minim2 = min(lengthhelp(x1, y1, p2x, p2y, p3x, p3y, x2, y2), lengthhelp(x1,y1,p2x, p2y, p4x, p4y, x2, y2))
ans3 = (abs(x1 - x2) + abs(y1 - y2))
print(min(min(minim, minim2), ans3))
``` | instruction | 0 | 24,756 | 1 | 49,512 |
Yes | output | 1 | 24,756 | 1 | 49,513 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0.
One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.
Input
The first line contains three integers a, b and c (-10^9≤ a, b, c≤ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue.
The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≤ x_1, y_1, x_2, y_2≤ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2).
Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
<image>
Submitted Solution:
```
from math import *
a, b, c = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
m = -1
if a == 0 or b == 0:
print(abs(x2 - x1) + abs(y2 - y1))
else:
tr = [[[x1, ((a * x1 + c) / -b)], [((b * y1 + c) / -a), y1]],
[[x2, ((a * x2 + c) / -b)], [((b * y2 + c) / -a), y2]]]
for i in range(2):
for j in range(2):
sqr = sqrt((tr[0][i][0] - tr[1][j][0])*(tr[0][i][0] - tr[1][j][0]) + (tr[0][i][1] - tr[1][j][1])*(tr[0][i][1] - tr[1][j][1]))
if m == -1:
m = abs(x1 - tr[0][i][0]) + abs(y1 - tr[0][i][1]) + abs(x2 - tr[1][j][0]) + abs(y2 - tr[1][j][1]) + sqr
else:
m = min(abs(x1 - tr[0][i][0]) + abs(y1 - tr[0][i][1]) + abs(x2 - tr[1][j][0]) + abs(y2 - tr[1][j][1]) + sqr, m)
print(min(m, abs(x2 - x1) + abs(y2 - y1)))
``` | instruction | 0 | 24,757 | 1 | 49,514 |
Yes | output | 1 | 24,757 | 1 | 49,515 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0.
One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.
Input
The first line contains three integers a, b and c (-10^9≤ a, b, c≤ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue.
The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≤ x_1, y_1, x_2, y_2≤ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2).
Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
<image>
Submitted Solution:
```
a, b, c = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
ans = abs(x1 - x2) + abs(y1 - y2)
if a == 0 or b == 0:
print(ans)
exit(0)
s1 = (x1, (-c - a * x1) / b)
s2 = ((-c - b * y1) / a, y1)
f1 = (x2, (-c - a * x2) / b)
f2 = ((-c - b * y2) / a, y2)
a1 = abs(y1 - s1[1]) + min(((s1[0] - f1[0]) ** 2 + (s1[1] - f1[1]) ** 2) ** 0.5 + abs(y2 - f1[1]), ((s1[0] - f2[0]) ** 2 + (s1[1] - f2[1]) ** 2) ** 0.5 + abs(x2 - f2[0]))
a2 = abs(x1 - s2[0]) + min(((s2[0] - f1[0]) ** 2 + (s2[1] - f1[1]) ** 2) ** 0.5 + abs(y2 - f1[1]), ((s2[0] - f2[0]) ** 2 + (s2[1] - f2[1]) ** 2) ** 0.5 + abs(x2 - f2[0]))
print(min(ans, min(a1, a2)))
``` | instruction | 0 | 24,758 | 1 | 49,516 |
Yes | output | 1 | 24,758 | 1 | 49,517 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0.
One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.
Input
The first line contains three integers a, b and c (-10^9≤ a, b, c≤ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue.
The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≤ x_1, y_1, x_2, y_2≤ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2).
Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
<image>
Submitted Solution:
```
from math import sqrt
a, b, c = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
res1 = abs(x1 - x2) + abs(y1 - y2)
res2 = 10 ** 10 + 7
res3 = 10 ** 10 + 7
res4 = 10 ** 10 + 7
res5 = 10 ** 10 + 7
if b != 0 and a != 0:
x = (-y1 * b - c) / a
y = (-x2 * a - c) / b
res2 = abs(x1 - x) + abs(y2 - y) + sqrt(abs(x - x2) ** 2 + abs(y1 - y) ** 2)
if a != 0:
x = (-y1 * b - c) / a
xx = (-y2 * b - c) / a
res4 = abs(x1 - x) + abs(x2 - xx) + sqrt(abs(x - xx) ** 2 + abs(y1 - y2) ** 2)
if b != 0:
y = (-x1 * a - c) / b
yy = (-x2 * a - c) / b
res5 = abs(y1 - y) + abs(y2 - yy) + sqrt(abs(y - yy) ** 2 + abs(x1 - x2) ** 2)
if a != 0 and b != 0:
x = (-y2 * b - c) / a
y = (-x1 * a - c) / b
res3 = abs(y1 - y) + abs(x2 - x) + sqrt(abs(x1 - x) ** 2 + abs(y2 - y) ** 2)
print(min(res1, res2, res3, res4, res5))
print(res1, res2, res3, res4, res5)
``` | instruction | 0 | 24,759 | 1 | 49,518 |
No | output | 1 | 24,759 | 1 | 49,519 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0.
One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.
Input
The first line contains three integers a, b and c (-10^9≤ a, b, c≤ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue.
The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≤ x_1, y_1, x_2, y_2≤ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2).
Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
<image>
Submitted Solution:
```
from math import ceil,floor
a,b,c = (int(e) for e in input().split(' '))
x1,y1,x2,y2 = (int(e) for e in input().split(' '))
def dist(x1,y1,x2,y2):
return ((x1-x2)**2+(y1-y2)**2)**.5
def m_dist(x1,y1,x2,y2):
return abs(x1-x2)+abs(y1-y2)
def project(x,y):
return ((-c*a+b*b*x+a*b*y)/(a*a+b*b),(-a*b*x+a*a*y-b*c)/(a*a+b*b))
def xtoy(x):
return (c-a*x)/b
def ytox(y):
return (-c-b*y)/a
point1x,point1y = project(x1,y1)
point1s = []
t = int(ceil(point1x))
point1s.append((t,xtoy(t)))
t = int(floor(point1x))
point1s.append((t,xtoy(t)))
t = int(ceil(point1y))
point1s.append((ytox(t),t))
t = int(floor(point1y))
point1s.append((ytox(t),t))
point2x,point2y = project(x2,y2)
point2s = []
t = int(ceil(point2x))
point2s.append((t,xtoy(t)))
t = int(floor(point2x))
point2s.append((t,xtoy(t)))
t = int(ceil(point2y))
point2s.append((ytox(t),t))
t = int(floor(point2y))
point2s.append((ytox(t),t))
res = float('inf')
for p1 in point1s:
for p2 in point2s:
t = m_dist(x1,y1,p1[0],p1[1])
t += dist(p1[0],p1[1],p2[0],p2[1])
t += m_dist(x2,y2,p2[0],p2[1])
if(res>t):
res = t
# print(p1,p2)
print(res)
# print(point1x,point1y)
# print(point2x,point2y)
``` | instruction | 0 | 24,760 | 1 | 49,520 |
No | output | 1 | 24,760 | 1 | 49,521 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0.
One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.
Input
The first line contains three integers a, b and c (-10^9≤ a, b, c≤ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue.
The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≤ x_1, y_1, x_2, y_2≤ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2).
Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
<image>
Submitted Solution:
```
from math import sqrt
a, b, c = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
res1 = abs(x1 - x2) + abs(y1 - y2)
res2 = 10 ** 9 + 7
res3 = 10 ** 9 + 7
res4 = 10 ** 9 + 7
res5 = 10 ** 9 + 7
if b != 0 and a != 0:
x = (-y1 * b - c) / a
y = (-x2 * a - c) / b
res2 = abs(x1 - x) + abs(y2 - y) + sqrt(abs(x - x2) ** 2 + abs(y1 - y) ** 2)
if a != 0:
x = (-y1 * b - c) / a
xx = (-y2 * b - c) / a
res4 = abs(x1 - x) + abs(x2 - xx) + sqrt(abs(x - xx) ** 2 + abs(y1 - y2) ** 2)
if b != 0:
y = (-x1 * a - c) / b
yy = (-x2 * a - c) / b
res5 = abs(y1 - y) + abs(y2 - yy) + sqrt(abs(y - yy) ** 2 + abs(x1 - x2) ** 2)
if a != 0 and b != 0:
x = (-y2 * b - c) / a
y = (-x1 * a - c) / b
res3 = abs(y1 - y) + abs(x2 - x) + sqrt(abs(x1 - x) ** 2 + abs(y2 - y) ** 2)
print(min(res1, res2, res3, res4, res5))
# print(res1, res2, res3, res4, res5)
``` | instruction | 0 | 24,761 | 1 | 49,522 |
No | output | 1 | 24,761 | 1 | 49,523 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem we consider a very simplified model of Barcelona city.
Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x, y) for which ax + by + c = 0.
One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.
Input
The first line contains three integers a, b and c (-10^9≤ a, b, c≤ 10^9, at least one of a and b is not zero) representing the Diagonal Avenue.
The next line contains four integers x_1, y_1, x_2 and y_2 (-10^9≤ x_1, y_1, x_2, y_2≤ 10^9) denoting the points A = (x_1, y_1) and B = (x_2, y_2).
Output
Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1 1 -3
0 3 3 0
Output
4.2426406871
Input
3 1 -9
0 3 3 -1
Output
6.1622776602
Note
The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.
<image>
Submitted Solution:
```
a, b, c = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
if x1 > x2 and y1 > y2:
x1, y1, x2, y2 = x2, y2, x1, y1
if x1 < x2 and y1 > y2:
x2 = x1 - (x2 - x1)
c += 2 * x1 * a
a = -a
x1, y1, x2, y2 = x2, y2, x1, y1
if x1 > x2 and y1 < y2:
x1 = x2 - (x1 - x2)
c += 2 * x2 * a
a = -a
x1, y1, x2, y2 = x2, y2, x1, y1
ans = x2 - x1 + y2 - y1
if a == 0 or b == 0:
print(ans)
xa = -(c + b * y1) / a
ya = -(c + a * x1) / b
xb = -(c + b * y2) / a
yb = -(c + a * x2) / b
if y1 <= ya <= y2 and ya <= yb:
if yb <= y2:
ans = ya - y1 + y2 - yb + ((yb - ya) * (yb - ya) + (x2 - x1) * (x2 - x1)) ** 0.5
else:
ans = ya - y1 + x2 - xb + ((y2 - ya) * (y2 - ya) + (xb - x1) * (xb - x1)) ** 0.5
print(ans)
elif x1 <= xa <= x2 and xa <= xb:
if xb <= x2:
ans = xa - x1 + x2 - xb + ((xb - xa) * (xb - xa) + (y2 - y1) * (y2 - y1)) ** 0.5
else:
ans = xa - x1 + y2 - yb + ((x2 - xa) * (x2 - xa) + (yb - y1) * (yb - y1)) ** 0.5
print(ans)
else:
print(ans)
``` | instruction | 0 | 24,762 | 1 | 49,524 |
No | output | 1 | 24,762 | 1 | 49,525 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a magical land there are n cities conveniently numbered 1, 2, ..., n. Some pairs of these cities are connected by magical colored roads. Magic is unstable, so at any time, new roads may appear between two cities.
Vicky the witch has been tasked with performing deliveries between some pairs of cities. However, Vicky is a beginner, so she can only complete a delivery if she can move from her starting city to her destination city through a double rainbow. A double rainbow is a sequence of cities c_1, c_2, ..., c_k satisfying the following properties:
* For each i with 1 ≤ i ≤ k - 1, the cities c_i and c_{i + 1} are connected by a road.
* For each i with 1 ≤ i ≤ (k - 1)/(2), the roads connecting c_{2i} with c_{2i - 1} and c_{2i + 1} have the same color.
For example if k = 5, the road between c_1 and c_2 must be the same color as the road between c_2 and c_3, and the road between c_3 and c_4 must be the same color as the road between c_4 and c_5.
Vicky has a list of events in chronological order, where each event is either a delivery she must perform, or appearance of a new road. Help her determine which of her deliveries she will be able to complete.
Input
The first line contains four integers n, m, c, and q (2 ≤ n ≤ 10^5, 1 ≤ m, c, q ≤ 10^5), denoting respectively the number of cities, the number of roads initially present, the number of different colors the roads can take, and the number of events.
Each of the following m lines contains three integers x, y, and z (1 ≤ x, y ≤ n, 1 ≤ z ≤ c), describing that there initially exists a bidirectional road with color z between cities x and y.
Then q lines follow, describing the events. Each event is one of the following two types:
1. + x y z (1 ≤ x, y ≤ n, 1 ≤ z ≤ c), meaning a road with color z appears between cities x and y;
2. ? x y (1 ≤ x, y ≤ n), meaning you should determine whether Vicky can make a delivery starting at city x and ending at city y. It is guaranteed that x ≠ y.
It is guaranteed that at any moment, there is at most one road connecting any pair of cities, and that no road connects a city to itself. It is guaranteed that the input contains at least one event of the second type.
Output
For each event of the second type, print a single line containing "Yes" (without quotes) if the delivery can be made, or a single line containing "No" (without quotes) otherwise.
Example
Input
4 3 2 4
1 2 1
2 3 1
3 4 2
? 1 4
? 4 1
+ 3 1 2
? 4 1
Output
Yes
No
Yes
Note
The following picture corresponds to the sample.
<image>
For her first delivery, Vicky can use the sequence 1, 2, 3, 4 which is a double rainbow. However, she cannot complete the second delivery, as she can only reach city 3. After adding the road between cities 1 and 3, she can now complete a delivery from city 4 to city 1 by using the double rainbow 4, 3, 1.
Submitted Solution:
```
def add_edge(graph, cnt, x, y, z):
gx = graph.setdefault(x, {})
gxz = gx.get(z)
if gxz:
gxz.add(y)
else:
yt = cnt.get(y)
if yt:
gx[z] = yt
else:
gx[z] = {y}
cnt[y] = gx[z]
def solve(rd):
n, m, c, q = map(int, next(rd).split())
graph = {}
cnt = {}
for _ in range(m):
x, y, z = map(int, next(rd).split())
add_edge(graph, cnt, x, y, z)
add_edge(graph, cnt, y, x, z)
# print(graph)
# print(cnt)
for _ in range(q):
line = next(rd).split()
if line[0] == '?':
x, y = map(int, line[1:])
yes = False
if y in cnt[x]:
yes = True
if yes is False and graph.get(y):
# print(graph[y])
for _, yt in graph[y].items():
if len(yt & cnt[x]) > 0:
yes = True
break
print('Yes' if yes else 'No')
else:
x, y, z = map(int, line[1:])
add_edge(graph, cnt, x, y, z)
add_edge(graph, cnt, y, x, z)
rd.close()
def read_in():
while True:
s = input()
c = yield s
if c is False:
break
if __name__ == '__main__':
solve(read_in())
``` | instruction | 0 | 24,823 | 1 | 49,646 |
No | output | 1 | 24,823 | 1 | 49,647 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a magical land there are n cities conveniently numbered 1, 2, ..., n. Some pairs of these cities are connected by magical colored roads. Magic is unstable, so at any time, new roads may appear between two cities.
Vicky the witch has been tasked with performing deliveries between some pairs of cities. However, Vicky is a beginner, so she can only complete a delivery if she can move from her starting city to her destination city through a double rainbow. A double rainbow is a sequence of cities c_1, c_2, ..., c_k satisfying the following properties:
* For each i with 1 ≤ i ≤ k - 1, the cities c_i and c_{i + 1} are connected by a road.
* For each i with 1 ≤ i ≤ (k - 1)/(2), the roads connecting c_{2i} with c_{2i - 1} and c_{2i + 1} have the same color.
For example if k = 5, the road between c_1 and c_2 must be the same color as the road between c_2 and c_3, and the road between c_3 and c_4 must be the same color as the road between c_4 and c_5.
Vicky has a list of events in chronological order, where each event is either a delivery she must perform, or appearance of a new road. Help her determine which of her deliveries she will be able to complete.
Input
The first line contains four integers n, m, c, and q (2 ≤ n ≤ 10^5, 1 ≤ m, c, q ≤ 10^5), denoting respectively the number of cities, the number of roads initially present, the number of different colors the roads can take, and the number of events.
Each of the following m lines contains three integers x, y, and z (1 ≤ x, y ≤ n, 1 ≤ z ≤ c), describing that there initially exists a bidirectional road with color z between cities x and y.
Then q lines follow, describing the events. Each event is one of the following two types:
1. + x y z (1 ≤ x, y ≤ n, 1 ≤ z ≤ c), meaning a road with color z appears between cities x and y;
2. ? x y (1 ≤ x, y ≤ n), meaning you should determine whether Vicky can make a delivery starting at city x and ending at city y. It is guaranteed that x ≠ y.
It is guaranteed that at any moment, there is at most one road connecting any pair of cities, and that no road connects a city to itself. It is guaranteed that the input contains at least one event of the second type.
Output
For each event of the second type, print a single line containing "Yes" (without quotes) if the delivery can be made, or a single line containing "No" (without quotes) otherwise.
Example
Input
4 3 2 4
1 2 1
2 3 1
3 4 2
? 1 4
? 4 1
+ 3 1 2
? 4 1
Output
Yes
No
Yes
Note
The following picture corresponds to the sample.
<image>
For her first delivery, Vicky can use the sequence 1, 2, 3, 4 which is a double rainbow. However, she cannot complete the second delivery, as she can only reach city 3. After adding the road between cities 1 and 3, she can now complete a delivery from city 4 to city 1 by using the double rainbow 4, 3, 1.
Submitted Solution:
```
n, m, c , q = [int(i) for i in input().split(' ')]
from collections import *
lookup = defaultdict(list)
colors = defaultdict(int)
def checkcolor(path, colors):
# print('path', path)
if not path:
return True
if len(path) <= 2:
return True
for i in range(1, (len(path) - 1)//2+1):
# print((path[2*i-1-1], path[2*i-1]), (path[2*i-1], path[2*i+1-1]))
if colors[(path[2*i-1-1], path[2*i-1])] != colors[(path[2*i-1], path[2*i+1-1])]:
return False
return True
def hel(map, colors, x, y):
# print(map, x, y, map[x])
def rec(curpath, node, cache):
# print(curpath, node, cache)
if node in cache:
return False
# print('s')
if node == y:
# print('sssssss')
if checkcolor(curpath+[node], colors):
return True
else:
return False
# print('ss')
cache.add(node)
for i in map[node]:
# print('ssss', i)
if rec(curpath+[node], i, cache):
return True
cache.remove(node)
return False
return rec([], x, set())
"""
4 3 2 4
1 2 1
2 3 1
3 4 2
? 1 4
? 4 1
+ 3 1 2
? 4 1
4 3 2 1
1 2 1
2 3 1
3 4 2
? 4 1
"""
for _ in range(m):
x, y, z = [int(i) for i in input().split(' ')]
lookup[x].append(y)
lookup[y].append(x)
colors[(x, y)] = z
colors[(y, x)] = z
for _ in range(q):
tmp = input().split(' ')
if tmp[0] == '?':
print('YES' if hel(lookup, colors, int(tmp[1]), int(tmp[2])) else 'NO')
else:
lookup[int(tmp[1])].append(int(tmp[2]))
lookup[int(tmp[2])].append(int(tmp[1]))
colors[(int(tmp[1]), int(tmp[2]))] = int(tmp[3])
colors[(int(tmp[2]), int(tmp[1]))] = int(tmp[3])
# print(colors)
``` | instruction | 0 | 24,824 | 1 | 49,648 |
No | output | 1 | 24,824 | 1 | 49,649 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a magical land there are n cities conveniently numbered 1, 2, ..., n. Some pairs of these cities are connected by magical colored roads. Magic is unstable, so at any time, new roads may appear between two cities.
Vicky the witch has been tasked with performing deliveries between some pairs of cities. However, Vicky is a beginner, so she can only complete a delivery if she can move from her starting city to her destination city through a double rainbow. A double rainbow is a sequence of cities c_1, c_2, ..., c_k satisfying the following properties:
* For each i with 1 ≤ i ≤ k - 1, the cities c_i and c_{i + 1} are connected by a road.
* For each i with 1 ≤ i ≤ (k - 1)/(2), the roads connecting c_{2i} with c_{2i - 1} and c_{2i + 1} have the same color.
For example if k = 5, the road between c_1 and c_2 must be the same color as the road between c_2 and c_3, and the road between c_3 and c_4 must be the same color as the road between c_4 and c_5.
Vicky has a list of events in chronological order, where each event is either a delivery she must perform, or appearance of a new road. Help her determine which of her deliveries she will be able to complete.
Input
The first line contains four integers n, m, c, and q (2 ≤ n ≤ 10^5, 1 ≤ m, c, q ≤ 10^5), denoting respectively the number of cities, the number of roads initially present, the number of different colors the roads can take, and the number of events.
Each of the following m lines contains three integers x, y, and z (1 ≤ x, y ≤ n, 1 ≤ z ≤ c), describing that there initially exists a bidirectional road with color z between cities x and y.
Then q lines follow, describing the events. Each event is one of the following two types:
1. + x y z (1 ≤ x, y ≤ n, 1 ≤ z ≤ c), meaning a road with color z appears between cities x and y;
2. ? x y (1 ≤ x, y ≤ n), meaning you should determine whether Vicky can make a delivery starting at city x and ending at city y. It is guaranteed that x ≠ y.
It is guaranteed that at any moment, there is at most one road connecting any pair of cities, and that no road connects a city to itself. It is guaranteed that the input contains at least one event of the second type.
Output
For each event of the second type, print a single line containing "Yes" (without quotes) if the delivery can be made, or a single line containing "No" (without quotes) otherwise.
Example
Input
4 3 2 4
1 2 1
2 3 1
3 4 2
? 1 4
? 4 1
+ 3 1 2
? 4 1
Output
Yes
No
Yes
Note
The following picture corresponds to the sample.
<image>
For her first delivery, Vicky can use the sequence 1, 2, 3, 4 which is a double rainbow. However, she cannot complete the second delivery, as she can only reach city 3. After adding the road between cities 1 and 3, she can now complete a delivery from city 4 to city 1 by using the double rainbow 4, 3, 1.
Submitted Solution:
```
print('Yes')
``` | instruction | 0 | 24,825 | 1 | 49,650 |
No | output | 1 | 24,825 | 1 | 49,651 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a magical land there are n cities conveniently numbered 1, 2, ..., n. Some pairs of these cities are connected by magical colored roads. Magic is unstable, so at any time, new roads may appear between two cities.
Vicky the witch has been tasked with performing deliveries between some pairs of cities. However, Vicky is a beginner, so she can only complete a delivery if she can move from her starting city to her destination city through a double rainbow. A double rainbow is a sequence of cities c_1, c_2, ..., c_k satisfying the following properties:
* For each i with 1 ≤ i ≤ k - 1, the cities c_i and c_{i + 1} are connected by a road.
* For each i with 1 ≤ i ≤ (k - 1)/(2), the roads connecting c_{2i} with c_{2i - 1} and c_{2i + 1} have the same color.
For example if k = 5, the road between c_1 and c_2 must be the same color as the road between c_2 and c_3, and the road between c_3 and c_4 must be the same color as the road between c_4 and c_5.
Vicky has a list of events in chronological order, where each event is either a delivery she must perform, or appearance of a new road. Help her determine which of her deliveries she will be able to complete.
Input
The first line contains four integers n, m, c, and q (2 ≤ n ≤ 10^5, 1 ≤ m, c, q ≤ 10^5), denoting respectively the number of cities, the number of roads initially present, the number of different colors the roads can take, and the number of events.
Each of the following m lines contains three integers x, y, and z (1 ≤ x, y ≤ n, 1 ≤ z ≤ c), describing that there initially exists a bidirectional road with color z between cities x and y.
Then q lines follow, describing the events. Each event is one of the following two types:
1. + x y z (1 ≤ x, y ≤ n, 1 ≤ z ≤ c), meaning a road with color z appears between cities x and y;
2. ? x y (1 ≤ x, y ≤ n), meaning you should determine whether Vicky can make a delivery starting at city x and ending at city y. It is guaranteed that x ≠ y.
It is guaranteed that at any moment, there is at most one road connecting any pair of cities, and that no road connects a city to itself. It is guaranteed that the input contains at least one event of the second type.
Output
For each event of the second type, print a single line containing "Yes" (without quotes) if the delivery can be made, or a single line containing "No" (without quotes) otherwise.
Example
Input
4 3 2 4
1 2 1
2 3 1
3 4 2
? 1 4
? 4 1
+ 3 1 2
? 4 1
Output
Yes
No
Yes
Note
The following picture corresponds to the sample.
<image>
For her first delivery, Vicky can use the sequence 1, 2, 3, 4 which is a double rainbow. However, she cannot complete the second delivery, as she can only reach city 3. After adding the road between cities 1 and 3, she can now complete a delivery from city 4 to city 1 by using the double rainbow 4, 3, 1.
Submitted Solution:
```
def add_edge(graph, cnt, x, y, z):
gx = graph.setdefault(x, {})
gxz = gx.get(z)
if gxz:
gxz.add(y)
cnt.setdefault(y, gxz)
else:
yt = cnt.get(y)
if yt:
gx[z] = yt
else:
gx[z] = {y}
cnt[y] = gx[z]
def solve(rd):
n, m, c, q = map(int, next(rd).split())
graph = {}
cnt = {}
for _ in range(m):
x, y, z = map(int, next(rd).split())
add_edge(graph, cnt, x, y, z)
add_edge(graph, cnt, y, x, z)
# print(cnt)
# print(graph)
for _ in range(q):
line = next(rd).split()
if line[0] == '?':
x, y = map(int, line[1:])
yes = False
if y in cnt[x]:
yes = True
if yes is False and graph.get(y):
# print(graph[y])
for _, yt in graph[y].items():
if len(yt & cnt[x]) > 0:
yes = True
break
print('Yes' if yes else 'No')
else:
x, y, z = map(int, line[1:])
add_edge(graph, cnt, x, y, z)
add_edge(graph, cnt, y, x, z)
rd.close()
def read_in():
while True:
s = input()
c = yield s
if c is False:
break
if __name__ == '__main__':
solve(read_in())
``` | instruction | 0 | 24,826 | 1 | 49,652 |
No | output | 1 | 24,826 | 1 | 49,653 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
Input
The first line of the input contains two positive integers, n and m — the number of the cities and the number of roads in Berland (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers xi, yi (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
Output
Print a single integer — the minimum number of separated cities after the reform.
Examples
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
Note
In the first sample the following road orientation is allowed: <image>, <image>, <image>.
The second sample: <image>, <image>, <image>, <image>, <image>.
The third sample: <image>, <image>, <image>, <image>, <image>. | instruction | 0 | 25,188 | 1 | 50,376 |
Tags: data structures, dfs and similar, dsu, graphs, greedy
Correct Solution:
```
import sys
input = sys.stdin.buffer.readline
def dfs(l):
a = [0]*len(l)
counter = 0
for i in range(len(l)):
if a[i] != 1:
fl = True
stack = [[i, 0]]
while stack:
x, par = stack.pop()
a[x] = 1
for j in l[x]:
if j == par:
continue
elif a[j] == 1:
fl = False
a[j] = 1
stack = []
break
else:
a[j] = 1
stack.append([j, x])
if fl:
counter += 1
return counter
n, m = list(map(int, input().split()))
l = {i: [] for i in range(n)}
for i in range(m):
x, y = list(map(int, input().split()))
l[x-1].append(y-1)
l[y-1].append(x-1)
ans = dfs(l)
print(ans)
``` | output | 1 | 25,188 | 1 | 50,377 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.