message stringlengths 2 22.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 16 109k | cluster float64 1 1 | __index_level_0__ int64 32 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid β for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai β b for every 1 β€ i β€ k) and choose a storage in some city s (s = aj for some 1 β€ j β€ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ n) β the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 β€ u, v β€ n, 1 β€ l β€ 109, u β v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 β€ ai β€ n) β the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Output
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
Examples
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note
<image>
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened. | instruction | 0 | 82,210 | 1 | 164,420 |
Tags: graphs
Correct Solution:
```
def solve(roads):
if roads:
min_way = min(roads)
return min_way + 1
return '-1'
def roads_backery2city(backery, all_roads):
roads = []
for u, v, w in all_roads:
if backery[u] and not backery[v] or backery[v] and not backery[u]:
roads.append(w)
return roads
n, m, k = map(int, input().split())
if k == 0:
print('-1')
else:
all_roads = [map(lambda x: int(x)-1, input().split()) for _ in range(m)]
backery = [False] * n
for i in map(lambda x: int(x)-1, input().split()):
backery[i] = True
roads = roads_backery2city(backery, all_roads)
print(solve(roads))
``` | output | 1 | 82,210 | 1 | 164,421 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid β for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai β b for every 1 β€ i β€ k) and choose a storage in some city s (s = aj for some 1 β€ j β€ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ n) β the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 β€ u, v β€ n, 1 β€ l β€ 109, u β v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 β€ ai β€ n) β the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Output
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
Examples
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note
<image>
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened. | instruction | 0 | 82,211 | 1 | 164,422 |
Tags: graphs
Correct Solution:
```
from sys import stdin,stdout
n,m,k=[int(x) for x in stdin.readline().split()]
d={}
for i in range(m):
u,v,l=[int(x) for x in stdin.readline().split()]
if u not in d:
d[u]={v:l}
elif v not in d[u]:
d[u][v]=l
else:
if d[u][v]>l:
d[u][v]=l
if v not in d:
d[v]={u:l}
elif u not in d[v]:
d[v][u]=l
else:
if d[v][u]>l:
d[v][u]=l
if k>0:
store=[int(x) for x in stdin.readline().split()]
store=set(store)
if k==0:
stdout.write("-1")
else:
ans=float('inf')
# for i in range(k):
# if store[i] in d:
# for j in d[store[i]]:
# if j not in st:
# ans=min(d[i][j],ans)
for i in store:
if i in d:
for j in d[i]:
if j not in store:
ans=min(d[i][j],ans)
if ans==float("inf"):
stdout.write("-1")
else:
stdout.write(str(ans))
``` | output | 1 | 82,211 | 1 | 164,423 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid β for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai β b for every 1 β€ i β€ k) and choose a storage in some city s (s = aj for some 1 β€ j β€ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ n) β the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 β€ u, v β€ n, 1 β€ l β€ 109, u β v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 β€ ai β€ n) β the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Output
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
Examples
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note
<image>
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened. | instruction | 0 | 82,212 | 1 | 164,424 |
Tags: graphs
Correct Solution:
```
n, m, k = map(int, input().split())
if k == 0:
print(-1)
quit()
edge = []
for _ in range(m):
u, v, l = map(int, input().split())
edge.append((u, v, l))
a = list(map(int, input().split()))
tr = [True]*(n+1)
for i in a:
tr[i] = False
ans = float("+inf")
for i in edge:
u, v, l = i
if tr[u]^tr[v]:
ans = min(ans, l)
if ans == float("+inf"):
print("-1")
else:
print(ans)
``` | output | 1 | 82,212 | 1 | 164,425 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid β for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai β b for every 1 β€ i β€ k) and choose a storage in some city s (s = aj for some 1 β€ j β€ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ n) β the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 β€ u, v β€ n, 1 β€ l β€ 109, u β v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 β€ ai β€ n) β the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Output
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
Examples
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note
<image>
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened. | instruction | 0 | 82,213 | 1 | 164,426 |
Tags: graphs
Correct Solution:
```
n, m, k = map(int, input().split())
g = [[] for i in range(n)]
for i in range(m):
s, v, w = map(int, input().split())
s, v = s - 1, v - 1
g[s].append((v, w))
g[v].append((s, w))
if not k:
print(-1)
exit()
stores = {num - 1 for num in map(int, input().split())}
minw = float('inf')
for s in stores:
for v, w in g[s]:
if v not in stores:
minw = min(minw, w)
print(minw if minw != float('inf') else -1)
``` | output | 1 | 82,213 | 1 | 164,427 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid β for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai β b for every 1 β€ i β€ k) and choose a storage in some city s (s = aj for some 1 β€ j β€ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ n) β the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 β€ u, v β€ n, 1 β€ l β€ 109, u β v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 β€ ai β€ n) β the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Output
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
Examples
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note
<image>
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened. | instruction | 0 | 82,214 | 1 | 164,428 |
Tags: graphs
Correct Solution:
```
import math
n, m, k = input().split(' ')
n = int(n)
k = int(k)
m = int(m)
e = {}
for _ in range(m):
a, b, c = input().split(' ')
a = int(a)
b = int(b)
c = int(c)
x = e.get(a, [])
x.append((b, c))
e[a] = x
x = e.get(b, [])
x.append((a, c))
e[b] = x
d = math.inf
#print(e)
if k > 0:
c = set()
for i in input().split(' '):
c.add(int(i))
#print(c)
for i in c:
if i in e.keys():
for j in e[i]:
if j[0] not in c:
d = min(d, j[1])
print(d if d != math.inf else -1)
else:
print(-1)
``` | output | 1 | 82,214 | 1 | 164,429 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid β for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai β b for every 1 β€ i β€ k) and choose a storage in some city s (s = aj for some 1 β€ j β€ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ n) β the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 β€ u, v β€ n, 1 β€ l β€ 109, u β v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 β€ ai β€ n) β the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Output
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
Examples
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note
<image>
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened. | instruction | 0 | 82,215 | 1 | 164,430 |
Tags: graphs
Correct Solution:
```
import sys
n, m, k = map(int, input().split())
r = [input().split() for j in range(m)]
a, v = set(),sys.maxsize
if k:
a.update(input().split())
for ri in r:
if (ri[0] in a) ^ (ri[1] in a):
v = min(v, int(ri[2]))
print(-1 if v == sys.maxsize else v)
``` | output | 1 | 82,215 | 1 | 164,431 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid β for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai β b for every 1 β€ i β€ k) and choose a storage in some city s (s = aj for some 1 β€ j β€ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ n) β the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 β€ u, v β€ n, 1 β€ l β€ 109, u β v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 β€ ai β€ n) β the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Output
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
Examples
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note
<image>
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened. | instruction | 0 | 82,216 | 1 | 164,432 |
Tags: graphs
Correct Solution:
```
n, m, k = map(int, input(). split(' '))
a = [[] for i in range(n)]
for i in range(m):
u, v, l = map(int, input().split(' '))
a[u - 1].append([v - 1, l])
a[v - 1].append([u - 1, l])
if k != 0:
cklad = set(list(map(lambda x: int(x) - 1, input().split(' '))))
min1 = -1
for i in cklad:
for j in range(len(a[i])):
if a[i][j][0] not in cklad:
if min1 > a[i][j][1] or min1 == -1:
min1 = a[i][j][1]
print(min1)
else:
print(-1)
``` | output | 1 | 82,216 | 1 | 164,433 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid β for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai β b for every 1 β€ i β€ k) and choose a storage in some city s (s = aj for some 1 β€ j β€ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ n) β the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 β€ u, v β€ n, 1 β€ l β€ 109, u β v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 β€ ai β€ n) β the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Output
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
Examples
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note
<image>
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened. | instruction | 0 | 82,217 | 1 | 164,434 |
Tags: graphs
Correct Solution:
```
def bakery():
inp = list(map(int, input().split(" ")))
citiesCount, roadsCount, bakeriesCount = inp[0], inp[1], inp[2]
roads = []
bkrs = []
result = 0
for i in range(0, roadsCount):
roads.append(list(map(int, input().split(" "))))
if bakeriesCount != 0:
bkrs = list(map(int, input().split(" ")))
else:
print(-1)
return
bakeries = [0 for i in range(0, citiesCount)]
for i in range(0, len(bkrs)):
bakeries[bkrs[i] - 1] = bkrs[i]
for i in range(0, roadsCount):
if (roads[i][0] == bakeries[roads[i][0] - 1] and roads[i][1] != bakeries[roads[i][1] - 1]) or (roads[i][0] != bakeries[roads[i][0] - 1] and roads[i][1] == bakeries[roads[i][1] - 1]):
if result > roads[i][2] or result == 0:
result = roads[i][2]
if result != 0:
print(result)
else:
print(-1)
return
bakery()
# Mon Nov 25 2019 16:00:06 GMT+0300 (MSK)
``` | output | 1 | 82,217 | 1 | 164,435 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid β for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai β b for every 1 β€ i β€ k) and choose a storage in some city s (s = aj for some 1 β€ j β€ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ n) β the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 β€ u, v β€ n, 1 β€ l β€ 109, u β v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 β€ ai β€ n) β the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Output
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
Examples
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note
<image>
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.
Submitted Solution:
```
n,m,k = map(int, input().split())
edge = []
for i in range(m):
a,b,c = map(int, input().split())
edge.append([a,b,c])
if k!=0:
flour = [0]*(n+1)
for i in list(map(int, input().split())):
flour[i] = 1
# print(flour)
ans = 10**18
for e in edge:
# print(e)
if flour[e[0]] != flour[e[1]]:
ans = min(ans, e[2])
if ans == 10**18:
print(-1)
else:
print(ans)
else:
print(-1)
"""
3 7 0
1 3 9
1 2 5
1 2 21
1 2 12
1 2 13
2 3 19
2 3 8
"""
``` | instruction | 0 | 82,218 | 1 | 164,436 |
Yes | output | 1 | 82,218 | 1 | 164,437 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid β for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai β b for every 1 β€ i β€ k) and choose a storage in some city s (s = aj for some 1 β€ j β€ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ n) β the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 β€ u, v β€ n, 1 β€ l β€ 109, u β v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 β€ ai β€ n) β the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Output
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
Examples
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note
<image>
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.
Submitted Solution:
```
n,m,k= map(int,input().split())
arr = []
for _ in range(m):
arr.append(list(map(int,input().split())))
if k>0:
flours = list(map(int,input().split()))
mp={}
for i in flours:
mp[i]=0
if k==0:
print(-1)
else:
ans = []
for i in range(m):
if arr[i][0] in mp and arr[i][1] not in mp:
ans.append(arr[i][2])
if arr[i][1] in mp and arr[i][0] not in mp:
ans.append(arr[i][2])
if len(ans)==0:
print(-1)
else:
print(min(ans))
``` | instruction | 0 | 82,219 | 1 | 164,438 |
Yes | output | 1 | 82,219 | 1 | 164,439 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid β for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai β b for every 1 β€ i β€ k) and choose a storage in some city s (s = aj for some 1 β€ j β€ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ n) β the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 β€ u, v β€ n, 1 β€ l β€ 109, u β v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 β€ ai β€ n) β the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Output
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
Examples
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note
<image>
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.
Submitted Solution:
```
import sys
import io
import os
total = 0
failed = 0
def debug(*args):
if('LOCALTEST' in os.environ):
print(*args, file=sys.stderr)
def run(test,res):
x = io.StringIO()
with io.StringIO(test) as sys.stdin:
with x as sys.stdout:
work()
z = x.getvalue().strip()
sys.stdout = sys.__stdout__
print("Passed?", z == res)
print("Expected: ", res)
print("Actual : ", z)
global total, failed
total += 1
failed += 1 if (z != res) else 0
def work():
n, m, k = map(int, input().split())
if k == 0:
print(-1)
return
roads = []
for i in range(m):
roads.append(map(int, input().split()))
a = set(map(int, input().split()))
ok = [x in a for x in range(n+1)]
min = -1
for r in roads:
u, m, l = r
if ok[u] != ok[m] and (l<min or min == -1):
min = l
print(min)
def test():
run("""5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5""", """3""")
run("""3 7 0
1 3 9
1 2 5
1 2 21
1 2 12
1 2 13
2 3 19
2 3 8""", """-1""")
run("""3 1 1
1 2 3
3""", """-1""")
if('LOCALTEST' in os.environ):
test()
print("\n Result: %s (%d total, %d failed)" % ("FAILED" if (failed>0) else "PASSED", total, failed))
else:
work()
``` | instruction | 0 | 82,220 | 1 | 164,440 |
Yes | output | 1 | 82,220 | 1 | 164,441 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid β for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai β b for every 1 β€ i β€ k) and choose a storage in some city s (s = aj for some 1 β€ j β€ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ n) β the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 β€ u, v β€ n, 1 β€ l β€ 109, u β v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 β€ ai β€ n) β the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Output
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
Examples
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note
<image>
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.
Submitted Solution:
```
n, m, k = map(int, input().split())
roads = [input().split() for i in range(m)]
max_dist = 10 ** 9 + 1
minDis = max_dist
storage = set()
if k != 0:
storage.update(input().split())
for r in roads:
if (r[0] in storage) ^ (r[1] in storage):
minDis = min(minDis, int(r[2]))
if minDis < max_dist:
print(minDis)
else:
print(-1)
``` | instruction | 0 | 82,221 | 1 | 164,442 |
Yes | output | 1 | 82,221 | 1 | 164,443 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid β for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai β b for every 1 β€ i β€ k) and choose a storage in some city s (s = aj for some 1 β€ j β€ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ n) β the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 β€ u, v β€ n, 1 β€ l β€ 109, u β v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 β€ ai β€ n) β the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Output
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
Examples
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note
<image>
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.
Submitted Solution:
```
import queue, math
from itertools import repeat
def main():
n, m, k = input().split()
if(n == k or k == '0'): return -1
G = [[] for i in repeat(None, int(n) + 1)]
for i in range(int(m)):
line = list(map(int, input().split()))
G[line[0]].append((line[1], line[2]))
G[line[1]].append((line[0], line[2]))
K = list(map(int, input().split()))
ans = math.inf
for i in K:
for l in G[i]:
if(ans > l[1]):ans = l[1]
return (ans if ans != math.inf else -1)
print(main())
``` | instruction | 0 | 82,222 | 1 | 164,444 |
No | output | 1 | 82,222 | 1 | 164,445 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid β for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai β b for every 1 β€ i β€ k) and choose a storage in some city s (s = aj for some 1 β€ j β€ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ n) β the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 β€ u, v β€ n, 1 β€ l β€ 109, u β v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 β€ ai β€ n) β the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Output
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
Examples
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note
<image>
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.
Submitted Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
# = input()
# = int(input())
#() = (i for i in input().split())
# = [i for i in input().split()]
(n, m, k) = (int(i) for i in input().split())
ways = {i : float('Inf') for i in range(1, n+1)}
for i in range(m):
(u, v, l) = (int(i) for i in input().split())
if ways[u] > l:
ways[u] =l
if ways[v] > l:
ways[v] = l
ans = float('Inf')
if k!=0:
a = [int(i) for i in input().split()]
ans = min([ways[i] for i in a])
if ans == float('Inf'):
print(-1)
else:
print(ans)
finish = time.time()
#print(finish - start)
``` | instruction | 0 | 82,223 | 1 | 164,446 |
No | output | 1 | 82,223 | 1 | 164,447 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid β for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai β b for every 1 β€ i β€ k) and choose a storage in some city s (s = aj for some 1 β€ j β€ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ n) β the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 β€ u, v β€ n, 1 β€ l β€ 109, u β v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 β€ ai β€ n) β the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Output
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
Examples
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note
<image>
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.
Submitted Solution:
```
from collections import defaultdict
INFINITY = 1000000001
def solve(backery, n, graph):
min_way = INFINITY
for u in backery:
for w in graph[u]:
if w < min_way:
min_way = w
return '-1' if min_way == INFINITY else min_way + 1
def construct_graph(n, edges):
graph = defaultdict(list)
for u, v, w in edges:
graph[u].append(w)
graph[v].append(w)
return graph
n, m, k = map(int, input().split())
if k == 0:
print('-1')
else:
edges = [map(lambda x: int(x)-1, input().split()) for _ in range(m)]
backery = set(map(lambda x: int(x)-1, input().split()))
graph = construct_graph(n, edges)
print(solve(backery, n, graph))
``` | instruction | 0 | 82,224 | 1 | 164,448 |
No | output | 1 | 82,224 | 1 | 164,449 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid β for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai β b for every 1 β€ i β€ k) and choose a storage in some city s (s = aj for some 1 β€ j β€ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 105, 0 β€ k β€ n) β the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then m lines follow. Each of them contains three integers u, v and l (1 β€ u, v β€ n, 1 β€ l β€ 109, u β v) meaning that there is a road between cities u and v of length of l kilometers .
If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 β€ ai β€ n) β the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.
Output
Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the n cities, print - 1 in the only line.
Examples
Input
5 4 2
1 2 5
1 2 3
2 3 4
1 4 10
1 5
Output
3
Input
3 1 1
1 2 3
3
Output
-1
Note
<image>
Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened.
Submitted Solution:
```
n, m, k = map(int, input().split())
p = dict()
for i in range(m):
u, v, l = map(int, input().split())
if u in p:
if v in p[u]:
if p[u][v] > l:
p[u][v] = l
else:
p[u][v] = l
else:
p[u] = dict()
p[u][v] = l
if v in p:
if u in p[v]:
if p[v][u] > l:
p[v][u] = l
else:
p[v][u] = l
else:
p[v] = dict()
p[v][u] = l
if k > 0:
a = list(map(int, input().split()))
result = list()
for j in a:
if j in p:
result.append(min(p[j]))
if len(result) > 0:
print(min(result))
else:
print(-1)
else:
print(-1)
``` | instruction | 0 | 82,225 | 1 | 164,450 |
No | output | 1 | 82,225 | 1 | 164,451 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often. | instruction | 0 | 82,283 | 1 | 164,566 |
Tags: implementation, math
Correct Solution:
```
from math import gcd
a,b = map(int,input().split(' '))
gcd = gcd(a,b)
a,b = a/gcd, b/gcd
if abs(a-b) <= 1: print('Equal')
elif a < b: print('Dasha')
else: print('Masha')
``` | output | 1 | 82,283 | 1 | 164,567 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often. | instruction | 0 | 82,284 | 1 | 164,568 |
Tags: implementation, math
Correct Solution:
```
from math import gcd
a, b = (int(x) for x in input().split())
upper = a * b / gcd(a,b)
m, d = upper/b, upper/a
if a < b: d -= 1
else: m -= 1
if d > m: print("Dasha")
elif m > d: print("Masha")
else: print("Equal")
``` | output | 1 | 82,284 | 1 | 164,569 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often. | instruction | 0 | 82,285 | 1 | 164,570 |
Tags: implementation, math
Correct Solution:
```
a,b=map(int,input().split())
d=a
f=b
def gcd(a,b):
if a == 0:
return b
return gcd(b % a,a)
def lcm(a,b):
return (a*b) / gcd(a,b)
z=lcm(a,b)
#print(z)
x=(z-1)//d
y=(z-1)//f
#print(x,y)
if(a>b):
x=x+1
else:
y=y+1
#print(x,y)
if(x==y):
print("Equal")
elif(x>y):
print("Dasha")
else:
print("Masha")
``` | output | 1 | 82,285 | 1 | 164,571 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often. | instruction | 0 | 82,286 | 1 | 164,572 |
Tags: implementation, math
Correct Solution:
```
import math
import sys
input=sys.stdin.readline
a,b=list(map(int,input().split()))
x=math.gcd(a,b)
a,b=a//x,b//x
if abs(a-b)==1:
print('Equal')
elif a<b:
print('Dasha')
else:
print('Masha')
``` | output | 1 | 82,286 | 1 | 164,573 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often. | instruction | 0 | 82,287 | 1 | 164,574 |
Tags: implementation, math
Correct Solution:
```
from math import gcd
n,m=map(int,input().split())
lcm=n*m//(gcd(n,m))
a,b=lcm//n,lcm//m
# print()
if abs(a-b)<=1:
print("Equal")
elif a>b:
print("Dasha")
else:
print("Masha")
``` | output | 1 | 82,287 | 1 | 164,575 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often. | instruction | 0 | 82,288 | 1 | 164,576 |
Tags: implementation, math
Correct Solution:
```
'''input
2 3
'''
from math import gcd
def lcm(a,b):
h=gcd(a,b)
l=(a*b)//(h)
return l
d,m=map(int,input().strip().split(' '))
l=lcm(d,m)
a=l//d-1
b=l//m-1
if a<b:
a+=1
else:
b+=1
if a<b:
print("Masha")
elif a>b:
print("Dasha")
else:
print("Equal")
'''
d=5
m=3
0-3 -> m(3)
3-5 -> d(5)
5-6 -> m(6)
6-9 -> m(9)
9-10 -> d(10)
10-12 -> m(12)
12-15 -> m(15)
'''
``` | output | 1 | 82,288 | 1 | 164,577 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often. | instruction | 0 | 82,289 | 1 | 164,578 |
Tags: implementation, math
Correct Solution:
```
a, b = map(int, input().split(' '))
def gcd(x, y):
while(y):
x, y = y, x % y
return x
def lcm(x, y):
lcm = (x*y)//gcd(x,y)
return lcm
abm = lcm(a, b)
ca = abm / a
cb = abm / b
if a < b:
cb += 1
else:
ca += 1
if ca > cb:
print("Dasha")
elif cb > ca:
print("Masha")
else:
print("Equal")
``` | output | 1 | 82,289 | 1 | 164,579 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often. | instruction | 0 | 82,290 | 1 | 164,580 |
Tags: implementation, math
Correct Solution:
```
a,b = input().split(" ")
a,b = int(a),int(b)
c=a-b
if(a%c == 0 and b%c == 0):
print("Equal")
elif(a>b):
print("Masha")
else:
print("Dasha")
# Made By Mostafa_Khaled
``` | output | 1 | 82,290 | 1 | 164,581 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Submitted Solution:
```
from fractions import gcd
a, b = map(int, input().split())
a, b = a // gcd(a, b), b // gcd(a, b)
if abs(a - b) <= 1: print("Equal")
elif a < b: print("Dasha")
else: print("Masha")
``` | instruction | 0 | 82,291 | 1 | 164,582 |
Yes | output | 1 | 82,291 | 1 | 164,583 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Submitted Solution:
```
a, b = input().split()
a = int(a); b = int(b)
def gcd(a, b):
if a < b:
return gcd(b, a)
elif not a%b:
return b
return gcd(b, a%b)
lcm = a*b//gcd(a, b)
if a > b:
d_count = lcm//a
m_count = lcm//b - 1
if m_count > d_count:
print("Masha")
elif m_count == d_count:
print("Equal")
else:
print("Dasha")
else:
d_count = lcm//a - 1
m_count = lcm//b
if m_count > d_count:
print("Masha")
elif m_count == d_count:
print("Equal")
else:
print("Dasha")
``` | instruction | 0 | 82,292 | 1 | 164,584 |
Yes | output | 1 | 82,292 | 1 | 164,585 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Submitted Solution:
```
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
#from bisect import bisect_left as bl #c++ lowerbound bl(array,element)
#from bisect import bisect_right as br #c++ upperbound br(array,element)
import math
def main():
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
a,b=map(int,input().split(" "))
lcm=(a*b)//math.gcd(a,b)
if abs(lcm//a-lcm//b)<=1:
print("Equal")
elif lcm//a<lcm//b:
print("Masha")
else:
print("Dasha")
#-----------------------------BOSS-------------------------------------!
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
``` | instruction | 0 | 82,293 | 1 | 164,586 |
Yes | output | 1 | 82,293 | 1 | 164,587 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Submitted Solution:
```
import math
import sys
def solve(a, b):
g = math.gcd(a, b)
a = a // g
b = b // g
if a < b:
if a > b - 1:
return "Masha"
if a == b - 1:
return "Equal"
return "Dasha"
else: # a > b
if b > a - 1:
return "Dasha"
if b == a - 1:
return "Equal"
return "Masha"
def readinput():
n, m = map(int, sys.stdin.readline().rstrip().split(" "))
print(solve(n, m))
readinput()
``` | instruction | 0 | 82,294 | 1 | 164,588 |
Yes | output | 1 | 82,294 | 1 | 164,589 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Submitted Solution:
```
import sys
input=sys.stdin.readline
a,b=list(map(int,input().split()))
if abs(a-b)==1:
print('Equal')
elif a<b:
print('Dasha')
else:
print('Masha')
``` | instruction | 0 | 82,295 | 1 | 164,590 |
No | output | 1 | 82,295 | 1 | 164,591 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Submitted Solution:
```
a, b = map(int, input().split())
en = a * b
dasha, masha = en // a, en // b
if a > b:
dasha += 1
else:
masha += 1
if masha > dasha:
print('Masha')
elif masha < dasha:
print('Dasha')
else:
print('Equal')
``` | instruction | 0 | 82,296 | 1 | 164,592 |
No | output | 1 | 82,296 | 1 | 164,593 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Submitted Solution:
```
def factorise(n):
dic={}
while n%2==0:
if 2 in dic:
dic[2]+=1
else:
dic[2]=1
n=n/2
for i in range(3,int(n**0.5)+1):
while n%i==0:
if i in dic:
dic[i]+=1
else:
dic[i]=1
n=n/i
if n>2:
dic[n]=1
return dic
def lcm(a,b):
x =factorise(a)
y =factorise(b)
all_fac= list(set(list(x.keys())+list(y.keys())))
pro =1
for i in all_fac:
if i in x and i in y:
pro *= i**min(x[i],y[i])
elif i in x:
pro *= i**(x[i])
elif i in y:
pro *= i**(y[i])
return pro
a,b = map(int,input().split())
dasha =0
masha = 0
lcm = lcm(a,b)
if a>b:
dasha = lcm//a
masha = (lcm//b)-1
elif b>a:
dasha = (lcm//a)-1
masha = (lcm//b)
else:
print('Equal')
if dasha>masha:
print('Dasha')
elif masha>dasha:
print('Masha')
else:
print('Equal')
``` | instruction | 0 | 82,297 | 1 | 164,594 |
No | output | 1 | 82,297 | 1 | 164,595 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Submitted Solution:
```
a, b = (int(x) for x in input().split())
if abs(a-b) == 1: print('Equal')
elif a < b: print('Dasha')
else: print('Masha')
``` | instruction | 0 | 82,298 | 1 | 164,596 |
No | output | 1 | 82,298 | 1 | 164,597 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n districts in the town, the i-th district belongs to the a_i-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n-1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n-1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n-1 roads to satisfy all the conditions.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 5000) β the number of districts. The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the gang the i-th district belongs to.
It is guaranteed that the sum of n does not exceed 5000 (β n β€ 5000).
Output
For each test case, print:
* NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
* YES on the first line and n-1 roads on the next n-1 lines. Each road should be presented as a pair of integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are two districts the i-th road connects.
For each road i, the condition a[x_i] β a[y_i] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
Input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
Output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4 | instruction | 0 | 82,777 | 1 | 165,554 |
Tags: constructive algorithms, dfs and similar
Correct Solution:
```
from collections import Counter
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
n = int(input())
a = [int(x) for x in input().split()]
d = {}
c = Counter()
for i, e in enumerate(a, start=1):
c[e] += 1
if e not in d:
d[e] = []
d[e].append(i)
t = c.most_common(2)
r = []
#print(d)
while len(t) == 2 and t[1][1] > 0:
#print(t)
x = t[0][0]
y = t[1][0]
xi = d[x].pop()
yi = d[y][-1]
c[x] -= 1
r.append((xi, yi))
t = c.most_common(2)
#print(r)
#print(d)
#print("====")
if len(r) == n -1:
print("YES")
for i in range(n-1):
print(*r[i])
else:
print("NO")
``` | output | 1 | 82,777 | 1 | 165,555 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n districts in the town, the i-th district belongs to the a_i-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n-1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n-1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n-1 roads to satisfy all the conditions.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 5000) β the number of districts. The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the gang the i-th district belongs to.
It is guaranteed that the sum of n does not exceed 5000 (β n β€ 5000).
Output
For each test case, print:
* NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
* YES on the first line and n-1 roads on the next n-1 lines. Each road should be presented as a pair of integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are two districts the i-th road connects.
For each road i, the condition a[x_i] β a[y_i] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
Input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
Output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4 | instruction | 0 | 82,778 | 1 | 165,556 |
Tags: constructive algorithms, dfs and similar
Correct Solution:
```
for t in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
if len(set(a)) == 1:
print("NO")
else:
print("YES")
ind1 = 0
a1 = a[0]
for i in range(1, n):
if a[i] != a1:
ind2 = i
a2 = a[i]
break
for i in range(1, n):
if a[i] != a1:
print(1, i + 1)
else:
print(i + 1, ind2 + 1)
``` | output | 1 | 82,778 | 1 | 165,557 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n districts in the town, the i-th district belongs to the a_i-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n-1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n-1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n-1 roads to satisfy all the conditions.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 5000) β the number of districts. The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the gang the i-th district belongs to.
It is guaranteed that the sum of n does not exceed 5000 (β n β€ 5000).
Output
For each test case, print:
* NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
* YES on the first line and n-1 roads on the next n-1 lines. Each road should be presented as a pair of integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are two districts the i-th road connects.
For each road i, the condition a[x_i] β a[y_i] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
Input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
Output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4 | instruction | 0 | 82,779 | 1 | 165,558 |
Tags: constructive algorithms, dfs and similar
Correct Solution:
```
import sys
import math
from collections import defaultdict,Counter
# input=sys.stdin.readline
# def print(x):
# sys.stdout.write(str(x)+"\n")
# sys.stdout=open("CP1/output.txt",'w')
# sys.stdin=open("CP1/input.txt",'r')
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# mod=pow(10,9)+7
t=int(input())
for i in range(t):
n=int(input())
a=list(map(int,input().split()))
ind=0
for j in range(1,n):
if a[j]!=a[0]:
ind=j+1
break
if ind==0:
print("NO")
else:
print("YES")
for j in range(1,n):
if a[j]!=a[0]:
print(1,j+1)
# l.append([1,j+1])
else:
print(ind,j+1)
# l.append([ind,j+1])
# for j in l:
# print(*j)
``` | output | 1 | 82,779 | 1 | 165,559 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n districts in the town, the i-th district belongs to the a_i-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n-1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n-1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n-1 roads to satisfy all the conditions.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 5000) β the number of districts. The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the gang the i-th district belongs to.
It is guaranteed that the sum of n does not exceed 5000 (β n β€ 5000).
Output
For each test case, print:
* NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
* YES on the first line and n-1 roads on the next n-1 lines. Each road should be presented as a pair of integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are two districts the i-th road connects.
For each road i, the condition a[x_i] β a[y_i] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
Input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
Output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4 | instruction | 0 | 82,780 | 1 | 165,560 |
Tags: constructive algorithms, dfs and similar
Correct Solution:
```
from sys import stdin,stdout
from collections import defaultdict
ans = []
def main():
n = int(stdin.readline())
arr = list(map(int,stdin.readline().split()))
dictt = defaultdict(list)
semians = []
for i in range(n):
dictt[arr[i]].append(i+1)
if len(dictt) == 1:
ans.append('NO')
return
ans.append('YES')
keys = list(dictt.keys())
head = dictt[keys[0]][0]
for key in keys[1:]:
for i in dictt[key]:
semians.append(str(head) +' '+ str(i))
if len(dictt[keys[0]]) > 1:
for i in dictt[keys[0]][1:]:
semians.append(str(dictt[keys[1]][0]) + ' ' + str(i) )
ans.append('\n'.join(semians))
if __name__== '__main__':
for i in range(int(stdin.readline())):
main()
stdout.write('\n'.join(ans))
``` | output | 1 | 82,780 | 1 | 165,561 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n districts in the town, the i-th district belongs to the a_i-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n-1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n-1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n-1 roads to satisfy all the conditions.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 5000) β the number of districts. The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the gang the i-th district belongs to.
It is guaranteed that the sum of n does not exceed 5000 (β n β€ 5000).
Output
For each test case, print:
* NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
* YES on the first line and n-1 roads on the next n-1 lines. Each road should be presented as a pair of integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are two districts the i-th road connects.
For each road i, the condition a[x_i] β a[y_i] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
Input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
Output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4 | instruction | 0 | 82,781 | 1 | 165,562 |
Tags: constructive algorithms, dfs and similar
Correct Solution:
```
for _ in range(int(input()) if True else 1):
n = int(input())
a = list(map(int, input().split()))
if len(set(a)) == 1:
print("NO")
continue
print("YES")
p = list(set(a))
i1 = a.index(p[0])
i2 = a.index(p[1])
for i in range(n):
if a[i] == p[0]:
if i != i1:
print(i+1, i2+1)
else:
print(i+1, i1+1)
``` | output | 1 | 82,781 | 1 | 165,563 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n districts in the town, the i-th district belongs to the a_i-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n-1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n-1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n-1 roads to satisfy all the conditions.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 5000) β the number of districts. The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the gang the i-th district belongs to.
It is guaranteed that the sum of n does not exceed 5000 (β n β€ 5000).
Output
For each test case, print:
* NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
* YES on the first line and n-1 roads on the next n-1 lines. Each road should be presented as a pair of integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are two districts the i-th road connects.
For each road i, the condition a[x_i] β a[y_i] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
Input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
Output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4 | instruction | 0 | 82,782 | 1 | 165,564 |
Tags: constructive algorithms, dfs and similar
Correct Solution:
```
def district_connect(arr):
if len(set(arr))==1:
print("NO")
return []
l = []
k = arr[0]
for i in arr:
if i!=k:
index = arr.index(i)
break
for i in range(1,len(arr)):
if arr[i]!=k:
l.append([1,i+1])
else:
l.append([i+1,index+1])
print("YES")
return l
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int,input().split()))
result = district_connect(arr)
for i in result:
print(i[0],i[1])
``` | output | 1 | 82,782 | 1 | 165,565 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n districts in the town, the i-th district belongs to the a_i-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n-1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n-1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n-1 roads to satisfy all the conditions.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 5000) β the number of districts. The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the gang the i-th district belongs to.
It is guaranteed that the sum of n does not exceed 5000 (β n β€ 5000).
Output
For each test case, print:
* NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
* YES on the first line and n-1 roads on the next n-1 lines. Each road should be presented as a pair of integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are two districts the i-th road connects.
For each road i, the condition a[x_i] β a[y_i] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
Input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
Output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4 | instruction | 0 | 82,783 | 1 | 165,566 |
Tags: constructive algorithms, dfs and similar
Correct Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 21 01:51:10 2020
@author: Dark Soul
"""
t=int(input(''))
for _ in range(t):
n=int(input(''))
arr=list(map(int,input().split()))
sol=[]
for i in range(n):
sol.append([arr[i],i+1])
sol.sort()
if sol[0][0]==sol[n-1][0]:
print('NO')
continue
print('YES')
ans=[]
x=sol[0][0]
ind=-1
for i in range(n-1):
if sol[i][0]!=sol[n-1][0]:
print(sol[i][1],sol[n-1][1])
for i in range(n-2,-1,-1):
if sol[i][0]==sol[n-1][0]:
print(sol[i][1],sol[0][1])
``` | output | 1 | 82,783 | 1 | 165,567 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n districts in the town, the i-th district belongs to the a_i-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n-1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n-1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n-1 roads to satisfy all the conditions.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 5000) β the number of districts. The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the gang the i-th district belongs to.
It is guaranteed that the sum of n does not exceed 5000 (β n β€ 5000).
Output
For each test case, print:
* NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
* YES on the first line and n-1 roads on the next n-1 lines. Each road should be presented as a pair of integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are two districts the i-th road connects.
For each road i, the condition a[x_i] β a[y_i] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
Input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
Output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4 | instruction | 0 | 82,784 | 1 | 165,568 |
Tags: constructive algorithms, dfs and similar
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
c = {}
for j, i in enumerate(a):
c[i] = c.get(i, []) + [j+1]
x = list(c.values())
if len(x) == 1:
print('NO')
continue
print('YES')
s = x[0].pop(0)
for i in x[1]:
print(s, i)
for i in range(len(x)):
if i == 1:
continue
for j in x[i]:
print(x[1][0], j)
``` | output | 1 | 82,784 | 1 | 165,569 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n districts in the town, the i-th district belongs to the a_i-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n-1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n-1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n-1 roads to satisfy all the conditions.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 5000) β the number of districts. The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the gang the i-th district belongs to.
It is guaranteed that the sum of n does not exceed 5000 (β n β€ 5000).
Output
For each test case, print:
* NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
* YES on the first line and n-1 roads on the next n-1 lines. Each road should be presented as a pair of integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are two districts the i-th road connects.
For each road i, the condition a[x_i] β a[y_i] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
Input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
Output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4
Submitted Solution:
```
from collections import Counter
t = int(input())
for _ in range(t):
count = int(input()) - 1
A = [int(a) for a in input().split()]
availables = set(A)
counter = Counter(A)
if len(availables) == 1:
print('NO')
else:
print('YES')
first, j = A[0], 0
second, i = A[0], 0
while second == first:
i += 1
second = A[i]
for k, a in enumerate(A):
if a != first:
print(j + 1, k + 1)
occured = False
for k, a in enumerate(A):
if not occured and a == first:
occured = True
elif occured and a == first:
print(i + 1, k + 1)
``` | instruction | 0 | 82,785 | 1 | 165,570 |
Yes | output | 1 | 82,785 | 1 | 165,571 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n districts in the town, the i-th district belongs to the a_i-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n-1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n-1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n-1 roads to satisfy all the conditions.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 5000) β the number of districts. The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the gang the i-th district belongs to.
It is guaranteed that the sum of n does not exceed 5000 (β n β€ 5000).
Output
For each test case, print:
* NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
* YES on the first line and n-1 roads on the next n-1 lines. Each road should be presented as a pair of integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are two districts the i-th road connects.
For each road i, the condition a[x_i] β a[y_i] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
Input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
Output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4
Submitted Solution:
```
import sys
import bisect as bi
import collections as cc
import heapq as hp
input=sys.stdin.readline
I=lambda:list(map(int,input().split()))
ar=[]
for i in range(1,10):
j=str(i)
while int(j)<=10000:
ar.append(int(j))
j+=str(i)
for tc in range(int(input())):
n,=I()
l=I()
if len(set(l))==1:
print("NO")
continue
else:
ans=[]
prev=-1
to=[]
for i in range(1,n):
if l[i]!=l[0]:
ans.append([i+1,1])
prev=i+1
else:
to.append(i+1)
for j in to:
ans.append([j,prev])
print("YES")
for i in ans:
print(*i)
``` | instruction | 0 | 82,786 | 1 | 165,572 |
Yes | output | 1 | 82,786 | 1 | 165,573 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n districts in the town, the i-th district belongs to the a_i-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n-1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n-1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n-1 roads to satisfy all the conditions.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 5000) β the number of districts. The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the gang the i-th district belongs to.
It is guaranteed that the sum of n does not exceed 5000 (β n β€ 5000).
Output
For each test case, print:
* NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
* YES on the first line and n-1 roads on the next n-1 lines. Each road should be presented as a pair of integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are two districts the i-th road connects.
For each road i, the condition a[x_i] β a[y_i] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
Input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
Output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4
Submitted Solution:
```
#print(" ".join(map(str,array)))
t = int(input())
for _ in range(t):
n = int(input())
a = [int(s) for s in input().split(" ")]
first = a[0]
no = True
for el in a:
if el != first:
no = False
if no:
print("NO")
else:
print("YES")
center = a[0]
#dct_center = set()
for i in range(n):
if a[i] != center:
tail = i+1
break
for i in range(1, n):
if a[i] != center:
print(1, i+1)
else:
print(tail, i+1)
``` | instruction | 0 | 82,787 | 1 | 165,574 |
Yes | output | 1 | 82,787 | 1 | 165,575 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n districts in the town, the i-th district belongs to the a_i-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n-1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n-1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n-1 roads to satisfy all the conditions.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 5000) β the number of districts. The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the gang the i-th district belongs to.
It is guaranteed that the sum of n does not exceed 5000 (β n β€ 5000).
Output
For each test case, print:
* NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
* YES on the first line and n-1 roads on the next n-1 lines. Each road should be presented as a pair of integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are two districts the i-th road connects.
For each road i, the condition a[x_i] β a[y_i] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
Input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
Output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4
Submitted Solution:
```
# only gravity will pull me down
from collections import *
from math import *
from re import *
test = int(input())
for t in range(1,test+1) :
n = int(input())
a = list(map(int, input().split()))
s = set(a)
if(len(s)==1):
print('NO')
else:
print('YES')
x = a[0]
alt=0
for idx in range(n):
if a[idx] != x:
alt=idx+1
break
for i in range(1,n):
if(a[i] != x):
print(1, i+1)
else:
print(alt, i+1)
``` | instruction | 0 | 82,788 | 1 | 165,576 |
Yes | output | 1 | 82,788 | 1 | 165,577 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n districts in the town, the i-th district belongs to the a_i-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n-1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n-1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n-1 roads to satisfy all the conditions.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 5000) β the number of districts. The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the gang the i-th district belongs to.
It is guaranteed that the sum of n does not exceed 5000 (β n β€ 5000).
Output
For each test case, print:
* NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
* YES on the first line and n-1 roads on the next n-1 lines. Each road should be presented as a pair of integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are two districts the i-th road connects.
For each road i, the condition a[x_i] β a[y_i] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
Input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
Output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4
Submitted Solution:
```
# cook your dish here
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
if(len(list(set(a)))==1):
print("NO")
continue
val=0
for i in range(1,n):
if(a[i]!=a[0]):
print(1,i+1)
val=i+1
for i in range(1,n):
if(a[0]==a[i]):
print(i+1,val)
``` | instruction | 0 | 82,789 | 1 | 165,578 |
No | output | 1 | 82,789 | 1 | 165,579 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n districts in the town, the i-th district belongs to the a_i-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n-1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n-1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n-1 roads to satisfy all the conditions.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 5000) β the number of districts. The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the gang the i-th district belongs to.
It is guaranteed that the sum of n does not exceed 5000 (β n β€ 5000).
Output
For each test case, print:
* NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
* YES on the first line and n-1 roads on the next n-1 lines. Each road should be presented as a pair of integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are two districts the i-th road connects.
For each road i, the condition a[x_i] β a[y_i] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
Input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
Output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4
Submitted Solution:
```
t = int(input())
from collections import defaultdict
def dfs(node,visited,path):
if len(path) == n-1:
return path
visited[node] = True
for i in range(n):
if visited[i] != True and arr[i] != arr[node]:
path.append((node+1,i+1))
return dfs(i,visited,path)
from collections import defaultdict
for _ in range(t):
n = int(input())
arr = list(map(int,input().strip().split()))[:n]
flag = False;
for i in range(n):
visited = defaultdict(int)
ans = dfs(i,visited,[])
if ans is not None:
print('YES')
for a,b in ans:
print(a,b)
flag = True
break
if flag == False:
print('NO')
``` | instruction | 0 | 82,790 | 1 | 165,580 |
No | output | 1 | 82,790 | 1 | 165,581 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n districts in the town, the i-th district belongs to the a_i-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n-1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n-1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n-1 roads to satisfy all the conditions.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 5000) β the number of districts. The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the gang the i-th district belongs to.
It is guaranteed that the sum of n does not exceed 5000 (β n β€ 5000).
Output
For each test case, print:
* NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
* YES on the first line and n-1 roads on the next n-1 lines. Each road should be presented as a pair of integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are two districts the i-th road connects.
For each road i, the condition a[x_i] β a[y_i] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
Input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
Output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4
Submitted Solution:
```
import sys, collections
tc = int(sys.stdin.readline())
for _ in range(tc):
n = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
gang = dict().fromkeys(set(arr))
for i in gang:
gang[i] = []
for idx, i in enumerate(arr):
gang[i].append(idx + 1)
if len(gang.keys()) < 2:
print('NO')
else:
res = sorted(gang.items())
res.sort(key=lambda x : len(x[1]))
adj = []
for i in range(1, len(res)):
for j in res[i][1]:
adj.append([res[0][1][0], j])
for i in range(1, len(res[0][1])):
adj.append([i, res[-1][1][0]])
print('YES')
for i in adj:
print(i[0], i[1])
``` | instruction | 0 | 82,791 | 1 | 165,582 |
No | output | 1 | 82,791 | 1 | 165,583 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n districts in the town, the i-th district belongs to the a_i-th bandit gang. Initially, no districts are connected to each other.
You are the mayor of the city and want to build n-1 two-way roads to connect all districts (two districts can be connected directly or through other connected districts).
If two districts belonging to the same gang are connected directly with a road, this gang will revolt.
You don't want this so your task is to build n-1 two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build n-1 roads to satisfy all the conditions.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 500) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (2 β€ n β€ 5000) β the number of districts. The second line of the test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^9), where a_i is the gang the i-th district belongs to.
It is guaranteed that the sum of n does not exceed 5000 (β n β€ 5000).
Output
For each test case, print:
* NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.
* YES on the first line and n-1 roads on the next n-1 lines. Each road should be presented as a pair of integers x_i and y_i (1 β€ x_i, y_i β€ n; x_i β y_i), where x_i and y_i are two districts the i-th road connects.
For each road i, the condition a[x_i] β a[y_i] should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).
Example
Input
4
5
1 2 2 1 3
3
1 1 1
4
1 1000 101 1000
4
1 2 3 4
Output
YES
1 3
3 5
5 4
1 2
NO
YES
1 2
2 3
3 4
YES
1 2
1 3
1 4
Submitted Solution:
```
from collections import Counter
from collections import defaultdict
def solve(lst):
uniq = []
c = defaultdict(list)
for i, n in enumerate(lst, 1):
c[n].append(i)
it = 0
while len(uniq) != len(lst):
for k in c:
if len(c[k]) > it:
uniq.append(k)
it += 1
ans = []
for i in range(1, len(uniq)):
if uniq[i - 1] == uniq[i]:
print("NO")
return
a, b = c[uniq[i - 1]].pop(0), c[uniq[i]][0]
ans.append((a, b))
print("YES")
for a in ans:
print(*a)
t = int(input())
for _ in range(t):
input()
lst = list(map(int, input().split()))
solve(lst)
``` | instruction | 0 | 82,792 | 1 | 165,584 |
No | output | 1 | 82,792 | 1 | 165,585 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length.
We also know that the President will soon ride along the Berland roads from city s to city t. Naturally, he will choose one of the shortest paths from s to t, but nobody can say for sure which path he will choose.
The Minister for Transport is really afraid that the President might get upset by the state of the roads in the country. That is the reason he is planning to repair the roads in the possible President's path.
Making the budget for such an event is not an easy task. For all possible distinct pairs s, t (s < t) find the number of roads that lie on at least one shortest path from s to t.
Input
The first line of the input contains integers n, m (2 β€ n β€ 500, 0 β€ m β€ nΒ·(n - 1) / 2) β the number of cities and roads, correspondingly. Then m lines follow, containing the road descriptions, one description per line. Each description contains three integers xi, yi, li (1 β€ xi, yi β€ n, xi β yi, 1 β€ li β€ 106), where xi, yi are the numbers of the cities connected by the i-th road and li is its length.
Output
Print the sequence of <image> integers c12, c13, ..., c1n, c23, c24, ..., c2n, ..., cn - 1, n, where cst is the number of roads that can lie on the shortest path from s to t. Print the elements of sequence c in the described order. If the pair of cities s and t don't have a path between them, then cst = 0.
Examples
Input
5 6
1 2 1
2 3 1
3 4 1
4 1 1
2 4 2
4 5 4
Output
1 4 1 2 1 5 6 1 2 1
Submitted Solution:
```
from math import inf
def Floyd_Warshall(n: int, E: [[int]]) -> [[int]]:
min_cost = [[inf for _ in range(n)] for _ in range(n)]
for i in range(n):
min_cost[i][i] = 0
for x,y,l in E:
min_cost[x - 1][y - 1] = l
min_cost[y - 1][x - 1] = l
for i in range(n):
for j in range(n):
for k in range(n):
min_cost[i][j] = min(min_cost[i][j], min_cost[i][k] + min_cost[k][j])
return min_cost
def end_edges_count(n: int, min_cost: [[int]], E: [[int]]) -> [[int]]:
end_edges = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n):
for x, y, l in E:
if min_cost[i][x] == inf:
continue
if min_cost[i][x - 1] + l == min_cost[i][y-1]:
end_edges[i][y - 1] += 1
else:
if min_cost[i][y - 1] + l == min_cost[i][x - 1]:
end_edges[i][x - 1] += 1
return end_edges
def shortest_paths_edges_count(n: int, min_cost: [[int]], end_edges: [[int]]) -> [[int]]:
shortest_paths_edges = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n):
for j in range(n):
if min_cost[i][j] == inf:
continue
for k in range(n):
if min_cost[i][k] + min_cost[k][j] == min_cost[i][j]:
shortest_paths_edges[i][j] += end_edges[i][k]
return shortest_paths_edges
inbox: str = input()
inbox: [str] = inbox.split(" ")
n = int(inbox[0])
m = int(inbox[1])
E: [int] = []
for i in range(m):
inbox: str = input()
inbox: [str] = inbox.split(" ")
E.append([int(inbox[0]), int(inbox[1]), int(inbox[2])])
min_cost: [[int]] = Floyd_Warshall(n, E)
end_edges: [[int]] = end_edges_count(n, min_cost, E)
shortest_paths_edges: [[int]] = shortest_paths_edges_count(n, min_cost, end_edges)
for i in range(n):
for j in range(i, n):
if i != j:
print(shortest_paths_edges[i][j], end=" ")
print()
``` | instruction | 0 | 82,912 | 1 | 165,824 |
No | output | 1 | 82,912 | 1 | 165,825 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length.
We also know that the President will soon ride along the Berland roads from city s to city t. Naturally, he will choose one of the shortest paths from s to t, but nobody can say for sure which path he will choose.
The Minister for Transport is really afraid that the President might get upset by the state of the roads in the country. That is the reason he is planning to repair the roads in the possible President's path.
Making the budget for such an event is not an easy task. For all possible distinct pairs s, t (s < t) find the number of roads that lie on at least one shortest path from s to t.
Input
The first line of the input contains integers n, m (2 β€ n β€ 500, 0 β€ m β€ nΒ·(n - 1) / 2) β the number of cities and roads, correspondingly. Then m lines follow, containing the road descriptions, one description per line. Each description contains three integers xi, yi, li (1 β€ xi, yi β€ n, xi β yi, 1 β€ li β€ 106), where xi, yi are the numbers of the cities connected by the i-th road and li is its length.
Output
Print the sequence of <image> integers c12, c13, ..., c1n, c23, c24, ..., c2n, ..., cn - 1, n, where cst is the number of roads that can lie on the shortest path from s to t. Print the elements of sequence c in the described order. If the pair of cities s and t don't have a path between them, then cst = 0.
Examples
Input
5 6
1 2 1
2 3 1
3 4 1
4 1 1
2 4 2
4 5 4
Output
1 4 1 2 1 5 6 1 2 1
Submitted Solution:
```
from math import inf
def Floyd_Warshall(n: int, E: [[int]]) -> [[int]]:
min_cost = [[inf for _ in range(n)] for _ in range(n)]
for i in range(n):
min_cost[i][i] = 0
for x,y,l in E:
min_cost[x - 1][y - 1] = l
min_cost[y - 1][x - 1] = l
for k in range(n):
for i in range(n):
for j in range(n):
min_cost[i][j] = min(min_cost[i][j], min_cost[i][k] + min_cost[k][j])
return min_cost
def end_edges_count(n: int, min_cost: [[int]], E: [[int]]) -> [[int]]:
end_edges = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n):
for x, y, l in E:
if min_cost[i][x] == inf:
continue
if min_cost[i][x - 1] + l == min_cost[i][y-1]:
end_edges[i][y - 1] += 1
else:
if min_cost[i][y - 1] + l == min_cost[i][x - 1]:
end_edges[i][x - 1] += 1
return end_edges
def shortest_paths_edges_count(n: int, min_cost: [[int]], end_edges: [[int]]):
for i in range(n):
for j in range(i + 1, n):
shortest_paths_edges = 0
if min_cost[i][j] != inf:
for k in range(n):
if min_cost[i][k] + min_cost[k][j] == min_cost[i][j]:
shortest_paths_edges += end_edges[i][k]
print(shortest_paths_edges, end=" ")
print()
n, m = [int(inbox) for inbox in input().split()]
E: [int] = [[int(inbox) for inbox in input().split()] for _ in range(m)]
min_cost: [[int]] = Floyd_Warshall(n, E)
end_edges: [[int]] = end_edges_count(n, min_cost, E)
shortest_paths_edges_count(n, min_cost, end_edges)
``` | instruction | 0 | 82,913 | 1 | 165,826 |
No | output | 1 | 82,913 | 1 | 165,827 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.