message stringlengths 2 22.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 16 109k | cluster float64 1 1 | __index_level_0__ int64 32 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova plans to go to the conference by train. Initially, the train is at the point 1 and the destination point of the path is the point L. The speed of the train is 1 length unit per minute (i.e. at the first minute the train is at the point 1, at the second minute β at the point 2 and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by v (i.e. the first lantern is at the point v, the second is at the point 2v and so on).
There is also exactly one standing train which occupies all the points from l to r inclusive.
Vova can see the lantern at the point p if p is divisible by v and there is no standing train at this position (p notβ [l; r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to t different conferences, so you should answer t independent queries.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of queries.
Then t lines follow. The i-th line contains four integers L_i, v_i, l_i, r_i (1 β€ L, v β€ 10^9, 1 β€ l β€ r β€ L) β destination point of the i-th path, the period of the lantern appearance and the segment occupied by the standing train.
Output
Print t lines. The i-th line should contain one integer β the answer for the i-th query.
Example
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0
Note
For the first example query, the answer is 3. There are lanterns at positions 2, 4, 6, 8 and 10, but Vova didn't see the lanterns at positions 4 and 6 because of the standing train.
For the second example query, the answer is 0 because the only lantern is at the point 51 and there is also a standing train at this point.
For the third example query, the answer is 1134 because there are 1234 lanterns, but Vova didn't see the lanterns from the position 100 to the position 199 inclusive.
For the fourth example query, the answer is 0 because the standing train covers the whole path. | instruction | 0 | 58,852 | 1 | 117,704 |
Tags: math
Correct Solution:
```
from sys import stdin
all_in = stdin.readlines()
t = int(all_in[0])
Lvlr = list(map(lambda x: tuple(map(int, x.split())), all_in[1:]))
ans = list()
for L, v, l, r in Lvlr:
ans.append(L // v - r // v + (l - 1) // v)
print('\n'.join(map(str, ans)))
``` | output | 1 | 58,852 | 1 | 117,705 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova plans to go to the conference by train. Initially, the train is at the point 1 and the destination point of the path is the point L. The speed of the train is 1 length unit per minute (i.e. at the first minute the train is at the point 1, at the second minute β at the point 2 and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by v (i.e. the first lantern is at the point v, the second is at the point 2v and so on).
There is also exactly one standing train which occupies all the points from l to r inclusive.
Vova can see the lantern at the point p if p is divisible by v and there is no standing train at this position (p notβ [l; r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to t different conferences, so you should answer t independent queries.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of queries.
Then t lines follow. The i-th line contains four integers L_i, v_i, l_i, r_i (1 β€ L, v β€ 10^9, 1 β€ l β€ r β€ L) β destination point of the i-th path, the period of the lantern appearance and the segment occupied by the standing train.
Output
Print t lines. The i-th line should contain one integer β the answer for the i-th query.
Example
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0
Note
For the first example query, the answer is 3. There are lanterns at positions 2, 4, 6, 8 and 10, but Vova didn't see the lanterns at positions 4 and 6 because of the standing train.
For the second example query, the answer is 0 because the only lantern is at the point 51 and there is also a standing train at this point.
For the third example query, the answer is 1134 because there are 1234 lanterns, but Vova didn't see the lanterns from the position 100 to the position 199 inclusive.
For the fourth example query, the answer is 0 because the standing train covers the whole path. | instruction | 0 | 58,853 | 1 | 117,706 |
Tags: math
Correct Solution:
```
import math
for i in range(int(input())):
a,b,c,d=map(int,input().split())
print(a//b-math.floor(d/b)+math.ceil(c/b)-1)
``` | output | 1 | 58,853 | 1 | 117,707 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova plans to go to the conference by train. Initially, the train is at the point 1 and the destination point of the path is the point L. The speed of the train is 1 length unit per minute (i.e. at the first minute the train is at the point 1, at the second minute β at the point 2 and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by v (i.e. the first lantern is at the point v, the second is at the point 2v and so on).
There is also exactly one standing train which occupies all the points from l to r inclusive.
Vova can see the lantern at the point p if p is divisible by v and there is no standing train at this position (p notβ [l; r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to t different conferences, so you should answer t independent queries.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of queries.
Then t lines follow. The i-th line contains four integers L_i, v_i, l_i, r_i (1 β€ L, v β€ 10^9, 1 β€ l β€ r β€ L) β destination point of the i-th path, the period of the lantern appearance and the segment occupied by the standing train.
Output
Print t lines. The i-th line should contain one integer β the answer for the i-th query.
Example
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0
Note
For the first example query, the answer is 3. There are lanterns at positions 2, 4, 6, 8 and 10, but Vova didn't see the lanterns at positions 4 and 6 because of the standing train.
For the second example query, the answer is 0 because the only lantern is at the point 51 and there is also a standing train at this point.
For the third example query, the answer is 1134 because there are 1234 lanterns, but Vova didn't see the lanterns from the position 100 to the position 199 inclusive.
For the fourth example query, the answer is 0 because the standing train covers the whole path. | instruction | 0 | 58,854 | 1 | 117,708 |
Tags: math
Correct Solution:
```
while True:
try:
t=int(input())
except:
break
for i in range(t):
count=0
L,v,l,r=map(int,input().split())
sum=L//v
ll=l//v
lr=r//v
if l==r and l%v==0:
sum=sum-(lr-ll)-1
elif l%v==0:
sum=sum-(lr-ll)
sum=sum-1
else:
sum=sum-(lr-ll)
if sum<=0:
sum=0
print(sum)
``` | output | 1 | 58,854 | 1 | 117,709 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova plans to go to the conference by train. Initially, the train is at the point 1 and the destination point of the path is the point L. The speed of the train is 1 length unit per minute (i.e. at the first minute the train is at the point 1, at the second minute β at the point 2 and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by v (i.e. the first lantern is at the point v, the second is at the point 2v and so on).
There is also exactly one standing train which occupies all the points from l to r inclusive.
Vova can see the lantern at the point p if p is divisible by v and there is no standing train at this position (p notβ [l; r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to t different conferences, so you should answer t independent queries.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of queries.
Then t lines follow. The i-th line contains four integers L_i, v_i, l_i, r_i (1 β€ L, v β€ 10^9, 1 β€ l β€ r β€ L) β destination point of the i-th path, the period of the lantern appearance and the segment occupied by the standing train.
Output
Print t lines. The i-th line should contain one integer β the answer for the i-th query.
Example
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0
Note
For the first example query, the answer is 3. There are lanterns at positions 2, 4, 6, 8 and 10, but Vova didn't see the lanterns at positions 4 and 6 because of the standing train.
For the second example query, the answer is 0 because the only lantern is at the point 51 and there is also a standing train at this point.
For the third example query, the answer is 1134 because there are 1234 lanterns, but Vova didn't see the lanterns from the position 100 to the position 199 inclusive.
For the fourth example query, the answer is 0 because the standing train covers the whole path. | instruction | 0 | 58,855 | 1 | 117,710 |
Tags: math
Correct Solution:
```
#!usr/bin/python
T = 0
L = 0
v = 0
l = 0
r = 0
def getAns(x):
return x//v
T = int(input())
while T > 0:
T = T - 1
L,v,l,r = map(int, input().strip().split())
print(getAns(L) - getAns(r) + getAns(l-1))
``` | output | 1 | 58,855 | 1 | 117,711 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova plans to go to the conference by train. Initially, the train is at the point 1 and the destination point of the path is the point L. The speed of the train is 1 length unit per minute (i.e. at the first minute the train is at the point 1, at the second minute β at the point 2 and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by v (i.e. the first lantern is at the point v, the second is at the point 2v and so on).
There is also exactly one standing train which occupies all the points from l to r inclusive.
Vova can see the lantern at the point p if p is divisible by v and there is no standing train at this position (p notβ [l; r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to t different conferences, so you should answer t independent queries.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of queries.
Then t lines follow. The i-th line contains four integers L_i, v_i, l_i, r_i (1 β€ L, v β€ 10^9, 1 β€ l β€ r β€ L) β destination point of the i-th path, the period of the lantern appearance and the segment occupied by the standing train.
Output
Print t lines. The i-th line should contain one integer β the answer for the i-th query.
Example
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0
Note
For the first example query, the answer is 3. There are lanterns at positions 2, 4, 6, 8 and 10, but Vova didn't see the lanterns at positions 4 and 6 because of the standing train.
For the second example query, the answer is 0 because the only lantern is at the point 51 and there is also a standing train at this point.
For the third example query, the answer is 1134 because there are 1234 lanterns, but Vova didn't see the lanterns from the position 100 to the position 199 inclusive.
For the fourth example query, the answer is 0 because the standing train covers the whole path.
Submitted Solution:
```
# ANSHUL GAUTAM
# IIIT-D
from math import *
from string import *
from itertools import *
'''
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
'''
T = int(input())
for _ in range(T):
ans = 0
destination,v,starting,ending = map(int, input().split())
total = destination//v
a = ending//v
b = (starting-1)//v
ans = total - a + b
print(ans)
``` | instruction | 0 | 58,856 | 1 | 117,712 |
Yes | output | 1 | 58,856 | 1 | 117,713 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova plans to go to the conference by train. Initially, the train is at the point 1 and the destination point of the path is the point L. The speed of the train is 1 length unit per minute (i.e. at the first minute the train is at the point 1, at the second minute β at the point 2 and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by v (i.e. the first lantern is at the point v, the second is at the point 2v and so on).
There is also exactly one standing train which occupies all the points from l to r inclusive.
Vova can see the lantern at the point p if p is divisible by v and there is no standing train at this position (p notβ [l; r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to t different conferences, so you should answer t independent queries.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of queries.
Then t lines follow. The i-th line contains four integers L_i, v_i, l_i, r_i (1 β€ L, v β€ 10^9, 1 β€ l β€ r β€ L) β destination point of the i-th path, the period of the lantern appearance and the segment occupied by the standing train.
Output
Print t lines. The i-th line should contain one integer β the answer for the i-th query.
Example
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0
Note
For the first example query, the answer is 3. There are lanterns at positions 2, 4, 6, 8 and 10, but Vova didn't see the lanterns at positions 4 and 6 because of the standing train.
For the second example query, the answer is 0 because the only lantern is at the point 51 and there is also a standing train at this point.
For the third example query, the answer is 1134 because there are 1234 lanterns, but Vova didn't see the lanterns from the position 100 to the position 199 inclusive.
For the fourth example query, the answer is 0 because the standing train covers the whole path.
Submitted Solution:
```
t=int(input())
for i in range(t):
a,b,c,d=map(int,input().split())
x=a//b
y=d//b
c=c-1
z=c//b
x=x-(y-z)
print(x)
``` | instruction | 0 | 58,857 | 1 | 117,714 |
Yes | output | 1 | 58,857 | 1 | 117,715 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova plans to go to the conference by train. Initially, the train is at the point 1 and the destination point of the path is the point L. The speed of the train is 1 length unit per minute (i.e. at the first minute the train is at the point 1, at the second minute β at the point 2 and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by v (i.e. the first lantern is at the point v, the second is at the point 2v and so on).
There is also exactly one standing train which occupies all the points from l to r inclusive.
Vova can see the lantern at the point p if p is divisible by v and there is no standing train at this position (p notβ [l; r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to t different conferences, so you should answer t independent queries.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of queries.
Then t lines follow. The i-th line contains four integers L_i, v_i, l_i, r_i (1 β€ L, v β€ 10^9, 1 β€ l β€ r β€ L) β destination point of the i-th path, the period of the lantern appearance and the segment occupied by the standing train.
Output
Print t lines. The i-th line should contain one integer β the answer for the i-th query.
Example
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0
Note
For the first example query, the answer is 3. There are lanterns at positions 2, 4, 6, 8 and 10, but Vova didn't see the lanterns at positions 4 and 6 because of the standing train.
For the second example query, the answer is 0 because the only lantern is at the point 51 and there is also a standing train at this point.
For the third example query, the answer is 1134 because there are 1234 lanterns, but Vova didn't see the lanterns from the position 100 to the position 199 inclusive.
For the fourth example query, the answer is 0 because the standing train covers the whole path.
Submitted Solution:
```
from sys import stdin,stdout
T=int(stdin.readline())
while T>0:
L,v,l,r=map(int , stdin.readline().split())
if l%v==0:
z=L//v - (r//v - l//v)-1
print(z)
else:
z=L//v - (r//v - l//v)
print(z)
T=T-1
``` | instruction | 0 | 58,858 | 1 | 117,716 |
Yes | output | 1 | 58,858 | 1 | 117,717 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova plans to go to the conference by train. Initially, the train is at the point 1 and the destination point of the path is the point L. The speed of the train is 1 length unit per minute (i.e. at the first minute the train is at the point 1, at the second minute β at the point 2 and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by v (i.e. the first lantern is at the point v, the second is at the point 2v and so on).
There is also exactly one standing train which occupies all the points from l to r inclusive.
Vova can see the lantern at the point p if p is divisible by v and there is no standing train at this position (p notβ [l; r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to t different conferences, so you should answer t independent queries.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of queries.
Then t lines follow. The i-th line contains four integers L_i, v_i, l_i, r_i (1 β€ L, v β€ 10^9, 1 β€ l β€ r β€ L) β destination point of the i-th path, the period of the lantern appearance and the segment occupied by the standing train.
Output
Print t lines. The i-th line should contain one integer β the answer for the i-th query.
Example
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0
Note
For the first example query, the answer is 3. There are lanterns at positions 2, 4, 6, 8 and 10, but Vova didn't see the lanterns at positions 4 and 6 because of the standing train.
For the second example query, the answer is 0 because the only lantern is at the point 51 and there is also a standing train at this point.
For the third example query, the answer is 1134 because there are 1234 lanterns, but Vova didn't see the lanterns from the position 100 to the position 199 inclusive.
For the fourth example query, the answer is 0 because the standing train covers the whole path.
Submitted Solution:
```
for _ in range(int(input())):
l,r,a,b=map(int,input().split())
q=l//r
t=0
if(a%r==0):
t=1
t=t+(b//r)-(a//r)
print(q-t)
``` | instruction | 0 | 58,859 | 1 | 117,718 |
Yes | output | 1 | 58,859 | 1 | 117,719 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova plans to go to the conference by train. Initially, the train is at the point 1 and the destination point of the path is the point L. The speed of the train is 1 length unit per minute (i.e. at the first minute the train is at the point 1, at the second minute β at the point 2 and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by v (i.e. the first lantern is at the point v, the second is at the point 2v and so on).
There is also exactly one standing train which occupies all the points from l to r inclusive.
Vova can see the lantern at the point p if p is divisible by v and there is no standing train at this position (p notβ [l; r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to t different conferences, so you should answer t independent queries.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of queries.
Then t lines follow. The i-th line contains four integers L_i, v_i, l_i, r_i (1 β€ L, v β€ 10^9, 1 β€ l β€ r β€ L) β destination point of the i-th path, the period of the lantern appearance and the segment occupied by the standing train.
Output
Print t lines. The i-th line should contain one integer β the answer for the i-th query.
Example
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0
Note
For the first example query, the answer is 3. There are lanterns at positions 2, 4, 6, 8 and 10, but Vova didn't see the lanterns at positions 4 and 6 because of the standing train.
For the second example query, the answer is 0 because the only lantern is at the point 51 and there is also a standing train at this point.
For the third example query, the answer is 1134 because there are 1234 lanterns, but Vova didn't see the lanterns from the position 100 to the position 199 inclusive.
For the fourth example query, the answer is 0 because the standing train covers the whole path.
Submitted Solution:
```
n = int(input())
for i in range(n):
d, p, l, r = [*map(int, input().split())]
if p != 0:
a = 1 if l % p == 0 else 0
print((d - r + l) // p - a)
else:
print(0)
``` | instruction | 0 | 58,860 | 1 | 117,720 |
No | output | 1 | 58,860 | 1 | 117,721 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova plans to go to the conference by train. Initially, the train is at the point 1 and the destination point of the path is the point L. The speed of the train is 1 length unit per minute (i.e. at the first minute the train is at the point 1, at the second minute β at the point 2 and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by v (i.e. the first lantern is at the point v, the second is at the point 2v and so on).
There is also exactly one standing train which occupies all the points from l to r inclusive.
Vova can see the lantern at the point p if p is divisible by v and there is no standing train at this position (p notβ [l; r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to t different conferences, so you should answer t independent queries.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of queries.
Then t lines follow. The i-th line contains four integers L_i, v_i, l_i, r_i (1 β€ L, v β€ 10^9, 1 β€ l β€ r β€ L) β destination point of the i-th path, the period of the lantern appearance and the segment occupied by the standing train.
Output
Print t lines. The i-th line should contain one integer β the answer for the i-th query.
Example
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0
Note
For the first example query, the answer is 3. There are lanterns at positions 2, 4, 6, 8 and 10, but Vova didn't see the lanterns at positions 4 and 6 because of the standing train.
For the second example query, the answer is 0 because the only lantern is at the point 51 and there is also a standing train at this point.
For the third example query, the answer is 1134 because there are 1234 lanterns, but Vova didn't see the lanterns from the position 100 to the position 199 inclusive.
For the fourth example query, the answer is 0 because the standing train covers the whole path.
Submitted Solution:
```
n = int(input())
for i in range(n):
[L, v, l, r] = map(int, input().split())
print((L - r + l - 1) // v)
``` | instruction | 0 | 58,861 | 1 | 117,722 |
No | output | 1 | 58,861 | 1 | 117,723 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova plans to go to the conference by train. Initially, the train is at the point 1 and the destination point of the path is the point L. The speed of the train is 1 length unit per minute (i.e. at the first minute the train is at the point 1, at the second minute β at the point 2 and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by v (i.e. the first lantern is at the point v, the second is at the point 2v and so on).
There is also exactly one standing train which occupies all the points from l to r inclusive.
Vova can see the lantern at the point p if p is divisible by v and there is no standing train at this position (p notβ [l; r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to t different conferences, so you should answer t independent queries.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of queries.
Then t lines follow. The i-th line contains four integers L_i, v_i, l_i, r_i (1 β€ L, v β€ 10^9, 1 β€ l β€ r β€ L) β destination point of the i-th path, the period of the lantern appearance and the segment occupied by the standing train.
Output
Print t lines. The i-th line should contain one integer β the answer for the i-th query.
Example
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0
Note
For the first example query, the answer is 3. There are lanterns at positions 2, 4, 6, 8 and 10, but Vova didn't see the lanterns at positions 4 and 6 because of the standing train.
For the second example query, the answer is 0 because the only lantern is at the point 51 and there is also a standing train at this point.
For the third example query, the answer is 1134 because there are 1234 lanterns, but Vova didn't see the lanterns from the position 100 to the position 199 inclusive.
For the fourth example query, the answer is 0 because the standing train covers the whole path.
Submitted Solution:
```
n=int(input())
for i in range(n):
L,v,l,r=[int(i) for i in input().split()]
st=L//v
hu=(r-l+1)
if hu < v:
if l%v==0 or r % v==0:
st-=1
else:
st=st-hu//v
print(st)
``` | instruction | 0 | 58,862 | 1 | 117,724 |
No | output | 1 | 58,862 | 1 | 117,725 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova plans to go to the conference by train. Initially, the train is at the point 1 and the destination point of the path is the point L. The speed of the train is 1 length unit per minute (i.e. at the first minute the train is at the point 1, at the second minute β at the point 2 and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by v (i.e. the first lantern is at the point v, the second is at the point 2v and so on).
There is also exactly one standing train which occupies all the points from l to r inclusive.
Vova can see the lantern at the point p if p is divisible by v and there is no standing train at this position (p notβ [l; r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to t different conferences, so you should answer t independent queries.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of queries.
Then t lines follow. The i-th line contains four integers L_i, v_i, l_i, r_i (1 β€ L, v β€ 10^9, 1 β€ l β€ r β€ L) β destination point of the i-th path, the period of the lantern appearance and the segment occupied by the standing train.
Output
Print t lines. The i-th line should contain one integer β the answer for the i-th query.
Example
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0
Note
For the first example query, the answer is 3. There are lanterns at positions 2, 4, 6, 8 and 10, but Vova didn't see the lanterns at positions 4 and 6 because of the standing train.
For the second example query, the answer is 0 because the only lantern is at the point 51 and there is also a standing train at this point.
For the third example query, the answer is 1134 because there are 1234 lanterns, but Vova didn't see the lanterns from the position 100 to the position 199 inclusive.
For the fourth example query, the answer is 0 because the standing train covers the whole path.
Submitted Solution:
```
import math
t = int(input())
for _ in range(t):
L, v, l, r = list(map(int, input().split()))
n_lanterns = math.floor(L/v)
print(L, v, l, r)
s = 0
# for k in range(v, L+1, v):
# # print(k)
# if k < l or k > r:
# s = s + 1
for k in range(v, l, v):
s = s + 1
for k in range( (math.floor(r/v)+1)*v , L+1, v):
s = s + 1
print(s)
``` | instruction | 0 | 58,863 | 1 | 117,726 |
No | output | 1 | 58,863 | 1 | 117,727 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 β€ n β€ 2 β
10^5) β the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 4 β
10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer β the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | instruction | 0 | 58,978 | 1 | 117,956 |
Tags: data structures, dp, greedy, math, sortings
Correct Solution:
```
n=int(input())
arr=list(map(int,input().split()))
temp={}
for i in range(n):
if i-arr[i] in temp:
temp[i-arr[i]]+=arr[i]
else:
temp[i-arr[i]]=arr[i]
print(max(temp.values()))
``` | output | 1 | 58,978 | 1 | 117,957 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 β€ n β€ 2 β
10^5) β the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 4 β
10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer β the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | instruction | 0 | 58,979 | 1 | 117,958 |
Tags: data structures, dp, greedy, math, sortings
Correct Solution:
```
import string
def main():
n = int(input())
b = list(map(int, input().split()))
dct = dict()
for i in range(n):
if b[i] - i in dct:
dct[b[i] - i] += b[i]
else:
dct[b[i] - i] = b[i]
print(dct[max(dct, key=lambda x: dct[x])])
if __name__ == "__main__":
t = 1
for i in range(t):
main()
``` | output | 1 | 58,979 | 1 | 117,959 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 β€ n β€ 2 β
10^5) β the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 4 β
10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer β the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | instruction | 0 | 58,980 | 1 | 117,960 |
Tags: data structures, dp, greedy, math, sortings
Correct Solution:
```
import sys
def fastio():
from io import StringIO
from atexit import register
global input
sys.stdin = StringIO(sys.stdin.read())
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.stdout = StringIO()
register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
def debug(*var, sep = ' ', end = '\n'):
print(*var, file=sys.stderr, end = end, sep = sep)
INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br
n, = I()
b = I()
common_dist = dict()
for i in range(n):
dist = b[i] - i
if dist in common_dist:
common_dist[dist].append(i)
else:
common_dist[dist] = [i]
max_but = 0
for key in common_dist:
vals = common_dist[key]
but = 0
for val in vals:
but += b[val]
if but > max_but:
max_but = but
print(max_but)
``` | output | 1 | 58,980 | 1 | 117,961 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 β€ n β€ 2 β
10^5) β the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 4 β
10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer β the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | instruction | 0 | 58,981 | 1 | 117,962 |
Tags: data structures, dp, greedy, math, sortings
Correct Solution:
```
n = int(input())
b = list(map(int, input().split()))
next_dest = [-1 for i in range(n)]
accumul = [b[i] for i in range(n)]
diff_to_b = {}
ans = 0
for i in range(n):
if b[i] - i in diff_to_b:
diff_to_b[b[i] - i] += b[i]
if diff_to_b[b[i] - i] > ans:
ans = diff_to_b[b[i] - i]
else:
diff_to_b[b[i] - i] = b[i]
if diff_to_b[b[i] - i] > ans:
ans = diff_to_b[b[i] - i]
print(ans)
# ans = 0
# for diff in diff_to_b:
# for i in range(n):
# for j in range(i+1, n):
# if b[i]-i == b[j] - j:
# next_dest[i] = j
# accumul[j] = accumul[i] + b[j]
# break
# print(max(accumul))
``` | output | 1 | 58,981 | 1 | 117,963 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 β€ n β€ 2 β
10^5) β the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 4 β
10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer β the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | instruction | 0 | 58,982 | 1 | 117,964 |
Tags: data structures, dp, greedy, math, sortings
Correct Solution:
```
from collections import defaultdict
input()
best = defaultdict(int)
a = [int(x) for x in input().split()]
for i, ai in enumerate(a):
best[ai - i] += ai
print(best[max(best, key=best.get)])
``` | output | 1 | 58,982 | 1 | 117,965 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 β€ n β€ 2 β
10^5) β the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 4 β
10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer β the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | instruction | 0 | 58,983 | 1 | 117,966 |
Tags: data structures, dp, greedy, math, sortings
Correct Solution:
```
import sys
input=sys.stdin.readline
from math import *
n=int(input())
a=[int(x) for x in input().split()]
maxi=max(a)
d={}
for i in range(n):
if a[i]-i in d:
d[a[i]-i]+=a[i]
else:
d[a[i]-i]=a[i]
for i in d:
maxi=max(maxi,d[i])
print(maxi)
``` | output | 1 | 58,983 | 1 | 117,967 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 β€ n β€ 2 β
10^5) β the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 4 β
10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer β the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | instruction | 0 | 58,984 | 1 | 117,968 |
Tags: data structures, dp, greedy, math, sortings
Correct Solution:
```
# from debug import debug
inf = int(1e10)
n = int(input())
lis = list(map(int, input().split()))
c = sorted([(lis[i]-i, lis[i]) for i in range(n)])
v = c[0][1]
ans = 0
for i in range(1, n):
if c[i-1][0] == c[i][0]:
v+=c[i][1]
else:
ans = max(ans, v)
v = c[i][1]
print(max(ans, v))
``` | output | 1 | 58,984 | 1 | 117,969 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 β€ n β€ 2 β
10^5) β the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 4 β
10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer β the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | instruction | 0 | 58,985 | 1 | 117,970 |
Tags: data structures, dp, greedy, math, sortings
Correct Solution:
```
n = int(input())
arr = list(map(int, input().split()))
d = {}
for i in range(n):
val = arr[i]-i
if val not in d:
d[val] = [arr[i]]
else:
d[val].append(arr[i])
m = -10**10
ans = []
for i in d:
m = max(m,sum(d[i]))
print(m)
``` | output | 1 | 58,985 | 1 | 117,971 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 β€ n β€ 2 β
10^5) β the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 4 β
10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer β the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6].
Submitted Solution:
```
import os,io
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n = int(input())
b = list(map(int,input().split()))
d = {}
mv = 0
for i in range(n):
v = b[i]-i
if v in d:
d[v]+=b[i]
else:
d[v]=b[i]
mv = max(mv,d[v])
print(mv)
``` | instruction | 0 | 58,986 | 1 | 117,972 |
Yes | output | 1 | 58,986 | 1 | 117,973 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 β€ n β€ 2 β
10^5) β the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 4 β
10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer β the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6].
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
dct = {}
for i in range(n):
if a[i] - i in dct:
dct[a[i] - i] += a[i]
else:
dct[a[i] - i] = a[i]
print(max(dct.values()))
``` | instruction | 0 | 58,987 | 1 | 117,974 |
Yes | output | 1 | 58,987 | 1 | 117,975 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 β€ n β€ 2 β
10^5) β the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 4 β
10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer β the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6].
Submitted Solution:
```
n = int(input())
r = list(map(int,input().split()))
ans = 0
d = {}
if n == 1:
print(r[0])
else:
for i in range(n):
r[i]-=i
if r[i] not in d:
d[r[i]] = r[i]+i
else:
d[r[i]] += r[i]+i
ma = 0
san = 0
for i in d:
if d[i] > ma:
ma = d[i]
san = i
for i in range(n):
if r[i] == san:
ans += r[i]+i
print(ans)
``` | instruction | 0 | 58,988 | 1 | 117,976 |
Yes | output | 1 | 58,988 | 1 | 117,977 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 β€ n β€ 2 β
10^5) β the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 4 β
10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer β the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6].
Submitted Solution:
```
n=int(input())
b=list(map(int,input().split()))
d={}
maxi=0
if n==1:
print(b[0])
else:
for i in range(n):
if i-b[i] not in d:
d[(i-b[i])]=[b[i]]
else:
d[i-b[i]].append(b[i])
for i in d.keys():
s=sum(d[i])
#print(d[i])
if s>maxi:
maxi=s
print(maxi)
``` | instruction | 0 | 58,989 | 1 | 117,978 |
Yes | output | 1 | 58,989 | 1 | 117,979 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 β€ n β€ 2 β
10^5) β the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 4 β
10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer β the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6].
Submitted Solution:
```
# from debug import debug
n = int(input())
lis = list(map(int, input().split()))
b = [lis[i]-lis[i-1] for i in range(1, n)]
#nge
nge = [-1]*n
stack = []
for i in range(n-2, -1, -1):
if lis[i]<lis[i+1]:
stack.append(i+1)
nge[i] = stack[-1]
elif lis[i] == lis[i+1]:
nge[i] = nge[i+1]
else:
while stack and lis[i]>=lis[stack[-1]]:
stack.pop()
if stack:
nge[i] = stack[-1]
else:
nge[i] = -1
ml = 0
for i in range(n):
l = lis[i]
x = i
path = []
while x != -1:
p1, e = lis[x], x
x = nge[x]
p2 = lis[x]
if p2-p1 == x-e:
l+=lis[x]
ml = max(ml, l)
print(ml)
``` | instruction | 0 | 58,990 | 1 | 117,980 |
No | output | 1 | 58,990 | 1 | 117,981 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 β€ n β€ 2 β
10^5) β the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 4 β
10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer β the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6].
Submitted Solution:
```
n = int(input())
b = list(map(int,input().split()))
dp = b.copy()
ov = [n-1]
maxi = dp[n-1]
for i in range(n-2,-1,-1):
for j,k in enumerate(ov):
if b[k]-b[i]==k-i:
dp[i]+=dp[k]
maxi = max(dp[i],maxi)
ov[j]=i
break
else:
ov.append(i)
#print(dp)
print(maxi)
``` | instruction | 0 | 58,991 | 1 | 117,982 |
No | output | 1 | 58,991 | 1 | 117,983 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 β€ n β€ 2 β
10^5) β the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 4 β
10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer β the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6].
Submitted Solution:
```
n = int(input())
b = [int(ele) for ele in input().split()]
diff = {}
for i in range(n):
diff[b[i]-i] = 0
for i in range(1,n):
diff[b[i]-i] += b[i]
maxi = 0
for key in diff.keys():
maxi = max(maxi, diff[key])
print(maxi)
``` | instruction | 0 | 58,992 | 1 | 117,984 |
No | output | 1 | 58,992 | 1 | 117,985 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 β€ n β€ 2 β
10^5) β the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 4 β
10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer β the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6].
Submitted Solution:
```
def main():
n=int(input())
b=list(map(int,input().split()))
c=b[:]
for i in range(n):
c[i]-=i
b[i]=[b[i],c[i]]
b.sort(key=lambda x:x[1])
max_count=0
for i in range(n):
if i==0:
a=b[i][1]
count=b[i][0]
max_count=count
elif a==b[i][1]:
count+=b[i][0]
max_count=max(max_count,count)
elif a!=b[i][1]:
a=b[i][1]
max_count=max(max_count,count)
count=b[i][0]
print(max_count)
if __name__=="__main__":
main()
``` | instruction | 0 | 58,993 | 1 | 117,986 |
No | output | 1 | 58,993 | 1 | 117,987 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's our faculty's 34th anniversary! To celebrate this great event, the Faculty of Computer Science, University of Indonesia (Fasilkom), held CPC - Coloring Pavements Competition. The gist of CPC is two players color the predetermined routes of Fasilkom in Blue and Red. There are N Checkpoints and M undirected predetermined routes. Routes i connects checkpoint U_i and V_i, for (1 β€ i β€ M). It is guaranteed that any pair of checkpoints are connected by using one or more routes.
The rules of CPC is as follows:
* Two players play in each round. One player plays as blue, the other plays as red. For simplicity, let's call these players Blue and Red.
* Blue will color every route in he walks on blue, Red will color the route he walks on red. Both players start at checkpoint number 1. Initially, all routes are gray.
* Each phase, from their current checkpoint, Blue and Red select a different gray route and moves to the checkpoint on the other end of the route simultaneously.
* The game ends when Blue or Red can no longer move. That is, there is no two distinct gray routes they can choose to continue moving.
Chaneka is interested in participating. However, she does not want to waste much energy. So, She is only interested in the number of final configurations of the routes after each round. Turns out, counting this is also exhausting, so Chaneka asks you to figure this out!
Two final configurations are considered different if there is a route U in a different color in the two configurations.
Input
The first line contains two integers N and M. N (2 β€ N β€ 2 β
10^3) denotes the number of checkpoints, M (1 β€ M β€ 2 β
N) denotes the number of routes. It is guaranteed that every checkpoint except checkpoint 1 has exactly two routes connecting it.
The next M lines each contains two integers U_i and V_i (1 β€ U_i, V_i β€ N, U_i β V_i), which denotes the checkpoint that route i connects.
It is guaranteed that for every pair of checkpoints, there exists a path connecting them directly or indirectly using the routes.
Output
Output a single integer which denotes the number of final configurations after each round of CPC modulo 10^9 + 7
Example
Input
5 6
1 2
2 3
3 4
4 1
1 5
5 1
Output
8
Note
Every possible final configuration for the example is listed below:
<image>
The blue-colored numbers give the series of moves Blue took, and the red-colored numbers give the series of moves Red took.
Submitted Solution:
```
from collections import defaultdict
n, m = map(int, input().split())
paths = defaultdict(lambda: defaultdict(int))
# k: counter of paths to connected nodes from k
for i in range(m):
u, v = map(int, input().split())
paths[u][v] += 1
paths[v][u] += 1
cache = {} # u, v where u < v
def countPaths(u, v):
u, v = min(u, v), max(u, v)
k = (u, v)
if k in cache:
return cache[k]
output = 0
for nodeU, countU in paths[u].items():
if countU > 0:
paths[u][nodeU] -= 1
paths[nodeU][u] -= 1
for nodeV, countV in paths[v].items():
if countV > 0:
paths[v][nodeV] -= 1
paths[nodeV][v] -= 1
output += countPaths(nodeU, nodeV)
paths[v][nodeV] += 1
paths[nodeV][v] += 1
paths[u][nodeU] -= 1
paths[nodeU][u] -= 1
output = output or 1
cache[k] = output
return output
print(countPaths(1, 1)*2)
``` | instruction | 0 | 59,043 | 1 | 118,086 |
No | output | 1 | 59,043 | 1 | 118,087 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's our faculty's 34th anniversary! To celebrate this great event, the Faculty of Computer Science, University of Indonesia (Fasilkom), held CPC - Coloring Pavements Competition. The gist of CPC is two players color the predetermined routes of Fasilkom in Blue and Red. There are N Checkpoints and M undirected predetermined routes. Routes i connects checkpoint U_i and V_i, for (1 β€ i β€ M). It is guaranteed that any pair of checkpoints are connected by using one or more routes.
The rules of CPC is as follows:
* Two players play in each round. One player plays as blue, the other plays as red. For simplicity, let's call these players Blue and Red.
* Blue will color every route in he walks on blue, Red will color the route he walks on red. Both players start at checkpoint number 1. Initially, all routes are gray.
* Each phase, from their current checkpoint, Blue and Red select a different gray route and moves to the checkpoint on the other end of the route simultaneously.
* The game ends when Blue or Red can no longer move. That is, there is no two distinct gray routes they can choose to continue moving.
Chaneka is interested in participating. However, she does not want to waste much energy. So, She is only interested in the number of final configurations of the routes after each round. Turns out, counting this is also exhausting, so Chaneka asks you to figure this out!
Two final configurations are considered different if there is a route U in a different color in the two configurations.
Input
The first line contains two integers N and M. N (2 β€ N β€ 2 β
10^3) denotes the number of checkpoints, M (1 β€ M β€ 2 β
N) denotes the number of routes. It is guaranteed that every checkpoint except checkpoint 1 has exactly two routes connecting it.
The next M lines each contains two integers U_i and V_i (1 β€ U_i, V_i β€ N, U_i β V_i), which denotes the checkpoint that route i connects.
It is guaranteed that for every pair of checkpoints, there exists a path connecting them directly or indirectly using the routes.
Output
Output a single integer which denotes the number of final configurations after each round of CPC modulo 10^9 + 7
Example
Input
5 6
1 2
2 3
3 4
4 1
1 5
5 1
Output
8
Note
Every possible final configuration for the example is listed below:
<image>
The blue-colored numbers give the series of moves Blue took, and the red-colored numbers give the series of moves Red took.
Submitted Solution:
```
n,k=map(int,input().split())
l=[]
l1=[]
List1=[]
def combin(n, k):
"""Nombre de combinaisons de n objets pris k a k"""
if k > n//2:
k = n-k
x = 1
y = 1
i = n-k+1
while i <= n:
x = (x*i)//y
y += 1
i += 1
return x
for i in range(k):
a,b=map(int,input().split())
l.append(a)
l.append(b)
#print(l)
#list1.append(l[1])
ch=""
acmp=0
for i in range(len(l)):
ch=ch+str(l[i])
l1=ch.split('1')
#print(l1)
for i in l1:
if (len(i)>0 and (len(i)//2)%2==0):
acmp+=1
#print(acmp)
cmp=0
nb=1
for i in range(2,len(l)-1):
if((l[i-1]==1 or l[i+1]==1)and l[i]!=1):
nb+=1
#print(nb)
if(nb==0):
if(k%2==0):
print(2)
else:
print(0)
elif(nb==2):
print(2)
else:
cmp=cmp+((nb-2*acmp)//2)+nb-2*acmp
for i in range(1,(nb-acmp)//2):
cmp=cmp+combin((nb-acmp)//2,i)
print(cmp)
``` | instruction | 0 | 59,044 | 1 | 118,088 |
No | output | 1 | 59,044 | 1 | 118,089 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.
There are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j costs <image> and can be used multiple times. Help Sajjad to find the minimum cost he needs to pay for tickets to visit all schools. He can start and finish in any school.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the number of schools.
Output
Print single integer: the minimum cost of tickets needed to visit all schools.
Examples
Input
2
Output
0
Input
10
Output
4
Note
In the first example we can buy a ticket between the schools that costs <image>.
Submitted Solution:
```
l = int(input())
if l % 2 == 0:
print(l // 2 - 1)
else:
print(l // 2)
``` | instruction | 0 | 59,373 | 1 | 118,746 |
Yes | output | 1 | 59,373 | 1 | 118,747 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.
There are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j costs <image> and can be used multiple times. Help Sajjad to find the minimum cost he needs to pay for tickets to visit all schools. He can start and finish in any school.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the number of schools.
Output
Print single integer: the minimum cost of tickets needed to visit all schools.
Examples
Input
2
Output
0
Input
10
Output
4
Note
In the first example we can buy a ticket between the schools that costs <image>.
Submitted Solution:
```
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def MAP2():return map(float,input().split())
def LIST(): return list(map(int, input().split()))
def STRING(): return input()
import string
import sys
from heapq import heappop , heappush
from bisect import *
from collections import deque , Counter , defaultdict
from math import *
from itertools import permutations , accumulate
dx = [-1 , 1 , 0 , 0 ]
dy = [0 , 0 , 1 , - 1]
#visited = [[False for i in range(m)] for j in range(n)]
#sys.stdin = open(r'input.txt' , 'r')
#sys.stdout = open(r'output.txt' , 'w')
#for tt in range(INT()):
#CODE
n = INT()
arr = []
for i in range(1 , n + 1):
arr.append(i)
arr.sort()
if n % 2 == 0 :
m = n // 2
else:
m = n // 2 + 1
sm = []
last = -1
for i in range(m):
if last == -1:
x = arr[i] + arr[n - i - 1]
sm.append(x % (n + 1))
last = arr[n - i - 1]
else:
if i != n - i - 1:
sm.append((last + arr[i]) % (n + 1))
x = arr[i] + arr[n - i - 1]
sm.append(x % (n + 1))
last = arr[n - i - 1]
else:
sm.append((last + arr[i]) % (n + 1))
#print(sm)
print(sum(sm))
``` | instruction | 0 | 59,374 | 1 | 118,748 |
Yes | output | 1 | 59,374 | 1 | 118,749 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.
There are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j costs <image> and can be used multiple times. Help Sajjad to find the minimum cost he needs to pay for tickets to visit all schools. He can start and finish in any school.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the number of schools.
Output
Print single integer: the minimum cost of tickets needed to visit all schools.
Examples
Input
2
Output
0
Input
10
Output
4
Note
In the first example we can buy a ticket between the schools that costs <image>.
Submitted Solution:
```
import sys
def solve():
num = int(input())
#num=10
#num2=10
print((num-1)//2)
if __name__ == '__main__':
solve()
``` | instruction | 0 | 59,375 | 1 | 118,750 |
Yes | output | 1 | 59,375 | 1 | 118,751 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.
There are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j costs <image> and can be used multiple times. Help Sajjad to find the minimum cost he needs to pay for tickets to visit all schools. He can start and finish in any school.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the number of schools.
Output
Print single integer: the minimum cost of tickets needed to visit all schools.
Examples
Input
2
Output
0
Input
10
Output
4
Note
In the first example we can buy a ticket between the schools that costs <image>.
Submitted Solution:
```
import sys
read=lambda:sys.stdin.readline().rstrip()
readi=lambda:int(sys.stdin.readline())
writeln=lambda x:sys.stdout.write(str(x)+"\n")
write=lambda x:sys.stdout.write(x)
N=readi()
writeln((N-1)//2)
``` | instruction | 0 | 59,376 | 1 | 118,752 |
Yes | output | 1 | 59,376 | 1 | 118,753 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.
There are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j costs <image> and can be used multiple times. Help Sajjad to find the minimum cost he needs to pay for tickets to visit all schools. He can start and finish in any school.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the number of schools.
Output
Print single integer: the minimum cost of tickets needed to visit all schools.
Examples
Input
2
Output
0
Input
10
Output
4
Note
In the first example we can buy a ticket between the schools that costs <image>.
Submitted Solution:
```
n=int(input())
if n%2==0:
print((n-2)/2)
if n%2==1:
print((n-1)/2)
``` | instruction | 0 | 59,377 | 1 | 118,754 |
No | output | 1 | 59,377 | 1 | 118,755 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.
There are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j costs <image> and can be used multiple times. Help Sajjad to find the minimum cost he needs to pay for tickets to visit all schools. He can start and finish in any school.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the number of schools.
Output
Print single integer: the minimum cost of tickets needed to visit all schools.
Examples
Input
2
Output
0
Input
10
Output
4
Note
In the first example we can buy a ticket between the schools that costs <image>.
Submitted Solution:
```
n=int(input())
if n%2==1:
ans=(n+1)//2
else:
ans=n//2-1
print(ans)
``` | instruction | 0 | 59,378 | 1 | 118,756 |
No | output | 1 | 59,378 | 1 | 118,757 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.
There are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j costs <image> and can be used multiple times. Help Sajjad to find the minimum cost he needs to pay for tickets to visit all schools. He can start and finish in any school.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the number of schools.
Output
Print single integer: the minimum cost of tickets needed to visit all schools.
Examples
Input
2
Output
0
Input
10
Output
4
Note
In the first example we can buy a ticket between the schools that costs <image>.
Submitted Solution:
```
n = int(input())
for i in range(n):
if(i % 4 == 0):
print("a", end = "")
elif(i % 4 == 1):
print("a", end = "")
elif(i % 4 == 2):
print("b", end = "")
else:
print("b", end = "")
``` | instruction | 0 | 59,379 | 1 | 118,758 |
No | output | 1 | 59,379 | 1 | 118,759 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.
There are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j costs <image> and can be used multiple times. Help Sajjad to find the minimum cost he needs to pay for tickets to visit all schools. He can start and finish in any school.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the number of schools.
Output
Print single integer: the minimum cost of tickets needed to visit all schools.
Examples
Input
2
Output
0
Input
10
Output
4
Note
In the first example we can buy a ticket between the schools that costs <image>.
Submitted Solution:
```
n = int(input())
if n % 2 == 0:
print(n // 2 - 1)
else:
print(n - 1)
``` | instruction | 0 | 59,380 | 1 | 118,760 |
No | output | 1 | 59,380 | 1 | 118,761 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Berland capital (as you very well know) contains n junctions, some pairs of which are connected by two-way roads. Unfortunately, the number of traffic jams in the capital has increased dramatically, that's why it was decided to build several new roads. Every road should connect two junctions.
The city administration noticed that in the cities of all the developed countries between any two roads one can drive along at least two paths so that the paths don't share any roads (but they may share the same junction). The administration decided to add the minimal number of roads so that this rules was fulfilled in the Berland capital as well. In the city road network should exist no more than one road between every pair of junctions before or after the reform.
Input
The first input line contains a pair of integers n, m (2 β€ n β€ 900, 1 β€ m β€ 100000), where n is the number of junctions and m is the number of roads. Each of the following m lines contains a description of a road that is given by the numbers of the connected junctions ai, bi (1 β€ ai, bi β€ n, ai β bi). The junctions are numbered from 1 to n. It is possible to reach any junction of the city from any other one moving along roads.
Output
On the first line print t β the number of added roads. Then on t lines print the descriptions of the added roads in the format of the input data. You can use any order of printing the roads themselves as well as the junctions linked by every road. If there are several solutions to that problem, print any of them.
If the capital doesn't need the reform, print the single number 0.
If there's no solution, print the single number -1.
Examples
Input
4 3
1 2
2 3
3 4
Output
1
1 4
Input
4 4
1 2
2 3
2 4
3 4
Output
1
1 3 | instruction | 0 | 60,143 | 1 | 120,286 |
Tags: graphs
Correct Solution:
```
import sys
import threading
def main():
p = input().split()
n = int(p[0]) #number of locations
m = int(p[1]) #number of passages
if n==1: #if there's only one location, there's nothing to do
print(0)
return
if n==2: #if there's only two nodes, the only edge between them, is always a bridge
print(-1)
return
g = graph(n,m)
g.buildBridgeTree()
e = graph(len(g.bridges)+1,len(g.bridges)) #create the new graph to put the bridge tree
e.conexions= []
e.pi=[]
e.discovery=[]
e.seen=[]
for i in range(len(g.bridges)+1):
e.conexions.append([])
e.pi.append(-1)
e.discovery.append(sys.float_info.max)
e.seen.append(False)
#clean the information
for i in range(len(g.bridges)):
e.conexions[g.cc[g.bridges[i][0]]].append((g.cc[g.bridges[i][1]],True))
e.conexions[g.cc[g.bridges[i][1]]].append((g.cc[g.bridges[i][0]],True))
#set the new edges
e.bridges=g.bridges
e.bridge=g.bridge
e.cc=g.cc
e.CC=e.CC
#some information is the same for the graph and its bridge tree
e.treeDiameter()
print(len(e.newEdges))
for i in range(len(e.newEdges)):
u = e.newEdges[i][0] +1
v = e.newEdges[i][1] +1
print(u, end=" ")
print(v)
class graph:
n = int() #number of nodes
edges= int() #number of edges
time = int()
conexions = [] #adjacency list
bridges=[] #are we using a removable edge or not for city i?
bridge=[]
discovery = [] #time to discover node i
seen = [] #visited or not
cc = [] #index of connected components
low = [] #low[i]=min(discovery(i),discovery(w)) w:any node discoverable from i
pi = [] #index of i's father
CC = []
newEdges = []
def __init__(self, n, m):
self.n = n
self.edges = m
for i in range(self.n):
self.conexions.append([])
self.discovery.append(sys.float_info.max)
self.low.append(sys.float_info.max)
self.seen.append(False)
self.cc.append(-1)
self.CC.append([])
self.bridge.append([])
def buildGraph(self):
#this method put every edge in the adjacency list
for i in range(self.edges):
p2=input().split()
self.conexions[int(p2[0])-1].append((int(p2[1])-1,False))
self.conexions[int(p2[1])-1].append((int(p2[0])-1,False))
def searchBridges (self):
self.time = 0
for i in range(self.n):
self.pi.append(-1)
for j in range(self.n):
self.bridge[i].append(False)
self.searchBridges_(0,-1) #we can start in any node 'cause the graph is connected
def searchBridges_(self,c,index):
self.seen[c]=True #mark as visited
self.time+=1
self.discovery[c]=self.low[c]= self.time
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if not self.seen[pos]:
self.pi[pos]=c #the node that discovered it
self.searchBridges_(pos,i) #search throw its not visited conexions
self.low[c]= min([self.low[c],self.low[pos]]) #definition of low
elif self.pi[c]!=pos: #It is not the node that discovered it
self.low[c]= min([self.low[c],self.discovery[pos]])
if self.pi[c]!=-1 and self.low[c]==self.discovery[c]: #it isn't the root and none of its connections is reacheable earlier than it
#then is bridge
self.bridges.append((c,self.pi[c]))
self.bridge[self.pi[c]][c]=self.bridge[c][self.pi[c]]= True
for i in range(len(self.conexions[c])):
if(self.conexions[c][i][0]==self.pi[c]):
self.conexions[c][i]=(self.pi[c],True)
self.conexions[self.pi[c]][index]=(c,True)
def findCC(self):
j = 0
for i in range(self.n):
self.pi[i]=-1
self.seen[i]=False
for i in range(self.n):
if self.seen[i]==False:
self.cc[i]=j #assign the index of the new connected component
self.CC[j].append(i)
self.findCC_(i,j) #search throw its not visited conexions
j+=1 #we finished the search in the connected component
def findCC_(self, c, j):
self.seen[c]=True #mark as visited
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if(self.seen[pos]==False and self.conexions[c][i][1]==False):
self.cc[pos]=j #assign the index of the connected component
self.CC[j].append(pos)
self.pi[pos]=c #the node that discovered it
self.findCC_(pos,j) #search throw its not visited conexions
def treeDiameter(self):
while self.n>1:
for i in range(self.n):
self.seen[i]=False
self.pi[i]=-1
self.discovery[0]=0
self.treeDiameter_(0) #search the distance from this node, to the others
max = -1
maxIndex = 0
for i in range(self.n):
if self.discovery[i]>max:
max= self.discovery[i] #look for the furthest node
maxIndex=i
for i in range(self.n):
self.seen[i]=False
self.pi[i]=-1
self.discovery[maxIndex]=0
self.treeDiameter_(maxIndex) #search the distance from the furthest node, to the others
max =-1
maxIndex2=-1
for i in range(self.n):
if self.discovery[i]>max :
max= self.discovery[i] #look for the furthest node to preview furthest node, and that is the diameter in a tree
maxIndex2=i
self.ReBuildTree(maxIndex,maxIndex2)
def treeDiameter_(self, c):
self.seen[c]=True #mark as visited
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if self.seen[pos]==False:
self.pi[pos]=c #the node that discovered it
self.discovery[pos]= self.discovery[c]+1 #distance to the node who discover it + 1
#we don't have to compare and search for other paths, since it's a tree and there is only one path between two nodes.
self.treeDiameter_(pos) #search throw its not visited conex16 ions
def buildBridgeTree(self):
self.buildGraph()
self.searchBridges()
self.findCC()
def ReBuildTree(self, u, v):
find=0
#search the new edge between the two connected components. It can't be a bridge
for i in range(len(self.CC[u])):
for j in range(len(self.CC[v])):
if not self.bridge[self.CC[u][i]][self.CC[v][j]]:
self.newEdges.append((self.CC[u][i],self.CC[v][j]))
find=1
break
if find==1:
break
newIndex=[]
temp = v
self.conexions[u]=None
self.conexions[v]=None
while self.pi[temp]!=u:
self.conexions[self.pi[temp]]=None
temp = self.pi[temp]
r =1
for i in range(self.n):
if(self.conexions[i]!=None):
newIndex.append(r)
r+=1
else:
newIndex.append(0)
#Assign a new index for the connected components
#And join using the zero index, those that are connected with the new edge
self.n=r
#the number of nodes is the same of the connected components that remain
if self.n==1:
return
newCC=[]
self.conexions=[]
for i in range(self.n):
self.conexions.append([])
newCC.append([])
for i in range(len(self.bridges)):
len0 = len(self.CC[self.cc[self.bridges[i][0]]])
if(len0!=0):
for j in range(len0):
newCC[newIndex[self.cc[self.bridges[i][0]]]].append(self.CC[self.cc[self.bridges[i][0]]][j])
self.CC[self.cc[self.bridges[i][0]]]=[]
len1 = len(self.CC[self.cc[self.bridges[i][1]]])
if(len1!=0):
for j in range(len1):
newCC[newIndex[self.cc[self.bridges[i][1]]]].append(self.CC[self.cc[self.bridges[i][1]]][j])
self.CC[self.cc[self.bridges[i][1]]]=[]
#put all elements in its new connected component
self.conexions[newIndex[self.cc[self.bridges[i][0]]]].append((newIndex[self.cc[self.bridges[i][1]]],True))
self.conexions[newIndex[self.cc[self.bridges[i][1]]]].append((newIndex[self.cc[self.bridges[i][0]]],True))
#set the new edges
self.CC= newCC
for i in range(len(self.cc)):
self.cc[i]=newIndex[self.cc[i]]
#update the connected component of each node
if __name__ == '__main__':
main()
``` | output | 1 | 60,143 | 1 | 120,287 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Berland capital (as you very well know) contains n junctions, some pairs of which are connected by two-way roads. Unfortunately, the number of traffic jams in the capital has increased dramatically, that's why it was decided to build several new roads. Every road should connect two junctions.
The city administration noticed that in the cities of all the developed countries between any two roads one can drive along at least two paths so that the paths don't share any roads (but they may share the same junction). The administration decided to add the minimal number of roads so that this rules was fulfilled in the Berland capital as well. In the city road network should exist no more than one road between every pair of junctions before or after the reform.
Input
The first input line contains a pair of integers n, m (2 β€ n β€ 900, 1 β€ m β€ 100000), where n is the number of junctions and m is the number of roads. Each of the following m lines contains a description of a road that is given by the numbers of the connected junctions ai, bi (1 β€ ai, bi β€ n, ai β bi). The junctions are numbered from 1 to n. It is possible to reach any junction of the city from any other one moving along roads.
Output
On the first line print t β the number of added roads. Then on t lines print the descriptions of the added roads in the format of the input data. You can use any order of printing the roads themselves as well as the junctions linked by every road. If there are several solutions to that problem, print any of them.
If the capital doesn't need the reform, print the single number 0.
If there's no solution, print the single number -1.
Examples
Input
4 3
1 2
2 3
3 4
Output
1
1 4
Input
4 4
1 2
2 3
2 4
3 4
Output
1
1 3 | instruction | 0 | 60,144 | 1 | 120,288 |
Tags: graphs
Correct Solution:
```
import sys
import threading
def main():
p = input().split()
n = int(p[0]) #number of locations
m = int(p[1]) #number of passages
if n==1:
print(0)
return
if n==2:
print(-1)
return
g = graph(n,m)
sys.setrecursionlimit(100000)
threading.stack_size(0x2000000)
t = threading.Thread(target=g.buildBridgeTree())
t.start()
t.join()
e = graph(len(g.bridges)+1,len(g.bridges))
e.conexions= []
e.pi=[]
e.discovery=[]
e.seen=[]
for i in range(len(g.bridges)+1):
e.conexions.append([])
e.pi.append(-1)
e.discovery.append(sys.float_info.max)
e.seen.append(False)
for i in range(len(g.bridges)):
e.conexions[g.cc[g.bridges[i][0]]].append((g.cc[g.bridges[i][1]],True))
e.conexions[g.cc[g.bridges[i][1]]].append((g.cc[g.bridges[i][0]],True))
e.bridges=g.bridges
e.bridge=g.bridge
e.cc=g.cc
e.CC=e.CC
e.CC
e.treeDiameter()
print(len(e.newEdges))
for i in range(len(e.newEdges)):
u = e.newEdges[i][0] +1
v = e.newEdges[i][1] +1
print(u, end=" ")
print(v)
class graph:
n = int() #number of nodes
edges= int() #number of edges
time = int()
conexions = [] #adjacency list
bridges=[] #are we using a removable edge or not for city i?
bridge=[]
discovery = [] #time to discover node i
seen = [] #visited or not
cc = [] #index of connected components
low = [] #low[i]=min(discovery(i),discovery(w)) w:any node discoverable from i
pi = [] #index of i's father
CC = []
newEdges = []
def __init__(self, n, m):
self.n = n
self.edges = m
for i in range(self.n):
self.conexions.append([])
self.discovery.append(sys.float_info.max)
self.low.append(sys.float_info.max)
self.seen.append(False)
self.cc.append(-1)
self.CC.append([])
self.bridge.append([])
def buildGraph(self):
#this method put every edge in the adjacency list
for i in range(self.edges):
p2=input().split()
self.conexions[int(p2[0])-1].append((int(p2[1])-1,False))
self.conexions[int(p2[1])-1].append((int(p2[0])-1,False))
def searchBridges (self):
self.time = 0
for i in range(self.n):
self.pi.append(-1)
for j in range(self.n):
self.bridge[i].append(False)
self.searchBridges_(0,-1) #we can start in any node 'cause the graph is connected
def searchBridges_(self,c,index):
self.seen[c]=True #mark as visited
self.time+=1
self.discovery[c]=self.low[c]= self.time
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if not self.seen[pos]:
self.pi[pos]=c #the node that discovered it
self.searchBridges_(pos,i) #search throw its not visited conexions
self.low[c]= min([self.low[c],self.low[pos]]) #definition of low
elif self.pi[c]!=pos: #It is not the node that discovered it
self.low[c]= min([self.low[c],self.discovery[pos]])
if self.pi[c]!=-1 and self.low[c]==self.discovery[c]: #it isn't the root and none of its connections is reacheable earlier than it
self.bridges.append((c,self.pi[c]))
self.bridge[self.pi[c]][c]=self.bridge[c][self.pi[c]]= True
for i in range(len(self.conexions[c])):
if(self.conexions[c][i][0]==self.pi[c]):
self.conexions[c][i]=(self.pi[c],True)
self.conexions[self.pi[c]][index]=(c,True)
def findCC(self):
j = 0
for i in range(self.n):
self.pi[i]=-1
self.seen[i]=False
for i in range(self.n):
if self.seen[i]==False:
self.cc[i]=j #assign the index of the new connected component
self.CC[j].append(i)
self.findCC_(i,j) #search throw its not visited conexions
j+=1 #we finished the search in the connected component
def findCC_(self, c, j):
self.seen[c]=True #mark as visited
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if(self.seen[pos]==False and self.conexions[c][i][1]==False):
self.cc[pos]=j #assign the index of the connected component
self.CC[j].append(pos)
self.pi[pos]=c #the node that discovered it
self.findCC_(pos,j) #search throw its not visited conexions
def treeDiameter(self):
while self.n>1:
for i in range(self.n):
self.seen[i]=False
self.pi[i]=-1
self.discovery[0]=0
self.treeDiameter_(0) #search the distance from this node, to the others
max = -1
maxIndex = 0
for i in range(self.n):
if self.discovery[i]>max:
max= self.discovery[i] #look for the furthest node
maxIndex=i
for i in range(self.n):
self.seen[i]=False
self.pi[i]=-1
self.discovery[maxIndex]=0
self.treeDiameter_(maxIndex) #search the distance from the furthest node, to the others
max =-1
maxIndex2=-1
for i in range(self.n):
if self.discovery[i]>max :
max= self.discovery[i] #look for the furthest node to preview furthest node, and that is the diameter in a tree
maxIndex2=i
self.ReBuildTree(maxIndex,maxIndex2)
def treeDiameter_(self, c):
self.seen[c]=True #mark as visited
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if self.seen[pos]==False:
self.pi[pos]=c #the node that discovered it
self.discovery[pos]= self.discovery[c]+1 #distance to the node who discover it + 1
#we don't have to compare and search for other paths, since it's a tree and there is only one path between two nodes.
self.treeDiameter_(pos) #search throw its not visited conex16 ions
def buildBridgeTree(self):
self.buildGraph()
self.searchBridges()
self.findCC()
def ReBuildTree(self, u, v):
find=0
for i in range(len(self.CC[u])):
for j in range(len(self.CC[v])):
if not self.bridge[self.CC[u][i]][self.CC[v][j]]:
self.newEdges.append((self.CC[u][i],self.CC[v][j]))
find=1
break
if find==1:
break
newIndex=[]
temp = v
self.conexions[u]=None
self.conexions[v]=None
while self.pi[temp]!=u:
self.conexions[self.pi[temp]]=None
temp = self.pi[temp]
r =1
for i in range(self.n):
if(self.conexions[i]!=None):
newIndex.append(r)
r+=1
else:
newIndex.append(0)
self.n=r
if self.n==1:
return
newCC=[]
self.conexions=[]
for i in range(self.n):
self.conexions.append([])
newCC.append([])
for i in range(len(self.bridges)):
len0 = len(self.CC[self.cc[self.bridges[i][0]]])
if(len0!=0):
for j in range(len0):
newCC[newIndex[self.cc[self.bridges[i][0]]]].append(self.CC[self.cc[self.bridges[i][0]]][j])
self.CC[self.cc[self.bridges[i][0]]]=[]
len1 = len(self.CC[self.cc[self.bridges[i][1]]])
if(len1!=0):
for j in range(len1):
newCC[newIndex[self.cc[self.bridges[i][1]]]].append(self.CC[self.cc[self.bridges[i][1]]][j])
self.CC[self.cc[self.bridges[i][1]]]=[]
self.conexions[newIndex[self.cc[self.bridges[i][0]]]].append((newIndex[self.cc[self.bridges[i][1]]],True))
self.conexions[newIndex[self.cc[self.bridges[i][1]]]].append((newIndex[self.cc[self.bridges[i][0]]],True))
self.CC= newCC
for i in range(len(self.cc)):
self.cc[i]=newIndex[self.cc[i]]
if __name__ == '__main__':
main()
``` | output | 1 | 60,144 | 1 | 120,289 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Berland capital (as you very well know) contains n junctions, some pairs of which are connected by two-way roads. Unfortunately, the number of traffic jams in the capital has increased dramatically, that's why it was decided to build several new roads. Every road should connect two junctions.
The city administration noticed that in the cities of all the developed countries between any two roads one can drive along at least two paths so that the paths don't share any roads (but they may share the same junction). The administration decided to add the minimal number of roads so that this rules was fulfilled in the Berland capital as well. In the city road network should exist no more than one road between every pair of junctions before or after the reform.
Input
The first input line contains a pair of integers n, m (2 β€ n β€ 900, 1 β€ m β€ 100000), where n is the number of junctions and m is the number of roads. Each of the following m lines contains a description of a road that is given by the numbers of the connected junctions ai, bi (1 β€ ai, bi β€ n, ai β bi). The junctions are numbered from 1 to n. It is possible to reach any junction of the city from any other one moving along roads.
Output
On the first line print t β the number of added roads. Then on t lines print the descriptions of the added roads in the format of the input data. You can use any order of printing the roads themselves as well as the junctions linked by every road. If there are several solutions to that problem, print any of them.
If the capital doesn't need the reform, print the single number 0.
If there's no solution, print the single number -1.
Examples
Input
4 3
1 2
2 3
3 4
Output
1
1 4
Input
4 4
1 2
2 3
2 4
3 4
Output
1
1 3 | instruction | 0 | 60,145 | 1 | 120,290 |
Tags: graphs
Correct Solution:
```
def dfs1(v):
mark[v]=1
st.append(v)
dp[v]=d[v]
for u in adj[v]:
if not mark[u]:
d[u]=d[v]+1
dfs1(u)
dp[v]=min(dp[u],dp[v])
elif d[u]<d[v]-1:
dp[v]=min(dp[v],d[u])
if dp[v]==d[v]:
ver[cnt[0]]=v
prv =-1
while prv != v:
prv = st.pop()
if d[ver[cnt[0]]]<d[prv]:
ver[cnt[0]]=prv
col[prv]=cnt[0]
for u in adj[prv]:
if col[u]!=-1 and col[u]!= cnt[0]:
child[cnt[0]].append(col[u])
cnt[0]+=1
cnt=[0]
N = 100000
adj=[[]for i in range(N)]
child=[[]for i in range(N)]
ord=[]
mark=[False]*N
dp=[-1]*N
d=[-1]*N
st = []
col=[-1]*N
ver =[-1]*N
n,m = map(int,input().split())
if n == 2 and m == 1:
print("-1")
exit(0)
for i in range(m):
u,v = map(int,input().split())
adj[v-1].append(u-1)
adj[u-1].append(v-1)
dfs1(0)
ver[cnt[0]-1] = 0
if cnt[0] == 1:
print("0")
exit(0)
for i in range(cnt[0]):
if len(child[i]) == 0:
ord.append(i)
if len(child[cnt[0]-1]) == 1:
ord.append(cnt[0]-1);
ans = int((len(ord)+1) /2)
print(ans)
ans1=0
ans2=0
for i in range(ans):
ans1 = ver[ord[i]]+1
ans2 = ver[ord[min(len(ord),int(i+len(ord)/2))]]+1
print(ans1,ans2)
``` | output | 1 | 60,145 | 1 | 120,291 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Berland capital (as you very well know) contains n junctions, some pairs of which are connected by two-way roads. Unfortunately, the number of traffic jams in the capital has increased dramatically, that's why it was decided to build several new roads. Every road should connect two junctions.
The city administration noticed that in the cities of all the developed countries between any two roads one can drive along at least two paths so that the paths don't share any roads (but they may share the same junction). The administration decided to add the minimal number of roads so that this rules was fulfilled in the Berland capital as well. In the city road network should exist no more than one road between every pair of junctions before or after the reform.
Input
The first input line contains a pair of integers n, m (2 β€ n β€ 900, 1 β€ m β€ 100000), where n is the number of junctions and m is the number of roads. Each of the following m lines contains a description of a road that is given by the numbers of the connected junctions ai, bi (1 β€ ai, bi β€ n, ai β bi). The junctions are numbered from 1 to n. It is possible to reach any junction of the city from any other one moving along roads.
Output
On the first line print t β the number of added roads. Then on t lines print the descriptions of the added roads in the format of the input data. You can use any order of printing the roads themselves as well as the junctions linked by every road. If there are several solutions to that problem, print any of them.
If the capital doesn't need the reform, print the single number 0.
If there's no solution, print the single number -1.
Examples
Input
4 3
1 2
2 3
3 4
Output
1
1 4
Input
4 4
1 2
2 3
2 4
3 4
Output
1
1 3 | instruction | 0 | 60,146 | 1 | 120,292 |
Tags: graphs
Correct Solution:
```
import sys
import threading
def main():
p = input().split()
n = int(p[0]) #number of locations
m = int(p[1]) #number of passages
if n==1: #if there's only one location, there's nothing to do
print(0)
return
if n==2: #if there's only two nodes, the only edge between them, is always a bridge
print(-1)
return
g = graph(n,m)
g.buildBridgeTree()
e = graph(len(g.bridges)+1,len(g.bridges))
e.conexions= []
e.pi=[]
e.discovery=[]
e.seen=[]
for i in range(len(g.bridges)+1):
e.conexions.append([])
e.pi.append(-1)
e.discovery.append(sys.float_info.max)
e.seen.append(False)
for i in range(len(g.bridges)):
e.conexions[g.cc[g.bridges[i][0]]].append((g.cc[g.bridges[i][1]],True))
e.conexions[g.cc[g.bridges[i][1]]].append((g.cc[g.bridges[i][0]],True))
e.bridges=g.bridges
e.bridge=g.bridge
e.cc=g.cc
e.CC=e.CC
e.CC
e.treeDiameter()
print(len(e.newEdges))
for i in range(len(e.newEdges)):
u = e.newEdges[i][0] +1
v = e.newEdges[i][1] +1
print(u, end=" ")
print(v)
class graph:
n = int() #number of nodes
edges= int() #number of edges
time = int()
conexions = [] #adjacency list
bridges=[] #are we using a removable edge or not for city i?
bridge=[]
discovery = [] #time to discover node i
seen = [] #visited or not
cc = [] #index of connected components
low = [] #low[i]=min(discovery(i),discovery(w)) w:any node discoverable from i
pi = [] #index of i's father
CC = []
newEdges = []
def __init__(self, n, m):
self.n = n
self.edges = m
for i in range(self.n):
self.conexions.append([])
self.discovery.append(sys.float_info.max)
self.low.append(sys.float_info.max)
self.seen.append(False)
self.cc.append(-1)
self.CC.append([])
self.bridge.append([])
def buildGraph(self):
#this method put every edge in the adjacency list
for i in range(self.edges):
p2=input().split()
self.conexions[int(p2[0])-1].append((int(p2[1])-1,False))
self.conexions[int(p2[1])-1].append((int(p2[0])-1,False))
def searchBridges (self):
self.time = 0
for i in range(self.n):
self.pi.append(-1)
for j in range(self.n):
self.bridge[i].append(False)
self.searchBridges_(0,-1) #we can start in any node 'cause the graph is connected
def searchBridges_(self,c,index):
self.seen[c]=True #mark as visited
self.time+=1
self.discovery[c]=self.low[c]= self.time
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if not self.seen[pos]:
self.pi[pos]=c #the node that discovered it
self.searchBridges_(pos,i) #search throw its not visited conexions
self.low[c]= min([self.low[c],self.low[pos]]) #definition of low
elif self.pi[c]!=pos: #It is not the node that discovered it
self.low[c]= min([self.low[c],self.discovery[pos]])
if self.pi[c]!=-1 and self.low[c]==self.discovery[c]: #it isn't the root and none of its connections is reacheable earlier than it
self.bridges.append((c,self.pi[c]))
self.bridge[self.pi[c]][c]=self.bridge[c][self.pi[c]]= True
for i in range(len(self.conexions[c])):
if(self.conexions[c][i][0]==self.pi[c]):
self.conexions[c][i]=(self.pi[c],True)
self.conexions[self.pi[c]][index]=(c,True)
def findCC(self):
j = 0
for i in range(self.n):
self.pi[i]=-1
self.seen[i]=False
for i in range(self.n):
if self.seen[i]==False:
self.cc[i]=j #assign the index of the new connected component
self.CC[j].append(i)
self.findCC_(i,j) #search throw its not visited conexions
j+=1 #we finished the search in the connected component
def findCC_(self, c, j):
self.seen[c]=True #mark as visited
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if(self.seen[pos]==False and self.conexions[c][i][1]==False):
self.cc[pos]=j #assign the index of the connected component
self.CC[j].append(pos)
self.pi[pos]=c #the node that discovered it
self.findCC_(pos,j) #search throw its not visited conexions
def treeDiameter(self):
while self.n>1:
for i in range(self.n):
self.seen[i]=False
self.pi[i]=-1
self.discovery[0]=0
self.treeDiameter_(0) #search the distance from this node, to the others
max = -1
maxIndex = 0
for i in range(self.n):
if self.discovery[i]>max:
max= self.discovery[i] #look for the furthest node
maxIndex=i
for i in range(self.n):
self.seen[i]=False
self.pi[i]=-1
self.discovery[maxIndex]=0
self.treeDiameter_(maxIndex) #search the distance from the furthest node, to the others
max =-1
maxIndex2=-1
for i in range(self.n):
if self.discovery[i]>max :
max= self.discovery[i] #look for the furthest node to preview furthest node, and that is the diameter in a tree
maxIndex2=i
self.ReBuildTree(maxIndex,maxIndex2)
def treeDiameter_(self, c):
self.seen[c]=True #mark as visited
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if self.seen[pos]==False:
self.pi[pos]=c #the node that discovered it
self.discovery[pos]= self.discovery[c]+1 #distance to the node who discover it + 1
#we don't have to compare and search for other paths, since it's a tree and there is only one path between two nodes.
self.treeDiameter_(pos) #search throw its not visited conex16 ions
def buildBridgeTree(self):
self.buildGraph()
self.searchBridges()
self.findCC()
def ReBuildTree(self, u, v):
find=0
for i in range(len(self.CC[u])):
for j in range(len(self.CC[v])):
if not self.bridge[self.CC[u][i]][self.CC[v][j]]:
self.newEdges.append((self.CC[u][i],self.CC[v][j]))
find=1
break
if find==1:
break
newIndex=[]
temp = v
self.conexions[u]=None
self.conexions[v]=None
while self.pi[temp]!=u:
self.conexions[self.pi[temp]]=None
temp = self.pi[temp]
r =1
for i in range(self.n):
if(self.conexions[i]!=None):
newIndex.append(r)
r+=1
else:
newIndex.append(0)
self.n=r
if self.n==1:
return
newCC=[]
self.conexions=[]
for i in range(self.n):
self.conexions.append([])
newCC.append([])
for i in range(len(self.bridges)):
len0 = len(self.CC[self.cc[self.bridges[i][0]]])
if(len0!=0):
for j in range(len0):
newCC[newIndex[self.cc[self.bridges[i][0]]]].append(self.CC[self.cc[self.bridges[i][0]]][j])
self.CC[self.cc[self.bridges[i][0]]]=[]
len1 = len(self.CC[self.cc[self.bridges[i][1]]])
if(len1!=0):
for j in range(len1):
newCC[newIndex[self.cc[self.bridges[i][1]]]].append(self.CC[self.cc[self.bridges[i][1]]][j])
self.CC[self.cc[self.bridges[i][1]]]=[]
self.conexions[newIndex[self.cc[self.bridges[i][0]]]].append((newIndex[self.cc[self.bridges[i][1]]],True))
self.conexions[newIndex[self.cc[self.bridges[i][1]]]].append((newIndex[self.cc[self.bridges[i][0]]],True))
self.CC= newCC
for i in range(len(self.cc)):
self.cc[i]=newIndex[self.cc[i]]
if __name__ == '__main__':
main()
``` | output | 1 | 60,146 | 1 | 120,293 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Berland capital (as you very well know) contains n junctions, some pairs of which are connected by two-way roads. Unfortunately, the number of traffic jams in the capital has increased dramatically, that's why it was decided to build several new roads. Every road should connect two junctions.
The city administration noticed that in the cities of all the developed countries between any two roads one can drive along at least two paths so that the paths don't share any roads (but they may share the same junction). The administration decided to add the minimal number of roads so that this rules was fulfilled in the Berland capital as well. In the city road network should exist no more than one road between every pair of junctions before or after the reform.
Input
The first input line contains a pair of integers n, m (2 β€ n β€ 900, 1 β€ m β€ 100000), where n is the number of junctions and m is the number of roads. Each of the following m lines contains a description of a road that is given by the numbers of the connected junctions ai, bi (1 β€ ai, bi β€ n, ai β bi). The junctions are numbered from 1 to n. It is possible to reach any junction of the city from any other one moving along roads.
Output
On the first line print t β the number of added roads. Then on t lines print the descriptions of the added roads in the format of the input data. You can use any order of printing the roads themselves as well as the junctions linked by every road. If there are several solutions to that problem, print any of them.
If the capital doesn't need the reform, print the single number 0.
If there's no solution, print the single number -1.
Examples
Input
4 3
1 2
2 3
3 4
Output
1
1 4
Input
4 4
1 2
2 3
2 4
3 4
Output
1
1 3
Submitted Solution:
```
import sys
import threading
def main():
p = input().split()
n = int(p[0]) #number of locations
m = int(p[1]) #number of passages
if n==1:
print(0)
return
if n==2:
print(-1)
return
g = graph(n,m)
sys.setrecursionlimit(100000)
threading.stack_size(0x2000000)
t = threading.Thread(target=g.buildBridgeTree())
t.start()
t.join()
e = graph(len(g.bridges)+1,len(g.bridges))
e.conexions= []
e.pi=[]
e.discovery=[]
e.seen=[]
for i in range(len(g.bridges)+1):
e.conexions.append([])
e.pi.append(-1)
e.discovery.append(sys.float_info.max)
e.seen.append(False)
for i in range(len(g.bridges)):
e.conexions[g.cc[g.bridges[i][0]]].append((g.cc[g.bridges[i][1]],True))
e.conexions[g.cc[g.bridges[i][1]]].append((g.cc[g.bridges[i][0]],True))
e.bridges=g.bridges
e.bridge=g.bridge
e.cc=g.cc
e.CC=e.CC
e.CC
e.treeDiameter()
print(len(e.newEdges))
for i in range(len(e.newEdges)):
u = e.newEdges[i][0] +1
v = e.newEdges[i][1] +1
print(u, end=" ")
print(v)
class graph:
n = int() #number of nodes
edges= int() #number of edges
time = int()
conexions = [] #adjacency list
bridges=[] #are we using a removable edge or not for city i?
bridge=[]
discovery = [] #time to discover node i
seen = [] #visited or not
cc = [] #index of connected components
low = [] #low[i]=min(discovery(i),discovery(w)) w:any node discoverable from i
pi = [] #index of i's father
CC =[]
newEdges = []
def __init__(self, n, m):
self.n = n
self.edges = m
for i in range(self.n):
self.conexions.append([])
self.discovery.append(sys.float_info.max)
self.low.append(sys.float_info.max)
self.seen.append(False)
self.cc.append(-1)
self.CC.append([])
self.bridge.append([])
def buildGraph(self):
#this method put every edge in the adjacency list
for i in range(self.edges):
p2=input().split()
self.conexions[int(p2[0])-1].append((int(p2[1])-1,False))
self.conexions[int(p2[1])-1].append((int(p2[0])-1,False))
def searchBridges (self):
self.time = 0
for i in range(self.n):
self.pi.append(-1)
for j in range(self.n):
self.bridge[i].append(False)
self.searchBridges_(0,-1) #we can start in any node 'cause the graph is connected
def searchBridges_(self,c,index):
self.seen[c]=True #mark as visited
self.time+=1
self.discovery[c]=self.low[c]= self.time
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if not self.seen[pos]:
self.pi[pos]=c #the node that discovered it
self.searchBridges_(pos,i) #search throw its not visited conexions
self.low[c]= min([self.low[c],self.low[pos]]) #definition of low
elif self.pi[c]!=pos: #It is not the node that discovered it
self.low[c]= min([self.low[c],self.discovery[pos]])
if self.pi[c]!=-1 and self.low[c]==self.discovery[c]: #it isn't the root and none of its connections is reacheable earlier than it
self.bridges.append((c,self.pi[c]))
self.bridge[self.pi[c]][c]=self.bridge[c][self.pi[c]]= True
for i in range(len(self.conexions[c])):
if(self.conexions[c][i][0]==self.pi[c]):
self.conexions[c][i]=(self.pi[c],True)
self.conexions[self.pi[c]][index]=(c,True)
def findCC(self):
j = 0
for i in range(self.n):
self.pi[i]=-1
self.seen[i]=False
for i in range(self.n):
if self.seen[i]==False:
self.cc[i]=j #assign the index of the new connected component
self.CC[j].append(i)
self.findCC_(i,j) #search throw its not visited conexions
j+=1 #we finished the search in the connected component
def findCC_(self, c, j):
self.seen[c]=True #mark as visited
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if(self.seen[pos]==False and self.conexions[c][i][1]==False):
self.cc[pos]=j #assign the index of the connected component
self.CC[j].append(pos)
self.pi[pos]=c #the node that discovered it
self.findCC_(pos,j) #search throw its not visited conexions
def treeDiameter(self):
while self.n>1:
for i in range(self.n):
self.seen[i]=False
self.pi[i]=-1
self.discovery[0]=0
self.treeDiameter_(0) #search the distance from this node, to the others
max = -1
maxIndex = 0
for i in range(self.n):
if self.discovery[i]>max:
max= self.discovery[i] #look for the furthest node
maxIndex=i
for i in range(self.n):
self.seen[i]=False
self.pi[i]=-1
self.discovery[maxIndex]=0
self.treeDiameter_(maxIndex) #search the distance from the furthest node, to the others
max =-1
maxIndex2=-1
for i in range(self.n):
if self.discovery[i]>max :
max= self.discovery[i] #look for the furthest node to preview furthest node, and that is the diameter in a tree
maxIndex2=i
self.ReBuildTree(maxIndex,maxIndex2)
def treeDiameter_(self, c):
self.seen[c]=True #mark as visited
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if self.seen[pos]==False:
self.pi[pos]=c #the node that discovered it
self.discovery[pos]= self.discovery[c]+1 #distance to the node who discover it + 1
#we don't have to compare and search for other paths, since it's a tree and there is only one path between two nodes.
self.treeDiameter_(pos) #search throw its not visited conexions
def buildBridgeTree(self):
self.buildGraph()
self.searchBridges()
self.findCC()
def ReBuildTree(self, u, v):
for i in range(len(self.CC[u])):
for j in range(len(self.CC[v])):
if not self.bridge[self.CC[u][i]][self.CC[v][j]]:
self.newEdges.append((self.CC[u][i],self.CC[v][j]))
i=len(self.CC[u])
break
newIndex=[]
temp = v
self.conexions[u]=None
self.conexions[v]=None
while self.pi[temp]!=u:
self.conexions[self.pi[temp]]=None
temp = self.pi[temp]
r =1
for i in range(self.n):
if(self.conexions[i]!=None):
newIndex.append(r)
r+=1
else:
newIndex.append(0)
self.n=r
if self.n==1:
return
newCC=[]
self.conexions=[]
for i in range(self.n):
self.conexions.append([])
newCC.append([])
for i in range(len(self.bridges)):
len0 = len(self.CC[self.cc[self.bridges[i][0]]])
len1 = len(self.CC[self.cc[self.bridges[i][1]]])
if(len1!=0):
for j in range(len0):
newCC[newIndex[self.cc[self.bridges[i][0]]]].append(self.CC[self.cc[self.bridges[i][0]]][j])
self.CC[self.cc[self.bridges[i][0]]]=[]
if(len1!=0):
for j in range(len1):
newCC[newIndex[self.cc[self.bridges[i][1]]]].append(self.CC[self.cc[self.bridges[i][1]]][j])
self.CC[self.cc[self.bridges[i][1]]]=[]
self.conexions[newIndex[self.cc[self.bridges[i][0]]]].append((newIndex[self.cc[self.bridges[i][1]]],True))
self.conexions[newIndex[self.cc[self.bridges[i][1]]]].append((newIndex[self.cc[self.bridges[i][0]]],True))
self.CC= newCC
for i in range(len(self.cc)):
self.cc[i]=newIndex[self.cc[i]]
if __name__ == '__main__':
main()
``` | instruction | 0 | 60,147 | 1 | 120,294 |
No | output | 1 | 60,147 | 1 | 120,295 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Berland capital (as you very well know) contains n junctions, some pairs of which are connected by two-way roads. Unfortunately, the number of traffic jams in the capital has increased dramatically, that's why it was decided to build several new roads. Every road should connect two junctions.
The city administration noticed that in the cities of all the developed countries between any two roads one can drive along at least two paths so that the paths don't share any roads (but they may share the same junction). The administration decided to add the minimal number of roads so that this rules was fulfilled in the Berland capital as well. In the city road network should exist no more than one road between every pair of junctions before or after the reform.
Input
The first input line contains a pair of integers n, m (2 β€ n β€ 900, 1 β€ m β€ 100000), where n is the number of junctions and m is the number of roads. Each of the following m lines contains a description of a road that is given by the numbers of the connected junctions ai, bi (1 β€ ai, bi β€ n, ai β bi). The junctions are numbered from 1 to n. It is possible to reach any junction of the city from any other one moving along roads.
Output
On the first line print t β the number of added roads. Then on t lines print the descriptions of the added roads in the format of the input data. You can use any order of printing the roads themselves as well as the junctions linked by every road. If there are several solutions to that problem, print any of them.
If the capital doesn't need the reform, print the single number 0.
If there's no solution, print the single number -1.
Examples
Input
4 3
1 2
2 3
3 4
Output
1
1 4
Input
4 4
1 2
2 3
2 4
3 4
Output
1
1 3
Submitted Solution:
```
import sys
import threading
def main():
p = input().split()
n = int(p[0]) #number of locations
m = int(p[1]) #number of passages
if n==1: #if there's only one location, there's nothing to do
print(0)
return
if n==2: #if there's only two nodes, the only edge between them, is always a bridge
print(-1)
return
g = graph(n,m)
e = graph(len(g.bridges)+1,len(g.bridges))
e.conexions= []
e.pi=[]
e.discovery=[]
e.seen=[]
for i in range(len(g.bridges)+1):
e.conexions.append([])
e.pi.append(-1)
e.discovery.append(sys.float_info.max)
e.seen.append(False)
for i in range(len(g.bridges)):
e.conexions[g.cc[g.bridges[i][0]]].append((g.cc[g.bridges[i][1]],True))
e.conexions[g.cc[g.bridges[i][1]]].append((g.cc[g.bridges[i][0]],True))
e.bridges=g.bridges
e.bridge=g.bridge
e.cc=g.cc
e.CC=e.CC
e.CC
e.treeDiameter()
print(len(e.newEdges))
for i in range(len(e.newEdges)):
u = e.newEdges[i][0] +1
v = e.newEdges[i][1] +1
print(u, end=" ")
print(v)
class graph:
n = int() #number of nodes
edges= int() #number of edges
time = int()
conexions = [] #adjacency list
bridges=[] #are we using a removable edge or not for city i?
bridge=[]
discovery = [] #time to discover node i
seen = [] #visited or not
cc = [] #index of connected components
low = [] #low[i]=min(discovery(i),discovery(w)) w:any node discoverable from i
pi = [] #index of i's father
CC = []
newEdges = []
def __init__(self, n, m):
self.n = n
self.edges = m
for i in range(self.n):
self.conexions.append([])
self.discovery.append(sys.float_info.max)
self.low.append(sys.float_info.max)
self.seen.append(False)
self.cc.append(-1)
self.CC.append([])
self.bridge.append([])
def buildGraph(self):
#this method put every edge in the adjacency list
for i in range(self.edges):
p2=input().split()
self.conexions[int(p2[0])-1].append((int(p2[1])-1,False))
self.conexions[int(p2[1])-1].append((int(p2[0])-1,False))
def searchBridges (self):
self.time = 0
for i in range(self.n):
self.pi.append(-1)
for j in range(self.n):
self.bridge[i].append(False)
self.searchBridges_(0,-1) #we can start in any node 'cause the graph is connected
def searchBridges_(self,c,index):
self.seen[c]=True #mark as visited
self.time+=1
self.discovery[c]=self.low[c]= self.time
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if not self.seen[pos]:
self.pi[pos]=c #the node that discovered it
self.searchBridges_(pos,i) #search throw its not visited conexions
self.low[c]= min([self.low[c],self.low[pos]]) #definition of low
elif self.pi[c]!=pos: #It is not the node that discovered it
self.low[c]= min([self.low[c],self.discovery[pos]])
if self.pi[c]!=-1 and self.low[c]==self.discovery[c]: #it isn't the root and none of its connections is reacheable earlier than it
self.bridges.append((c,self.pi[c]))
self.bridge[self.pi[c]][c]=self.bridge[c][self.pi[c]]= True
for i in range(len(self.conexions[c])):
if(self.conexions[c][i][0]==self.pi[c]):
self.conexions[c][i]=(self.pi[c],True)
self.conexions[self.pi[c]][index]=(c,True)
def findCC(self):
j = 0
for i in range(self.n):
self.pi[i]=-1
self.seen[i]=False
for i in range(self.n):
if self.seen[i]==False:
self.cc[i]=j #assign the index of the new connected component
self.CC[j].append(i)
self.findCC_(i,j) #search throw its not visited conexions
j+=1 #we finished the search in the connected component
def findCC_(self, c, j):
self.seen[c]=True #mark as visited
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if(self.seen[pos]==False and self.conexions[c][i][1]==False):
self.cc[pos]=j #assign the index of the connected component
self.CC[j].append(pos)
self.pi[pos]=c #the node that discovered it
self.findCC_(pos,j) #search throw its not visited conexions
def treeDiameter(self):
while self.n>1:
for i in range(self.n):
self.seen[i]=False
self.pi[i]=-1
self.discovery[0]=0
self.treeDiameter_(0) #search the distance from this node, to the others
max = -1
maxIndex = 0
for i in range(self.n):
if self.discovery[i]>max:
max= self.discovery[i] #look for the furthest node
maxIndex=i
for i in range(self.n):
self.seen[i]=False
self.pi[i]=-1
self.discovery[maxIndex]=0
self.treeDiameter_(maxIndex) #search the distance from the furthest node, to the others
max =-1
maxIndex2=-1
for i in range(self.n):
if self.discovery[i]>max :
max= self.discovery[i] #look for the furthest node to preview furthest node, and that is the diameter in a tree
maxIndex2=i
self.ReBuildTree(maxIndex,maxIndex2)
def treeDiameter_(self, c):
self.seen[c]=True #mark as visited
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if self.seen[pos]==False:
self.pi[pos]=c #the node that discovered it
self.discovery[pos]= self.discovery[c]+1 #distance to the node who discover it + 1
#we don't have to compare and search for other paths, since it's a tree and there is only one path between two nodes.
self.treeDiameter_(pos) #search throw its not visited conex16 ions
def buildBridgeTree(self):
self.buildGraph()
self.searchBridges()
self.findCC()
def ReBuildTree(self, u, v):
find=0
for i in range(len(self.CC[u])):
for j in range(len(self.CC[v])):
if not self.bridge[self.CC[u][i]][self.CC[v][j]]:
self.newEdges.append((self.CC[u][i],self.CC[v][j]))
find=1
break
if find==1:
break
newIndex=[]
temp = v
self.conexions[u]=None
self.conexions[v]=None
while self.pi[temp]!=u:
self.conexions[self.pi[temp]]=None
temp = self.pi[temp]
r =1
for i in range(self.n):
if(self.conexions[i]!=None):
newIndex.append(r)
r+=1
else:
newIndex.append(0)
self.n=r
if self.n==1:
return
newCC=[]
self.conexions=[]
for i in range(self.n):
self.conexions.append([])
newCC.append([])
for i in range(len(self.bridges)):
len0 = len(self.CC[self.cc[self.bridges[i][0]]])
if(len0!=0):
for j in range(len0):
newCC[newIndex[self.cc[self.bridges[i][0]]]].append(self.CC[self.cc[self.bridges[i][0]]][j])
self.CC[self.cc[self.bridges[i][0]]]=[]
len1 = len(self.CC[self.cc[self.bridges[i][1]]])
if(len1!=0):
for j in range(len1):
newCC[newIndex[self.cc[self.bridges[i][1]]]].append(self.CC[self.cc[self.bridges[i][1]]][j])
self.CC[self.cc[self.bridges[i][1]]]=[]
self.conexions[newIndex[self.cc[self.bridges[i][0]]]].append((newIndex[self.cc[self.bridges[i][1]]],True))
self.conexions[newIndex[self.cc[self.bridges[i][1]]]].append((newIndex[self.cc[self.bridges[i][0]]],True))
self.CC= newCC
for i in range(len(self.cc)):
self.cc[i]=newIndex[self.cc[i]]
if __name__ == '__main__':
main()
``` | instruction | 0 | 60,148 | 1 | 120,296 |
No | output | 1 | 60,148 | 1 | 120,297 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Berland capital (as you very well know) contains n junctions, some pairs of which are connected by two-way roads. Unfortunately, the number of traffic jams in the capital has increased dramatically, that's why it was decided to build several new roads. Every road should connect two junctions.
The city administration noticed that in the cities of all the developed countries between any two roads one can drive along at least two paths so that the paths don't share any roads (but they may share the same junction). The administration decided to add the minimal number of roads so that this rules was fulfilled in the Berland capital as well. In the city road network should exist no more than one road between every pair of junctions before or after the reform.
Input
The first input line contains a pair of integers n, m (2 β€ n β€ 900, 1 β€ m β€ 100000), where n is the number of junctions and m is the number of roads. Each of the following m lines contains a description of a road that is given by the numbers of the connected junctions ai, bi (1 β€ ai, bi β€ n, ai β bi). The junctions are numbered from 1 to n. It is possible to reach any junction of the city from any other one moving along roads.
Output
On the first line print t β the number of added roads. Then on t lines print the descriptions of the added roads in the format of the input data. You can use any order of printing the roads themselves as well as the junctions linked by every road. If there are several solutions to that problem, print any of them.
If the capital doesn't need the reform, print the single number 0.
If there's no solution, print the single number -1.
Examples
Input
4 3
1 2
2 3
3 4
Output
1
1 4
Input
4 4
1 2
2 3
2 4
3 4
Output
1
1 3
Submitted Solution:
```
import sys
import threading
def main():
p = input().split()
n = int(p[0]) #number of locations
m = int(p[1]) #number of passages
if n==1:
print(0)
return
if n==2:
print(-1)
return
g = graph(n,m)
sys.setrecursionlimit(100000)
threading.stack_size(0x2000000)
t = threading.Thread(target=g.buildBridgeTree())
t.start()
t.join()
e = graph(len(g.bridges)+1,len(g.bridges))
e.conexions= []
e.pi=[]
e.discovery=[]
e.seen=[]
for i in range(len(g.bridges)+1):
e.conexions.append([])
e.pi.append(-1)
e.discovery.append(sys.float_info.max)
e.seen.append(False)
for i in range(len(g.bridges)):
e.conexions[g.cc[g.bridges[i][0]]].append((g.cc[g.bridges[i][1]],True))
e.conexions[g.cc[g.bridges[i][1]]].append((g.cc[g.bridges[i][0]],True))
e.bridges=g.bridges
e.bridge=g.bridge
e.cc=g.cc
e.CC=e.CC
e.CC
e.treeDiameter()
print(len(e.newEdges))
for i in range(len(e.newEdges)):
u = e.newEdges[i][0] +1
v = e.newEdges[i][1] +1
print(u, end=" ")
print(v)
class graph:
n = int() #number of nodes
edges= int() #number of edges
time = int()
conexions = [] #adjacency list
bridges=[] #are we using a removable edge or not for city i?
bridge=[]
discovery = [] #time to discover node i
seen = [] #visited or not
cc = [] #index of connected components
low = [] #low[i]=min(discovery(i),discovery(w)) w:any node discoverable from i
pi = [] #index of i's father
CC =[]
newEdges = []
def __init__(self, n, m):
self.n = n
self.edges = m
for i in range(self.n):
self.conexions.append([])
self.discovery.append(sys.float_info.max)
self.low.append(sys.float_info.max)
self.seen.append(False)
self.cc.append(-1)
self.CC.append([])
self.bridge.append([])
def buildGraph(self):
#this method put every edge in the adjacency list
for i in range(self.edges):
p2=input().split()
self.conexions[int(p2[0])-1].append((int(p2[1])-1,False))
self.conexions[int(p2[1])-1].append((int(p2[0])-1,False))
def searchBridges (self):
self.time = 0
for i in range(self.n):
self.pi.append(-1)
for j in range(self.n):
self.bridge[i].append(False)
self.searchBridges_(0,-1) #we can start in any node 'cause the graph is connected
def searchBridges_(self,c,index):
self.seen[c]=True #mark as visited
self.time+=1
self.discovery[c]=self.low[c]= self.time
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if not self.seen[pos]:
self.pi[pos]=c #the node that discovered it
self.searchBridges_(pos,i) #search throw its not visited conexions
self.low[c]= min([self.low[c],self.low[pos]]) #definition of low
elif self.pi[c]!=pos: #It is not the node that discovered it
self.low[c]= min([self.low[c],self.discovery[pos]])
if self.pi[c]!=-1 and self.low[c]==self.discovery[c]: #it isn't the root and none of its connections is reacheable earlier than it
self.bridges.append((c,self.pi[c]))
self.bridge[self.pi[c]][c]=self.bridge[c][self.pi[c]]= True
for i in range(len(self.conexions[c])):
if(self.conexions[c][i][0]==self.pi[c]):
self.conexions[c][i]=(self.pi[c],True)
self.conexions[self.pi[c]][index]=(c,True)
def findCC(self):
j = 0
for i in range(self.n):
self.pi[i]=-1
self.seen[i]=False
for i in range(self.n):
if self.seen[i]==False:
self.cc[i]=j #assign the index of the new connected component
self.CC[j].append(i)
self.findCC_(i,j) #search throw its not visited conexions
j+=1 #we finished the search in the connected component
def findCC_(self, c, j):
self.seen[c]=True #mark as visited
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if(self.seen[pos]==False and self.conexions[c][i][1]==False):
self.cc[pos]=j #assign the index of the connected component
self.CC[j].append(pos)
self.pi[pos]=c #the node that discovered it
self.findCC_(pos,j) #search throw its not visited conexions
def treeDiameter(self):
while self.n>1:
for i in range(self.n):
self.seen[i]=False
self.pi[i]=-1
self.discovery[0]=0
self.treeDiameter_(0) #search the distance from this node, to the others
max = -1
maxIndex = 0
for i in range(self.n):
if self.discovery[i]>max:
max= self.discovery[i] #look for the furthest node
maxIndex=i
for i in range(self.n):
self.seen[i]=False
self.pi[i]=-1
self.discovery[maxIndex]=0
self.treeDiameter_(maxIndex) #search the distance from the furthest node, to the others
max =-1
maxIndex2=-1
for i in range(self.n):
if self.discovery[i]>max :
max= self.discovery[i] #look for the furthest node to preview furthest node, and that is the diameter in a tree
maxIndex2=i
self.ReBuildTree(maxIndex,maxIndex2)
def treeDiameter_(self, c):
self.seen[c]=True #mark as visited
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if self.seen[pos]==False:
self.pi[pos]=c #the node that discovered it
self.discovery[pos]= self.discovery[c]+1 #distance to the node who discover it + 1
#we don't have to compare and search for other paths, since it's a tree and there is only one path between two nodes.
self.treeDiameter_(pos) #search throw its not visited conexions
def buildBridgeTree(self):
self.buildGraph()
self.searchBridges()
self.findCC()
def ReBuildTree(self, u, v):
find=0
for i in range(len(self.CC[u])):
for j in range(len(self.CC[v])):
if not self.bridge[self.CC[u][i]][self.CC[v][j]]:
self.newEdges.append((self.CC[u][i],self.CC[v][j]))
find=1
break
if find==1:
break
newIndex=[]
temp = v
self.conexions[u]=None
self.conexions[v]=None
while self.pi[temp]!=u:
self.conexions[self.pi[temp]]=None
temp = self.pi[temp]
r =1
for i in range(self.n):
if(self.conexions[i]!=None):
newIndex.append(r)
r+=1
else:
newIndex.append(0)
self.n=r
if self.n==1:
return
newCC=[]
self.conexions=[]
for i in range(self.n):
self.conexions.append([])
newCC.append([])
for i in range(len(self.bridges)):
len0 = len(self.CC[self.cc[self.bridges[i][0]]])
len1 = len(self.CC[self.cc[self.bridges[i][1]]])
if(len1!=0):
for j in range(len0):
newCC[newIndex[self.cc[self.bridges[i][0]]]].append(self.CC[self.cc[self.bridges[i][0]]][j])
self.CC[self.cc[self.bridges[i][0]]]=[]
if(len1!=0):
for j in range(len1):
newCC[newIndex[self.cc[self.bridges[i][1]]]].append(self.CC[self.cc[self.bridges[i][1]]][j])
self.CC[self.cc[self.bridges[i][1]]]=[]
self.conexions[newIndex[self.cc[self.bridges[i][0]]]].append((newIndex[self.cc[self.bridges[i][1]]],True))
self.conexions[newIndex[self.cc[self.bridges[i][1]]]].append((newIndex[self.cc[self.bridges[i][0]]],True))
self.CC= newCC
for i in range(len(self.cc)):
self.cc[i]=newIndex[self.cc[i]]
if __name__ == '__main__':
main()
``` | instruction | 0 | 60,149 | 1 | 120,298 |
No | output | 1 | 60,149 | 1 | 120,299 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Berland capital (as you very well know) contains n junctions, some pairs of which are connected by two-way roads. Unfortunately, the number of traffic jams in the capital has increased dramatically, that's why it was decided to build several new roads. Every road should connect two junctions.
The city administration noticed that in the cities of all the developed countries between any two roads one can drive along at least two paths so that the paths don't share any roads (but they may share the same junction). The administration decided to add the minimal number of roads so that this rules was fulfilled in the Berland capital as well. In the city road network should exist no more than one road between every pair of junctions before or after the reform.
Input
The first input line contains a pair of integers n, m (2 β€ n β€ 900, 1 β€ m β€ 100000), where n is the number of junctions and m is the number of roads. Each of the following m lines contains a description of a road that is given by the numbers of the connected junctions ai, bi (1 β€ ai, bi β€ n, ai β bi). The junctions are numbered from 1 to n. It is possible to reach any junction of the city from any other one moving along roads.
Output
On the first line print t β the number of added roads. Then on t lines print the descriptions of the added roads in the format of the input data. You can use any order of printing the roads themselves as well as the junctions linked by every road. If there are several solutions to that problem, print any of them.
If the capital doesn't need the reform, print the single number 0.
If there's no solution, print the single number -1.
Examples
Input
4 3
1 2
2 3
3 4
Output
1
1 4
Input
4 4
1 2
2 3
2 4
3 4
Output
1
1 3
Submitted Solution:
```
import sys
import threading
def main():
try:
p = input().split()
n = int(p[0]) #number of locations
m = int(p[1]) #number of passages
if n==1:
print(0)
return
if n==2:
print(-1)
return
g = graph(n,m)
sys.setrecursionlimit(100000)
threading.stack_size(0x2000000)
t = threading.Thread(target=g.buildBridgeTree())
t.start()
t.join()
e = graph(len(g.bridges)+1,len(g.bridges))
e.conexions= []
e.pi=[]
e.discovery=[]
e.seen=[]
for i in range(len(g.bridges)+1):
e.conexions.append([])
e.pi.append(-1)
e.discovery.append(sys.float_info.max)
e.seen.append(False)
for i in range(len(g.bridges)):
e.conexions[g.cc[g.bridges[i][0]]].append((g.cc[g.bridges[i][1]],True))
e.conexions[g.cc[g.bridges[i][1]]].append((g.cc[g.bridges[i][0]],True))
e.bridges=g.bridges
e.bridge=g.bridge
e.cc=g.cc
e.CC=e.CC
e.CC
e.treeDiameter()
print(len(e.newEdges))
for i in range(len(e.newEdges)):
u = e.newEdges[i][0] +1
v = e.newEdges[i][1] +1
print(u, end=" ")
print(v)
except Exception as ex:
print(ex)
class graph:
n = int() #number of nodes
edges= int() #number of edges
time = int()
conexions = [] #adjacency list
bridges=[] #are we using a removable edge or not for city i?
bridge=[]
discovery = [] #time to discover node i
seen = [] #visited or not
cc = [] #index of connected components
low = [] #low[i]=min(discovery(i),discovery(w)) w:any node discoverable from i
pi = [] #index of i's father
CC =[]
newEdges = []
def __init__(self, n, m):
self.n = n
self.edges = m
for i in range(self.n):
self.conexions.append([])
self.discovery.append(sys.float_info.max)
self.low.append(sys.float_info.max)
self.seen.append(False)
self.cc.append(-1)
self.CC.append([])
self.bridge.append([])
def buildGraph(self):
#this method put every edge in the adjacency list
for i in range(self.edges):
p2=input().split()
self.conexions[int(p2[0])-1].append((int(p2[1])-1,False))
self.conexions[int(p2[1])-1].append((int(p2[0])-1,False))
def searchBridges (self):
self.time = 0
for i in range(self.n):
self.pi.append(-1)
for j in range(self.n):
self.bridge[i].append(False)
self.searchBridges_(0,-1) #we can start in any node 'cause the graph is connected
def searchBridges_(self,c,index):
self.seen[c]=True #mark as visited
self.time+=1
self.discovery[c]=self.low[c]= self.time
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if not self.seen[pos]:
self.pi[pos]=c #the node that discovered it
self.searchBridges_(pos,i) #search throw its not visited conexions
self.low[c]= min([self.low[c],self.low[pos]]) #definition of low
elif self.pi[c]!=pos: #It is not the node that discovered it
self.low[c]= min([self.low[c],self.discovery[pos]])
if self.pi[c]!=-1 and self.low[c]==self.discovery[c]: #it isn't the root and none of its connections is reacheable earlier than it
self.bridges.append((c,self.pi[c]))
self.bridge[self.pi[c]][c]=self.bridge[c][self.pi[c]]= True
for i in range(len(self.conexions[c])):
if(self.conexions[c][i][0]==self.pi[c]):
self.conexions[c][i]=(self.pi[c],True)
self.conexions[self.pi[c]][index]=(c,True)
def findCC(self):
j = 0
for i in range(self.n):
self.pi[i]=-1
self.seen[i]=False
for i in range(self.n):
if self.seen[i]==False:
self.cc[i]=j #assign the index of the new connected component
self.CC[j].append(i)
self.findCC_(i,j) #search throw its not visited conexions
j+=1 #we finished the search in the connected component
def findCC_(self, c, j):
self.seen[c]=True #mark as visited
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if(self.seen[pos]==False and self.conexions[c][i][1]==False):
self.cc[pos]=j #assign the index of the connected component
self.CC[j].append(pos)
self.pi[pos]=c #the node that discovered it
self.findCC_(pos,j) #search throw its not visited conexions
def treeDiameter(self):
while self.n>1:
for i in range(self.n):
self.seen[i]=False
self.pi[i]=-1
self.discovery[0]=0
self.treeDiameter_(0) #search the distance from this node, to the others
max = -1
maxIndex = 0
for i in range(self.n):
if self.discovery[i]>max:
max= self.discovery[i] #look for the furthest node
maxIndex=i
for i in range(self.n):
self.seen[i]=False
self.pi[i]=-1
self.discovery[maxIndex]=0
self.treeDiameter_(maxIndex) #search the distance from the furthest node, to the others
max =-1
maxIndex2=-1
for i in range(self.n):
if self.discovery[i]>max :
max= self.discovery[i] #look for the furthest node to preview furthest node, and that is the diameter in a tree
maxIndex2=i
self.ReBuildTree(maxIndex,maxIndex2)
def treeDiameter_(self, c):
self.seen[c]=True #mark as visited
for i in range(len(self.conexions[c])):
pos = self.conexions[c][i][0]
if self.seen[pos]==False:
self.pi[pos]=c #the node that discovered it
self.discovery[pos]= self.discovery[c]+1 #distance to the node who discover it + 1
#we don't have to compare and search for other paths, since it's a tree and there is only one path between two nodes.
self.treeDiameter_(pos) #search throw its not visited conexions
def buildBridgeTree(self):
self.buildGraph()
self.searchBridges()
self.findCC()
def ReBuildTree(self, u, v):
find=0
for i in range(len(self.CC[u])):
for j in range(len(self.CC[v])):
if not self.bridge[self.CC[u][i]][self.CC[v][j]]:
self.newEdges.append((self.CC[u][i],self.CC[v][j]))
find=1
break
if find==1:
break
newIndex=[]
temp = v
self.conexions[u]=None
self.conexions[v]=None
while self.pi[temp]!=u:
self.conexions[self.pi[temp]]=None
temp = self.pi[temp]
r =1
for i in range(self.n):
if(self.conexions[i]!=None):
newIndex.append(r)
r+=1
else:
newIndex.append(0)
self.n=r
if self.n==1:
return
newCC=[]
self.conexions=[]
for i in range(self.n):
self.conexions.append([])
newCC.append([])
for i in range(len(self.bridges)):
len0 = len(self.CC[self.cc[self.bridges[i][0]]])
len1 = len(self.CC[self.cc[self.bridges[i][1]]])
if(len0!=0):
for j in range(len0):
newCC[newIndex[self.cc[self.bridges[i][0]]]].append(self.CC[self.cc[self.bridges[i][0]]][j])
self.CC[self.cc[self.bridges[i][0]]]=[]
if(len1!=0):
for j in range(len1):
newCC[newIndex[self.cc[self.bridges[i][1]]]].append(self.CC[self.cc[self.bridges[i][1]]][j])
self.CC[self.cc[self.bridges[i][1]]]=[]
self.conexions[newIndex[self.cc[self.bridges[i][0]]]].append((newIndex[self.cc[self.bridges[i][1]]],True))
self.conexions[newIndex[self.cc[self.bridges[i][1]]]].append((newIndex[self.cc[self.bridges[i][0]]],True))
self.CC= newCC
for i in range(len(self.cc)):
self.cc[i]=newIndex[self.cc[i]]
if __name__ == '__main__':
main()
``` | instruction | 0 | 60,150 | 1 | 120,300 |
No | output | 1 | 60,150 | 1 | 120,301 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya commutes by train every day. There are n train stations in the city, and at the i-th station it's possible to buy only tickets to stations from i + 1 to ai inclusive. No tickets are sold at the last station.
Let Οi, j be the minimum number of tickets one needs to buy in order to get from stations i to station j. As Vasya is fond of different useless statistic he asks you to compute the sum of all values Οi, j among all pairs 1 β€ i < j β€ n.
Input
The first line of the input contains a single integer n (2 β€ n β€ 100 000) β the number of stations.
The second line contains n - 1 integer ai (i + 1 β€ ai β€ n), the i-th of them means that at the i-th station one may buy tickets to each station from i + 1 to ai inclusive.
Output
Print the sum of Οi, j among all pairs of 1 β€ i < j β€ n.
Examples
Input
4
4 4 4
Output
6
Input
5
2 3 5 5
Output
17
Note
In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6.
Consider the second sample:
* Ο1, 2 = 1
* Ο1, 3 = 2
* Ο1, 4 = 3
* Ο1, 5 = 3
* Ο2, 3 = 1
* Ο2, 4 = 2
* Ο2, 5 = 2
* Ο3, 4 = 1
* Ο3, 5 = 1
* Ο4, 5 = 1
Thus the answer equals 1 + 2 + 3 + 3 + 1 + 2 + 2 + 1 + 1 + 1 = 17. | instruction | 0 | 60,255 | 1 | 120,510 |
Tags: data structures, dp, greedy
Correct Solution:
```
import os, sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
mod = 998244353
from math import log2
from bisect import bisect_right
from math import log2
def sparse_table(ar):
n=len(ar)
t=int(log2(n))+1
sp=[[[0,0] for _ in range(t)] for _ in range(n)]
for i in range(n):
sp[i][0][0]=ar[i]
sp[i][0][1]=i
for j in range(1,t):
for i in range(n-2**j+1):
v1,i1=sp[i][j-1]
v2,i2=sp[i+2**(j-1)][j-1]
if v2>v1:
sp[i][j][0]=v2
sp[i][j][1]=i2
else:
sp[i][j][0] = v1
sp[i][j][1] = i1
return sp
def solve(i,j):
t=int(log2(j-i+1))
j=j-2**t+1
v1, i1 = sp[i][t]
v2, i2 = sp[j][t]
if v2 > v1:
return i2
return i1
n=int(input())
a=list(map(int,input().split()))
sp=sparse_table(a)
#print(sp)
dp=[0]*n
dp[n-2]=1
for i in range(n-3,-1,-1):
if a[i]==n:
t=solve(i+1,a[i]-2)
else:
t = solve(i + 1, a[i] - 1)
#print(t,'t')
dp[i]=dp[t]+(a[i]-1-i)-(a[i]-1-t)+(n-1-(a[i]-1))
#print(dp)
print(sum(dp))
``` | output | 1 | 60,255 | 1 | 120,511 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya commutes by train every day. There are n train stations in the city, and at the i-th station it's possible to buy only tickets to stations from i + 1 to ai inclusive. No tickets are sold at the last station.
Let Οi, j be the minimum number of tickets one needs to buy in order to get from stations i to station j. As Vasya is fond of different useless statistic he asks you to compute the sum of all values Οi, j among all pairs 1 β€ i < j β€ n.
Input
The first line of the input contains a single integer n (2 β€ n β€ 100 000) β the number of stations.
The second line contains n - 1 integer ai (i + 1 β€ ai β€ n), the i-th of them means that at the i-th station one may buy tickets to each station from i + 1 to ai inclusive.
Output
Print the sum of Οi, j among all pairs of 1 β€ i < j β€ n.
Examples
Input
4
4 4 4
Output
6
Input
5
2 3 5 5
Output
17
Note
In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6.
Consider the second sample:
* Ο1, 2 = 1
* Ο1, 3 = 2
* Ο1, 4 = 3
* Ο1, 5 = 3
* Ο2, 3 = 1
* Ο2, 4 = 2
* Ο2, 5 = 2
* Ο3, 4 = 1
* Ο3, 5 = 1
* Ο4, 5 = 1
Thus the answer equals 1 + 2 + 3 + 3 + 1 + 2 + 2 + 1 + 1 + 1 = 17. | instruction | 0 | 60,256 | 1 | 120,512 |
Tags: data structures, dp, greedy
Correct Solution:
```
import sys
input = sys.stdin.readline
def solve():
n = int(input())
a = list(map(int, input().split()))
T = [(-1,-1)]*(2*n)
for i in range(n-1):
T[i+n] = (a[i], i)
T[n+n-1] = (n, n-1)
for i in range(n-1,-1,-1):
T[i] = max(T[2*i], T[2*i+1])
dp = [0]*n
res = 0
for i in range(n-2,-1,-1):
l = i + n
r = a[i] - 1 + n
v = (-1,-1)
while l <= r:
if l % 2 == 1:
v = max(v, T[l])
if r % 2 == 0:
v = max(v, T[r])
l = (l+1)//2
r = (r-1)//2
#print(i,v[1])
dp[i] = dp[v[1]] + (n - v[1]) + (v[1] - i) - (a[i] - 1 - v[1]+1)
res += dp[i]
#print(dp)
print(res)
solve()
``` | output | 1 | 60,256 | 1 | 120,513 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya commutes by train every day. There are n train stations in the city, and at the i-th station it's possible to buy only tickets to stations from i + 1 to ai inclusive. No tickets are sold at the last station.
Let Οi, j be the minimum number of tickets one needs to buy in order to get from stations i to station j. As Vasya is fond of different useless statistic he asks you to compute the sum of all values Οi, j among all pairs 1 β€ i < j β€ n.
Input
The first line of the input contains a single integer n (2 β€ n β€ 100 000) β the number of stations.
The second line contains n - 1 integer ai (i + 1 β€ ai β€ n), the i-th of them means that at the i-th station one may buy tickets to each station from i + 1 to ai inclusive.
Output
Print the sum of Οi, j among all pairs of 1 β€ i < j β€ n.
Examples
Input
4
4 4 4
Output
6
Input
5
2 3 5 5
Output
17
Note
In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6.
Consider the second sample:
* Ο1, 2 = 1
* Ο1, 3 = 2
* Ο1, 4 = 3
* Ο1, 5 = 3
* Ο2, 3 = 1
* Ο2, 4 = 2
* Ο2, 5 = 2
* Ο3, 4 = 1
* Ο3, 5 = 1
* Ο4, 5 = 1
Thus the answer equals 1 + 2 + 3 + 3 + 1 + 2 + 2 + 1 + 1 + 1 = 17. | instruction | 0 | 60,257 | 1 | 120,514 |
Tags: data structures, dp, greedy
Correct Solution:
```
n=int(input())
a=list(map(int, input().split()))
a=[ai-1 for ai in a]
a[n:n] = [n - 1]
dp=[0]*n
ans=0
i=n-2
nmax=2**17
tree=[[0,0]]*2*nmax;
#Build Segment tree
j=0
while j<n:
tree[nmax + j] = [a[j], j]
j=j+1
j=nmax-1
while j>0:
tree[j]=max(tree[j*2],tree[j*2+1])
j=j-1
#get max of a interval [left, right]
def get(left, right):
ans=[-1,-1]
left=left+nmax
right=right+nmax+1
while left<right:
if (left & 1):
ans = max(ans, tree[left])
left = left + 1
if (right & 1):
right = right - 1
ans = max(ans, tree[right])
left = left // 2
right = right // 2
return ans[1]
while i>=0:
m = get(i + 1, a[i]);
dp[i] = dp[m] - (a[i] - m) + n - i - 1
ans += dp[i]
i=i-1
print(ans)
``` | output | 1 | 60,257 | 1 | 120,515 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya commutes by train every day. There are n train stations in the city, and at the i-th station it's possible to buy only tickets to stations from i + 1 to ai inclusive. No tickets are sold at the last station.
Let Οi, j be the minimum number of tickets one needs to buy in order to get from stations i to station j. As Vasya is fond of different useless statistic he asks you to compute the sum of all values Οi, j among all pairs 1 β€ i < j β€ n.
Input
The first line of the input contains a single integer n (2 β€ n β€ 100 000) β the number of stations.
The second line contains n - 1 integer ai (i + 1 β€ ai β€ n), the i-th of them means that at the i-th station one may buy tickets to each station from i + 1 to ai inclusive.
Output
Print the sum of Οi, j among all pairs of 1 β€ i < j β€ n.
Examples
Input
4
4 4 4
Output
6
Input
5
2 3 5 5
Output
17
Note
In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6.
Consider the second sample:
* Ο1, 2 = 1
* Ο1, 3 = 2
* Ο1, 4 = 3
* Ο1, 5 = 3
* Ο2, 3 = 1
* Ο2, 4 = 2
* Ο2, 5 = 2
* Ο3, 4 = 1
* Ο3, 5 = 1
* Ο4, 5 = 1
Thus the answer equals 1 + 2 + 3 + 3 + 1 + 2 + 2 + 1 + 1 + 1 = 17. | instruction | 0 | 60,258 | 1 | 120,516 |
Tags: data structures, dp, greedy
Correct Solution:
```
MAX_N = 100000
def maxi(a, b):
if a[0] > b[0]:
return a
else:
return b
class Segment_Tree:
def init(self, left, right, data, leftbound, rightbound):
self.data = data
self.left = left
self.right = right
self.leftbound = leftbound
self.rightbound = rightbound
return self
def build(self, a, leftbound, rightbound):
#print(leftbound, rightbound, a)
if len(a) == 0:
return self.init(-1, -1, [0, -1], MAX_N + 1, -1)
elif len(a) == 1:
return self.init(-1, -1, a[0], leftbound, rightbound)
else:
middle = (leftbound + rightbound) // 2
self.left = Segment_Tree()
self.right = Segment_Tree()
return self.init(self.left.build(a[:middle - leftbound], leftbound, middle), self.right.build(a[middle - leftbound:], middle, rightbound), maxi(self.left.data, self.right.data), leftbound, rightbound)
def get(self, l, r):
if l <= self.leftbound and r >= self.rightbound:
return self.data
elif l < self.left.rightbound and r > self.right.leftbound:
return maxi(self.left.get(l, r), self.right.get(l, r))
elif l >= self.right.leftbound:
return self.right.get(l, r)
else:
return self.left.get(l, r)
n = int(input())
a = list(map(int, input().split())) + [n]
a = [[a[i] - 1, i] for i in range(n)]
Tree = Segment_Tree()
Tree.build(a, 0, n)
dp = [0] * n
ans = 0
for i in range(n - 2, -1, -1):
m = Tree.get(i + 1, a[i][0] + 1)[1]
dp[i] = dp[m] - (a[i][0] - m) + n - i - 1;
ans += dp[i]
print(ans)
``` | output | 1 | 60,258 | 1 | 120,517 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.