message stringlengths 2 43.5k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 853 107k | cluster float64 24 24 | __index_level_0__ int64 1.71k 214k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1 β€ t β€ 10^4) β the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1 β€ n β€ 10^9) β how many years Polycarp has turned.
Output
Print t integers β the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11. | instruction | 0 | 60,774 | 24 | 121,548 |
Tags: implementation
Correct Solution:
```
from collections import defaultdict, deque
from itertools import permutations
from sys import stdin,stdout
from bisect import bisect_left, bisect_right
from copy import deepcopy
import os,sys
int_input=lambda : int(stdin.readline())
string_input=lambda : stdin.readline()
multi_int_input =lambda : map(int, stdin.readline().split())
multi_input = lambda : stdin.readline().split()
list_input=lambda : list(map(int,stdin.readline().split()))
string_list_input=lambda: list(string_input())
MOD = pow(10,9)+7
# stdin = open(os.path.join(sys.path[0],'input.in'),'r')
# sys.stdout = open(os.path.join(sys.path[0],'output2.in'),'w')
test = int_input()
arr = [9,18,27,36,45,54,63,72,81]
for _ in range(test):
n = int_input()
if n<=9:
print(n)
else:
string_no = str(n)
length_no = len(string_no)
first_digit = int(string_no[0])
maximum = str(first_digit)*length_no
maximum = int(maximum)
if maximum<=n:
print(arr[length_no-2]+first_digit)
else:
print(arr[length_no-2]+(first_digit-1))
``` | output | 1 | 60,774 | 24 | 121,549 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1 β€ t β€ 10^4) β the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1 β€ n β€ 10^9) β how many years Polycarp has turned.
Output
Print t integers β the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11. | instruction | 0 | 60,775 | 24 | 121,550 |
Tags: implementation
Correct Solution:
```
n = int(input())
for a in range(n):
x = input()
l = int(len(x))
count = 9*(l-1)
x = int(x)
count += x//((10**l-1)//9)
print(count)
``` | output | 1 | 60,775 | 24 | 121,551 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1 β€ t β€ 10^4) β the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1 β€ n β€ 10^9) β how many years Polycarp has turned.
Output
Print t integers β the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11. | instruction | 0 | 60,776 | 24 | 121,552 |
Tags: implementation
Correct Solution:
```
from math import *
def r1(t):
return t(input())
def r2(t):
return [t(i) for i in input().split()]
for _ in range(r1(int)):
n = r1(int)
ans = 0
for i in range(1, 10):
cn = i
while cn <= n:
ans += 1
cn = cn*10 + i
print(int(ans))
``` | output | 1 | 60,776 | 24 | 121,553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1 β€ t β€ 10^4) β the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1 β€ n β€ 10^9) β how many years Polycarp has turned.
Output
Print t integers β the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11.
Submitted Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
k=len(str(n))
k1=""
for i in range(k):
k1+="1"
c=9*(k-1)
k1=int(k1)
k2=k1
while((k1)<=n):
(k1)=(k1)+(k2)
c+=1
print(c)
``` | instruction | 0 | 60,777 | 24 | 121,554 |
Yes | output | 1 | 60,777 | 24 | 121,555 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1 β€ t β€ 10^4) β the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1 β€ n β€ 10^9) β how many years Polycarp has turned.
Output
Print t integers β the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11.
Submitted Solution:
```
t=int(input())
for i in range(t):
n=(input())
s=n[0]*len(n)
k=(len(n)-1)*9
k+=(int(n[0])-1)
if int(n)>=int(s):
k+=1
print(k)
``` | instruction | 0 | 60,778 | 24 | 121,556 |
Yes | output | 1 | 60,778 | 24 | 121,557 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1 β€ t β€ 10^4) β the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1 β€ n β€ 10^9) β how many years Polycarp has turned.
Output
Print t integers β the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11.
Submitted Solution:
```
t = int(input())
for i in range(t):
n = int(input())
L = len(str(n))
ans = (L - 1) * 9
for j in range(1,10):
if n >= int(str(j) * L):
ans += 1
print(ans)
``` | instruction | 0 | 60,779 | 24 | 121,558 |
Yes | output | 1 | 60,779 | 24 | 121,559 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1 β€ t β€ 10^4) β the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1 β€ n β€ 10^9) β how many years Polycarp has turned.
Output
Print t integers β the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11.
Submitted Solution:
```
t = int(input(''))
for v in range(t):
n = input('')
l = len(n)
ans = 9*(l-1)
s = int(n[0])
if(l == 1):
print(s)
else:
i = 1
res = True
while(i < l):
if(int(n[i]) > s):
res = True
break
elif(int(n[i]) < s):
res = False
break
else:
i = i+1
if(res):
print(ans + s)
else:
print(ans + s-1)
``` | instruction | 0 | 60,780 | 24 | 121,560 |
Yes | output | 1 | 60,780 | 24 | 121,561 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1 β€ t β€ 10^4) β the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1 β€ n β€ 10^9) β how many years Polycarp has turned.
Output
Print t integers β the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11.
Submitted Solution:
```
t=int(input())
for i in range(t):
n=input()
y=list(n)
x=len(y)
a=(x-1)*9
for i in range(1,9):
if(int(str(i)*x)<=int(n)):
a=a+1
print(a)
``` | instruction | 0 | 60,781 | 24 | 121,562 |
No | output | 1 | 60,781 | 24 | 121,563 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1 β€ t β€ 10^4) β the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1 β€ n β€ 10^9) β how many years Polycarp has turned.
Output
Print t integers β the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11.
Submitted Solution:
```
for i in range(int(input())):
n=input()
l=len(n)
k=1
for i in n:
if int(i)<int(n[0]):
k=0
break
s=9*(l-1)+int(n[0])-1
if k==1:
s+=1
print(s)
``` | instruction | 0 | 60,782 | 24 | 121,564 |
No | output | 1 | 60,782 | 24 | 121,565 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1 β€ t β€ 10^4) β the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1 β€ n β€ 10^9) β how many years Polycarp has turned.
Output
Print t integers β the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 25 01:44:59 2020
@author: Dark Soul
"""
for _ in range(int(input(''))):
s=list(input(''))
l=len(s)
s.sort()
if l==0:
l=1
print(9*(l-1)+(int(s[0])==int(s[l-1]))*int(s[0]))
``` | instruction | 0 | 60,783 | 24 | 121,566 |
No | output | 1 | 60,783 | 24 | 121,567 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1 β€ t β€ 10^4) β the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1 β€ n β€ 10^9) β how many years Polycarp has turned.
Output
Print t integers β the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 25 01:44:59 2020
@author: Dark Soul
"""
for _ in range(int(input(''))):
s=list(input(''))
l=len(s)
if l==1:
print(int(s[0]))
continue
ans=9*(l-1)
ans+=int(s[0])-1
s.sort()
if s[0]==s[l-1]:
ans+=1
print(ans)
``` | instruction | 0 | 60,784 | 24 | 121,568 |
No | output | 1 | 60,784 | 24 | 121,569 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4):
Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png)
1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4];
2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]);
3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4];
4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5) β the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1 β€ u, v β€ n, u β v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear).
The following line contains one integer k (2 β€ k β€ n) β the number of intersections in Polycarp's path from home to his workplace.
The last line contains k integers p_1, p_2, ..., p_k (1 β€ p_i β€ n, all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i β [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
Input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
Output
1 2
Input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
Output
0 0
Input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
Output
0 3 | instruction | 0 | 60,785 | 24 | 121,570 |
Tags: dfs and similar, graphs, shortest paths
Correct Solution:
```
from collections import defaultdict, deque
from sys import stdin
input = stdin.readline
if __name__ == '__main__':
n, m = map(int, input().split())
err = [tuple(map(int, input().split())) for _ in range(m)]
k = int(input())
prr = list(map(int, input().split()))
# Save reversed paths because we need BFS from t
dct = defaultdict(lambda: [])
for u, v in err:
dct[v].append(u)
# BFS from t
lim = 10 ** 6
cdct = defaultdict(lambda: (lim, 0))
r = prr[-1]
viz = {r}
q = deque(viz)
cdct[r] = (0, 1)
while q:
r = q.popleft()
d = cdct[r][0]
for c in dct[r]:
if c not in viz:
q.append(c)
viz.add(c)
cd, cp = cdct[c]
if d + 1 <= cd:
cdct[c] = d + 1, cp + 1
# For every step check if this has decreased distance
# if it did, min += 0
# otherwise min += 1
# For max:
# If there is more than one way -> Assume he has chosen the alternate way
# which makes max += 1
# however, if he actually chooses a path which is not optimal
# that has to be considered
# so we can decrease 1 if last p had more than one way
d = cdct[prr[0]][0] + 1
mn = 0
mx = 0
for i, p in enumerate(prr):
cd = cdct[p]
imn = int(cd[0] >= d)
mn += imn
d = cd[0]
if imn and cdct[prr[i - 1]][1] == 1:
mx += 1
mx += int(cd[1] > 1)
print(mn, mx)
``` | output | 1 | 60,785 | 24 | 121,571 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4):
Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png)
1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4];
2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]);
3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4];
4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5) β the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1 β€ u, v β€ n, u β v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear).
The following line contains one integer k (2 β€ k β€ n) β the number of intersections in Polycarp's path from home to his workplace.
The last line contains k integers p_1, p_2, ..., p_k (1 β€ p_i β€ n, all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i β [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
Input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
Output
1 2
Input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
Output
0 0
Input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
Output
0 3 | instruction | 0 | 60,786 | 24 | 121,572 |
Tags: dfs and similar, graphs, shortest paths
Correct Solution:
```
n, m = map(int, input().split())
Q = [[]for _ in range(n)]
for _ in range(m):
u, v = map(int, input().split())
u -= 1
v -= 1
Q[v].append(u)
k = int(input())
p = [int(T) - 1 for T in input().split()]
W = [-1] * n
E = [0] * n
q = [(p[-1], 0)]
for u, d in q:
if W[u] < 0:
W[u] = d
d += 1
for v in Q[u]:
q.append((v, d))
elif W[u] == d:
E[u] += 1
R = S = 0
for i in range(1, k):
u, v = p[i - 1], p[i]
if W[u] <= W[v]:
R += 1
S += 1
elif E[u]:
S += 1
print(R, S)
``` | output | 1 | 60,786 | 24 | 121,573 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4):
Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png)
1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4];
2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]);
3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4];
4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5) β the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1 β€ u, v β€ n, u β v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear).
The following line contains one integer k (2 β€ k β€ n) β the number of intersections in Polycarp's path from home to his workplace.
The last line contains k integers p_1, p_2, ..., p_k (1 β€ p_i β€ n, all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i β [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
Input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
Output
1 2
Input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
Output
0 0
Input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
Output
0 3 | instruction | 0 | 60,787 | 24 | 121,574 |
Tags: dfs and similar, graphs, shortest paths
Correct Solution:
```
# from debug import debug
inf = int(1e10)
n, m = map(int, input().split())
trans = [[] for i in range(n)]
graph = [[] for i in range(n)]
for i in range(m):
a,b = map(int, input().split())
trans[b-1].append(a-1)
graph[a-1].append(b-1)
k = int(input())
lis = [x-1 for x in map(int, input().split())]
s, d = lis[0], lis[-1]
v = [0]*n
dis = [inf]*n
v[d] = 1
dis[d] = 0
q = [d]
while q:
node = q.pop(0)
for i in trans[node]:
if not v[i] and dis[i] > dis[node]+1:
dis[i] = dis[node] + 1
q.append(i)
reb, ad = 0, 0
prev = s
for i in range(1, k-1):
if dis[lis[i]] + 1 > dis[prev]:
reb+=1
for j in graph[prev]:
if j != lis[i] and dis[j] + 1 == dis[prev]:
ad+=1
break
prev = lis[i]
print(reb, ad)
``` | output | 1 | 60,787 | 24 | 121,575 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4):
Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png)
1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4];
2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]);
3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4];
4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5) β the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1 β€ u, v β€ n, u β v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear).
The following line contains one integer k (2 β€ k β€ n) β the number of intersections in Polycarp's path from home to his workplace.
The last line contains k integers p_1, p_2, ..., p_k (1 β€ p_i β€ n, all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i β [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
Input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
Output
1 2
Input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
Output
0 0
Input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
Output
0 3 | instruction | 0 | 60,788 | 24 | 121,576 |
Tags: dfs and similar, graphs, shortest paths
Correct Solution:
```
from collections import deque
from sys import stdin
nV, nE = map(int, stdin.readline().split())
g = [[] for _ in range(nV + 1)]
rev = [[] for _ in range(nV + 1)]
for _ in range(nE):
u, v = map(int, stdin.readline().split())
g[u].append(v)
rev[v].append(u)
k = int(stdin.readline())
path = list(map(int, stdin.readline().split()))
def bfs(s, g):
UNDEF = -1
dist = [UNDEF] * (nV + 1)
dist[s] = 0
q = deque([s])
while q:
v = q.pop()
for to in g[v]:
if dist[to] == UNDEF:
dist[to] = 1 + dist[v]
q.appendleft(to)
return dist
dist = bfs(path[-1], rev)
mn = mx = 0
prev = None
for v in path:
if prev and dist[v] > dist[prev] - 1:
mn += 1
mx += 1
if prev and dist[v] == dist[prev] - 1:
for to in g[prev]:
if to != v and dist[to] == dist[v]:
mx += 1
break
prev = v
print('%d %d' % (mn, mx))
``` | output | 1 | 60,788 | 24 | 121,577 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4):
Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png)
1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4];
2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]);
3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4];
4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5) β the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1 β€ u, v β€ n, u β v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear).
The following line contains one integer k (2 β€ k β€ n) β the number of intersections in Polycarp's path from home to his workplace.
The last line contains k integers p_1, p_2, ..., p_k (1 β€ p_i β€ n, all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i β [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
Input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
Output
1 2
Input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
Output
0 0
Input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
Output
0 3 | instruction | 0 | 60,789 | 24 | 121,578 |
Tags: dfs and similar, graphs, shortest paths
Correct Solution:
```
# Legends Always Come Up with Solution
# Author: Manvir Singh
import os
import sys
from io import BytesIO, IOBase
from collections import deque
def bfs(tree,s):
n=len(tree)
h=[0]*(n+1)
v=[0]*(n+1)
q=deque()
q.append((s,0))
v[s]=1
while q:
c,p=q.popleft()
h[c]=h[p]+1
for i in tree[c]:
if not v[i]:
q.append((i,c))
v[i]=1
return h
def main():
n,m=map(int,input().split())
tree=[[] for _ in range(n+1)]
tree1=[[] for _ in range(n+1)]
for _ in range(m):
x,y=map(int,input().split())
tree[x].append(y)
tree1[y].append(x)
k=int(input())
a=list(map(int,input().split()))
h,mi,ma=bfs(tree1,a[-1]),0,0
for i in range(k-1):
if h[a[i+1]]>h[a[i]]-1:
mi,ma=mi+1,ma+1
else:
f=0
for j in tree[a[i]]:
if h[j]==h[a[i]]-1 and j!=a[i+1]:
f=1
break
ma+=f
print(mi,ma)
# region fastio
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")
if __name__ == "__main__":
main()
``` | output | 1 | 60,789 | 24 | 121,579 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4):
Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png)
1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4];
2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]);
3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4];
4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5) β the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1 β€ u, v β€ n, u β v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear).
The following line contains one integer k (2 β€ k β€ n) β the number of intersections in Polycarp's path from home to his workplace.
The last line contains k integers p_1, p_2, ..., p_k (1 β€ p_i β€ n, all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i β [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
Input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
Output
1 2
Input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
Output
0 0
Input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
Output
0 3 | instruction | 0 | 60,790 | 24 | 121,580 |
Tags: dfs and similar, graphs, shortest paths
Correct Solution:
```
import sys
n, m = list(map(int, sys.stdin.readline().strip().split()))
N = [[] for i in range (0, n+1)]
N2 = [[] for i in range (0, n+1)]
for i in range (0, m):
u, v = list(map(int, sys.stdin.readline().strip().split()))
N[u].append(v)
N2[v].append(u)
D = [n+10] * (n+1)
k = int(sys.stdin.readline().strip())
p = list(map(int, sys.stdin.readline().strip().split()))
D[p[k-1]] = 0
L = [p[k-1]]
i = 0
while i < len(L):
u = L[i]
for v in N2[u]:
if D[v] > D[u] + 1:
D[v] = D[u] + 1
L.append(v)
i = i + 1
m = 0
M = 0
for i in range (0, k-1):
q = p[i]
r = p[i+1]
if D[q] != 1 + D[r]:
m = m + 1
M = M + 1
c = 0
for v in N[q]:
if D[v] + 1 == D[q]:
c = c + 1
if c != 1 and D[q] == 1 + D[r]:
M = M + 1
print(m, M)
``` | output | 1 | 60,790 | 24 | 121,581 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4):
Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png)
1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4];
2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]);
3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4];
4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5) β the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1 β€ u, v β€ n, u β v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear).
The following line contains one integer k (2 β€ k β€ n) β the number of intersections in Polycarp's path from home to his workplace.
The last line contains k integers p_1, p_2, ..., p_k (1 β€ p_i β€ n, all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i β [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
Input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
Output
1 2
Input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
Output
0 0
Input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
Output
0 3 | instruction | 0 | 60,791 | 24 | 121,582 |
Tags: dfs and similar, graphs, shortest paths
Correct Solution:
```
from types import GeneratorType
INF = 1e10
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
class Graph:
def __init__(self, V):
self.V = V
self.adj = [[] for i in range(V)]
def addEdge(self, v, w):
self.adj[v].append(w)
import sys
input = sys.stdin.readline
n, m = list(map(int, input().split()))
G = Graph(n)
G_transpose = Graph(n)
for _ in range(m):
u, v = list(map(int, input().split()))
G.addEdge(u - 1, v - 1)
G_transpose.addEdge(v - 1, u - 1)
minimums = [0 for _ in range(n)]
mini = [0 for _ in range(n)]
from collections import deque
def BFS_SP(graph, start):
explored = set()
queue = deque([start])
dz = set()
while queue:
node = queue.popleft()
if node not in explored:
neighbours = graph.adj[node]
# print(neighbours)
# print(sd)
for neighbour in neighbours:
if neighbour not in dz:
sd[neighbour] = sd[node] + 1
minimums[neighbour] = sd[node] + 1
mini[neighbour] = 1
dz.add(neighbour)
queue.append(neighbour)
else:
# print(node,neighbour,sd[node],minimums[neighbour])
if sd[node] + 1 == minimums[neighbour]:
mini[neighbour] += 1
explored.add(node)
k = int(input())
path = list(map(int, input().split()))
sd = [INF for _ in range(n)]
sd[path[-1] - 1] = 0
BFS_SP(G_transpose, path[-1] - 1)
ans_min = 0
ans_max = 0
# print(distances,child_distances_from_goal)
# print(path)
for i in range(0, len(path) - 1):
# print(i)
next_dist = sd[path[i + 1] - 1]
# print(all_distances)
if sd[path[i] - 1] == sd[path[i + 1] - 1] + 1:
if mini[path[i] - 1] > 1:
ans_max += 1
else:
ans_min += 1
ans_max += 1
# print(ans_min, ans_max)
print(ans_min - 1, ans_max - 1)
#
``` | output | 1 | 60,791 | 24 | 121,583 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4):
Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png)
1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4];
2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]);
3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4];
4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5) β the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1 β€ u, v β€ n, u β v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear).
The following line contains one integer k (2 β€ k β€ n) β the number of intersections in Polycarp's path from home to his workplace.
The last line contains k integers p_1, p_2, ..., p_k (1 β€ p_i β€ n, all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i β [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
Input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
Output
1 2
Input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
Output
0 0
Input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
Output
0 3 | instruction | 0 | 60,792 | 24 | 121,584 |
Tags: dfs and similar, graphs, shortest paths
Correct Solution:
```
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
from heapq import heappop, heappush
def multi_dijkstra(G, starts):
n = len(G)
dist = [float("inf")]*n
visited = set()
heap = []
for i in starts:
heap.append((0, i))
while heap and len(visited) != n:
d, cur = heappop(heap)
if cur in visited:
continue
visited.add(cur)
dist[cur] = d
for to in G[cur]:
if to not in visited:
heappush(heap, (d+1, to))
return dist
n,m = map(int, input().split())
G = [[] for i in range(n)]
Gr = [[] for i in range(n)]
for i in range(m):
a,b = map(int, input().split())
a,b = a-1,b-1
Gr[a].append(b)
G[b].append(a)
k = int(input())
p = list(map(int, input().split()))
p = [i-1 for i in p]
s,t = p[0], p[-1]
dist = multi_dijkstra(G, [t])
mi = ma = 0
cur = s
for i in range(1, k-1):
nxt = p[i]
d = dist[cur]
f = False
dists = [dist[to] for to in Gr[cur]]
d_min = min(dists)
if dist[nxt] != d_min:
mi += 1
ma += 1
else:
if dists.count(d_min) > 1:
ma += 1
cur = nxt
print(mi, ma)
``` | output | 1 | 60,792 | 24 | 121,585 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4):
Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png)
1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4];
2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]);
3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4];
4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5) β the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1 β€ u, v β€ n, u β v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear).
The following line contains one integer k (2 β€ k β€ n) β the number of intersections in Polycarp's path from home to his workplace.
The last line contains k integers p_1, p_2, ..., p_k (1 β€ p_i β€ n, all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i β [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
Input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
Output
1 2
Input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
Output
0 0
Input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
Output
0 3
Submitted Solution:
```
from collections import defaultdict, deque
n, m = map(int, input().split())
e = defaultdict(list)
for _ in range(m):
a, b = map(int, input().split())
e[b].append(a)
plen, path = int(input()), list(map(int, input().split()))
tg = path[-1]
pathfrom = defaultdict(set)
pathsz = defaultdict(int)
q = deque([(tg, tg, 1)])
while q:
frm, to, sz = q.popleft()
if pathsz[to] == 0:
pathsz[to] = sz
pathfrom[to].add(frm)
for u in e[to]:
q.append((to, u, sz+1))
elif pathsz[to] == sz:
pathfrom[to].add(frm)
mn, mx = 0, 0
for i in range(plen-1):
u, v = path[i], path[i+1]
if v in pathfrom[u]:
if len(pathfrom[u]) > 1:
mx += 1
else:
mn += 1; mx += 1
print(mn, mx)
``` | instruction | 0 | 60,793 | 24 | 121,586 |
Yes | output | 1 | 60,793 | 24 | 121,587 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4):
Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png)
1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4];
2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]);
3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4];
4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5) β the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1 β€ u, v β€ n, u β v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear).
The following line contains one integer k (2 β€ k β€ n) β the number of intersections in Polycarp's path from home to his workplace.
The last line contains k integers p_1, p_2, ..., p_k (1 β€ p_i β€ n, all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i β [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
Input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
Output
1 2
Input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
Output
0 0
Input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
Output
0 3
Submitted Solution:
```
from collections import deque
from collections import defaultdict
no_nodes, e = list(map(int, input().strip().split()))
edges = []
for _ in range(e):
u, v = list(map(int, input().strip().split()))
edges.append((u, v))
k = int(input().strip())
given_path = list(map(int, input().strip().split()))
dest = given_path[-1]
class BFS_Graph:
def __init__(self, no_nodes):
self.graph = defaultdict(list)
self.no_nodes = no_nodes+1
def add_edge(self, u, v):
self.graph[u].append(v)
def add_edges_from_list(self, edges):
for edge in edges:
self.add_edge(edge[1], edge[0])
def BFS(self, source, destination=-1):
visited = [False]*self.no_nodes
pred = [-1]*self.no_nodes
dist = [float("inf")]*self.no_nodes
visited[source] = True
dist[source] = 0
queue = deque()
queue.append(source)
while queue:
u = queue.popleft()
if u == destination:
break
for i in self.graph[u]:
if visited[i] == False:
visited[i] = True
dist[i] = dist[u] + 1
pred[i] = [u]
queue.append(i)
else:
if dist[i] == dist[u] + 1:
pred[i].append(u)
return pred, dist
g = BFS_Graph(no_nodes)
g.add_edges_from_list(edges)
pred, dist = g.BFS(dest)
# print(pred)
# print(dist)
definite = []
possible = []
for i in range(1,k-1):
pre_node = given_path[i-1]
current_node = given_path[i]
if dist[current_node]>=dist[pre_node]:
definite.append(current_node)
else:
if len(pred[pre_node])>1:
possible.append(current_node)
# print(definite)
# print(possible)
max_val = len(definite) +len(possible)
min_val = len(definite)
print(min_val,max_val)
``` | instruction | 0 | 60,794 | 24 | 121,588 |
Yes | output | 1 | 60,794 | 24 | 121,589 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4):
Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png)
1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4];
2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]);
3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4];
4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5) β the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1 β€ u, v β€ n, u β v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear).
The following line contains one integer k (2 β€ k β€ n) β the number of intersections in Polycarp's path from home to his workplace.
The last line contains k integers p_1, p_2, ..., p_k (1 β€ p_i β€ n, all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i β [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
Input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
Output
1 2
Input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
Output
0 0
Input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
Output
0 3
Submitted Solution:
```
from sys import stdin, stdout
import math
n,m = map(int,stdin.readline().rstrip().split(' '))
roads = [[] for _ in range(n)]
roadsTo = [[] for _ in range(n)]
for _ in range(m):
u,v = map(int,stdin.readline().rstrip().split(' '))
u-=1
v-=1
roads[v].append(u)
roadsTo[u].append(v)
k = int(stdin.readline().rstrip())
p = list(map(int,stdin.readline().rstrip().split(' ')))
p = [i-1 for i in p]
visited = set([p[k-1]])
lastBatch = set([p[k-1]])
minLen = [0]*n
while len(visited)<n:
batch = set()
for i in lastBatch:
for j in roads[i]:
if j not in visited:
visited.add(j)
minLen[j] = minLen[i]+1
batch.add(j)
lastBatch = batch
minChg = 0
maxChg = 0
for i in range(len(p)-1):
nextUp = minLen[p[i+1]]
otherQuickest = min([minLen[j] for j in roadsTo[p[i]] if j!=p[i+1]]+[999999])
if otherQuickest<nextUp:
minChg+=1
maxChg+=1
elif otherQuickest==nextUp:
maxChg+=1
print(str(minChg)+' '+str(maxChg))
``` | instruction | 0 | 60,795 | 24 | 121,590 |
Yes | output | 1 | 60,795 | 24 | 121,591 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4):
Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png)
1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4];
2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]);
3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4];
4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5) β the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1 β€ u, v β€ n, u β v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear).
The following line contains one integer k (2 β€ k β€ n) β the number of intersections in Polycarp's path from home to his workplace.
The last line contains k integers p_1, p_2, ..., p_k (1 β€ p_i β€ n, all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i β [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
Input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
Output
1 2
Input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
Output
0 0
Input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
Output
0 3
Submitted Solution:
```
from collections import deque
n,m = map(int,input().split())
g = [list() for _ in range(n)]
for _ in range(m):
u,v = map(int,input().split())
u,v = u-1,v-1
g[v].append(u)
k = int(input())
path = list(map(int,input().split()))
for i in range(k):
path[i] -= 1
q = deque([])
q.append(path[-1])
dist = [-1]*(n)
isv = [-1]*(n)
dist[q[0]] = 0
while q:
top = q.popleft()
for i in g[top]:
if dist[i] == -1:
q.append(i)
dist[i] = dist[top]+1
isv[i] = 1
elif dist[i] == dist[top]+1:
isv[i]+=1
mn,mx = 0,0
for i in range(1,k):
# if the difference between current and previous not 1
# means that it is not the optimal path
# hence the ans += 1
if dist[path[i-1]]-1 != dist[path[i]]:
mn+=1
mx += 1
elif isv[path[i-1]] != -1 and isv[path[i-1]]>1:
mx += 1
print(mn,mx)
``` | instruction | 0 | 60,796 | 24 | 121,592 |
Yes | output | 1 | 60,796 | 24 | 121,593 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4):
Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png)
1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4];
2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]);
3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4];
4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5) β the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1 β€ u, v β€ n, u β v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear).
The following line contains one integer k (2 β€ k β€ n) β the number of intersections in Polycarp's path from home to his workplace.
The last line contains k integers p_1, p_2, ..., p_k (1 β€ p_i β€ n, all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i β [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
Input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
Output
1 2
Input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
Output
0 0
Input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
Output
0 3
Submitted Solution:
```
from math import *
n, m = map(int, input().split())
g = {}
for i in range(n):
g[i] = set()
for i in range(m):
a, b = map(int, input().split())
g[b-1].add(a-1)
k = int(input())
p = [int(i) - 1 for i in input().split()]
te = []
for i in range(n):
te.append([10**9, 0])
def dfs(v, d):
for u in g[v]:
if d < te[u][0]:
te[u][0] = d
te[u][1] = 1
elif d == te[u][0]:
te[u][1] += 1
else:
return
dfs(u, d + 1)
dfs(p[-1], 1)
ans1 = 0
ans2 = 0
for i in range(0, k - 1):
v = p[i]
dist, cnt = te[v]
if dist < k - i - 1:
ans1 += 1
ans2 += 1
elif cnt > 1:
ans2 += 1
print(ans1, ans2)
``` | instruction | 0 | 60,797 | 24 | 121,594 |
No | output | 1 | 60,797 | 24 | 121,595 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4):
Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png)
1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4];
2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]);
3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4];
4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5) β the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1 β€ u, v β€ n, u β v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear).
The following line contains one integer k (2 β€ k β€ n) β the number of intersections in Polycarp's path from home to his workplace.
The last line contains k integers p_1, p_2, ..., p_k (1 β€ p_i β€ n, all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i β [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
Input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
Output
1 2
Input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
Output
0 0
Input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
Output
0 3
Submitted Solution:
```
def bfs(graph, node, end):
visited = []
queue = []
visited.append(node)
queue.append(node)
nodes_dist = [-1]*len(graph)
nodes_routes = [0]*len(graph)
nodes_dist[node] = 0
while queue:
s = queue.pop(0)
for neighbour in graph[s]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
nodes_dist[neighbour] = nodes_dist[s] + 1
if nodes_dist[s] + 1 == nodes_dist[neighbour]:
nodes_routes[neighbour] += 1
return nodes_dist[end], nodes_routes[end]
n, m = [int(s) for s in input().split(' ')]
N = [set() for _ in range(n+1)]
for i in range(m):
u, v = [int(s) for s in input().split(' ')]
N[u].add(v)
k = int(input())
p = [int(s) for s in input().split(' ')]
min_re = 0
max_re = 0
for i in range(k-1):
next_node = p[i+1]
best_next_nodes = set()
dist, routes = bfs(N, p[i], p[-1])
for neighbour in N[p[i]]:
neighbour_dist, _ = bfs(N, neighbour, p[-1])
if neighbour_dist + 1 == dist:
best_next_nodes.add(neighbour)
if next_node not in best_next_nodes:
min_re += 1
max_re += 1
elif routes > 1:
max_re += 1
print(min_re, max_re)
``` | instruction | 0 | 60,798 | 24 | 121,596 |
No | output | 1 | 60,798 | 24 | 121,597 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4):
Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png)
1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4];
2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]);
3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4];
4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5) β the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1 β€ u, v β€ n, u β v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear).
The following line contains one integer k (2 β€ k β€ n) β the number of intersections in Polycarp's path from home to his workplace.
The last line contains k integers p_1, p_2, ..., p_k (1 β€ p_i β€ n, all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i β [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
Input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
Output
1 2
Input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
Output
0 0
Input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
Output
0 3
Submitted Solution:
```
import sys
def bfs(g,src,d,found):
q=[src]
d[src]=0
while q:
rmv=q.pop(0)
for child in g[rmv]:
if d[child]!=-1:
d[child]=d[rmv]+1
q.append(child)
found[child]=1
elif d[child]==d[rmv]+1:
found[child]+=1
n,m=map(int,sys.stdin.readline().split())
g=[]
gt=[]
for i in range(n):
g.append(list())
gt.append(list())
for _ in range(m):
u,v=map(int,sys.stdin.readline().split())
u-=1
v-=1
g[u].append(v)
gt[v].append(u)
k=int(sys.stdin.readline())
p=list(map(int,sys.stdin.readline().split()))
for i in range(len(p)):
p[i]-=1
d=[-1]*(n)
found={}
bfs(gt,p[-1],d,found)
#print(d)
mn=0
mx=0
for i in range(1,k):
if d[p[i-1]]-1!=d[p[i]]:
mn+=1
mx+=1
if found.get(p[i-1])!=None and found[p[i-1]]>1:
mx+=1
print(mn,mx)
``` | instruction | 0 | 60,799 | 24 | 121,598 |
No | output | 1 | 60,799 | 24 | 121,599 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.
Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.
Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.
Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4):
Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png)
1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4];
2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]);
3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4];
4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step).
The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
Input
The first line contains two integers n and m (2 β€ n β€ m β€ 2 β
10^5) β the number of intersections and one-way roads in Bertown, respectively.
Then m lines follow, each describing a road. Each line contains two integers u and v (1 β€ u, v β€ n, u β v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear).
The following line contains one integer k (2 β€ k β€ n) β the number of intersections in Polycarp's path from home to his workplace.
The last line contains k integers p_1, p_2, ..., p_k (1 β€ p_i β€ n, all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i β [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown.
Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
Examples
Input
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
Output
1 2
Input
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
Output
0 0
Input
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
Output
0 3
Submitted Solution:
```
n,m=map(int,input().split())
adj,uvs=[],[]
for i in range(m):
uvs.append([int(a) for a in input().split()])
k=int(input())
ks=[int(a) for a in input().split()]
uvs.sort()
ls,vis=[],[]
for i in range(n+1):
adj.append([])
ls.append(0)
vis.append(False)
for a in uvs:
adj[a[0]].append(a[1])
q=[]
mn,mx=0,0
for ii in range(len(ks[:-1])):
a1=ks[ii]
an=[]
best=10**100
for iii in range(len(adj[a1])):
a=adj[a1][iii]
ctr=0
if a!=ks[-1]:
dis=10**100
visited,distance=[],[]
for i in range(n+1):
visited.append(False)
distance.append(0)
q.append(a)
visited[a]=True
while len(q)>0:
s = q[0]
q.pop(0)
for u in adj[s]:
if (visited[u]):
break
visited[u] = True
distance[u] = distance[s]+1
if u==ks[-1]:
break
an.append(s)
if u!=ks[-1]:
q.append(u)
if distance[u]==best:
an.append(a)
if distance[u]>best:
pass
if best>distance[u]:
an=[]
best=min(distance[u],best)
an.append(a)
if ks[ii+1] in an:
if len(an)!=1:
mx+=1
else:
mn+=1
mx+=1
print(mn,mx)
``` | instruction | 0 | 60,800 | 24 | 121,600 |
No | output | 1 | 60,800 | 24 | 121,601 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.
Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.
Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously:
* there were b milliliters poured in total. That is, the bottle need to be emptied;
* after the process is over, the volumes of the drink in the mugs should be equal.
Input
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
Output
Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.
Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
Examples
Input
5 50
1 2 3 4 5
Output
12.000000
11.000000
10.000000
9.000000
8.000000
Input
2 2
1 100
Output
-1 | instruction | 0 | 60,937 | 24 | 121,874 |
Tags: math
Correct Solution:
```
def f(l1,al):
n,b = l1
c = (sum(al) + b)/n
if max(al)>c:
return [-1]
return [c-a for a in al]
l1 = list(map(int,input().split()))
l2 = list(map(int,input().split()))
[print(r) for r in f(l1,l2)]
``` | output | 1 | 60,937 | 24 | 121,875 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.
Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.
Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously:
* there were b milliliters poured in total. That is, the bottle need to be emptied;
* after the process is over, the volumes of the drink in the mugs should be equal.
Input
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
Output
Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.
Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
Examples
Input
5 50
1 2 3 4 5
Output
12.000000
11.000000
10.000000
9.000000
8.000000
Input
2 2
1 100
Output
-1 | instruction | 0 | 60,938 | 24 | 121,876 |
Tags: math
Correct Solution:
```
n,b=map(int,input().split())
d=list(map(int,input().split()))
l1=[]
mx=max(d)
for item in d:
l1.append(mx-item)
b-=(mx-item)
if b<0:
print(-1)
else:
for i in range(len(l1)):
l1[i]+=(b/n)
for item in l1:
print(format(item,'.6f'))
``` | output | 1 | 60,938 | 24 | 121,877 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.
Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.
Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously:
* there were b milliliters poured in total. That is, the bottle need to be emptied;
* after the process is over, the volumes of the drink in the mugs should be equal.
Input
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
Output
Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.
Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
Examples
Input
5 50
1 2 3 4 5
Output
12.000000
11.000000
10.000000
9.000000
8.000000
Input
2 2
1 100
Output
-1 | instruction | 0 | 60,939 | 24 | 121,878 |
Tags: math
Correct Solution:
```
I = lambda: map(int, input().split())
n, b = I()
A = list(I())
x = (b+sum(A)) / n
if x < max(A):
print(-1)
else:
print(*(x-a for a in A), sep='\n')
``` | output | 1 | 60,939 | 24 | 121,879 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.
Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.
Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously:
* there were b milliliters poured in total. That is, the bottle need to be emptied;
* after the process is over, the volumes of the drink in the mugs should be equal.
Input
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
Output
Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.
Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
Examples
Input
5 50
1 2 3 4 5
Output
12.000000
11.000000
10.000000
9.000000
8.000000
Input
2 2
1 100
Output
-1 | instruction | 0 | 60,940 | 24 | 121,880 |
Tags: math
Correct Solution:
```
n,m=map(int,input().split())
a=list(map(int,input().split()))
s=0
for j in range(n):
s=s+a[j]
s=s+m
e=s/n
t=0
for i in range(n):
if e-a[i]>=0:
a[i]=e-a[i]
else:
print(-1)
t=1
break
if t==0:
print(*a,sep="\n")
``` | output | 1 | 60,940 | 24 | 121,881 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.
Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.
Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously:
* there were b milliliters poured in total. That is, the bottle need to be emptied;
* after the process is over, the volumes of the drink in the mugs should be equal.
Input
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
Output
Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.
Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
Examples
Input
5 50
1 2 3 4 5
Output
12.000000
11.000000
10.000000
9.000000
8.000000
Input
2 2
1 100
Output
-1 | instruction | 0 | 60,941 | 24 | 121,882 |
Tags: math
Correct Solution:
```
n, b = map(int, input().split())
a = [int(i) for i in input().split()]
d = [(b + sum(a)) / len(a) - i for i in a]
print(-1) if any(i for i in d if i < 0) else [print(i) for i in d]
``` | output | 1 | 60,941 | 24 | 121,883 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.
Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.
Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously:
* there were b milliliters poured in total. That is, the bottle need to be emptied;
* after the process is over, the volumes of the drink in the mugs should be equal.
Input
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
Output
Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.
Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
Examples
Input
5 50
1 2 3 4 5
Output
12.000000
11.000000
10.000000
9.000000
8.000000
Input
2 2
1 100
Output
-1 | instruction | 0 | 60,942 | 24 | 121,884 |
Tags: math
Correct Solution:
```
x=[int(i) for i in input().split()]
n=x[0]
y=[int(i) for i in input().split()]
if x[1]<max(y)*n-sum(y):
print(-1)
raise SystemExit
for i in range(n):
s=x[1]/n-y[i]++sum(y)/n
print('%.6f'% s)
``` | output | 1 | 60,942 | 24 | 121,885 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.
Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.
Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously:
* there were b milliliters poured in total. That is, the bottle need to be emptied;
* after the process is over, the volumes of the drink in the mugs should be equal.
Input
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
Output
Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.
Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
Examples
Input
5 50
1 2 3 4 5
Output
12.000000
11.000000
10.000000
9.000000
8.000000
Input
2 2
1 100
Output
-1 | instruction | 0 | 60,943 | 24 | 121,886 |
Tags: math
Correct Solution:
```
n, b = map(int, input().split())
t = list(map(int, input().split()))
s, d = sum(t), max(t)
b -= d * n - s
if b < 0: print(-1)
else:
d += b / n
for i in range(n): t[i] = str(d - t[i])
print('\n'.join(t))
``` | output | 1 | 60,943 | 24 | 121,887 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.
Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.
Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously:
* there were b milliliters poured in total. That is, the bottle need to be emptied;
* after the process is over, the volumes of the drink in the mugs should be equal.
Input
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
Output
Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.
Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
Examples
Input
5 50
1 2 3 4 5
Output
12.000000
11.000000
10.000000
9.000000
8.000000
Input
2 2
1 100
Output
-1 | instruction | 0 | 60,944 | 24 | 121,888 |
Tags: math
Correct Solution:
```
n, b = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
sumN = 0
for i in range(n):
sumN += a[i]
ans = (sumN + b) / n
if ans - max(a) < 0:
print(-1)
exit(0)
for i in range(n):
print(ans - a[i])
``` | output | 1 | 60,944 | 24 | 121,889 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.
Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.
Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously:
* there were b milliliters poured in total. That is, the bottle need to be emptied;
* after the process is over, the volumes of the drink in the mugs should be equal.
Input
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
Output
Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.
Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
Examples
Input
5 50
1 2 3 4 5
Output
12.000000
11.000000
10.000000
9.000000
8.000000
Input
2 2
1 100
Output
-1
Submitted Solution:
```
I=lambda:map(int,input().split())
P=print
n,b=I()
a=list(I())
x=(b+sum(a))/n
if any(i>x for i in a):P(-1)
else:[P(x-i)for i in a]
``` | instruction | 0 | 60,945 | 24 | 121,890 |
Yes | output | 1 | 60,945 | 24 | 121,891 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.
Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.
Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously:
* there were b milliliters poured in total. That is, the bottle need to be emptied;
* after the process is over, the volumes of the drink in the mugs should be equal.
Input
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
Output
Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.
Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
Examples
Input
5 50
1 2 3 4 5
Output
12.000000
11.000000
10.000000
9.000000
8.000000
Input
2 2
1 100
Output
-1
Submitted Solution:
```
a,b=map(int,input().split())
x=[int(q) for q in input().split()]
l=sum(x)
total = l+b
each = total/a
reqd=0
z=max(x)
for i in range(a):
reqd+=abs(x[i]-z)
if b<reqd:
print(-1)
else:
for i in range(a):
print(each-x[i])
``` | instruction | 0 | 60,946 | 24 | 121,892 |
Yes | output | 1 | 60,946 | 24 | 121,893 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.
Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.
Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously:
* there were b milliliters poured in total. That is, the bottle need to be emptied;
* after the process is over, the volumes of the drink in the mugs should be equal.
Input
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
Output
Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.
Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
Examples
Input
5 50
1 2 3 4 5
Output
12.000000
11.000000
10.000000
9.000000
8.000000
Input
2 2
1 100
Output
-1
Submitted Solution:
```
n,t=map(int,input().split())
l=list(map(int,input().split()))
X=float((sum(l)+t)/n )
#print(X)
for i in l:
if(i>X):
print(-1)
break
else:
for i in l:
print("{0:.6f}".format(X-i))
``` | instruction | 0 | 60,947 | 24 | 121,894 |
Yes | output | 1 | 60,947 | 24 | 121,895 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.
Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.
Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously:
* there were b milliliters poured in total. That is, the bottle need to be emptied;
* after the process is over, the volumes of the drink in the mugs should be equal.
Input
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
Output
Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.
Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
Examples
Input
5 50
1 2 3 4 5
Output
12.000000
11.000000
10.000000
9.000000
8.000000
Input
2 2
1 100
Output
-1
Submitted Solution:
```
n, b = tuple(map(int, input().split()))
a = list(map(int, input().split()))
maximum = max(a)
d = []
for i in range(n):
diff = maximum - a[i]
if b >= diff:
d.append(a[i] + diff)
b -= diff
el = d[0]
res = 'ok'
for j in d:
if j != el:
res = 'not ok'
add = 0
if len(d) != len(a):
res = 'not ok'
else:
add += b / len(d)
# b -= b
if res == 'ok':
ans = [i-j for i,j in zip(d,a)]
for k in range(n):
ans[k] += add
if type(ans[k]) == int:
print ('{:.6f}'.format(ans[k]))
else:
print (ans[k])
else:
print (-1)
``` | instruction | 0 | 60,948 | 24 | 121,896 |
Yes | output | 1 | 60,948 | 24 | 121,897 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.
Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.
Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously:
* there were b milliliters poured in total. That is, the bottle need to be emptied;
* after the process is over, the volumes of the drink in the mugs should be equal.
Input
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
Output
Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.
Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
Examples
Input
5 50
1 2 3 4 5
Output
12.000000
11.000000
10.000000
9.000000
8.000000
Input
2 2
1 100
Output
-1
Submitted Solution:
```
n,b= map(int, input().split())
b=float(b)
arr = list(map(float, (input().split())))
total = b + sum(arr)
totalone = total/n
need=0.000000
needarr=[]
flag = ''
for i in range(n):
need=totalone-arr[i]
if(need > b):
print(-1)
flag='no'
break
else:
needarr.append(need)
if(flag!='no'):
for i in range(n):
print('{0:.6f}'.format(needarr[i]))
``` | instruction | 0 | 60,949 | 24 | 121,898 |
No | output | 1 | 60,949 | 24 | 121,899 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.
Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.
Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously:
* there were b milliliters poured in total. That is, the bottle need to be emptied;
* after the process is over, the volumes of the drink in the mugs should be equal.
Input
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
Output
Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.
Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
Examples
Input
5 50
1 2 3 4 5
Output
12.000000
11.000000
10.000000
9.000000
8.000000
Input
2 2
1 100
Output
-1
Submitted Solution:
```
r = lambda: int(input())
ra = lambda: [*map(int, input().split())]
p = lambda a: print('{:.6f}'.format(a))
n, m = ra()
a = ra()
b = []
e = (m+sum(a))/n
for i in range(n):
b.append(float(('{:.6f}'.format(e-a[i]))))
m-=abs(b[len(b)-1])
a[i]+=b[len(b)-1]
if m<0:
print(-1)
else:
for i in b:
p(i)
``` | instruction | 0 | 60,950 | 24 | 121,900 |
No | output | 1 | 60,950 | 24 | 121,901 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.
Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.
Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously:
* there were b milliliters poured in total. That is, the bottle need to be emptied;
* after the process is over, the volumes of the drink in the mugs should be equal.
Input
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
Output
Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.
Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
Examples
Input
5 50
1 2 3 4 5
Output
12.000000
11.000000
10.000000
9.000000
8.000000
Input
2 2
1 100
Output
-1
Submitted Solution:
```
n, b = map(int, input().split())
a = list(float(x) for x in input().split())
s = max(a)
c = [0]*n
for i in range(n):
m = s-a[i]
b = b-m
c[i] = c[i]+m
if b > 0:
for i in range(n):
c[i] = c[i] + b/n
for i in range(n):
print("%.6f" % c[i])
else:
print("-1")
``` | instruction | 0 | 60,951 | 24 | 121,902 |
No | output | 1 | 60,951 | 24 | 121,903 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same.
Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled.
Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously:
* there were b milliliters poured in total. That is, the bottle need to be emptied;
* after the process is over, the volumes of the drink in the mugs should be equal.
Input
The first line contains a pair of integers n, b (2 β€ n β€ 100, 1 β€ b β€ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the current volume of drink in the i-th mug.
Output
Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique.
Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma.
Examples
Input
5 50
1 2 3 4 5
Output
12.000000
11.000000
10.000000
9.000000
8.000000
Input
2 2
1 100
Output
-1
Submitted Solution:
```
n, b = map(int, input().split())
s = list(map(int, input().split()))
sb = sum(s) + b
sb /= n
if sb - max(s) > 0:
for x in s:
print(sb-x)
else:
print('-1')
``` | instruction | 0 | 60,952 | 24 | 121,904 |
No | output | 1 | 60,952 | 24 | 121,905 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is making a quest for his friends. He has already made n tasks, for each task the boy evaluated how interesting it is as an integer qi, and the time ti in minutes needed to complete the task.
An interesting feature of his quest is: each participant should get the task that is best suited for him, depending on his preferences. The task is chosen based on an interactive quiz that consists of some questions. The player should answer these questions with "yes" or "no". Depending on the answer to the question, the participant either moves to another question or goes to one of the tasks that are in the quest. In other words, the quest is a binary tree, its nodes contain questions and its leaves contain tasks.
We know that answering any of the questions that are asked before getting a task takes exactly one minute from the quest player. Polycarp knows that his friends are busy people and they can't participate in the quest for more than T minutes. Polycarp wants to choose some of the n tasks he made, invent the corresponding set of questions for them and use them to form an interactive quiz as a binary tree so that no matter how the player answers quiz questions, he spends at most T minutes on completing the whole quest (that is, answering all the questions and completing the task). Specifically, the quest can contain zero questions and go straight to the task. Each task can only be used once (i.e., the people who give different answers to questions should get different tasks).
Polycarp wants the total "interest" value of the tasks involved in the quest to be as large as possible. Help him determine the maximum possible total interest value of the task considering that the quest should be completed in T minutes at any variant of answering questions.
Input
The first line contains two integers n and T (1 β€ n β€ 1000, 1 β€ T β€ 100) β the number of tasks made by Polycarp and the maximum time a quest player should fit into.
Next n lines contain two integers ti, qi (1 β€ ti β€ T, 1 β€ qi β€ 1000) each β the time in minutes needed to complete the i-th task and its interest value.
Output
Print a single integer β the maximum possible total interest value of all the tasks in the quest.
Examples
Input
5 5
1 1
1 1
2 2
3 3
4 4
Output
11
Input
5 5
4 1
4 2
4 3
4 4
4 5
Output
9
Input
2 2
1 1
2 10
Output
10
Note
In the first sample test all the five tasks can be complemented with four questions and joined into one quest.
In the second sample test it is impossible to use all the five tasks, but you can take two of them, the most interesting ones.
In the third sample test the optimal strategy is to include only the second task into the quest.
Here is the picture that illustrates the answers to the sample tests. The blue circles represent the questions, the two arrows that go from every circle represent where a person goes depending on his answer to that question. The tasks are the red ovals.
<image> | instruction | 0 | 63,525 | 24 | 127,050 |
Tags: dp, greedy
Correct Solution:
```
from collections import defaultdict
if __name__ == '__main__':
n, T = [int(_) for _ in input().split()]
data = defaultdict(list)
for i in range(n):
t, q = [int(_) for _ in input().split()]
data[T - t].append(q)
prev_level = []
for level_id in range(1, T + 1):
level = sorted(data[T - level_id] + prev_level, reverse=True)
if T - level_id <= 10:
max_size = 2 ** (T - level_id)
level = level[:max_size]
if len(level) % 2 == 1:
level.append(0)
prev_level = [
level[i] + level[i + 1]
for i in range(0, len(level), 2)
]
print(prev_level[0])
``` | output | 1 | 63,525 | 24 | 127,051 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has a coins, Barbara has b coins and Cerene has c coins. Recently Polycarp has returned from the trip around the world and brought n coins.
He wants to distribute all these n coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives A coins to Alice, B coins to Barbara and C coins to Cerene (A+B+C=n), then a + A = b + B = c + C.
Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.
Your task is to find out if it is possible to distribute all n coins between sisters in a way described above.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
The next t lines describe test cases. Each test case is given on a new line and consists of four space-separated integers a, b, c and n (1 β€ a, b, c, n β€ 10^8) β the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
Output
For each test case, print "YES" if Polycarp can distribute all n coins between his sisters and "NO" otherwise.
Example
Input
5
5 3 2 8
100 101 102 105
3 2 1 100000000
10 20 15 14
101 101 101 3
Output
YES
YES
NO
NO
YES | instruction | 0 | 64,100 | 24 | 128,200 |
Tags: math
Correct Solution:
```
t=int(input())
for q in range(t):
a=[int(x) for x in input().split()]
total=a[3]
a=a[0:3]
a.sort(reverse=True)
diff1=a[0]-a[2]
diff2=a[0]-a[1]
total=total-diff1-diff2
if total%3==0 and total>=0:
print("YES")
else:
print("NO")
``` | output | 1 | 64,100 | 24 | 128,201 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has a coins, Barbara has b coins and Cerene has c coins. Recently Polycarp has returned from the trip around the world and brought n coins.
He wants to distribute all these n coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives A coins to Alice, B coins to Barbara and C coins to Cerene (A+B+C=n), then a + A = b + B = c + C.
Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.
Your task is to find out if it is possible to distribute all n coins between sisters in a way described above.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
The next t lines describe test cases. Each test case is given on a new line and consists of four space-separated integers a, b, c and n (1 β€ a, b, c, n β€ 10^8) β the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
Output
For each test case, print "YES" if Polycarp can distribute all n coins between his sisters and "NO" otherwise.
Example
Input
5
5 3 2 8
100 101 102 105
3 2 1 100000000
10 20 15 14
101 101 101 3
Output
YES
YES
NO
NO
YES | instruction | 0 | 64,101 | 24 | 128,202 |
Tags: math
Correct Solution:
```
def call():
a,b,c,n=map(int,input().split())
l=[a,b,c]
l.sort()
n-=2*l[2]-l[1]-l[0]
print(("YES","NO")[n<0 or n%3!=0])
for _ in range(int(input())):
call()
``` | output | 1 | 64,101 | 24 | 128,203 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has a coins, Barbara has b coins and Cerene has c coins. Recently Polycarp has returned from the trip around the world and brought n coins.
He wants to distribute all these n coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives A coins to Alice, B coins to Barbara and C coins to Cerene (A+B+C=n), then a + A = b + B = c + C.
Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.
Your task is to find out if it is possible to distribute all n coins between sisters in a way described above.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
The next t lines describe test cases. Each test case is given on a new line and consists of four space-separated integers a, b, c and n (1 β€ a, b, c, n β€ 10^8) β the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
Output
For each test case, print "YES" if Polycarp can distribute all n coins between his sisters and "NO" otherwise.
Example
Input
5
5 3 2 8
100 101 102 105
3 2 1 100000000
10 20 15 14
101 101 101 3
Output
YES
YES
NO
NO
YES | instruction | 0 | 64,102 | 24 | 128,204 |
Tags: math
Correct Solution:
```
n = int(input())
for i in range(n):
x = list(map(int, input().split()))
if sum(x) % 3 == 0:
mean = sum(x) // 3
flag = True
for i in range(3):
if x[i] > mean:
flag = False
if flag:
print("YES")
else:
print("NO")
else:
print("NO")
``` | output | 1 | 64,102 | 24 | 128,205 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has a coins, Barbara has b coins and Cerene has c coins. Recently Polycarp has returned from the trip around the world and brought n coins.
He wants to distribute all these n coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives A coins to Alice, B coins to Barbara and C coins to Cerene (A+B+C=n), then a + A = b + B = c + C.
Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.
Your task is to find out if it is possible to distribute all n coins between sisters in a way described above.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
The next t lines describe test cases. Each test case is given on a new line and consists of four space-separated integers a, b, c and n (1 β€ a, b, c, n β€ 10^8) β the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
Output
For each test case, print "YES" if Polycarp can distribute all n coins between his sisters and "NO" otherwise.
Example
Input
5
5 3 2 8
100 101 102 105
3 2 1 100000000
10 20 15 14
101 101 101 3
Output
YES
YES
NO
NO
YES | instruction | 0 | 64,103 | 24 | 128,206 |
Tags: math
Correct Solution:
```
t = int(input())
for i in range(t):
a, b, c, n = map(int, input().split())
mx = max(max(a, b), c)
mn = mx-a + mx-b + mx-c
res = "NO" if n<mn or (n-mn)%3!=0 else "YES"
print(res)
``` | output | 1 | 64,103 | 24 | 128,207 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has a coins, Barbara has b coins and Cerene has c coins. Recently Polycarp has returned from the trip around the world and brought n coins.
He wants to distribute all these n coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives A coins to Alice, B coins to Barbara and C coins to Cerene (A+B+C=n), then a + A = b + B = c + C.
Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.
Your task is to find out if it is possible to distribute all n coins between sisters in a way described above.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
The next t lines describe test cases. Each test case is given on a new line and consists of four space-separated integers a, b, c and n (1 β€ a, b, c, n β€ 10^8) β the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
Output
For each test case, print "YES" if Polycarp can distribute all n coins between his sisters and "NO" otherwise.
Example
Input
5
5 3 2 8
100 101 102 105
3 2 1 100000000
10 20 15 14
101 101 101 3
Output
YES
YES
NO
NO
YES | instruction | 0 | 64,104 | 24 | 128,208 |
Tags: math
Correct Solution:
```
n=int(input())
for i in range(n):
l=list(map(int,input().split()))
s=sum(l)
k=l.pop()
l.sort()
a=l[2]
b=a-l[0]
c=a-l[1]
k=k-(b+c)
if(k>=0):
if((k%3)==0):
print("YES")
else:
print("NO")
else:
print("NO")
``` | output | 1 | 64,104 | 24 | 128,209 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has a coins, Barbara has b coins and Cerene has c coins. Recently Polycarp has returned from the trip around the world and brought n coins.
He wants to distribute all these n coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives A coins to Alice, B coins to Barbara and C coins to Cerene (A+B+C=n), then a + A = b + B = c + C.
Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.
Your task is to find out if it is possible to distribute all n coins between sisters in a way described above.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
The next t lines describe test cases. Each test case is given on a new line and consists of four space-separated integers a, b, c and n (1 β€ a, b, c, n β€ 10^8) β the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
Output
For each test case, print "YES" if Polycarp can distribute all n coins between his sisters and "NO" otherwise.
Example
Input
5
5 3 2 8
100 101 102 105
3 2 1 100000000
10 20 15 14
101 101 101 3
Output
YES
YES
NO
NO
YES | instruction | 0 | 64,105 | 24 | 128,210 |
Tags: math
Correct Solution:
```
num_test = int(input())
for test in range(num_test):
test_input = input().split()
a = int(test_input[0])
b = int(test_input[1])
c = int(test_input[2])
n = int(test_input[3])
all_coin = a + b + c + n
if all_coin % 3 != 0:
print("NO")
else:
divided = int(all_coin / 3)
if divided < a or divided < b or divided < c:
print("NO")
else:
print("YES")
``` | output | 1 | 64,105 | 24 | 128,211 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.