message stringlengths 2 22.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 16 109k | cluster float64 1 1 | __index_level_0__ int64 32 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mr. A wants to get to the destination on the Yamanote line.
After getting on the train, Mr. A gets up for a minute and sleeps for b minutes repeatedly. It is c minutes after boarding to the destination, and you can get off if you are awake at this time, but on the contrary, if you are sleeping, you will miss it. Also, Mr. A will continue to take the same train even if he overtakes it, and the train will take 60 minutes to go around the Yamanote line. Therefore, Mr. A will arrive at the destination after 60t + c (t is a non-negative integer) minutes.
How many minutes will A be able to reach his destination? Output -1 when it is unreachable. However, if the time you arrive at your destination is the boundary between the time you are sleeping and the time you are awake, you can get off.
Constraints
* 0 <a, b, c <60
Input
The input is given in one line in the following format.
a b c
The input consists of three integers a, b, c.
a is the time you are awake, b is the time you are sleeping, and c is the time it takes to get to your destination after boarding. The unit of a, b, and c is minutes.
Output
If you can reach your destination, output the time it takes to reach your destination. Otherwise, output -1.
Examples
Input
10 10 5
Output
5
Input
50 40 51
Output
111
Input
20 20 20
Output
20
Input
30 30 40
Output
-1
Submitted Solution:
```
import math
a, b, c = map(int, input().split())
for k in range(60):
t = math.ceil((k * (a + b) - c) / 60)
if t <= (k * (a + b) + a - c) / 60:
print(t * 60 + c)
break
else:
print(-1)
``` | instruction | 0 | 96,052 | 1 | 192,104 |
Yes | output | 1 | 96,052 | 1 | 192,105 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mr. A wants to get to the destination on the Yamanote line.
After getting on the train, Mr. A gets up for a minute and sleeps for b minutes repeatedly. It is c minutes after boarding to the destination, and you can get off if you are awake at this time, but on the contrary, if you are sleeping, you will miss it. Also, Mr. A will continue to take the same train even if he overtakes it, and the train will take 60 minutes to go around the Yamanote line. Therefore, Mr. A will arrive at the destination after 60t + c (t is a non-negative integer) minutes.
How many minutes will A be able to reach his destination? Output -1 when it is unreachable. However, if the time you arrive at your destination is the boundary between the time you are sleeping and the time you are awake, you can get off.
Constraints
* 0 <a, b, c <60
Input
The input is given in one line in the following format.
a b c
The input consists of three integers a, b, c.
a is the time you are awake, b is the time you are sleeping, and c is the time it takes to get to your destination after boarding. The unit of a, b, and c is minutes.
Output
If you can reach your destination, output the time it takes to reach your destination. Otherwise, output -1.
Examples
Input
10 10 5
Output
5
Input
50 40 51
Output
111
Input
20 20 20
Output
20
Input
30 30 40
Output
-1
Submitted Solution:
```
from itertools import count
a,b,c=map(int,input().split())
f=False
for n in count(0):
p=(c+60*n)//(a+b)
if n and d==((a+b)*p-(c+60*n)) and e==((c+60*n)-((a+b)*p+a)):
n=-1
break
if (a+b)*p<=c+60*n<=(a+b)*p+a:
break
else:
d=(a+b)*p-(c+60*n)
e=(c+60*n)-((a+b)*p+a)
if n>=0:
print(c+60*n)
else:
print(n)
``` | instruction | 0 | 96,053 | 1 | 192,106 |
No | output | 1 | 96,053 | 1 | 192,107 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mr. A wants to get to the destination on the Yamanote line.
After getting on the train, Mr. A gets up for a minute and sleeps for b minutes repeatedly. It is c minutes after boarding to the destination, and you can get off if you are awake at this time, but on the contrary, if you are sleeping, you will miss it. Also, Mr. A will continue to take the same train even if he overtakes it, and the train will take 60 minutes to go around the Yamanote line. Therefore, Mr. A will arrive at the destination after 60t + c (t is a non-negative integer) minutes.
How many minutes will A be able to reach his destination? Output -1 when it is unreachable. However, if the time you arrive at your destination is the boundary between the time you are sleeping and the time you are awake, you can get off.
Constraints
* 0 <a, b, c <60
Input
The input is given in one line in the following format.
a b c
The input consists of three integers a, b, c.
a is the time you are awake, b is the time you are sleeping, and c is the time it takes to get to your destination after boarding. The unit of a, b, and c is minutes.
Output
If you can reach your destination, output the time it takes to reach your destination. Otherwise, output -1.
Examples
Input
10 10 5
Output
5
Input
50 40 51
Output
111
Input
20 20 20
Output
20
Input
30 30 40
Output
-1
Submitted Solution:
```
def solve(a, b, c):
l = 0; r = a;
for t in range(114514):
p = 60*t + c
if l <= p <= r:
print(p)
exit()
l = r + b
r = l + a
print(-1)
a, b, c = map(int, input().split())
solve(a,b,c)
``` | instruction | 0 | 96,054 | 1 | 192,108 |
No | output | 1 | 96,054 | 1 | 192,109 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mr. A wants to get to the destination on the Yamanote line.
After getting on the train, Mr. A gets up for a minute and sleeps for b minutes repeatedly. It is c minutes after boarding to the destination, and you can get off if you are awake at this time, but on the contrary, if you are sleeping, you will miss it. Also, Mr. A will continue to take the same train even if he overtakes it, and the train will take 60 minutes to go around the Yamanote line. Therefore, Mr. A will arrive at the destination after 60t + c (t is a non-negative integer) minutes.
How many minutes will A be able to reach his destination? Output -1 when it is unreachable. However, if the time you arrive at your destination is the boundary between the time you are sleeping and the time you are awake, you can get off.
Constraints
* 0 <a, b, c <60
Input
The input is given in one line in the following format.
a b c
The input consists of three integers a, b, c.
a is the time you are awake, b is the time you are sleeping, and c is the time it takes to get to your destination after boarding. The unit of a, b, and c is minutes.
Output
If you can reach your destination, output the time it takes to reach your destination. Otherwise, output -1.
Examples
Input
10 10 5
Output
5
Input
50 40 51
Output
111
Input
20 20 20
Output
20
Input
30 30 40
Output
-1
Submitted Solution:
```
from itertools import count
a,b,c=map(int,input().split())
for n in count(0):
if (a+b)*n<=c+60*n<=(a+b)*n+a:break
if n>0 and a+b<=60 and c>a:
n=-1
break
print(c+60*n)
``` | instruction | 0 | 96,055 | 1 | 192,110 |
No | output | 1 | 96,055 | 1 | 192,111 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Mr. A wants to get to the destination on the Yamanote line.
After getting on the train, Mr. A gets up for a minute and sleeps for b minutes repeatedly. It is c minutes after boarding to the destination, and you can get off if you are awake at this time, but on the contrary, if you are sleeping, you will miss it. Also, Mr. A will continue to take the same train even if he overtakes it, and the train will take 60 minutes to go around the Yamanote line. Therefore, Mr. A will arrive at the destination after 60t + c (t is a non-negative integer) minutes.
How many minutes will A be able to reach his destination? Output -1 when it is unreachable. However, if the time you arrive at your destination is the boundary between the time you are sleeping and the time you are awake, you can get off.
Constraints
* 0 <a, b, c <60
Input
The input is given in one line in the following format.
a b c
The input consists of three integers a, b, c.
a is the time you are awake, b is the time you are sleeping, and c is the time it takes to get to your destination after boarding. The unit of a, b, and c is minutes.
Output
If you can reach your destination, output the time it takes to reach your destination. Otherwise, output -1.
Examples
Input
10 10 5
Output
5
Input
50 40 51
Output
111
Input
20 20 20
Output
20
Input
30 30 40
Output
-1
Submitted Solution:
```
def solve(a, b, c):
l = 0; r = a;
for t in range(50):
t = l // 60
p = 60*t + c
if l <= p <= r:
print(p)
exit()
l = r + b
r = l + a
print(-1)
a, b, c = map(int, input().split())
solve(a,b,c)
``` | instruction | 0 | 96,056 | 1 | 192,112 |
No | output | 1 | 96,056 | 1 | 192,113 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created.
We assume that Bajtek can only heap up snow drifts at integer coordinates.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of snow drifts. Each of the following n lines contains two integers xi and yi (1 ≤ xi, yi ≤ 1000) — the coordinates of the i-th snow drift.
Note that the north direction coinсides with the direction of Oy axis, so the east direction coinсides with the direction of the Ox axis. All snow drift's locations are distinct.
Output
Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one.
Examples
Input
2
2 1
1 2
Output
1
Input
2
2 1
4 1
Output
0 | instruction | 0 | 96,400 | 1 | 192,800 |
Tags: brute force, dfs and similar, dsu, graphs
Correct Solution:
```
from collections import deque
n = int(input())
grafs = dict()
xx = set();yy = set()
skautnes = []
for e in range(n):
x, y = map(int, input().split())
xx.add(str(x) + 'x');yy.add(str(y) + 'y')
skautnes.append( (str(x) + 'x',str(y) + 'y') )
for i in xx.union(yy):
grafs[i] = set()
for s in skautnes:
grafs[s[0]].add(s[1])
grafs[s[1]].add(s[0])
def bfs(grafs, root):
apmekletie = set(root)
rinda = deque([root])
while rinda:
tgd = rinda.popleft()
for neighbour in grafs[tgd]:
if neighbour not in apmekletie:
apmekletie.add(neighbour)
rinda.append(neighbour)
return(apmekletie)
dalas = []
apmekletasvirsotnes = set()
for e in grafs:
if e not in apmekletasvirsotnes:
dala = bfs(grafs, e)
dalas.append(dala)
apmekletasvirsotnes.update(dala)
print(len(dalas) - 1)
``` | output | 1 | 96,400 | 1 | 192,801 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created.
We assume that Bajtek can only heap up snow drifts at integer coordinates.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of snow drifts. Each of the following n lines contains two integers xi and yi (1 ≤ xi, yi ≤ 1000) — the coordinates of the i-th snow drift.
Note that the north direction coinсides with the direction of Oy axis, so the east direction coinсides with the direction of the Ox axis. All snow drift's locations are distinct.
Output
Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one.
Examples
Input
2
2 1
1 2
Output
1
Input
2
2 1
4 1
Output
0 | instruction | 0 | 96,403 | 1 | 192,806 |
Tags: brute force, dfs and similar, dsu, graphs
Correct Solution:
```
import collections
def breadth_first_search(graph, root):
visited = set()
visited.add(root)
queue = collections.deque([root])
while queue:
vertex = queue.popleft()
for neighbour in graph[vertex]:
if neighbour not in visited:
visited.add(neighbour)
queue.append(neighbour)
return visited
# The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of snow drifts.
# Each of the following n lines contains two integers xi and yi (1 ≤ xi, yi ≤ 1000) — the coordinates of the i-th snow drift.
N = int(input())
drifts = []
xcoord = set()
ycoord = set()
for n in range(N):
xi, yi = map(int, input().split())
sxi = 'x' + str(xi)
xcoord.add(sxi)
syi = 'y' + str(yi)
ycoord.add(syi)
drifts.append( (sxi, syi) )
graph = dict()
for v in xcoord | ycoord:
graph[v] = set()
for e in drifts:
graph[e[0]].add(e[1])
graph[e[1]].add(e[0])
# print(graph)
parts = []
seen = set()
for v in graph:
if v not in seen:
area = breadth_first_search(graph, v)
parts.append(area)
seen.update(area)
print(len(parts) - 1)
``` | output | 1 | 96,403 | 1 | 192,807 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created.
We assume that Bajtek can only heap up snow drifts at integer coordinates.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of snow drifts. Each of the following n lines contains two integers xi and yi (1 ≤ xi, yi ≤ 1000) — the coordinates of the i-th snow drift.
Note that the north direction coinсides with the direction of Oy axis, so the east direction coinсides with the direction of the Ox axis. All snow drift's locations are distinct.
Output
Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one.
Examples
Input
2
2 1
1 2
Output
1
Input
2
2 1
4 1
Output
0 | instruction | 0 | 96,406 | 1 | 192,812 |
Tags: brute force, dfs and similar, dsu, graphs
Correct Solution:
```
def iceskate():
n = int(input())
drifts = []
for i in range(n):
drifts.append(list(map(int,input().split())))
# print(drifts)
disconnected = []
app_index = 0
for j in drifts:
disconnected.append([app_index])
for k in drifts[drifts.index(j)+1:]:
if j[0] == k[0] or j[1] == k[1]:
disconnected[app_index].append(drifts.index(k))
app_index +=1
# print(disconnected)
disconnected = connectSets(disconnected,n)
# print(disconnected)
print(len(disconnected)-1)
def connectSets(disconnected,n):
freq = [-1]*n
i=0
while i < n:
delFlag = 0
for k in range(len(disconnected[i])):
if freq[disconnected[i][k]] >= i or freq[disconnected[i][k]] == -1:
freq[disconnected[i][k]] = i
else:
# print(disconnected[freq[i[k]]])
loc = freq[disconnected[i][k]]
delFlag = 1
break
if delFlag == 1:
# print('freq=', end =' ')
# print(freq)
# print(len(freq))
# print('disconnected=', end = ' ')
# print(disconnected)
# print(freq)
# print(loc)
# for k in range(len(disconnected[i])):
# freq[disconnected[i][k]] = min(loc, freq[disconnected[i][k]])
disconnected[loc] += disconnected[i]
del disconnected[i]
i = loc
n-=1
else:
i +=1
# print(freq)
return disconnected
iceskate()
``` | output | 1 | 96,406 | 1 | 192,813 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on the preparations for the celebration.
<image>
Twilight Sparkle wanted to track the path of Nightmare Moon. Unfortunately, she didn't know the exact path. What she knew is the parity of the number of times that each place Nightmare Moon visited. Can you help Twilight Sparkle to restore any path that is consistent with this information?
Ponyville can be represented as an undirected graph (vertices are places, edges are roads between places) without self-loops and multi-edges. The path can start and end at any place (also it can be empty). Each place can be visited multiple times. The path must not visit more than 4n places.
Input
The first line contains two integers n and m (2 ≤ n ≤ 105; 0 ≤ m ≤ 105) — the number of places and the number of roads in Ponyville. Each of the following m lines contains two integers ui, vi (1 ≤ ui, vi ≤ n; ui ≠ vi), these integers describe a road between places ui and vi.
The next line contains n integers: x1, x2, ..., xn (0 ≤ xi ≤ 1) — the parity of the number of times that each place must be visited. If xi = 0, then the i-th place must be visited even number of times, else it must be visited odd number of times.
Output
Output the number of visited places k in the first line (0 ≤ k ≤ 4n). Then output k integers — the numbers of places in the order of path. If xi = 0, then the i-th place must appear in the path even number of times, else i-th place must appear in the path odd number of times. Note, that given road system has no self-loops, therefore any two neighbouring places in the path must be distinct.
If there is no required path, output -1. If there multiple possible paths, you can output any of them.
Examples
Input
3 2
1 2
2 3
1 1 1
Output
3
1 2 3
Input
5 7
1 2
1 3
1 4
1 5
3 4
3 5
4 5
0 1 0 1 0
Output
10
2 1 3 4 5 4 5 4 3 1
Input
2 0
0 0
Output
0 | instruction | 0 | 96,513 | 1 | 193,026 |
Tags: constructive algorithms, dfs and similar, graphs
Correct Solution:
```
"""
Codeforces Contest 259 Div 1 Problem C
Author : chaotic_iak
Language: Python 3.3.4
"""
def main():
n,m = read()
edges = [[] for _ in range(n+1)]
for i in range(m):
a,b = read()
edges[a].append(b)
edges[b].append(a)
parity = [0] + read()
mn = 0
while mn < n+1 and not parity[mn]: mn += 1
if mn == n+1:
print(0)
return
visited = [0] * (n+1)
dfs_stack = [mn]
dfs_path = []
while dfs_stack: dfs(dfs_stack, dfs_path, visited, edges, parity)
if parity[mn]:
dfs_path.pop()
parity[mn] = 0
if sum(parity):
print(-1)
else:
print(len(dfs_path))
print(" ".join(map(str, dfs_path)))
def dfs(dfs_stack, dfs_path, visited, edges, parity):
v = dfs_stack.pop()
if v > 0:
if visited[v]:
dfs_stack.pop()
return
visited[v] = 1
dfs_path.append(v)
parity[v] = 1 - parity[v]
for i in edges[v]:
dfs_stack.append(-v)
dfs_stack.append(i)
else:
v = -v
u = dfs_path[-1]
dfs_path.append(v)
if parity[u]:
dfs_path.append(u)
dfs_path.append(v)
parity[u] = 1 - parity[u]
else:
parity[v] = 1 - parity[v]
################################### NON-SOLUTION STUFF BELOW
def read(mode=2):
# 0: String
# 1: List of strings
# 2: List of integers
inputs = input().strip()
if mode == 0: return inputs
if mode == 1: return inputs.split()
if mode == 2: return list(map(int, inputs.split()))
def read_str(): return read(0)
def read_int(): return read(2)[0]
def write(s="\n"):
if isinstance(s, list): s = " ".join(map(str, s))
s = str(s)
print(s, end="")
main()
``` | output | 1 | 96,513 | 1 | 193,027 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on the preparations for the celebration.
<image>
Twilight Sparkle wanted to track the path of Nightmare Moon. Unfortunately, she didn't know the exact path. What she knew is the parity of the number of times that each place Nightmare Moon visited. Can you help Twilight Sparkle to restore any path that is consistent with this information?
Ponyville can be represented as an undirected graph (vertices are places, edges are roads between places) without self-loops and multi-edges. The path can start and end at any place (also it can be empty). Each place can be visited multiple times. The path must not visit more than 4n places.
Input
The first line contains two integers n and m (2 ≤ n ≤ 105; 0 ≤ m ≤ 105) — the number of places and the number of roads in Ponyville. Each of the following m lines contains two integers ui, vi (1 ≤ ui, vi ≤ n; ui ≠ vi), these integers describe a road between places ui and vi.
The next line contains n integers: x1, x2, ..., xn (0 ≤ xi ≤ 1) — the parity of the number of times that each place must be visited. If xi = 0, then the i-th place must be visited even number of times, else it must be visited odd number of times.
Output
Output the number of visited places k in the first line (0 ≤ k ≤ 4n). Then output k integers — the numbers of places in the order of path. If xi = 0, then the i-th place must appear in the path even number of times, else i-th place must appear in the path odd number of times. Note, that given road system has no self-loops, therefore any two neighbouring places in the path must be distinct.
If there is no required path, output -1. If there multiple possible paths, you can output any of them.
Examples
Input
3 2
1 2
2 3
1 1 1
Output
3
1 2 3
Input
5 7
1 2
1 3
1 4
1 5
3 4
3 5
4 5
0 1 0 1 0
Output
10
2 1 3 4 5 4 5 4 3 1
Input
2 0
0 0
Output
0 | instruction | 0 | 96,514 | 1 | 193,028 |
Tags: constructive algorithms, dfs and similar, graphs
Correct Solution:
```
"""
Codeforces Contest 259 Div 1 Problem C
Author : chaotic_iak
Language: Python 3.3.4
"""
def main():
# Read N and M from problem statement.
n,m = read()
# Set up an adjacency list for each vertex.
edges = [[] for _ in range(n+1)]
for i in range(m):
a,b = read()
edges[a].append(b)
edges[b].append(a)
# Read the parity array. Pad with 0 at the front to make it 1-based like in problem.
parity = [0] + read()
# Find odd vertex with minimum index.
mn = 0
while mn < n+1 and not parity[mn]: mn += 1
# If none, then no need to output anything.
if mn == n+1:
print(0)
return
# Create the visited array, stack for DFS, and list to store the resulting path.
visited = [0] * (n+1)
dfs_stack = [mn]
dfs_path = []
# Call one step of DFS as long as the stack is not empty. (See below.)
while dfs_stack: dfs(dfs_stack, dfs_path, visited, edges, parity)
# If the root is odd, remove the last element of the path.
if parity[mn]:
dfs_path.pop()
parity[mn] = 0
# If there's still an odd vertex, then we have two separate components with
# odd vertices; we failed.
if sum(parity):
print(-1)
# Otherwise, print the resulting path.
else:
print(len(dfs_path))
print(" ".join(map(str, dfs_path)))
def dfs(dfs_stack, dfs_path, visited, edges, parity):
# Take top element of the stack. This is positive if it's a downward edge (away
# from root) and negative if it's an upward edge (to root), and its absolute
# value is the index of the destination vertex.
v = dfs_stack.pop()
# If a downward edge:
if v > 0:
# If target vertex has been visited, no need to visit it again. Pop the
# top item of the stack (which is a return edge for this vertex, but unused).
if visited[v]:
dfs_stack.pop()
return
# Set current vertex as visited and append it to path. Toggle the parity.
visited[v] = 1
dfs_path.append(v)
parity[v] = 1 - parity[v]
# Add a pair of downward/upward edges for each neighbor of this vertex.
for i in edges[v]:
dfs_stack.append(-v)
dfs_stack.append(i)
# If an upward edge:
else:
# Set v as absolute value. Take the child just visited, u, which is stored as
# the last element of final path. Append the current vertex v to final path
# and toggle the parity of v.
v = -v
u = dfs_path[-1]
dfs_path.append(v)
parity[v] = 1 - parity[v]
# If u is odd, visit u and return back to v one more time. This toggles the
# parity of u but not v.
if parity[u]:
dfs_path.append(u)
dfs_path.append(v)
parity[u] = 1 - parity[u]
parity[v] = 1 - parity[v]
# If u is even, we don't need to do anything.
else:
pass
################################### NON-SOLUTION STUFF BELOW
def read(mode=2):
# 0: String
# 1: List of strings
# 2: List of integers
inputs = input().strip()
if mode == 0: return inputs
if mode == 1: return inputs.split()
if mode == 2: return list(map(int, inputs.split()))
def read_str(): return read(0)
def read_int(): return read(2)[0]
def write(s="\n"):
if isinstance(s, list): s = " ".join(map(str, s))
s = str(s)
print(s, end="")
main()
``` | output | 1 | 96,514 | 1 | 193,029 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on the preparations for the celebration.
<image>
Twilight Sparkle wanted to track the path of Nightmare Moon. Unfortunately, she didn't know the exact path. What she knew is the parity of the number of times that each place Nightmare Moon visited. Can you help Twilight Sparkle to restore any path that is consistent with this information?
Ponyville can be represented as an undirected graph (vertices are places, edges are roads between places) without self-loops and multi-edges. The path can start and end at any place (also it can be empty). Each place can be visited multiple times. The path must not visit more than 4n places.
Input
The first line contains two integers n and m (2 ≤ n ≤ 105; 0 ≤ m ≤ 105) — the number of places and the number of roads in Ponyville. Each of the following m lines contains two integers ui, vi (1 ≤ ui, vi ≤ n; ui ≠ vi), these integers describe a road between places ui and vi.
The next line contains n integers: x1, x2, ..., xn (0 ≤ xi ≤ 1) — the parity of the number of times that each place must be visited. If xi = 0, then the i-th place must be visited even number of times, else it must be visited odd number of times.
Output
Output the number of visited places k in the first line (0 ≤ k ≤ 4n). Then output k integers — the numbers of places in the order of path. If xi = 0, then the i-th place must appear in the path even number of times, else i-th place must appear in the path odd number of times. Note, that given road system has no self-loops, therefore any two neighbouring places in the path must be distinct.
If there is no required path, output -1. If there multiple possible paths, you can output any of them.
Examples
Input
3 2
1 2
2 3
1 1 1
Output
3
1 2 3
Input
5 7
1 2
1 3
1 4
1 5
3 4
3 5
4 5
0 1 0 1 0
Output
10
2 1 3 4 5 4 5 4 3 1
Input
2 0
0 0
Output
0 | instruction | 0 | 96,515 | 1 | 193,030 |
Tags: constructive algorithms, dfs and similar, graphs
Correct Solution:
```
"""
Codeforces Contest 259 Div 1 Problem C
Author : chaotic_iak
Language: Python 3.3.4
"""
def main():
# Read N and M from problem statement.
n,m = read()
# Set up an adjacency list for each vertex.
edges = [[] for _ in range(n+1)]
for i in range(m):
a,b = read()
edges[a].append(b)
edges[b].append(a)
# Read the parity array. Pad with 0 at the front to make it 1-based like in problem.
parity = [0] + read()
# Find odd vertex with minimum index.
mn = 0
while mn < n+1 and not parity[mn]: mn += 1
# If none, then no need to output anything.
if mn == n+1:
print(0)
return
# Create the visited array, stack for DFS, and list to store the resulting path.
visited = [0] * (n+1)
dfs_stack = [mn]
dfs_path = []
# Call one step of DFS as long as the stack is not empty. (See below.)
while dfs_stack: dfs(dfs_stack, dfs_path, visited, edges, parity)
# If the root is odd, remove the last element of the path.
if parity[mn]:
dfs_path.pop()
parity[mn] = 0
# If there's still an odd vertex, then we have two separate components with
# odd vertices; we failed.
if sum(parity):
print(-1)
# Otherwise, print the resulting path.
else:
print(len(dfs_path))
print(" ".join(map(str, dfs_path)))
def dfs(dfs_stack, dfs_path, visited, edges, parity):
# Take top element of the stack. This is positive if it's a downward edge (away
# from root) and negative if it's an upward edge (to root), and its absolute
# value is the index of the destination vertex.
v = dfs_stack.pop()
# If a downward edge:
if v > 0:
# If target vertex has been visited, no need to visit it again. Pop the
# top item of the stack (which is a return edge for this vertex, but unused).
if visited[v]:
dfs_stack.pop()
return
# Set current vertex as visited and append it to path. Toggle the parity.
visited[v] = 1
dfs_path.append(v)
parity[v] = 1 - parity[v]
# Add a pair of downward/upward edges for each neighbor of this vertex.
for i in edges[v]:
dfs_stack.append(-v)
dfs_stack.append(i)
# If an upward edge:
else:
# Set v as absolute value. Take the child just visited, u, which is stored as
# the last element of final path. Append the current vertex v to final path
# and toggle the parity of v.
v = -v
u = dfs_path[-1]
dfs_path.append(v)
parity[v] = 1 - parity[v]
# If u is odd, visit u and return back to v one more time. This toggles the
# parity of u but not v.
if parity[u]:
dfs_path.append(u)
dfs_path.append(v)
parity[u] = 1 - parity[u]
parity[v] = 1 - parity[v]
# If u is even, we don't need to do anything.
else:
pass
################################### NON-SOLUTION STUFF BELOW
def read(mode=2):
# 0: String
# 1: List of strings
# 2: List of integers
inputs = input().strip()
if mode == 0: return inputs
if mode == 1: return inputs.split()
if mode == 2: return list(map(int, inputs.split()))
def read_str(): return read(0)
def read_int(): return read(2)[0]
def write(s="\n"):
if isinstance(s, list): s = " ".join(map(str, s))
s = str(s)
print(s, end="")
main()
# Made By Mostafa_Khaled
``` | output | 1 | 96,515 | 1 | 193,031 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on the preparations for the celebration.
<image>
Twilight Sparkle wanted to track the path of Nightmare Moon. Unfortunately, she didn't know the exact path. What she knew is the parity of the number of times that each place Nightmare Moon visited. Can you help Twilight Sparkle to restore any path that is consistent with this information?
Ponyville can be represented as an undirected graph (vertices are places, edges are roads between places) without self-loops and multi-edges. The path can start and end at any place (also it can be empty). Each place can be visited multiple times. The path must not visit more than 4n places.
Input
The first line contains two integers n and m (2 ≤ n ≤ 105; 0 ≤ m ≤ 105) — the number of places and the number of roads in Ponyville. Each of the following m lines contains two integers ui, vi (1 ≤ ui, vi ≤ n; ui ≠ vi), these integers describe a road between places ui and vi.
The next line contains n integers: x1, x2, ..., xn (0 ≤ xi ≤ 1) — the parity of the number of times that each place must be visited. If xi = 0, then the i-th place must be visited even number of times, else it must be visited odd number of times.
Output
Output the number of visited places k in the first line (0 ≤ k ≤ 4n). Then output k integers — the numbers of places in the order of path. If xi = 0, then the i-th place must appear in the path even number of times, else i-th place must appear in the path odd number of times. Note, that given road system has no self-loops, therefore any two neighbouring places in the path must be distinct.
If there is no required path, output -1. If there multiple possible paths, you can output any of them.
Examples
Input
3 2
1 2
2 3
1 1 1
Output
3
1 2 3
Input
5 7
1 2
1 3
1 4
1 5
3 4
3 5
4 5
0 1 0 1 0
Output
10
2 1 3 4 5 4 5 4 3 1
Input
2 0
0 0
Output
0 | instruction | 0 | 96,516 | 1 | 193,032 |
Tags: constructive algorithms, dfs and similar, graphs
Correct Solution:
```
import sys
readline = sys.stdin.readline
N, M = map(int, readline().split())
Edge = [[] for _ in range(N)]
for _ in range(M):
u, v = map(int, readline().split())
u -= 1
v -= 1
Edge[u].append(v)
Edge[v].append(u)
Pr = list(map(int, readline().split()))
Po = Pr[:]
if sum(Pr) == 0:
print(0)
else:
st = Pr.index(1)
stack = [~st, st]
used = set([st])
ans = []
P = [None]*N
pre = None
while stack:
vn = stack.pop()
if vn >= 0:
Pr[vn] ^= 1
ans.append(vn)
for vf in Edge[vn]:
if vf in used:
continue
P[vf] = vn
stack.append(~vf)
stack.append(vf)
used.add(vf)
else:
vn = ~vn
if pre != vn:
Pr[vn] ^= 1
ans.append(vn)
if vn == st:
if Pr[vn]:
ans.pop()
Pr[vn] ^= 1
break
if Pr[vn]:
ans.append(P[vn])
ans.append(vn)
Pr[vn] ^= 1
Pr[P[vn]] ^= 1
if stack and stack[-1] >= 0:
ans.append(P[vn])
Pr[P[vn]] ^= 1
pre = vn
for a in ans:
Po[a] ^= 1
for a, b in zip(ans, ans[1:]):
assert a != b
if sum(Po):
print(-1)
else:
print(len(ans))
assert len(ans) <= 4*N
print(*[a+1 for a in ans])
``` | output | 1 | 96,516 | 1 | 193,033 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on the preparations for the celebration.
<image>
Twilight Sparkle wanted to track the path of Nightmare Moon. Unfortunately, she didn't know the exact path. What she knew is the parity of the number of times that each place Nightmare Moon visited. Can you help Twilight Sparkle to restore any path that is consistent with this information?
Ponyville can be represented as an undirected graph (vertices are places, edges are roads between places) without self-loops and multi-edges. The path can start and end at any place (also it can be empty). Each place can be visited multiple times. The path must not visit more than 4n places.
Input
The first line contains two integers n and m (2 ≤ n ≤ 105; 0 ≤ m ≤ 105) — the number of places and the number of roads in Ponyville. Each of the following m lines contains two integers ui, vi (1 ≤ ui, vi ≤ n; ui ≠ vi), these integers describe a road between places ui and vi.
The next line contains n integers: x1, x2, ..., xn (0 ≤ xi ≤ 1) — the parity of the number of times that each place must be visited. If xi = 0, then the i-th place must be visited even number of times, else it must be visited odd number of times.
Output
Output the number of visited places k in the first line (0 ≤ k ≤ 4n). Then output k integers — the numbers of places in the order of path. If xi = 0, then the i-th place must appear in the path even number of times, else i-th place must appear in the path odd number of times. Note, that given road system has no self-loops, therefore any two neighbouring places in the path must be distinct.
If there is no required path, output -1. If there multiple possible paths, you can output any of them.
Examples
Input
3 2
1 2
2 3
1 1 1
Output
3
1 2 3
Input
5 7
1 2
1 3
1 4
1 5
3 4
3 5
4 5
0 1 0 1 0
Output
10
2 1 3 4 5 4 5 4 3 1
Input
2 0
0 0
Output
0
Submitted Solution:
```
def dfs(now, visited, down, odd, line):
visited[now] = True
path = []
nextDown = down + [now]
for i in line[now]:
if (i in visited):
continue
temp = dfs(i, visited, nextDown, odd, line)
odd[now] ^= temp[0]
path += [now] + temp[1]
temp = False
nextDown.reverse()
if (not odd[now]):
temp = True
path += nextDown + down[1:]
odd[now] = True
return temp, path + [now]
if (__name__ == "__main__"):
s = input().split()
n = int(s[0])
m = int(s[1])
line = {}
for i in range(n):
line[i + 1] = []
for i in range(m):
s = input().split()
u = int(s[0])
v = int(s[1])
line[u].append(v)
line[v].append(u)
odd = {}
num = 0
s = input().split()
for i in range(n):
odd[i + 1] = int(s[i])
num += odd[i + 1]
ans = []
for i in range(n):
if (odd[i + 1]):
ans = dfs(i + 1, {}, [], odd.copy(), line)[1]
break
flag = True
for i in range(len(ans)):
if (num):
odd[ans[i]] ^= 1
if (odd[ans[i]]):
num += 1
else:
num -= 1
continue
flag = False
print(i)
for j in range(i):
print(ans[j], end = " ")
break
if (flag):
if (num):
print(-1)
else:
print(0)
``` | instruction | 0 | 96,517 | 1 | 193,034 |
No | output | 1 | 96,517 | 1 | 193,035 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on the preparations for the celebration.
<image>
Twilight Sparkle wanted to track the path of Nightmare Moon. Unfortunately, she didn't know the exact path. What she knew is the parity of the number of times that each place Nightmare Moon visited. Can you help Twilight Sparkle to restore any path that is consistent with this information?
Ponyville can be represented as an undirected graph (vertices are places, edges are roads between places) without self-loops and multi-edges. The path can start and end at any place (also it can be empty). Each place can be visited multiple times. The path must not visit more than 4n places.
Input
The first line contains two integers n and m (2 ≤ n ≤ 105; 0 ≤ m ≤ 105) — the number of places and the number of roads in Ponyville. Each of the following m lines contains two integers ui, vi (1 ≤ ui, vi ≤ n; ui ≠ vi), these integers describe a road between places ui and vi.
The next line contains n integers: x1, x2, ..., xn (0 ≤ xi ≤ 1) — the parity of the number of times that each place must be visited. If xi = 0, then the i-th place must be visited even number of times, else it must be visited odd number of times.
Output
Output the number of visited places k in the first line (0 ≤ k ≤ 4n). Then output k integers — the numbers of places in the order of path. If xi = 0, then the i-th place must appear in the path even number of times, else i-th place must appear in the path odd number of times. Note, that given road system has no self-loops, therefore any two neighbouring places in the path must be distinct.
If there is no required path, output -1. If there multiple possible paths, you can output any of them.
Examples
Input
3 2
1 2
2 3
1 1 1
Output
3
1 2 3
Input
5 7
1 2
1 3
1 4
1 5
3 4
3 5
4 5
0 1 0 1 0
Output
10
2 1 3 4 5 4 5 4 3 1
Input
2 0
0 0
Output
0
Submitted Solution:
```
def dfs(now, visited, down, even, line):
visited[now] = True
path = []
nextDown = down + [now]
for i in line[now]:
if (i in visited):
continue
temp = dfs(i, visited, nextDown, even, line)
even[now] ^= temp[0]
path += [now] + temp[1]
temp = False
nextDown.reverse()
if (even[now]):
temp = True
path += nextDown + down[1:]
even[now] = True
return temp, path + [now]
if (__name__ == "__main__"):
s = input().split()
n = int(s[0])
m = int(s[1])
line = {}
for i in range(n):
line[i + 1] = []
for i in range(m):
s = input().split()
u = int(s[0])
v = int(s[1])
line[u].append(v)
line[v].append(u)
odd = {}
even = {}
num = 0
s = input().split()
for i in range(n):
odd[i + 1] = int(s[i])
even[i + 1] = not odd[i + 1]
num += odd[i + 1]
ans = []
for i in range(n):
if (odd[i + 1]):
ans = dfs(i + 1, {}, [], even, line)[1] + [i + 1]
break
flag = True
for i in range(len(ans)):
if (num):
odd[ans[i]] ^= 1
if (odd[ans[i]]):
num += 1
else:
num -= 1
continue
flag = False
print(i)
for j in range(i):
print(ans[j], end = " ")
break
if (flag):
if (num):
print(-1)
else:
print(0)
``` | instruction | 0 | 96,518 | 1 | 193,036 |
No | output | 1 | 96,518 | 1 | 193,037 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on the preparations for the celebration.
<image>
Twilight Sparkle wanted to track the path of Nightmare Moon. Unfortunately, she didn't know the exact path. What she knew is the parity of the number of times that each place Nightmare Moon visited. Can you help Twilight Sparkle to restore any path that is consistent with this information?
Ponyville can be represented as an undirected graph (vertices are places, edges are roads between places) without self-loops and multi-edges. The path can start and end at any place (also it can be empty). Each place can be visited multiple times. The path must not visit more than 4n places.
Input
The first line contains two integers n and m (2 ≤ n ≤ 105; 0 ≤ m ≤ 105) — the number of places and the number of roads in Ponyville. Each of the following m lines contains two integers ui, vi (1 ≤ ui, vi ≤ n; ui ≠ vi), these integers describe a road between places ui and vi.
The next line contains n integers: x1, x2, ..., xn (0 ≤ xi ≤ 1) — the parity of the number of times that each place must be visited. If xi = 0, then the i-th place must be visited even number of times, else it must be visited odd number of times.
Output
Output the number of visited places k in the first line (0 ≤ k ≤ 4n). Then output k integers — the numbers of places in the order of path. If xi = 0, then the i-th place must appear in the path even number of times, else i-th place must appear in the path odd number of times. Note, that given road system has no self-loops, therefore any two neighbouring places in the path must be distinct.
If there is no required path, output -1. If there multiple possible paths, you can output any of them.
Examples
Input
3 2
1 2
2 3
1 1 1
Output
3
1 2 3
Input
5 7
1 2
1 3
1 4
1 5
3 4
3 5
4 5
0 1 0 1 0
Output
10
2 1 3 4 5 4 5 4 3 1
Input
2 0
0 0
Output
0
Submitted Solution:
```
def dfs(ans, path, graph, xs):
n = len(graph)
#print("path", path, "xs", xs)
if len(path) > n * 4:
return False
u = 0
while u < n:
if not xs[u] or xs[u] & 1 != 0:
break
u += 1
if u == n:
if ans == [] or path != [] and len(ans) > len(path):
ans.clear()
ans.extend(list(map(lambda x: x + 1, path)))
return True
ret = False
for v in graph[path[-1]]:
if xs[v] and xs[v] & 1 == 0:
continue
xs[v] += 1
ret = ret or dfs(ans, path + [v], graph, xs)
xs[v] -= 1
return ret
n, m = tuple(map(int, input().split()))
graph = [[] for _ in range(n)]
for _ in range(m):
u, v = tuple(map(int, input().split()))
graph[u - 1].append(v - 1)
graph[v - 1].append(u - 1)
xs = list(map(int, input().split()))
path = []
for u in range(n):
if xs[u] & 1:
xs[u] += 1
dfs(path, [u], graph[:], xs)
break
if len(path) == 1 and n == 1:
print(-1)
else:
print(len(path))
for u in path:
print(u, end=' ')
print()
``` | instruction | 0 | 96,519 | 1 | 193,038 |
No | output | 1 | 96,519 | 1 | 193,039 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on the preparations for the celebration.
<image>
Twilight Sparkle wanted to track the path of Nightmare Moon. Unfortunately, she didn't know the exact path. What she knew is the parity of the number of times that each place Nightmare Moon visited. Can you help Twilight Sparkle to restore any path that is consistent with this information?
Ponyville can be represented as an undirected graph (vertices are places, edges are roads between places) without self-loops and multi-edges. The path can start and end at any place (also it can be empty). Each place can be visited multiple times. The path must not visit more than 4n places.
Input
The first line contains two integers n and m (2 ≤ n ≤ 105; 0 ≤ m ≤ 105) — the number of places and the number of roads in Ponyville. Each of the following m lines contains two integers ui, vi (1 ≤ ui, vi ≤ n; ui ≠ vi), these integers describe a road between places ui and vi.
The next line contains n integers: x1, x2, ..., xn (0 ≤ xi ≤ 1) — the parity of the number of times that each place must be visited. If xi = 0, then the i-th place must be visited even number of times, else it must be visited odd number of times.
Output
Output the number of visited places k in the first line (0 ≤ k ≤ 4n). Then output k integers — the numbers of places in the order of path. If xi = 0, then the i-th place must appear in the path even number of times, else i-th place must appear in the path odd number of times. Note, that given road system has no self-loops, therefore any two neighbouring places in the path must be distinct.
If there is no required path, output -1. If there multiple possible paths, you can output any of them.
Examples
Input
3 2
1 2
2 3
1 1 1
Output
3
1 2 3
Input
5 7
1 2
1 3
1 4
1 5
3 4
3 5
4 5
0 1 0 1 0
Output
10
2 1 3 4 5 4 5 4 3 1
Input
2 0
0 0
Output
0
Submitted Solution:
```
import sys
readline = sys.stdin.readline
N, M = map(int, readline().split())
Edge = [[] for _ in range(N)]
for _ in range(M):
u, v = map(int, readline().split())
u -= 1
v -= 1
Edge[u].append(v)
Edge[v].append(u)
Pr = list(map(int, readline().split()))
if sum(Pr) == 0:
print(0)
else:
st = Pr.index(1)
stack = [~st, st]
used = set([st])
ans = []
P = [None]*N
pre = None
while stack:
vn = stack.pop()
if vn >= 0:
Pr[vn] ^= 1
ans.append(vn)
for vf in Edge[vn]:
if vf in used:
continue
P[vf] = vn
stack.append(~vf)
stack.append(vf)
used.add(vf)
else:
vn = ~vn
if pre != vn:
Pr[vn] ^= 1
ans.append(vn)
if vn == st:
if Pr[vn]:
ans.pop()
Pr[vn] ^= 1
break
if Pr[vn]:
ans.append(P[vn])
ans.append(vn)
Pr[vn] ^= 1
Pr[P[vn]] ^= 1
pre = vn
if sum(Pr):
print(-1)
else:
print(len(ans))
print(*[a+1 for a in ans])
``` | instruction | 0 | 96,520 | 1 | 193,040 |
No | output | 1 | 96,520 | 1 | 193,041 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from 1 to n.
It is known that, from the capital (the city with the number 1), you can reach any other city by moving along the roads.
The President of Berland plans to improve the country's road network. The budget is enough to repair exactly n-1 roads. The President plans to choose a set of n-1 roads such that:
* it is possible to travel from the capital to any other city along the n-1 chosen roads,
* if d_i is the number of roads needed to travel from the capital to city i, moving only along the n-1 chosen roads, then d_1 + d_2 + ... + d_n is minimized (i.e. as minimal as possible).
In other words, the set of n-1 roads should preserve the connectivity of the country, and the sum of distances from city 1 to all cities should be minimized (where you can only use the n-1 chosen roads).
The president instructed the ministry to prepare k possible options to choose n-1 roads so that both conditions above are met.
Write a program that will find k possible ways to choose roads for repair. If there are fewer than k ways, then the program should output all possible valid ways to choose roads.
Input
The first line of the input contains integers n, m and k (2 ≤ n ≤ 2⋅10^5, n-1 ≤ m ≤ 2⋅10^5, 1 ≤ k ≤ 2⋅10^5), where n is the number of cities in the country, m is the number of roads and k is the number of options to choose a set of roads for repair. It is guaranteed that m ⋅ k ≤ 10^6.
The following m lines describe the roads, one road per line. Each line contains two integers a_i, b_i (1 ≤ a_i, b_i ≤ n, a_i ≠ b_i) — the numbers of the cities that the i-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital.
Output
Print t (1 ≤ t ≤ k) — the number of ways to choose a set of roads for repair. Recall that you need to find k different options; if there are fewer than k of them, then you need to find all possible different valid options.
In the following t lines, print the options, one per line. Print an option as a string of m characters where the j-th character is equal to '1' if the j-th road is included in the option, and is equal to '0' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the t lines should be different.
Since it is guaranteed that m ⋅ k ≤ 10^6, the total length of all the t lines will not exceed 10^6.
If there are several answers, output any of them.
Examples
Input
4 4 3
1 2
2 3
1 4
4 3
Output
2
1110
1011
Input
4 6 3
1 2
2 3
1 4
4 3
2 4
1 3
Output
1
101001
Input
5 6 2
1 2
1 3
2 4
2 5
3 4
3 5
Output
2
111100
110110 | instruction | 0 | 96,949 | 1 | 193,898 |
Tags: brute force, dfs and similar, graphs, shortest paths
Correct Solution:
```
import sys
import io, os
input = sys.stdin.buffer.readline
n,m,k = map(int,input().split())
AB = []
g = [[] for _ in range(n)]
for i in range(m):
a,b = map(int, input().split())
a,b = a-1, b-1
g[a].append(b)
g[b].append(a)
AB.append((a,b))
from collections import deque
q = deque([])
q.append(0)
d = [-1]*n
d[0] = 0
while q:
v = q.popleft()
for u in g[v]:
if d[u] == -1:
d[u] = d[v]+1
q.append(u)
#print(d)
inc = [[] for _ in range(n)]
for i, (a, b) in enumerate(AB):
if d[a]+1 == d[b]:
inc[b].append(i)
if d[b]+1 == d[a]:
inc[a].append(i)
#print(inc)
F = [0]*n
res = []
for i in range(k):
s = ['0']*m
for j in range(1, n):
s[inc[j][F[j]]] = '1'
res.append(''.join(s))
flag = False
for j in range(1, n):
if F[j]+1 < len(inc[j]):
flag = True
F[j] += 1
break
else:
F[j] = 0
if not flag:
break
print(len(res))
print(*res, sep='\n')
``` | output | 1 | 96,949 | 1 | 193,899 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from 1 to n.
It is known that, from the capital (the city with the number 1), you can reach any other city by moving along the roads.
The President of Berland plans to improve the country's road network. The budget is enough to repair exactly n-1 roads. The President plans to choose a set of n-1 roads such that:
* it is possible to travel from the capital to any other city along the n-1 chosen roads,
* if d_i is the number of roads needed to travel from the capital to city i, moving only along the n-1 chosen roads, then d_1 + d_2 + ... + d_n is minimized (i.e. as minimal as possible).
In other words, the set of n-1 roads should preserve the connectivity of the country, and the sum of distances from city 1 to all cities should be minimized (where you can only use the n-1 chosen roads).
The president instructed the ministry to prepare k possible options to choose n-1 roads so that both conditions above are met.
Write a program that will find k possible ways to choose roads for repair. If there are fewer than k ways, then the program should output all possible valid ways to choose roads.
Input
The first line of the input contains integers n, m and k (2 ≤ n ≤ 2⋅10^5, n-1 ≤ m ≤ 2⋅10^5, 1 ≤ k ≤ 2⋅10^5), where n is the number of cities in the country, m is the number of roads and k is the number of options to choose a set of roads for repair. It is guaranteed that m ⋅ k ≤ 10^6.
The following m lines describe the roads, one road per line. Each line contains two integers a_i, b_i (1 ≤ a_i, b_i ≤ n, a_i ≠ b_i) — the numbers of the cities that the i-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital.
Output
Print t (1 ≤ t ≤ k) — the number of ways to choose a set of roads for repair. Recall that you need to find k different options; if there are fewer than k of them, then you need to find all possible different valid options.
In the following t lines, print the options, one per line. Print an option as a string of m characters where the j-th character is equal to '1' if the j-th road is included in the option, and is equal to '0' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the t lines should be different.
Since it is guaranteed that m ⋅ k ≤ 10^6, the total length of all the t lines will not exceed 10^6.
If there are several answers, output any of them.
Examples
Input
4 4 3
1 2
2 3
1 4
4 3
Output
2
1110
1011
Input
4 6 3
1 2
2 3
1 4
4 3
2 4
1 3
Output
1
101001
Input
5 6 2
1 2
1 3
2 4
2 5
3 4
3 5
Output
2
111100
110110 | instruction | 0 | 96,950 | 1 | 193,900 |
Tags: brute force, dfs and similar, graphs, shortest paths
Correct Solution:
```
from math import inf
import heapq
n, m, k = map(int, input().split(' '))
edge = [[] for _ in range(n)]
for i in range(m):
a, b = map(int, input().split(' '))
edge[a-1].append((i, b-1))
edge[b-1].append((i, a-1))
d = [inf for _ in range(n)]
d[0] = 0
q = [0 for _ in range(n)]
f = 0
r = 1
while f < r:
i = q[f]
f += 1
for (_, j) in edge[i]:
if d[j] == inf:
d[j] = d[i]+1
q[r] = j
r += 1
#print(d)
pre = [[] for _ in range(n)]
for i in range(n):
for (ind, j) in edge[i]:
if d[j] == d[i]-1:
pre[i].append((ind, j))
#print(pre)
s = [0 for _ in range(n)]
top = n
sol = []
while top > 0:
#print(top, s)
if top == n:
u = ['0' for _ in range(m)]
#print(s)
for i in range(1,n):
u[pre[i][s[i]][0]] = '1'
sol.append("".join(u))
if len(sol) == k:
break
top -= 1
else:
s[top] += 1
if s[top] == len(pre[top]):
top -= 1
#s[top] += 1
else:
top += 1
if top < n:
s[top] = -1
print(len(sol))
for x in sol:
print(x)
``` | output | 1 | 96,950 | 1 | 193,901 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from 1 to n.
It is known that, from the capital (the city with the number 1), you can reach any other city by moving along the roads.
The President of Berland plans to improve the country's road network. The budget is enough to repair exactly n-1 roads. The President plans to choose a set of n-1 roads such that:
* it is possible to travel from the capital to any other city along the n-1 chosen roads,
* if d_i is the number of roads needed to travel from the capital to city i, moving only along the n-1 chosen roads, then d_1 + d_2 + ... + d_n is minimized (i.e. as minimal as possible).
In other words, the set of n-1 roads should preserve the connectivity of the country, and the sum of distances from city 1 to all cities should be minimized (where you can only use the n-1 chosen roads).
The president instructed the ministry to prepare k possible options to choose n-1 roads so that both conditions above are met.
Write a program that will find k possible ways to choose roads for repair. If there are fewer than k ways, then the program should output all possible valid ways to choose roads.
Input
The first line of the input contains integers n, m and k (2 ≤ n ≤ 2⋅10^5, n-1 ≤ m ≤ 2⋅10^5, 1 ≤ k ≤ 2⋅10^5), where n is the number of cities in the country, m is the number of roads and k is the number of options to choose a set of roads for repair. It is guaranteed that m ⋅ k ≤ 10^6.
The following m lines describe the roads, one road per line. Each line contains two integers a_i, b_i (1 ≤ a_i, b_i ≤ n, a_i ≠ b_i) — the numbers of the cities that the i-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital.
Output
Print t (1 ≤ t ≤ k) — the number of ways to choose a set of roads for repair. Recall that you need to find k different options; if there are fewer than k of them, then you need to find all possible different valid options.
In the following t lines, print the options, one per line. Print an option as a string of m characters where the j-th character is equal to '1' if the j-th road is included in the option, and is equal to '0' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the t lines should be different.
Since it is guaranteed that m ⋅ k ≤ 10^6, the total length of all the t lines will not exceed 10^6.
If there are several answers, output any of them.
Examples
Input
4 4 3
1 2
2 3
1 4
4 3
Output
2
1110
1011
Input
4 6 3
1 2
2 3
1 4
4 3
2 4
1 3
Output
1
101001
Input
5 6 2
1 2
1 3
2 4
2 5
3 4
3 5
Output
2
111100
110110 | instruction | 0 | 96,951 | 1 | 193,902 |
Tags: brute force, dfs and similar, graphs, shortest paths
Correct Solution:
```
from collections import deque
import sys
input = sys.stdin.readline
def bfs(s):
q = deque()
q.append(s)
dist = [-1] * (n + 1)
dist[s] = 0
while q:
i = q.popleft()
di = dist[i]
for j, c in G[i]:
if dist[j] == -1:
dist[j] = di + 1
q.append(j)
return dist
n, m, k = map(int, input().split())
G = [[] for _ in range(n + 1)]
for i in range(m):
a, b = map(int, input().split())
G[a].append((b, i))
G[b].append((a, i))
dist = bfs(1)
parent = [[] for _ in range(n + 1)]
for i in range(2, n + 1):
di = dist[i]
for j, c in G[i]:
if dist[j] == di - 1:
parent[i].append(c)
t = 1
for s in parent:
t *= max(1, len(s))
if t > k:
t = k
break
print(t)
x = [0] * (n - 1)
l = [len(parent[i]) for i in range(2, n + 1)]
ans = ["0"] * m
for i in range(n - 1):
ans[parent[i + 2][x[i]]] = "1"
print("".join(ans))
for i in range(t - 1):
ans = ["0"] * m
for i in range(n - 1):
if x[i] + 1 < l[i]:
x[i] += 1
break
x[i] += 1
x[i] %= l[i]
for i in range(n - 1):
ans[parent[i + 2][x[i]]] = "1"
print("".join(ans))
``` | output | 1 | 96,951 | 1 | 193,903 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from 1 to n.
It is known that, from the capital (the city with the number 1), you can reach any other city by moving along the roads.
The President of Berland plans to improve the country's road network. The budget is enough to repair exactly n-1 roads. The President plans to choose a set of n-1 roads such that:
* it is possible to travel from the capital to any other city along the n-1 chosen roads,
* if d_i is the number of roads needed to travel from the capital to city i, moving only along the n-1 chosen roads, then d_1 + d_2 + ... + d_n is minimized (i.e. as minimal as possible).
In other words, the set of n-1 roads should preserve the connectivity of the country, and the sum of distances from city 1 to all cities should be minimized (where you can only use the n-1 chosen roads).
The president instructed the ministry to prepare k possible options to choose n-1 roads so that both conditions above are met.
Write a program that will find k possible ways to choose roads for repair. If there are fewer than k ways, then the program should output all possible valid ways to choose roads.
Input
The first line of the input contains integers n, m and k (2 ≤ n ≤ 2⋅10^5, n-1 ≤ m ≤ 2⋅10^5, 1 ≤ k ≤ 2⋅10^5), where n is the number of cities in the country, m is the number of roads and k is the number of options to choose a set of roads for repair. It is guaranteed that m ⋅ k ≤ 10^6.
The following m lines describe the roads, one road per line. Each line contains two integers a_i, b_i (1 ≤ a_i, b_i ≤ n, a_i ≠ b_i) — the numbers of the cities that the i-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital.
Output
Print t (1 ≤ t ≤ k) — the number of ways to choose a set of roads for repair. Recall that you need to find k different options; if there are fewer than k of them, then you need to find all possible different valid options.
In the following t lines, print the options, one per line. Print an option as a string of m characters where the j-th character is equal to '1' if the j-th road is included in the option, and is equal to '0' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the t lines should be different.
Since it is guaranteed that m ⋅ k ≤ 10^6, the total length of all the t lines will not exceed 10^6.
If there are several answers, output any of them.
Examples
Input
4 4 3
1 2
2 3
1 4
4 3
Output
2
1110
1011
Input
4 6 3
1 2
2 3
1 4
4 3
2 4
1 3
Output
1
101001
Input
5 6 2
1 2
1 3
2 4
2 5
3 4
3 5
Output
2
111100
110110 | instruction | 0 | 96,952 | 1 | 193,904 |
Tags: brute force, dfs and similar, graphs, shortest paths
Correct Solution:
```
from math import inf
nmk = list(map(int, input().split(' ')))
n = nmk[0]
m = nmk[1]
k = nmk[2]
a = []
for i in range(m):
a.append(list(map(int, input().split(' '))))
smej = [[] for j in range(n)]
nums = {}
t = 0
for i in a:
nums.update({(i[0], i[1]):t})
t += 1
smej[i[0]-1].append(i[1]-1)
smej[i[1]-1].append(i[0]-1)
dists = [inf for i in range(n)]
dists[0] = 0
q = [0]
while len(q) > 0:
u = q.pop(0)
for i in smej[u]:
if (dists[i] == inf):
q.append(i)
dists[i] = dists[u] + 1
p = [[] for i in range(n)]
for i in range(1, n):
for j in smej[i]:
if(dists[j] == dists[i]-1):
try:
p[i].append(nums[(j+1,i+1)])
except:
p[i].append(nums[(i+1,j+1)])
am = 1
p.pop(0)
for i in range(len(p)):
am *= len(p[i])
if(am < k):
print(am)
else:
print(k)
f = [0 for i in range(len(p))]
ans = []
for i in range(k):
s = ['0'for i in range(m)]
for j in range(len(p)):
s[p[j][f[j]]] = '1'
s = ''.join(s)
ans.append(s)
ok = False
first = True
for j in range(len(p)-1, -1, -1):
if first:
first = False
if(f[j]+1 == len(p[j])):
if(j == 0):
ok = True
break
f[j] = 0
f[j-1]+= 1
else:
f[j]+= 1
break
else:
if(f[j] == len(p[j])):
if(j == 0):
ok = True
break
else:
f[j] = 0
f[j-1] += 1
if ok:
break
for j in range(len(ans)):
print(ans[j])
``` | output | 1 | 96,952 | 1 | 193,905 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from 1 to n.
It is known that, from the capital (the city with the number 1), you can reach any other city by moving along the roads.
The President of Berland plans to improve the country's road network. The budget is enough to repair exactly n-1 roads. The President plans to choose a set of n-1 roads such that:
* it is possible to travel from the capital to any other city along the n-1 chosen roads,
* if d_i is the number of roads needed to travel from the capital to city i, moving only along the n-1 chosen roads, then d_1 + d_2 + ... + d_n is minimized (i.e. as minimal as possible).
In other words, the set of n-1 roads should preserve the connectivity of the country, and the sum of distances from city 1 to all cities should be minimized (where you can only use the n-1 chosen roads).
The president instructed the ministry to prepare k possible options to choose n-1 roads so that both conditions above are met.
Write a program that will find k possible ways to choose roads for repair. If there are fewer than k ways, then the program should output all possible valid ways to choose roads.
Input
The first line of the input contains integers n, m and k (2 ≤ n ≤ 2⋅10^5, n-1 ≤ m ≤ 2⋅10^5, 1 ≤ k ≤ 2⋅10^5), where n is the number of cities in the country, m is the number of roads and k is the number of options to choose a set of roads for repair. It is guaranteed that m ⋅ k ≤ 10^6.
The following m lines describe the roads, one road per line. Each line contains two integers a_i, b_i (1 ≤ a_i, b_i ≤ n, a_i ≠ b_i) — the numbers of the cities that the i-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital.
Output
Print t (1 ≤ t ≤ k) — the number of ways to choose a set of roads for repair. Recall that you need to find k different options; if there are fewer than k of them, then you need to find all possible different valid options.
In the following t lines, print the options, one per line. Print an option as a string of m characters where the j-th character is equal to '1' if the j-th road is included in the option, and is equal to '0' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the t lines should be different.
Since it is guaranteed that m ⋅ k ≤ 10^6, the total length of all the t lines will not exceed 10^6.
If there are several answers, output any of them.
Examples
Input
4 4 3
1 2
2 3
1 4
4 3
Output
2
1110
1011
Input
4 6 3
1 2
2 3
1 4
4 3
2 4
1 3
Output
1
101001
Input
5 6 2
1 2
1 3
2 4
2 5
3 4
3 5
Output
2
111100
110110 | instruction | 0 | 96,953 | 1 | 193,906 |
Tags: brute force, dfs and similar, graphs, shortest paths
Correct Solution:
```
import sys
input=lambda:sys.stdin.readline().rstrip()
sys.setrecursionlimit(200001)
n,m,k=map(int,input().split())
edge=[[]for _ in range(n)]
di={}
for i in range(m):
a,b=map(int,input().split())
a-=1
b-=1
if a>b:a,b=b,a
di[(a,b)]=i
edge[a].append(b)
edge[b].append(a)
d=[10**10]*n
d[0]=0
root=[set()for _ in range(n)]
queue=[0]
for node in queue:
for mode in edge[node]:
if d[mode]!=10**10:
if d[mode]+1==d[node]:
root[node].add(mode)
elif d[mode]==d[node]+1:
root[mode].add(node)
continue
d[mode]=d[node]+1
queue.append(mode)
for i in range(n):root[i]=list(root[i])
t=1
for i in range(1,n):t*=len(root[i])
print(min(t,k))
for i in range(min(t,k)):
ans=["0"]*m
for j in range(1,n):
i,jj=divmod(i,len(root[j]))
ans[di[(min(j,root[j][jj]),max(j,root[j][jj]))]]="1"
print("".join(ans))
``` | output | 1 | 96,953 | 1 | 193,907 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from 1 to n.
It is known that, from the capital (the city with the number 1), you can reach any other city by moving along the roads.
The President of Berland plans to improve the country's road network. The budget is enough to repair exactly n-1 roads. The President plans to choose a set of n-1 roads such that:
* it is possible to travel from the capital to any other city along the n-1 chosen roads,
* if d_i is the number of roads needed to travel from the capital to city i, moving only along the n-1 chosen roads, then d_1 + d_2 + ... + d_n is minimized (i.e. as minimal as possible).
In other words, the set of n-1 roads should preserve the connectivity of the country, and the sum of distances from city 1 to all cities should be minimized (where you can only use the n-1 chosen roads).
The president instructed the ministry to prepare k possible options to choose n-1 roads so that both conditions above are met.
Write a program that will find k possible ways to choose roads for repair. If there are fewer than k ways, then the program should output all possible valid ways to choose roads.
Input
The first line of the input contains integers n, m and k (2 ≤ n ≤ 2⋅10^5, n-1 ≤ m ≤ 2⋅10^5, 1 ≤ k ≤ 2⋅10^5), where n is the number of cities in the country, m is the number of roads and k is the number of options to choose a set of roads for repair. It is guaranteed that m ⋅ k ≤ 10^6.
The following m lines describe the roads, one road per line. Each line contains two integers a_i, b_i (1 ≤ a_i, b_i ≤ n, a_i ≠ b_i) — the numbers of the cities that the i-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital.
Output
Print t (1 ≤ t ≤ k) — the number of ways to choose a set of roads for repair. Recall that you need to find k different options; if there are fewer than k of them, then you need to find all possible different valid options.
In the following t lines, print the options, one per line. Print an option as a string of m characters where the j-th character is equal to '1' if the j-th road is included in the option, and is equal to '0' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the t lines should be different.
Since it is guaranteed that m ⋅ k ≤ 10^6, the total length of all the t lines will not exceed 10^6.
If there are several answers, output any of them.
Examples
Input
4 4 3
1 2
2 3
1 4
4 3
Output
2
1110
1011
Input
4 6 3
1 2
2 3
1 4
4 3
2 4
1 3
Output
1
101001
Input
5 6 2
1 2
1 3
2 4
2 5
3 4
3 5
Output
2
111100
110110 | instruction | 0 | 96,954 | 1 | 193,908 |
Tags: brute force, dfs and similar, graphs, shortest paths
Correct Solution:
```
n, m, k = [int(x) for x in input().split()]
E = [[] for _ in range(n)]
for i in range(m):
a, b = [int(x) for x in input().split()]
E[a - 1].append((b - 1, i))
E[b - 1].append((a - 1, i))
Q = [0]
INFTY = n + 1
levels = [INFTY] * n
levels[0] = 0
E_min = [[] for _ in range(n)]
# bfs
while len(Q) > 0:
city = Q.pop(0)
for adj, i in E[city]:
if levels[city] + 1 <= levels[adj]:
if levels[adj] == INFTY:
Q.append(adj)
levels[adj] = levels[city] + 1
E_min[adj].append(i)
def gen_poss(city, selected, all_poss, next_choice, poss):
if len(all_poss) >= k:
return
if poss >= k:
all_poss.append(''.join(selected))
return
if city == n:
all_poss.append(''.join(selected))
else:
# choose one from E_min[edge]
for i in E_min[city]:
selected[i] = '1'
next_city = next_choice[city] # skip edges with only one choice
gen_poss(next_city, selected, all_poss, next_choice, poss * len(E_min[city]))
selected[i] = '0'
next_choice = [0] * n
selected = ['0'] * m
nc = n
for i in range(n - 1, -1, -1):
next_choice[i] = nc
if len(E_min[i]) > 1:
nc = i
if len(E_min[i]) >= 1:
selected[E_min[i][0]] = '1'
all_poss = []
gen_poss(next_choice[0], selected, all_poss, next_choice, 1)
print('{}\n{}'.format(len(all_poss), '\n'.join(all_poss)))
``` | output | 1 | 96,954 | 1 | 193,909 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from 1 to n.
It is known that, from the capital (the city with the number 1), you can reach any other city by moving along the roads.
The President of Berland plans to improve the country's road network. The budget is enough to repair exactly n-1 roads. The President plans to choose a set of n-1 roads such that:
* it is possible to travel from the capital to any other city along the n-1 chosen roads,
* if d_i is the number of roads needed to travel from the capital to city i, moving only along the n-1 chosen roads, then d_1 + d_2 + ... + d_n is minimized (i.e. as minimal as possible).
In other words, the set of n-1 roads should preserve the connectivity of the country, and the sum of distances from city 1 to all cities should be minimized (where you can only use the n-1 chosen roads).
The president instructed the ministry to prepare k possible options to choose n-1 roads so that both conditions above are met.
Write a program that will find k possible ways to choose roads for repair. If there are fewer than k ways, then the program should output all possible valid ways to choose roads.
Input
The first line of the input contains integers n, m and k (2 ≤ n ≤ 2⋅10^5, n-1 ≤ m ≤ 2⋅10^5, 1 ≤ k ≤ 2⋅10^5), where n is the number of cities in the country, m is the number of roads and k is the number of options to choose a set of roads for repair. It is guaranteed that m ⋅ k ≤ 10^6.
The following m lines describe the roads, one road per line. Each line contains two integers a_i, b_i (1 ≤ a_i, b_i ≤ n, a_i ≠ b_i) — the numbers of the cities that the i-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital.
Output
Print t (1 ≤ t ≤ k) — the number of ways to choose a set of roads for repair. Recall that you need to find k different options; if there are fewer than k of them, then you need to find all possible different valid options.
In the following t lines, print the options, one per line. Print an option as a string of m characters where the j-th character is equal to '1' if the j-th road is included in the option, and is equal to '0' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the t lines should be different.
Since it is guaranteed that m ⋅ k ≤ 10^6, the total length of all the t lines will not exceed 10^6.
If there are several answers, output any of them.
Examples
Input
4 4 3
1 2
2 3
1 4
4 3
Output
2
1110
1011
Input
4 6 3
1 2
2 3
1 4
4 3
2 4
1 3
Output
1
101001
Input
5 6 2
1 2
1 3
2 4
2 5
3 4
3 5
Output
2
111100
110110 | instruction | 0 | 96,955 | 1 | 193,910 |
Tags: brute force, dfs and similar, graphs, shortest paths
Correct Solution:
```
import sys
import os
class Path():
def __init__(self, idx, s, d):
self.idx = idx
self.s = s
self.d = d
def __eq__(self, rhs):
return self.s == rhs.s and self.d == rhs.d
def __hash__(self):
return self.s * 100000 + self.d
def shortestPath(n, k, paths):
ps = dict()
for p in paths:
if p.s not in ps:
ps[p.s] = [p]
else:
ps[p.s].append(p)
if p.d not in ps:
ps[p.d] = [p]
else:
ps[p.d].append(p)
d = set()
open = set()
d.add(1)
for e in paths:
if e.s == 1 or e.d == 1:
open.add(e)
general_edges = []
general_choices = []
while len(d) < n:
newd = set()
choices = dict()
for e in open:
oe = e.s
if e.s in d:
oe = e.d
newd.add(oe)
if oe not in choices:
choices[oe] = [e.idx]
else:
choices[oe].append(e.idx)
d.update(newd)
for oe, p in choices.items():
if len(p) > 1:
general_choices.append(p)
else:
general_edges.append(p[0])
open = set()
for node in newd:
for p in ps[node]:
if (p.s in d) != (p.d in d):
open.add(p)
maxk = 1
for choice in general_choices:
maxk *= len(choice)
k = min(k, maxk)
print(k)
output = ['0'] * len(paths)
for e in general_edges:
output[e] = '1'
for e in general_choices:
output[e[0]] = '1'
for i in range(k):
print(''.join(output))
for choice in general_choices:
done = False
for i in range(len(choice) - 1):
if output[choice[i]] == '1':
output[choice[i]] = '0'
output[choice[i + 1]] = '1'
done = True
break
if done:
break
output[choice[len(choice) - 1]] = '0'
output[choice[0]] = '1'
def main():
n, m, k = (int(x) for x in input().split())
paths = []
for i in range(m):
path = [int(x) for x in input().split()]
paths.append(Path(i, path[0], path[1]))
shortestPath(n, k, paths)
if __name__ == '__main__':
main()
``` | output | 1 | 96,955 | 1 | 193,911 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from 1 to n.
It is known that, from the capital (the city with the number 1), you can reach any other city by moving along the roads.
The President of Berland plans to improve the country's road network. The budget is enough to repair exactly n-1 roads. The President plans to choose a set of n-1 roads such that:
* it is possible to travel from the capital to any other city along the n-1 chosen roads,
* if d_i is the number of roads needed to travel from the capital to city i, moving only along the n-1 chosen roads, then d_1 + d_2 + ... + d_n is minimized (i.e. as minimal as possible).
In other words, the set of n-1 roads should preserve the connectivity of the country, and the sum of distances from city 1 to all cities should be minimized (where you can only use the n-1 chosen roads).
The president instructed the ministry to prepare k possible options to choose n-1 roads so that both conditions above are met.
Write a program that will find k possible ways to choose roads for repair. If there are fewer than k ways, then the program should output all possible valid ways to choose roads.
Input
The first line of the input contains integers n, m and k (2 ≤ n ≤ 2⋅10^5, n-1 ≤ m ≤ 2⋅10^5, 1 ≤ k ≤ 2⋅10^5), where n is the number of cities in the country, m is the number of roads and k is the number of options to choose a set of roads for repair. It is guaranteed that m ⋅ k ≤ 10^6.
The following m lines describe the roads, one road per line. Each line contains two integers a_i, b_i (1 ≤ a_i, b_i ≤ n, a_i ≠ b_i) — the numbers of the cities that the i-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital.
Output
Print t (1 ≤ t ≤ k) — the number of ways to choose a set of roads for repair. Recall that you need to find k different options; if there are fewer than k of them, then you need to find all possible different valid options.
In the following t lines, print the options, one per line. Print an option as a string of m characters where the j-th character is equal to '1' if the j-th road is included in the option, and is equal to '0' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the t lines should be different.
Since it is guaranteed that m ⋅ k ≤ 10^6, the total length of all the t lines will not exceed 10^6.
If there are several answers, output any of them.
Examples
Input
4 4 3
1 2
2 3
1 4
4 3
Output
2
1110
1011
Input
4 6 3
1 2
2 3
1 4
4 3
2 4
1 3
Output
1
101001
Input
5 6 2
1 2
1 3
2 4
2 5
3 4
3 5
Output
2
111100
110110
Submitted Solution:
```
import sys
input=lambda:sys.stdin.readline().rstrip()
sys.setrecursionlimit(200001)
n,m,k=map(int,input().split())
edge=[[]for _ in range(n)]
di={}
for i in range(m):
a,b=map(int,input().split())
a-=1
b-=1
if a>b:a,b=b,a
di[(a,b)]=i
edge[a].append(b)
edge[b].append(a)
d=[10**10]*n
d[0]=0
root=[set()for _ in range(n)]
queue=[0]
for node in queue:
for mode in edge[node]:
if d[mode]!=10**10:
if d[mode]+1==d[node]:
root[node].add(mode)
elif d[mode]==d[node]+1:
root[mode].add(node)
continue
d[mode]=d[node]+1
queue.append(mode)
for i in range(n):root[i]=list(root[i])
ans=["0"]*m
anss=[]
def f(i,k):
for j in root[i]:
ii=min(i,j)
jj=max(i,j)
ans[di[(ii,jj)]]="1"
if i==n-1:
k-=1
anss.append("".join(ans))
else:
k-=f(i+1,k)
ans[di[(ii,jj)]]="0"
if k==0:break
return k
f(1,k)
print(len(anss))
for i in anss:print(i)
``` | instruction | 0 | 96,956 | 1 | 193,912 |
No | output | 1 | 96,956 | 1 | 193,913 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from 1 to n.
It is known that, from the capital (the city with the number 1), you can reach any other city by moving along the roads.
The President of Berland plans to improve the country's road network. The budget is enough to repair exactly n-1 roads. The President plans to choose a set of n-1 roads such that:
* it is possible to travel from the capital to any other city along the n-1 chosen roads,
* if d_i is the number of roads needed to travel from the capital to city i, moving only along the n-1 chosen roads, then d_1 + d_2 + ... + d_n is minimized (i.e. as minimal as possible).
In other words, the set of n-1 roads should preserve the connectivity of the country, and the sum of distances from city 1 to all cities should be minimized (where you can only use the n-1 chosen roads).
The president instructed the ministry to prepare k possible options to choose n-1 roads so that both conditions above are met.
Write a program that will find k possible ways to choose roads for repair. If there are fewer than k ways, then the program should output all possible valid ways to choose roads.
Input
The first line of the input contains integers n, m and k (2 ≤ n ≤ 2⋅10^5, n-1 ≤ m ≤ 2⋅10^5, 1 ≤ k ≤ 2⋅10^5), where n is the number of cities in the country, m is the number of roads and k is the number of options to choose a set of roads for repair. It is guaranteed that m ⋅ k ≤ 10^6.
The following m lines describe the roads, one road per line. Each line contains two integers a_i, b_i (1 ≤ a_i, b_i ≤ n, a_i ≠ b_i) — the numbers of the cities that the i-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital.
Output
Print t (1 ≤ t ≤ k) — the number of ways to choose a set of roads for repair. Recall that you need to find k different options; if there are fewer than k of them, then you need to find all possible different valid options.
In the following t lines, print the options, one per line. Print an option as a string of m characters where the j-th character is equal to '1' if the j-th road is included in the option, and is equal to '0' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the t lines should be different.
Since it is guaranteed that m ⋅ k ≤ 10^6, the total length of all the t lines will not exceed 10^6.
If there are several answers, output any of them.
Examples
Input
4 4 3
1 2
2 3
1 4
4 3
Output
2
1110
1011
Input
4 6 3
1 2
2 3
1 4
4 3
2 4
1 3
Output
1
101001
Input
5 6 2
1 2
1 3
2 4
2 5
3 4
3 5
Output
2
111100
110110
Submitted Solution:
```
import sys
input=lambda:sys.stdin.readline().rstrip()
sys.setrecursionlimit(200001)
n,m,k=map(int,input().split())
edge=[[]for _ in range(n)]
di={}
for i in range(m):
a,b=map(int,input().split())
a-=1
b-=1
if a>b:a,b=b,a
di[(a,b)]=i
edge[a].append(b)
edge[b].append(a)
d=[10**10]*n
d[0]=0
root=[set()for _ in range(n)]
queue=[0]
for node in queue:
for mode in edge[node]:
if d[mode]!=10**10:
if d[mode]+1==d[node]:
root[node].add(mode)
elif d[mode]==d[node]+1:
root[mode].add(node)
continue
d[mode]=d[node]+1
queue.append(mode)
for i in range(n):root[i]=list(root[i])
ans=["0"]*m
anss=[]
def f(i,k):
for j in root[i]:
ii=min(i,j)
jj=max(i,j)
ans[di[(ii,jj)]]="1"
if i==n-1:
k-=1
anss.append("".join(ans))
if k==0:
print(len(anss))
for i in anss:print(i)
exit()
else:
k-=f(i+1,k)
ans[di[(ii,jj)]]="0"
if k==0:break
return k
f(1,k)
print(len(anss))
for i in anss:print(i)
``` | instruction | 0 | 96,957 | 1 | 193,914 |
No | output | 1 | 96,957 | 1 | 193,915 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from 1 to n.
It is known that, from the capital (the city with the number 1), you can reach any other city by moving along the roads.
The President of Berland plans to improve the country's road network. The budget is enough to repair exactly n-1 roads. The President plans to choose a set of n-1 roads such that:
* it is possible to travel from the capital to any other city along the n-1 chosen roads,
* if d_i is the number of roads needed to travel from the capital to city i, moving only along the n-1 chosen roads, then d_1 + d_2 + ... + d_n is minimized (i.e. as minimal as possible).
In other words, the set of n-1 roads should preserve the connectivity of the country, and the sum of distances from city 1 to all cities should be minimized (where you can only use the n-1 chosen roads).
The president instructed the ministry to prepare k possible options to choose n-1 roads so that both conditions above are met.
Write a program that will find k possible ways to choose roads for repair. If there are fewer than k ways, then the program should output all possible valid ways to choose roads.
Input
The first line of the input contains integers n, m and k (2 ≤ n ≤ 2⋅10^5, n-1 ≤ m ≤ 2⋅10^5, 1 ≤ k ≤ 2⋅10^5), where n is the number of cities in the country, m is the number of roads and k is the number of options to choose a set of roads for repair. It is guaranteed that m ⋅ k ≤ 10^6.
The following m lines describe the roads, one road per line. Each line contains two integers a_i, b_i (1 ≤ a_i, b_i ≤ n, a_i ≠ b_i) — the numbers of the cities that the i-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital.
Output
Print t (1 ≤ t ≤ k) — the number of ways to choose a set of roads for repair. Recall that you need to find k different options; if there are fewer than k of them, then you need to find all possible different valid options.
In the following t lines, print the options, one per line. Print an option as a string of m characters where the j-th character is equal to '1' if the j-th road is included in the option, and is equal to '0' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the t lines should be different.
Since it is guaranteed that m ⋅ k ≤ 10^6, the total length of all the t lines will not exceed 10^6.
If there are several answers, output any of them.
Examples
Input
4 4 3
1 2
2 3
1 4
4 3
Output
2
1110
1011
Input
4 6 3
1 2
2 3
1 4
4 3
2 4
1 3
Output
1
101001
Input
5 6 2
1 2
1 3
2 4
2 5
3 4
3 5
Output
2
111100
110110
Submitted Solution:
```
import sys,math
from collections import defaultdict,deque
import itertools
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
n,m,k = inpl()
g = [[] for _ in range(n)]
edge = {}
for i in range(m):
a,b = inpl()
a,b = a-1,b-1
g[a].append(b)
g[b].append(a)
edge[(a,b)] = i
edge[(b,a)] = i
de = set()
dist = [INF] * n
dist[0] = 0
pre = [-1] * n
q = deque()
q.append(0)
while q:
now = q.popleft()
for nx in g[now]:
if pre[now] == nx: continue
if dist[nx] == INF:
dist[nx] = dist[now] + 1
pre[nx] = now
q.append(nx)
elif dist[nx] > dist[now] + 1:
de.add(edge[pre[nx],nx])
pre[nx] = now
dist[nx] = dist[now] + 1
q.append(nx)
elif dist[nx] == dist[now] + 1:
de.add(edge[pre[nx],nx])
de.add(edge[now,nx])
else:
de.add(edge[now,nx])
# print(de)
de = list(de)
cnt = 0
for pt in itertools.combinations(de,m-(n-1)):
if cnt == k: break
res = ['1'] * m
for x in pt:
res[x] = '0'
print(''.join(res))
cnt += 1
``` | instruction | 0 | 96,958 | 1 | 193,916 |
No | output | 1 | 96,958 | 1 | 193,917 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from 1 to n.
It is known that, from the capital (the city with the number 1), you can reach any other city by moving along the roads.
The President of Berland plans to improve the country's road network. The budget is enough to repair exactly n-1 roads. The President plans to choose a set of n-1 roads such that:
* it is possible to travel from the capital to any other city along the n-1 chosen roads,
* if d_i is the number of roads needed to travel from the capital to city i, moving only along the n-1 chosen roads, then d_1 + d_2 + ... + d_n is minimized (i.e. as minimal as possible).
In other words, the set of n-1 roads should preserve the connectivity of the country, and the sum of distances from city 1 to all cities should be minimized (where you can only use the n-1 chosen roads).
The president instructed the ministry to prepare k possible options to choose n-1 roads so that both conditions above are met.
Write a program that will find k possible ways to choose roads for repair. If there are fewer than k ways, then the program should output all possible valid ways to choose roads.
Input
The first line of the input contains integers n, m and k (2 ≤ n ≤ 2⋅10^5, n-1 ≤ m ≤ 2⋅10^5, 1 ≤ k ≤ 2⋅10^5), where n is the number of cities in the country, m is the number of roads and k is the number of options to choose a set of roads for repair. It is guaranteed that m ⋅ k ≤ 10^6.
The following m lines describe the roads, one road per line. Each line contains two integers a_i, b_i (1 ≤ a_i, b_i ≤ n, a_i ≠ b_i) — the numbers of the cities that the i-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital.
Output
Print t (1 ≤ t ≤ k) — the number of ways to choose a set of roads for repair. Recall that you need to find k different options; if there are fewer than k of them, then you need to find all possible different valid options.
In the following t lines, print the options, one per line. Print an option as a string of m characters where the j-th character is equal to '1' if the j-th road is included in the option, and is equal to '0' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the t lines should be different.
Since it is guaranteed that m ⋅ k ≤ 10^6, the total length of all the t lines will not exceed 10^6.
If there are several answers, output any of them.
Examples
Input
4 4 3
1 2
2 3
1 4
4 3
Output
2
1110
1011
Input
4 6 3
1 2
2 3
1 4
4 3
2 4
1 3
Output
1
101001
Input
5 6 2
1 2
1 3
2 4
2 5
3 4
3 5
Output
2
111100
110110
Submitted Solution:
```
n, m, k = [int(x) for x in input().split()]
E = [[] for _ in range(n)]
for i in range(m):
a, b = [int(x) for x in input().split()]
E[a - 1].append((b - 1, i))
Q = [0]
INFTY = n + 1
levels = [INFTY] * n
levels[0] = 0
E_min = [[] for _ in range(n)]
while len(Q) > 0:
city = Q.pop(0)
for adj, i in E[city]:
if levels[city] + 1 <= levels[adj]:
if levels[adj] == INFTY:
Q.append(adj)
levels[adj] = levels[city] + 1
E_min[adj].append(i)
def gen_poss(edge, selected, all_poss):
if len(all_poss) >= k:
return
if edge == n:
all_poss.append(''.join(selected))
else:
# choose one from E_min[edge]
if len(E_min[edge]) == 0:
gen_poss(edge + 1, selected, all_poss)
else:
for i in E_min[edge]:
selected[i] = '1'
gen_poss(edge + 1, selected, all_poss)
selected[i] = '0'
all_poss = []
gen_poss(0, ['0'] * m, all_poss)
print('{}\n{}'.format(len(all_poss), '\n'.join(all_poss)))
``` | instruction | 0 | 96,959 | 1 | 193,918 |
No | output | 1 | 96,959 | 1 | 193,919 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
Input
7 1 4
Output
6
Input
30 20 10
Output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4. | instruction | 0 | 97,482 | 1 | 194,964 |
Tags: implementation, math, sortings
Correct Solution:
```
x1,x2,x3=sorted(map(int, input().split()))
print(x3-x1)
``` | output | 1 | 97,482 | 1 | 194,965 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
Input
7 1 4
Output
6
Input
30 20 10
Output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4. | instruction | 0 | 97,483 | 1 | 194,966 |
Tags: implementation, math, sortings
Correct Solution:
```
straight=list(map(int,input().split()))
min_straight=min(straight)
max_straight=max(straight)
list1=[]
for j in straight:
list1.append(abs(j-straight[0]))
min_distance=sum(list1)
list1=[]
for i in range(min_straight,max_straight+1):
for j in straight:
list1.append(abs(j-i))
if sum(list1)<min_distance:
min_distance=sum(list1)
list1=[]
print (min_distance)
``` | output | 1 | 97,483 | 1 | 194,967 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
Input
7 1 4
Output
6
Input
30 20 10
Output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4. | instruction | 0 | 97,484 | 1 | 194,968 |
Tags: implementation, math, sortings
Correct Solution:
```
def length():
leastdistance = 0
place = sorted(map(int, input().split()))
for L in range(len(place)):
leastdistance += int(abs(place[1]-place[L]))
print(leastdistance)
length()
``` | output | 1 | 97,484 | 1 | 194,969 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
Input
7 1 4
Output
6
Input
30 20 10
Output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4. | instruction | 0 | 97,485 | 1 | 194,970 |
Tags: implementation, math, sortings
Correct Solution:
```
l=sorted(map(int,input().split()));print(-l[0]+l[2])
``` | output | 1 | 97,485 | 1 | 194,971 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
Input
7 1 4
Output
6
Input
30 20 10
Output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4. | instruction | 0 | 97,486 | 1 | 194,972 |
Tags: implementation, math, sortings
Correct Solution:
```
a,b,c = [int(i) for i in input().split()]
print(max(abs(a-b), abs(a-c), abs(b-c)))
``` | output | 1 | 97,486 | 1 | 194,973 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
Input
7 1 4
Output
6
Input
30 20 10
Output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4. | instruction | 0 | 97,487 | 1 | 194,974 |
Tags: implementation, math, sortings
Correct Solution:
```
A=sorted(list(map(int,input().split())))
print(-A[0]+A[2])
``` | output | 1 | 97,487 | 1 | 194,975 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
Input
7 1 4
Output
6
Input
30 20 10
Output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4. | instruction | 0 | 97,488 | 1 | 194,976 |
Tags: implementation, math, sortings
Correct Solution:
```
x,y,z=map(int,input().split())
l=[]
l.append(x)
l.append(y)
l.append(z)
l.sort()
a=abs(x-l[1])
b=abs(y-l[1])
c=abs(z-l[1])
print(a+b+c)
``` | output | 1 | 97,488 | 1 | 194,977 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
Input
7 1 4
Output
6
Input
30 20 10
Output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4. | instruction | 0 | 97,489 | 1 | 194,978 |
Tags: implementation, math, sortings
Correct Solution:
```
x1, x2, x3 = map(int, input().split())
diff1 = abs(x1 -x2)
diff2 = abs(x2 -x3)
diff3 = abs(x1 - x3)
print(max(diff1, diff2, diff3))
``` | output | 1 | 97,489 | 1 | 194,979 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
Input
7 1 4
Output
6
Input
30 20 10
Output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4.
Submitted Solution:
```
a = list(map(int, input().split()))
a.sort()
k = (a[1])
print(abs(k-a[0])+abs(k-a[1])+abs(k-a[2]))
``` | instruction | 0 | 97,490 | 1 | 194,980 |
Yes | output | 1 | 97,490 | 1 | 194,981 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
Input
7 1 4
Output
6
Input
30 20 10
Output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4.
Submitted Solution:
```
abc = list(map(int,input().split(" ")))
a = int(abc[0])
b = int(abc[1])
c = int(abc[2])
max = max(abc)
min = min(abc)
s = a
if b < max and b > min:
s = b
elif c < max and c > min:
s = c
print(str(abs(a-s)+abs(b-s)+abs(c-s)))
``` | instruction | 0 | 97,491 | 1 | 194,982 |
Yes | output | 1 | 97,491 | 1 | 194,983 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
Input
7 1 4
Output
6
Input
30 20 10
Output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4.
Submitted Solution:
```
a = list(map(int,input().split()))
a.sort()
print(abs(a[0]-a[1])+a[2]-a[1])
``` | instruction | 0 | 97,492 | 1 | 194,984 |
Yes | output | 1 | 97,492 | 1 | 194,985 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
Input
7 1 4
Output
6
Input
30 20 10
Output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4.
Submitted Solution:
```
import sys
data = sys.stdin.readline()
x, y, z = data.split(" ")
x = int(x)
y = int(y)
z = int(z)
avg1 = abs((x - y)) + abs((x - z))
avg2 = abs((y - x)) + abs((y - z))
avg3 = abs((z - x)) + abs((z - y))
smallest = min (avg1, avg2, avg3)
sys.stdout.write (str(smallest))
``` | instruction | 0 | 97,493 | 1 | 194,986 |
Yes | output | 1 | 97,493 | 1 | 194,987 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
Input
7 1 4
Output
6
Input
30 20 10
Output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4.
Submitted Solution:
```
l=list(map(int,input().split(' ')))
l.sort()
mid=(l[0] + l[2])//2
print(abs(mid-l[0]) + abs(mid-l[2]) + abs(mid-l[1]))
``` | instruction | 0 | 97,494 | 1 | 194,988 |
No | output | 1 | 97,494 | 1 | 194,989 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
Input
7 1 4
Output
6
Input
30 20 10
Output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4.
Submitted Solution:
```
x = list(map(int, input().split()))
x.sort()
total = 0
total+= x[2] - x[1]
total+= x[1] - x[0]
``` | instruction | 0 | 97,495 | 1 | 194,990 |
No | output | 1 | 97,495 | 1 | 194,991 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
Input
7 1 4
Output
6
Input
30 20 10
Output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4.
Submitted Solution:
```
li=sorted(map(int,input().split()))
print(li)
x=(li[2]-li[1])+(li[1]-li[0])
print(x)
``` | instruction | 0 | 97,496 | 1 | 194,992 |
No | output | 1 | 97,496 | 1 | 194,993 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
Input
7 1 4
Output
6
Input
30 20 10
Output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4.
Submitted Solution:
```
x1, x2, x3 = map(int, input().split())
mini = min(x1, x2, x3)
maxi = max(x1, x2, x3)
m = (mini+maxi)//2
ans = abs(x1-m)+abs(x2-m)+abs(x3-m)
print(ans)
``` | instruction | 0 | 97,497 | 1 | 194,994 |
No | output | 1 | 97,497 | 1 | 194,995 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct.
It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates.
It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs.
Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance.
Input
The first line contains one integer number n (2 ≤ n ≤ 2·105).
The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct.
Output
Print two integer numbers — the minimal distance and the quantity of pairs with this distance.
Examples
Input
4
6 -3 0 4
Output
2 1
Input
3
-2 0 2
Output
2 2
Note
In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance. | instruction | 0 | 97,514 | 1 | 195,028 |
Tags: implementation, sortings
Correct Solution:
```
import math
n=int(input())
li=[int(x) for x in input().split()]
li.sort()
diff=math.inf
c=0
for i in range(n-1):
temp=li[i+1]-li[i]
if temp==diff:
c+=1
elif temp<diff:
diff = temp
c=1
print(diff,c)
``` | output | 1 | 97,514 | 1 | 195,029 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct.
It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates.
It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs.
Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance.
Input
The first line contains one integer number n (2 ≤ n ≤ 2·105).
The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct.
Output
Print two integer numbers — the minimal distance and the quantity of pairs with this distance.
Examples
Input
4
6 -3 0 4
Output
2 1
Input
3
-2 0 2
Output
2 2
Note
In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance. | instruction | 0 | 97,515 | 1 | 195,030 |
Tags: implementation, sortings
Correct Solution:
```
n=int(input())
m=sorted(map(int,input().split()))
d = [m[i]-m[i-1] for i in range(1,n)]
a=min(d)
print(a, d.count(a))
``` | output | 1 | 97,515 | 1 | 195,031 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct.
It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates.
It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs.
Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance.
Input
The first line contains one integer number n (2 ≤ n ≤ 2·105).
The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct.
Output
Print two integer numbers — the minimal distance and the quantity of pairs with this distance.
Examples
Input
4
6 -3 0 4
Output
2 1
Input
3
-2 0 2
Output
2 2
Note
In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance. | instruction | 0 | 97,516 | 1 | 195,032 |
Tags: implementation, sortings
Correct Solution:
```
n = int(input())
cities = sorted(map(int, input().split()))
mn = float("inf")
count = 0
for i in range(1, n):
dist = cities[i] - cities[i - 1]
if dist < mn:
mn = dist
count = 1
elif dist == mn:
count += 1
print(mn, count)
``` | output | 1 | 97,516 | 1 | 195,033 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct.
It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates.
It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs.
Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance.
Input
The first line contains one integer number n (2 ≤ n ≤ 2·105).
The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct.
Output
Print two integer numbers — the minimal distance and the quantity of pairs with this distance.
Examples
Input
4
6 -3 0 4
Output
2 1
Input
3
-2 0 2
Output
2 2
Note
In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance. | instruction | 0 | 97,517 | 1 | 195,034 |
Tags: implementation, sortings
Correct Solution:
```
#!/usr/bin/python3
# Copyright (C) 2017 Sayutin Dmitry.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 3
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.
def main():
input()
a = list(map(int, input().split()))
a.sort()
mn, cnt = 10 ** 18, 0
for i in range(1, len(a)):
f = a[i] - a[i - 1]
if f == mn:
cnt += 1
if f < mn:
mn, cnt = f, 1
print(mn, cnt)
main()
``` | output | 1 | 97,517 | 1 | 195,035 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct.
It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates.
It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs.
Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance.
Input
The first line contains one integer number n (2 ≤ n ≤ 2·105).
The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct.
Output
Print two integer numbers — the minimal distance and the quantity of pairs with this distance.
Examples
Input
4
6 -3 0 4
Output
2 1
Input
3
-2 0 2
Output
2 2
Note
In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance. | instruction | 0 | 97,518 | 1 | 195,036 |
Tags: implementation, sortings
Correct Solution:
```
n=int(input())
a=[int(x) for x in input().split()]
a=sorted(a)
m=1<<31
i=1
#print(a)
for j in range(n-1):
d=abs(a[j]-a[j+1])
if d<m:
m=d
i=1
elif d==m:
i+=1
print(m,i)
``` | output | 1 | 97,518 | 1 | 195,037 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct.
It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates.
It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs.
Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance.
Input
The first line contains one integer number n (2 ≤ n ≤ 2·105).
The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct.
Output
Print two integer numbers — the minimal distance and the quantity of pairs with this distance.
Examples
Input
4
6 -3 0 4
Output
2 1
Input
3
-2 0 2
Output
2 2
Note
In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance. | instruction | 0 | 97,519 | 1 | 195,038 |
Tags: implementation, sortings
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
a.sort()
c = 0
m = 2000000005
f = 0
for i in range(n - 1):
if abs(a[i] - a[i+1]) < m:
m = abs(a[i] - a[i+1])
c = 1
elif abs(a[i] - a[i+1]) == m:
c += 1
else:
continue
print(m, c)
``` | output | 1 | 97,519 | 1 | 195,039 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct.
It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates.
It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs.
Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance.
Input
The first line contains one integer number n (2 ≤ n ≤ 2·105).
The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct.
Output
Print two integer numbers — the minimal distance and the quantity of pairs with this distance.
Examples
Input
4
6 -3 0 4
Output
2 1
Input
3
-2 0 2
Output
2 2
Note
In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance. | instruction | 0 | 97,520 | 1 | 195,040 |
Tags: implementation, sortings
Correct Solution:
```
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
#from fractions import gcd
#from itertools import combinations # (string,3) 3回
#from collections import deque
from collections import defaultdict
#import bisect
#
# d = m - k[i] - k[j]
# if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも!!
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
return list(map(int,input().split()))
def I():
return int(input())
n = I()
dic = defaultdict(int)
A = readInts()
A = sorted(A)
MIN = float('inf')
for i in range(n-1):
m = abs(A[i] - A[i+1])
dic[m] += 1
if MIN > m:
MIN = m
print(MIN,dic[MIN])
``` | output | 1 | 97,520 | 1 | 195,041 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.