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. Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has n junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads can have different lengths. Initially each junction has exactly one taxi standing there. The taxi driver from the i-th junction agrees to drive Petya (perhaps through several intermediate junctions) to some other junction if the travel distance is not more than ti meters. Also, the cost of the ride doesn't depend on the distance and is equal to ci bourles. Taxis can't stop in the middle of a road. Each taxi can be used no more than once. Petya can catch taxi only in the junction, where it stands initially. At the moment Petya is located on the junction x and the volleyball stadium is on the junction y. Determine the minimum amount of money Petya will need to drive to the stadium. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 0 ≀ m ≀ 1000). They are the number of junctions and roads in the city correspondingly. The junctions are numbered from 1 to n, inclusive. The next line contains two integers x and y (1 ≀ x, y ≀ n). They are the numbers of the initial and final junctions correspondingly. Next m lines contain the roads' description. Each road is described by a group of three integers ui, vi, wi (1 ≀ ui, vi ≀ n, 1 ≀ wi ≀ 109) β€” they are the numbers of the junctions connected by the road and the length of the road, correspondingly. The next n lines contain n pairs of integers ti and ci (1 ≀ ti, ci ≀ 109), which describe the taxi driver that waits at the i-th junction β€” the maximum distance he can drive and the drive's cost. The road can't connect the junction with itself, but between a pair of junctions there can be more than one road. All consecutive numbers in each line are separated by exactly one space character. Output If taxis can't drive Petya to the destination point, print "-1" (without the quotes). Otherwise, print the drive's minimum cost. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 4 4 1 3 1 2 3 1 4 1 2 4 1 2 3 5 2 7 7 2 1 2 7 7 Output 9 Note An optimal way β€” ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles.
instruction
0
51,894
1
103,788
Tags: shortest paths Correct Solution: ``` import os import sys from io import BytesIO, IOBase # 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") # ------------------------------ from math import factorial from collections import Counter, defaultdict from heapq import heapify, heappop, heappush def RL(): return map(int, sys.stdin.readline().rstrip().split()) def RLL(): return list(map(int, sys.stdin.readline().rstrip().split())) def N(): return int(input()) def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0 def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0 def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2) def ctd(chr): return ord(chr)-ord("a") mod = 998244353 INF = float('inf') from heapq import heappush, heappop # ------------------------------ # f = open('./input.txt') # sys.stdin = f def main(): n, m = RL() x, y = RL() gp = defaultdict(dict) for _ in range(m): u, v, w = RL() gp[u][v] = min(gp[u].get(v, INF), w) gp[v][u] = min(gp[v].get(u, INF), w) cars = [[0, 0]] for _ in range(n): t, c = RL() cars.append([t, c]) disarr = [[INF]*(n+1) for _ in range(n+1)] for i in range(1, n+1): disarr[i][i] = 0 newg = [[INF]*(n+1) for _ in range(n+1)] dnex = defaultdict(list) def dij(s): q = [(0, s)] while q: d, nd = heappop(q) for nex in gp[nd]: dis = gp[nd][nex]+disarr[s][nd] if dis<disarr[s][nex]: disarr[s][nex] = dis heappush(q, (dis, nex)) for i in range(1, n+1): cd, cc = cars[s] d = disarr[s][i] if d<=cd: newg[s][i] = min(newg[s][i], cc) dnex[s].append(i) def dij1(s): q = [(0, s)] cst = [INF] * (n + 1) cst[s] = 0 while q: d, nd = heappop(q) for nex in dnex[nd]: cs = cst[nd]+newg[nd][nex] if cs<cst[nex]: cst[nex] = cs heappush(q, (cs, nex)) if cst[y]==INF: print(-1) else: print(cst[y]) for i in range(1, n+1): dij(i) dij1(x) # for i in newg:print(i) if __name__ == "__main__": main() ```
output
1
51,894
1
103,789
Provide tags and a correct Python 3 solution for this coding contest problem. Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has n junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads can have different lengths. Initially each junction has exactly one taxi standing there. The taxi driver from the i-th junction agrees to drive Petya (perhaps through several intermediate junctions) to some other junction if the travel distance is not more than ti meters. Also, the cost of the ride doesn't depend on the distance and is equal to ci bourles. Taxis can't stop in the middle of a road. Each taxi can be used no more than once. Petya can catch taxi only in the junction, where it stands initially. At the moment Petya is located on the junction x and the volleyball stadium is on the junction y. Determine the minimum amount of money Petya will need to drive to the stadium. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 0 ≀ m ≀ 1000). They are the number of junctions and roads in the city correspondingly. The junctions are numbered from 1 to n, inclusive. The next line contains two integers x and y (1 ≀ x, y ≀ n). They are the numbers of the initial and final junctions correspondingly. Next m lines contain the roads' description. Each road is described by a group of three integers ui, vi, wi (1 ≀ ui, vi ≀ n, 1 ≀ wi ≀ 109) β€” they are the numbers of the junctions connected by the road and the length of the road, correspondingly. The next n lines contain n pairs of integers ti and ci (1 ≀ ti, ci ≀ 109), which describe the taxi driver that waits at the i-th junction β€” the maximum distance he can drive and the drive's cost. The road can't connect the junction with itself, but between a pair of junctions there can be more than one road. All consecutive numbers in each line are separated by exactly one space character. Output If taxis can't drive Petya to the destination point, print "-1" (without the quotes). Otherwise, print the drive's minimum cost. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 4 4 1 3 1 2 3 1 4 1 2 4 1 2 3 5 2 7 7 2 1 2 7 7 Output 9 Note An optimal way β€” ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles.
instruction
0
51,895
1
103,790
Tags: shortest paths Correct Solution: ``` from sys import stdin import heapq n,m = [int(x) for x in stdin.readline().split()] start,end = [int(x)-1 for x in stdin.readline().split()] graph = [{} for x in range(n)] taxis = [] for road in range(m): l,r,d = [int(x) for x in stdin.readline().split()] l -= 1 r -= 1 if not r in graph[l]: graph[l][r] = d graph[r][l] = d else: graph[l][r] = min(graph[l][r], d) graph[r][l] = min(graph[r][l], d) for junc in range(n): d,c = [int(x) for x in stdin.readline().split()] taxis.append((d,c)) def canGo(l,die): q = [] dists = [float('inf') for x in range(n)] visited = [False for x in range(n)] dists[l] = 0 heapq.heappush(q,(0,l)) while q: d,nxt = heapq.heappop(q) if not visited[nxt]: visited[nxt] = True for x in graph[nxt]: if not visited[x]: if d+graph[nxt][x] < dists[x]: dists[x] = d+graph[nxt][x] heapq.heappush(q,(dists[x], x)) out = [] for x in range(n): if dists[x] <= die: out.append(x) #print(l,die,dists) return out q = [] dists = [float('inf') for x in range(n)] visited = [False for x in range(n)] dists[start] = 0 heapq.heappush(q,(0,start)) while q: d,nxt = heapq.heappop(q) if not visited[nxt]: visited[nxt] = True for x in canGo(nxt,taxis[nxt][0]): if not visited[x]: if d+taxis[nxt][1] < dists[x]: dists[x] = d+taxis[nxt][1] heapq.heappush(q,(dists[x], x)) if dists[end] != float('inf'): print(dists[end]) else: print(-1) ```
output
1
51,895
1
103,791
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has n junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads can have different lengths. Initially each junction has exactly one taxi standing there. The taxi driver from the i-th junction agrees to drive Petya (perhaps through several intermediate junctions) to some other junction if the travel distance is not more than ti meters. Also, the cost of the ride doesn't depend on the distance and is equal to ci bourles. Taxis can't stop in the middle of a road. Each taxi can be used no more than once. Petya can catch taxi only in the junction, where it stands initially. At the moment Petya is located on the junction x and the volleyball stadium is on the junction y. Determine the minimum amount of money Petya will need to drive to the stadium. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 0 ≀ m ≀ 1000). They are the number of junctions and roads in the city correspondingly. The junctions are numbered from 1 to n, inclusive. The next line contains two integers x and y (1 ≀ x, y ≀ n). They are the numbers of the initial and final junctions correspondingly. Next m lines contain the roads' description. Each road is described by a group of three integers ui, vi, wi (1 ≀ ui, vi ≀ n, 1 ≀ wi ≀ 109) β€” they are the numbers of the junctions connected by the road and the length of the road, correspondingly. The next n lines contain n pairs of integers ti and ci (1 ≀ ti, ci ≀ 109), which describe the taxi driver that waits at the i-th junction β€” the maximum distance he can drive and the drive's cost. The road can't connect the junction with itself, but between a pair of junctions there can be more than one road. All consecutive numbers in each line are separated by exactly one space character. Output If taxis can't drive Petya to the destination point, print "-1" (without the quotes). Otherwise, print the drive's minimum cost. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 4 4 1 3 1 2 3 1 4 1 2 4 1 2 3 5 2 7 7 2 1 2 7 7 Output 9 Note An optimal way β€” ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles. Submitted Solution: ``` n, m = [int(x) for x in input().split()] start, end = [int(x)-1 for x in input().split()] roads = [[]]*m taxis = [()]*n for i in range(m): u, v, w = [int(x) for x in input().split()] roads[u-1].append((v-1, w)) roads[v-1].append((u-1, w)) for i in range(n): t, c = [int(x) for x in input().split()] taxis[i] = (t, c) mins = {} def where(curr, dist): ans = [] for r in roads[curr]: if r[1] <= dist: ans.append(r[0]) ans.extend(where(r[0], dist - r[1])) return ans def makemins(curr=start, cost=0, path=[]): if cost < mins.get(curr, 1000000000000000000): mins[curr] = cost if curr == end: return letsgo = where(curr, taxis[curr][0]) for l in letsgo: if not l in path: makemins(l, cost+taxis[curr][1], path + [curr]) makemins() print(mins.get(end)) ```
instruction
0
51,896
1
103,792
No
output
1
51,896
1
103,793
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has n junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads can have different lengths. Initially each junction has exactly one taxi standing there. The taxi driver from the i-th junction agrees to drive Petya (perhaps through several intermediate junctions) to some other junction if the travel distance is not more than ti meters. Also, the cost of the ride doesn't depend on the distance and is equal to ci bourles. Taxis can't stop in the middle of a road. Each taxi can be used no more than once. Petya can catch taxi only in the junction, where it stands initially. At the moment Petya is located on the junction x and the volleyball stadium is on the junction y. Determine the minimum amount of money Petya will need to drive to the stadium. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 0 ≀ m ≀ 1000). They are the number of junctions and roads in the city correspondingly. The junctions are numbered from 1 to n, inclusive. The next line contains two integers x and y (1 ≀ x, y ≀ n). They are the numbers of the initial and final junctions correspondingly. Next m lines contain the roads' description. Each road is described by a group of three integers ui, vi, wi (1 ≀ ui, vi ≀ n, 1 ≀ wi ≀ 109) β€” they are the numbers of the junctions connected by the road and the length of the road, correspondingly. The next n lines contain n pairs of integers ti and ci (1 ≀ ti, ci ≀ 109), which describe the taxi driver that waits at the i-th junction β€” the maximum distance he can drive and the drive's cost. The road can't connect the junction with itself, but between a pair of junctions there can be more than one road. All consecutive numbers in each line are separated by exactly one space character. Output If taxis can't drive Petya to the destination point, print "-1" (without the quotes). Otherwise, print the drive's minimum cost. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 4 4 1 3 1 2 3 1 4 1 2 4 1 2 3 5 2 7 7 2 1 2 7 7 Output 9 Note An optimal way β€” ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles. Submitted Solution: ``` from math import inf n, m = [int(x) for x in input().split()] start, end = [int(x)-1 for x in input().split()] roads = [[]]*m taxis = [()]*n for i in range(m): u, v, w = [int(x) for x in input().split()] roads[u-1].append((v-1, w)) roads[v-1].append((u-1, w)) for i in range(n): t, c = [int(x) for x in input().split()] taxis[i] = (t, c) mins = {} def where(curr, dist, used): ans = [] for r in roads[curr]: if r[1] <= dist: if r[0] not in used: ans.append(r[0]) ans.extend(where(r[0], dist - r[1], used)) if end in ans: return [end] return list(set(ans)) def makemins(curr=start, cost=0, path=[]): if cost < mins.get(curr, inf): mins[curr] = cost if cost < mins.get(end, inf): if curr == end: return letsgo = where(curr, taxis[curr][0], path+[curr]) for l in letsgo: if l not in path: makemins(l, cost+taxis[curr][1], path+[curr]) makemins() print(mins.get(end, -1)) ```
instruction
0
51,897
1
103,794
No
output
1
51,897
1
103,795
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has n junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads can have different lengths. Initially each junction has exactly one taxi standing there. The taxi driver from the i-th junction agrees to drive Petya (perhaps through several intermediate junctions) to some other junction if the travel distance is not more than ti meters. Also, the cost of the ride doesn't depend on the distance and is equal to ci bourles. Taxis can't stop in the middle of a road. Each taxi can be used no more than once. Petya can catch taxi only in the junction, where it stands initially. At the moment Petya is located on the junction x and the volleyball stadium is on the junction y. Determine the minimum amount of money Petya will need to drive to the stadium. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 0 ≀ m ≀ 1000). They are the number of junctions and roads in the city correspondingly. The junctions are numbered from 1 to n, inclusive. The next line contains two integers x and y (1 ≀ x, y ≀ n). They are the numbers of the initial and final junctions correspondingly. Next m lines contain the roads' description. Each road is described by a group of three integers ui, vi, wi (1 ≀ ui, vi ≀ n, 1 ≀ wi ≀ 109) β€” they are the numbers of the junctions connected by the road and the length of the road, correspondingly. The next n lines contain n pairs of integers ti and ci (1 ≀ ti, ci ≀ 109), which describe the taxi driver that waits at the i-th junction β€” the maximum distance he can drive and the drive's cost. The road can't connect the junction with itself, but between a pair of junctions there can be more than one road. All consecutive numbers in each line are separated by exactly one space character. Output If taxis can't drive Petya to the destination point, print "-1" (without the quotes). Otherwise, print the drive's minimum cost. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 4 4 1 3 1 2 3 1 4 1 2 4 1 2 3 5 2 7 7 2 1 2 7 7 Output 9 Note An optimal way β€” ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles. Submitted Solution: ``` from heapq import * f = lambda: map(int, input().split()) inf = 2e13 n, m = f() x, y = f() p = [[] for i in range(n + 1)] for j in range(m): u, v, w = f() p[u].append((v, w)) p[v].append((u, w)) d = [(0, 0)] + [tuple(f()) for k in range(n)] h = [(0, x)] H = [inf] * (n + 1) H[x] = 0 while h: r, u = heappop(h) if H[u] < r: continue if u == y: exit(print(r)) t, c = d[u] r += c s = [(w - t, v) for v, w in p[u] if w <= t] heapify(s) S = [0] * (n + 1) while s: t, u = heappop(s) if S[u] < t: continue if H[u] > r: heappush(h, (r, u)) H[u] = r for v, w in p[u]: if S[v] > w + t: heappush(s, (w + t, v)) S[v] = w + t print(-1) ```
instruction
0
51,898
1
103,796
No
output
1
51,898
1
103,797
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has n junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads can have different lengths. Initially each junction has exactly one taxi standing there. The taxi driver from the i-th junction agrees to drive Petya (perhaps through several intermediate junctions) to some other junction if the travel distance is not more than ti meters. Also, the cost of the ride doesn't depend on the distance and is equal to ci bourles. Taxis can't stop in the middle of a road. Each taxi can be used no more than once. Petya can catch taxi only in the junction, where it stands initially. At the moment Petya is located on the junction x and the volleyball stadium is on the junction y. Determine the minimum amount of money Petya will need to drive to the stadium. Input The first line contains two integers n and m (1 ≀ n ≀ 1000, 0 ≀ m ≀ 1000). They are the number of junctions and roads in the city correspondingly. The junctions are numbered from 1 to n, inclusive. The next line contains two integers x and y (1 ≀ x, y ≀ n). They are the numbers of the initial and final junctions correspondingly. Next m lines contain the roads' description. Each road is described by a group of three integers ui, vi, wi (1 ≀ ui, vi ≀ n, 1 ≀ wi ≀ 109) β€” they are the numbers of the junctions connected by the road and the length of the road, correspondingly. The next n lines contain n pairs of integers ti and ci (1 ≀ ti, ci ≀ 109), which describe the taxi driver that waits at the i-th junction β€” the maximum distance he can drive and the drive's cost. The road can't connect the junction with itself, but between a pair of junctions there can be more than one road. All consecutive numbers in each line are separated by exactly one space character. Output If taxis can't drive Petya to the destination point, print "-1" (without the quotes). Otherwise, print the drive's minimum cost. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 4 4 1 3 1 2 3 1 4 1 2 4 1 2 3 5 2 7 7 2 1 2 7 7 Output 9 Note An optimal way β€” ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles. Submitted Solution: ``` n, m = [int(a) for a in input().split()] x, y = [int(a) for a in input().split()] x-=1 y-=1 adj = [[] for _ in range(n)] for i in range(m): u,v,w = [int(a) for a in input().split()] u-=1 v-=1 adj[u].append((v,w)) adj[v].append((u,w)) for i in range(n): adj[i].sort(key=lambda x: x[1]) taxi = [] for i in range(n): t,c = [int(a) for a in input().split()] taxi.append((t,c)) dp = [2**1000]*n dp[x] = 0 marked1 = [False]*n stk1 = [x] while stk1: u1 = stk1.pop() if marked1[u1]: continue marked1[u1] = True # see where you can get in t_u1 marked2 = [False]*n t_u1, c_u1 = taxi[u1] stk2 = [(u1,0)] while stk2: u2,d = stk2.pop() if marked2[u2]: continue marked2[u2] = True dp[u2] = min(dp[u2], dp[u1]+c_u1) for v2,w in adj[u2]: if not marked2[v2] and w+d <= t_u1: stk2.append((v2,w+d)) for v1,w in adj[u1]: if not marked1[v1]: stk1.append(v1) if dp[y] == 2**1000: print(-1) else: print(dp[y]) ```
instruction
0
51,899
1
103,798
No
output
1
51,899
1
103,799
Provide a correct Python 3 solution for this coding contest problem. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 3 2
instruction
0
52,082
1
104,164
"Correct Solution: ``` # -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0140 """ import sys from sys import stdin input = stdin.readline def solve(path, start, goal): path_a = [0]*100 # ??????????????????????????? path_b = [0]*100 # ?????????????????Β¨??????????????????????????????????????Β°?????Β§??????????????Β’??? try: path_a = path[start][0][:path[start][0].index(goal)+1] except ValueError: pass try: path_b = path[start][1][:path[start][1].index(goal)+1] except ValueError: pass # ??????????????????????Β±???????????????????????????Β§???start??????goal?????Β§????????????????????Β§1?????????????????? if len(path_a) < len(path_b): return ' '.join(map(str, path_a)) else: return ' '.join(map(str, path_b)) def main(args): path = [] # ?????????n??????????????????????????????????????? # ?????????[n]???????????????????????????????????????????????Β¨????????Β΄?????Β¨??????????????????????????Β¨????????Β΄????????Β°?????? path.append([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], []]) # 0???????????? path.append([[1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2], [1, 0]]) # 1 path.append([[2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2], [2, 1, 0]]) # 2 path.append([[3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2], [3, 2, 1, 0]]) # 3 path.append([[4, 5, 6, 7, 8, 9, 5, 4, 3, 2], [4, 3, 2, 1, 0]]) # 4 path.append([[5, 6, 7, 8, 9, 5, 4, 3, 2], [5, 4, 3, 2, 1, 0]]) # 5 path.append([[6, 7, 8, 9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5], []]) # 6 path.append([[7, 8, 9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6], []]) # 7 path.append([[8, 9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7], []]) # 8 path.append([[9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8], []]) # 9 n = int(input()) for _ in range(n): start, goal = map(int, input().split()) result = solve(path, start, goal) print(result) if __name__ == '__main__': main(sys.argv[1:]) ```
output
1
52,082
1
104,165
Provide a correct Python 3 solution for this coding contest problem. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 3 2
instruction
0
52,083
1
104,166
"Correct Solution: ``` n = int(input()) for _ in range(n): start, end = map(int, input().split()) anslist = [] if start <= 5: if start > end: [anslist.append(i) for i in range(start, end-1, -1)] else: [anslist.append(i) for i in range(start, end+1)] elif start < end: [anslist.append(i) for i in range(start, end+1)] else: [anslist.append(i) for i in range(start, 10)] if end <= 5: [anslist.append(i) for i in range(5, end-1, -1)] else: [anslist.append(i) for i in range(5, 0, -1)] [anslist.append(i) for i in range(0, end+1)] print(' '.join(str(ans) for ans in anslist)) ```
output
1
52,083
1
104,167
Provide a correct Python 3 solution for this coding contest problem. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 3 2
instruction
0
52,084
1
104,168
"Correct Solution: ``` # Aizu Problem 00140: Bus Line # import sys, math, os, copy # read input: PYDEV = os.environ.get('PYDEV') if PYDEV=="True": sys.stdin = open("sample-input.txt", "rt") def bus_line(a, b): line = [] if max(a, b) <= 5: if a < b: return list(range(a, b + 1)) else: return list(range(a, b - 1, -1)) if a <= 5: # stop must be in circle: return list(range(a, b + 1)) if b <= 5: # start must be in circle: return list(range(a, 10)) + list(range(5, b - 1, -1)) # at this point both start and stop are in circle: if a < b: return list(range(a, b + 1)) else: return list(range(a, 10)) + [5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5] + \ list(range(6, b + 1)) n = int(input()) for __ in range(n): a, b = [int(_) for _ in input().split()] print(' '.join([str(_) for _ in bus_line(a, b)])) ```
output
1
52,084
1
104,169
Provide a correct Python 3 solution for this coding contest problem. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 3 2
instruction
0
52,085
1
104,170
"Correct Solution: ``` A = "012345678954321012345678" B = "6789543210123456789" n = int(input()) for i in range(n): a, b = input().split() if int(a) < int(b): print(" ".join(A[A.index(a):A.index(b,A.index(a))+1])) else: print(" ".join(B[B.index(a):B.index(b,B.index(a))+1])) ```
output
1
52,085
1
104,171
Provide a correct Python 3 solution for this coding contest problem. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 3 2
instruction
0
52,086
1
104,172
"Correct Solution: ``` # AOJ 0140 Bus Line # Python3 2018.6.23 bal4u for i in range(int(input())): s, e = map(int, input().split()) a = [] if s <= 5: if s > e: [a.append(i) for i in range(s, e-1, -1)] else: [a.append(i) for i in range(s, e+1)] elif s < e: [a.append(i) for i in range(s, e+1)] else: [a.append(i) for i in range(s, 10)] if e <= 5: [a.append(i) for i in range(5, e-1, -1)] else: [a.append(i) for i in range(5, 0, -1)] [a.append(i) for i in range(0, e+1)] print(*a) ```
output
1
52,086
1
104,173
Provide a correct Python 3 solution for this coding contest problem. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 3 2
instruction
0
52,087
1
104,174
"Correct Solution: ``` dic = {} for i in range(10): for j in range(10): if i < j: dic[(i, j)] = list(range(i, j + 1)) elif i > j: if i <= 5: dic[(i, j)] = list(range(i, j - 1, -1)) elif j <= 5: dic[(i, j)] = list(range(i, 10)) + list(range(5, j - 1, -1)) else: dic[(i, j)] = list(range(i, 10)) + list(range(5, -1, -1)) + list(range(1, j + 1)) n = int(input()) for _ in range(n): s, g = map(int, input().split()) print(*dic[(s, g)]) ```
output
1
52,087
1
104,175
Provide a correct Python 3 solution for this coding contest problem. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 3 2
instruction
0
52,088
1
104,176
"Correct Solution: ``` for i in range(int(input())): s, e = map(int, input().split()) a = [] if s <= 5: if s > e: [a.append(i) for i in range(s, e - 1, -1)] else: [a.append(i) for i in range(s, e + 1)] elif s < e: [a.append(i) for i in range(s, e + 1)] else: [a.append(i) for i in range(s, 10)] if e <= 5: [a.append(i) for i in range(5, e - 1, -1)] else: [a.append(i) for i in range(5, 0, -1)] [a.append(i) for i in range(0, e + 1)] print(*a) ```
output
1
52,088
1
104,177
Provide a correct Python 3 solution for this coding contest problem. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 3 2
instruction
0
52,089
1
104,178
"Correct Solution: ``` # -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0140 """ import sys from sys import stdin input = stdin.readline def solve(start, goal): path = [] # ?????????n??????????????????????????????????????? # ????????????????????Β¨????????Β΄?????Β¨????????????????????Β¨????????Β΄????????Β°?????? path.append([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], []]) path.append([[1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2], [1, 0]]) path.append([[2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2], [2, 1, 0]]) path.append([[3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2], [3, 2, 1, 0]]) path.append([[4, 5, 6, 7, 8, 9, 5, 4, 3, 2], [4, 3, 2, 1, 0]]) path.append([[5, 6, 7, 8, 9, 5, 4, 3, 2], [5, 4, 3, 2, 1, 0]]) path.append([[6, 7, 8, 9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5], []]) path.append([[7, 8, 9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6], []]) path.append([[8, 9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7], []]) path.append([[9, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8], []]) path_a = [0]*100 # ??????????????????????????? path_b = [0]*100 try: path_a = path[start][0][:path[start][0].index(goal)+1] except ValueError: pass try: path_b = path[start][1][:path[start][1].index(goal)+1] except ValueError: pass if len(path_a) < len(path_b): return ' '.join(map(str, path_a)) else: return ' '.join(map(str, path_b)) def main(args): n = int(input()) for _ in range(n): start, goal = map(int, input().split()) result = solve(start, goal) print(result) if __name__ == '__main__': main(sys.argv[1:]) ```
output
1
52,089
1
104,179
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 3 2 Submitted Solution: ``` def printAns(ans): for i in range(len(ans)): if i == 0: print(ans[i], end="") else: print(" ", end="") print(ans[i], end="") print("") N = int(input()) for l in range(N): a,b = [int(i) for i in input().split()] ans = [] if a < b: for i in range(a, b+1): ans.append(i) else: # a > b if a <= 5: for i in range(a, b-1, -1): ans.append(i) else: if b <= 5: for i in range(a, 10): ans.append(i) for i in range(5, b-1, -1): ans.append(i) else: for i in range(a, 10): ans.append(i) for i in range(5, 0, -1): ans.append(i) for i in range(b+1): ans.append(i) printAns(ans) ```
instruction
0
52,090
1
104,180
Yes
output
1
52,090
1
104,181
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 3 2 Submitted Solution: ``` # -*- coding: utf-8 -*- import sys import os N = int(input()) for i in range(N): a, b = map(int, input().split()) if a < b: lst = [j for j in range(a, b+1)] print(*lst) # a > b else: # easy if a <= 5: lst = list(range(a, b - 1, -1)) elif b <= 5: lst = list(range(a, 10)) + list(range(5, b - 1, -1)) # i.e. 7 -> 6 else: lst = [j for j in range(a, 9 + 1)] + [j for j in range(5, 0, -1)] + list(range(0, b+1)) print(*lst) ```
instruction
0
52,091
1
104,182
Yes
output
1
52,091
1
104,183
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 3 2 Submitted Solution: ``` def line(i,j): retval=[] if i<=5 and j<=5: if i<j: for k in range(i,j+1): retval.append(k) else: for k in range(i,j-1,-1): retval.append(k) elif i<=5 and j>5: for k in range(i,j+1): retval.append(k) elif i>5 and j>5: if i<j: for k in range(i,j+1): retval.append(k) else: for k in range(i,10): retval.append(k) retval+=[5,4,3,2,1,0,1,2,3,4,5] for k in range(6,j+1): retval.append(k) else: for k in range(i,10): retval.append(k) for k in range(5,j-1,-1): retval.append(k) return " ".join([str(i) for i in retval]) N=int(input()) for i in range(N): print(line(*[int(j) for j in input().split(" ")])) ```
instruction
0
52,092
1
104,184
Yes
output
1
52,092
1
104,185
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 3 2 Submitted Solution: ``` # pythonっぽくγͺくてダァい stops=[0,1,2,3,4,5,6,7,8,9,5,4,3,2,1] istops=[[0],[1,14],[2,13],[3,12],[4,11],[5,10],[6],[7],[8],[9]] def asloop(cur, end): result=[] for i in range(cur, cur+len(stops)): x=stops[i%len(stops)] result.append(x) if x==end: return result def asrecurse(cur, end, result=None): if result is None: result=[] x=stops[cur] result.append(x) next=(cur+1)%len(stops) if x==end: return result return asrecurse(next, end, result) def loop(start, end): result=[i for i in range(len(stops)+1)] for i in istops[start]: a=asloop(i, end) if len(a) < len(result): result=a return result nloop=int(input()) for i in range(nloop): inputs=input().split() result=loop(int(inputs[0]), int(inputs[1])) print(' '.join([str(x) for x in result])) ```
instruction
0
52,093
1
104,186
Yes
output
1
52,093
1
104,187
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 3 2 Submitted Solution: ``` # -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0140 """ import sys from sys import stdin from enum import Enum input = stdin.readline class Graph(object): """ single source shortest path """ class Status(Enum): """ ?????????????Β¨??????ΒΆ??? """ white = 1 # ????Β¨???? gray = 2 # ?Β¨??????? black = 3 #?Β¨??????? def __init__(self, data): num_of_nodes = len(data) self.color = [Graph.Status.white] * num_of_nodes # ????????????????Β¨??????ΒΆ??? self.M = [[float('inf')] * num_of_nodes for _ in range(num_of_nodes)] self._make_matrix(data) # data????????????????????Β£??\??????(?????\?ΒΆ???Β¨???????????????????????Β§????????????) self.d = [float('inf')] * num_of_nodes # ?Β§???????????????????(?????????) self.p = [-1] * num_of_nodes # ????????????????????????????Β¨????????????????????????? def _make_matrix(self, data): for f, t, c in data: self.M[f][t] = c def dijkstra(self, start): self.d[start] = 0 self.p[start] = -1 while True: mincost = float('inf') # ??\??????????????Β§??????????????Β¨?????????????????????u??????????????? for i in range(len(self.d)): if self.color[i] != Graph.Status.black and self.d[i] < mincost: # S????Β±???????????????????S??Β¨??\?ΒΆ?????????????????????????????????????????Β°??????????????????? mincost = self.d[i] u = i # u??????????????????ID if mincost == float('inf'): break self.color[u] = Graph.Status.black # ?????????u???S????Β±???????????????Β΄??? for v in range(len(self.d)): if self.color[v] != Graph.Status.black and self.M[u][v] != float('inf'): # v????????????????????????????????Β°??????S???????????Β£???u????????????????????????????????????????????Β°??????????????Β±??Β§??Β΄??Β°?????? if self.d[u] + self.M[u][v] < self.d[v]: self.d[v] = self.d[u] + self.M[u][v] self.p[v] = u self.color[v] = Graph.Status.gray def main(args): data = [] # from, to, cost data.append([0, 1, 10]) data.append([1, 2, 10]) data.append([2, 3, 10]) data.append([3, 4, 10]) data.append([4, 5, 10]) data.append([5, 6, 10]) data.append([6, 7, 10]) data.append([7, 8, 10]) data.append([8, 9, 10]) data.append([9, 5, 10]) data.append([5, 4, 10]) data.append([4, 3, 10]) data.append([3, 2, 10]) data.append([2, 1, 10]) data.append([1, 0, 10]) n = int(input()) for _ in range(n): start, goal = map(int, input().split()) g = Graph(data) g.dijkstra(start) path = [goal] u = goal while g.p[u] != start: path.append(g.p[u]) u = g.p[u] path.append(start) path.reverse() print(' '.join(map(str, path))) if __name__ == '__main__': main(sys.argv[1:]) ```
instruction
0
52,094
1
104,188
No
output
1
52,094
1
104,189
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 3 2 Submitted Solution: ``` # -*- coding: utf-8 -*- import sys import os N = int(input()) for i in range(N): a, b = map(int, input().split()) if a < b: lst = [j for j in range(a, b+1)] print(*lst) # a > b else: # easy if a <= 5: lst = [j for j in range(a, b - 1, -1)] elif b <= 5: lst = [j for j in range(a, 9+1)] + [j for j in range(5, b - 1, -1)] # i.e. 7 -> 6 else: lst = [j for j in range(a, 9 + 1)] + [j for j in range(5, b - 1, -1)] + list(range(0, b+1)) print(*lst) ```
instruction
0
52,095
1
104,190
No
output
1
52,095
1
104,191
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 3 2 Submitted Solution: ``` # -*- coding: utf-8 -*- import sys import os N = int(input()) for i in range(N): a, b = map(int, input().split()) if a < b: lst = [j for j in range(a, b+1)] print(*lst) # a > b else: # easy if a <= 5: lst = [j for j in range(a, b - 1, -1)] else: lst = [j for j in range(a, 9+1)] + [j for j in range(5, b - 1, -1)] print(*lst) ```
instruction
0
52,096
1
104,192
No
output
1
52,096
1
104,193
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a bus route as shown in Figure 1. There are 10 stops, each numbered 0-9. The bus turns back at stop 0, but the other side is a circulation route, which circulates in the order of 5 β†’ 6 β†’ 7 β†’ 8 β†’ 9 β†’ 5 as shown in the figure. <image> For this bus route, create a program that inputs the stop to get on and the stop to get off, and outputs the number of the stop that goes from getting on to getting off. However, you can take a two-way bus at stops 1-5, but we will take a bus that arrives at the stop on a shorter route. For example, if you are going from stop 4 to stop 2, take the bus going to the left and follow the route "4 β†’ 3 β†’ 2". Also, once you get on the bus, you will not get off the bus. The same stop is not designated as a boarding stop or a getting-off stop. Input Given multiple datasets. The first line gives the number of datasets n (n ≀ 20). For each dataset, the stop number to get on and the stop number to get off are given on one line separated by blanks. Output For each data set, output the list of passing stop numbers on one line separated by blanks. Example Input 2 2 4 4 2 Output 2 3 4 4 3 2 Submitted Solution: ``` RIGHT_DIRECT = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] LEFT_DIRECT = [6, 7, 8, 9, 5, 4, 3, 2, 1, 0] ROTATE_DIRECT = [5, 6, 7, 8, 9, 5, 6, 7, 8, 9] for _ in range(int(input())): begin_number, end_number = [int(item) for item in input().split(" ")] output = [] if 5 <= begin_number <= 9 and 5 <= end_number <= 9: index = ROTATE_DIRECT.index(begin_number) while ROTATE_DIRECT[index] != end_number: output.append(ROTATE_DIRECT[index]) index += 1 output.append(ROTATE_DIRECT[index]) elif begin_number < end_number: begin_index = RIGHT_DIRECT.index(begin_number) end_index = RIGHT_DIRECT.index(end_number) for item in RIGHT_DIRECT[begin_index:end_index + 1]: output.append(item) else: begin_index = LEFT_DIRECT.index(begin_number) end_index = LEFT_DIRECT.index(end_number) for item in LEFT_DIRECT[begin_index:end_index + 1]: output.append(item) output = [str(item) for item in output] print(" ".join(output)) ```
instruction
0
52,097
1
104,194
No
output
1
52,097
1
104,195
Provide tags and a correct Python 3 solution for this coding contest problem. Arthur owns a ski resort on a mountain. There are n landing spots on the mountain numbered from 1 to n from the top to the foot of the mountain. The spots are connected with one-directional ski tracks. All tracks go towards the foot of the mountain, so there are no directed cycles formed by the tracks. There are at most two tracks leaving each spot, but many tracks may enter the same spot. A skier can start skiing from one spot and stop in another spot if there is a sequence of tracks that lead from the starting spot and end in the ending spot. Unfortunately, recently there were many accidents, because the structure of the resort allows a skier to go through dangerous paths, by reaching high speed and endangering himself and the other customers. Here, a path is called dangerous, if it consists of at least two tracks. Arthur wants to secure his customers by closing some of the spots in a way that there are no dangerous paths in the resort. When a spot is closed, all tracks entering and leaving that spot become unusable. Formally, after closing some of the spots, there should not be a path that consists of two or more tracks. Arthur doesn't want to close too many spots. He will be happy to find any way to close at most 4/7n spots so that the remaining part is safe. Help him find any suitable way to do so. Input The first line contains a single positive integer T β€” the number of test cases. T test case description follows. The first line of each description contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of landing spots and tracks respectively. The following m lines describe the tracks. Each of these lines contains two integers x and y (1 ≀ x < y ≀ n) β€” indices of the starting and finishing spots for the respective track. It is guaranteed that at most two tracks start at each spot. There may be tracks in which starting and finishing spots both coincide. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, print a single integer k (0 ≀ k ≀ 4/7n) β€” the number of spots to be closed. In the next line, print k distinct integers β€” indices of all spots to be closed, in any order. If there are several answers, you may output any of them. Note that you don't have to minimize k. It can be shown that a suitable answer always exists. Example Input 2 4 6 1 2 1 3 2 3 2 4 3 4 3 4 7 6 1 2 1 3 2 4 2 5 3 6 3 7 Output 2 3 4 4 4 5 6 7 Note In the first sample case, closing any two spots is suitable. In the second sample case, closing only the spot 1 is also suitable.
instruction
0
52,392
1
104,784
Tags: constructive algorithms, graphs, greedy Correct Solution: ``` import sys inputr = lambda: sys.stdin.readline().rstrip('\n') input = sys.stdin.readline for _ in range(int(input())): n, m = map(int, input().split()) adj = [[] for _ in range(n)] for _ in range(m): a, b = map(int, input().split()) a -= 1 b -= 1 adj[a].append(b) LP = [0] * n r = [] for i in range(n): if LP[i] < 2: for j in adj[i]: LP[j] = max(LP[j], LP[i] + 1) else: r.append(str(i+1)) print(len(r)) print(*r) assert 7 * len(r) <= 4 * n ```
output
1
52,392
1
104,785
Provide tags and a correct Python 3 solution for this coding contest problem. Arthur owns a ski resort on a mountain. There are n landing spots on the mountain numbered from 1 to n from the top to the foot of the mountain. The spots are connected with one-directional ski tracks. All tracks go towards the foot of the mountain, so there are no directed cycles formed by the tracks. There are at most two tracks leaving each spot, but many tracks may enter the same spot. A skier can start skiing from one spot and stop in another spot if there is a sequence of tracks that lead from the starting spot and end in the ending spot. Unfortunately, recently there were many accidents, because the structure of the resort allows a skier to go through dangerous paths, by reaching high speed and endangering himself and the other customers. Here, a path is called dangerous, if it consists of at least two tracks. Arthur wants to secure his customers by closing some of the spots in a way that there are no dangerous paths in the resort. When a spot is closed, all tracks entering and leaving that spot become unusable. Formally, after closing some of the spots, there should not be a path that consists of two or more tracks. Arthur doesn't want to close too many spots. He will be happy to find any way to close at most 4/7n spots so that the remaining part is safe. Help him find any suitable way to do so. Input The first line contains a single positive integer T β€” the number of test cases. T test case description follows. The first line of each description contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of landing spots and tracks respectively. The following m lines describe the tracks. Each of these lines contains two integers x and y (1 ≀ x < y ≀ n) β€” indices of the starting and finishing spots for the respective track. It is guaranteed that at most two tracks start at each spot. There may be tracks in which starting and finishing spots both coincide. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, print a single integer k (0 ≀ k ≀ 4/7n) β€” the number of spots to be closed. In the next line, print k distinct integers β€” indices of all spots to be closed, in any order. If there are several answers, you may output any of them. Note that you don't have to minimize k. It can be shown that a suitable answer always exists. Example Input 2 4 6 1 2 1 3 2 3 2 4 3 4 3 4 7 6 1 2 1 3 2 4 2 5 3 6 3 7 Output 2 3 4 4 4 5 6 7 Note In the first sample case, closing any two spots is suitable. In the second sample case, closing only the spot 1 is also suitable.
instruction
0
52,393
1
104,786
Tags: constructive algorithms, graphs, greedy Correct Solution: ``` import sys input = sys.stdin.readline readline=lambda:map(int,input().split()); for _ in range(int(input())): n,m=readline(); g=[[] for i in range(n)]; for i in range(m): a,b=readline(); g[a-1].append(b-1); f=[0]*n; for i in range(n): if f[i]<2: for j in g[i]: f[j]=max(f[j],f[i]+1); ans=[i+1 for i in range(n) if f[i]==2]; print(len(ans)); print(*ans); ```
output
1
52,393
1
104,787
Provide tags and a correct Python 3 solution for this coding contest problem. Arthur owns a ski resort on a mountain. There are n landing spots on the mountain numbered from 1 to n from the top to the foot of the mountain. The spots are connected with one-directional ski tracks. All tracks go towards the foot of the mountain, so there are no directed cycles formed by the tracks. There are at most two tracks leaving each spot, but many tracks may enter the same spot. A skier can start skiing from one spot and stop in another spot if there is a sequence of tracks that lead from the starting spot and end in the ending spot. Unfortunately, recently there were many accidents, because the structure of the resort allows a skier to go through dangerous paths, by reaching high speed and endangering himself and the other customers. Here, a path is called dangerous, if it consists of at least two tracks. Arthur wants to secure his customers by closing some of the spots in a way that there are no dangerous paths in the resort. When a spot is closed, all tracks entering and leaving that spot become unusable. Formally, after closing some of the spots, there should not be a path that consists of two or more tracks. Arthur doesn't want to close too many spots. He will be happy to find any way to close at most 4/7n spots so that the remaining part is safe. Help him find any suitable way to do so. Input The first line contains a single positive integer T β€” the number of test cases. T test case description follows. The first line of each description contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of landing spots and tracks respectively. The following m lines describe the tracks. Each of these lines contains two integers x and y (1 ≀ x < y ≀ n) β€” indices of the starting and finishing spots for the respective track. It is guaranteed that at most two tracks start at each spot. There may be tracks in which starting and finishing spots both coincide. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, print a single integer k (0 ≀ k ≀ 4/7n) β€” the number of spots to be closed. In the next line, print k distinct integers β€” indices of all spots to be closed, in any order. If there are several answers, you may output any of them. Note that you don't have to minimize k. It can be shown that a suitable answer always exists. Example Input 2 4 6 1 2 1 3 2 3 2 4 3 4 3 4 7 6 1 2 1 3 2 4 2 5 3 6 3 7 Output 2 3 4 4 4 5 6 7 Note In the first sample case, closing any two spots is suitable. In the second sample case, closing only the spot 1 is also suitable.
instruction
0
52,394
1
104,788
Tags: constructive algorithms, graphs, greedy Correct Solution: ``` from sys import stdin, gettrace from collections import deque if gettrace(): inputi = input else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() def readGraph(n,m): adj = [set() for _ in range(n)] adjr = [set() for _ in range(n)] for _ in range(m): u, v = map(int, inputi().split()) adj[u - 1].add(v - 1) adjr[v - 1].add(u - 1) return adj, adjr def solve(n, adj, adjr): path = [0] * n res = [] for i in range(n): for j in adjr[i]: path[i] = max(path[i], path[j]+1) if path[i] == 2: res.append(i+1) path[i] = -1 return res def main(): t = int(inputi()) for _ in range(t): n,m = map(int, inputi().split()) adj, adjr = readGraph(n, m) res = solve(n, adj, adjr) print(len(res)) print(' '.join(map(str, res))) if __name__ == "__main__": main() ```
output
1
52,394
1
104,789
Provide tags and a correct Python 3 solution for this coding contest problem. Arthur owns a ski resort on a mountain. There are n landing spots on the mountain numbered from 1 to n from the top to the foot of the mountain. The spots are connected with one-directional ski tracks. All tracks go towards the foot of the mountain, so there are no directed cycles formed by the tracks. There are at most two tracks leaving each spot, but many tracks may enter the same spot. A skier can start skiing from one spot and stop in another spot if there is a sequence of tracks that lead from the starting spot and end in the ending spot. Unfortunately, recently there were many accidents, because the structure of the resort allows a skier to go through dangerous paths, by reaching high speed and endangering himself and the other customers. Here, a path is called dangerous, if it consists of at least two tracks. Arthur wants to secure his customers by closing some of the spots in a way that there are no dangerous paths in the resort. When a spot is closed, all tracks entering and leaving that spot become unusable. Formally, after closing some of the spots, there should not be a path that consists of two or more tracks. Arthur doesn't want to close too many spots. He will be happy to find any way to close at most 4/7n spots so that the remaining part is safe. Help him find any suitable way to do so. Input The first line contains a single positive integer T β€” the number of test cases. T test case description follows. The first line of each description contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of landing spots and tracks respectively. The following m lines describe the tracks. Each of these lines contains two integers x and y (1 ≀ x < y ≀ n) β€” indices of the starting and finishing spots for the respective track. It is guaranteed that at most two tracks start at each spot. There may be tracks in which starting and finishing spots both coincide. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, print a single integer k (0 ≀ k ≀ 4/7n) β€” the number of spots to be closed. In the next line, print k distinct integers β€” indices of all spots to be closed, in any order. If there are several answers, you may output any of them. Note that you don't have to minimize k. It can be shown that a suitable answer always exists. Example Input 2 4 6 1 2 1 3 2 3 2 4 3 4 3 4 7 6 1 2 1 3 2 4 2 5 3 6 3 7 Output 2 3 4 4 4 5 6 7 Note In the first sample case, closing any two spots is suitable. In the second sample case, closing only the spot 1 is also suitable.
instruction
0
52,395
1
104,790
Tags: constructive algorithms, graphs, greedy Correct Solution: ``` import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, m = map(int, input().split()) ms = [] for _ in range(m): ms.append(list(map(int,input().split()))) Graph = [[] for _ in range(n)] for z,s in ms: Graph[z-1].append(s-1) #dels = [False for _ in range(n)] ones = [False for _ in range(n)] nones = [False for _ in range(n)] #ress = [-1 for _ in range (n)] r = [] for i in range(n): if ones[i]: r.append(i+1) #ress[i]=2 for x in Graph[i]: pass elif nones[i]: #ress[i]=1 for x in Graph[i]: ones[x]=True else: #ress[i]=0 for x in Graph[i]: nones[x]=True print(len(r)) print(*r) ```
output
1
52,395
1
104,791
Provide tags and a correct Python 3 solution for this coding contest problem. Arthur owns a ski resort on a mountain. There are n landing spots on the mountain numbered from 1 to n from the top to the foot of the mountain. The spots are connected with one-directional ski tracks. All tracks go towards the foot of the mountain, so there are no directed cycles formed by the tracks. There are at most two tracks leaving each spot, but many tracks may enter the same spot. A skier can start skiing from one spot and stop in another spot if there is a sequence of tracks that lead from the starting spot and end in the ending spot. Unfortunately, recently there were many accidents, because the structure of the resort allows a skier to go through dangerous paths, by reaching high speed and endangering himself and the other customers. Here, a path is called dangerous, if it consists of at least two tracks. Arthur wants to secure his customers by closing some of the spots in a way that there are no dangerous paths in the resort. When a spot is closed, all tracks entering and leaving that spot become unusable. Formally, after closing some of the spots, there should not be a path that consists of two or more tracks. Arthur doesn't want to close too many spots. He will be happy to find any way to close at most 4/7n spots so that the remaining part is safe. Help him find any suitable way to do so. Input The first line contains a single positive integer T β€” the number of test cases. T test case description follows. The first line of each description contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of landing spots and tracks respectively. The following m lines describe the tracks. Each of these lines contains two integers x and y (1 ≀ x < y ≀ n) β€” indices of the starting and finishing spots for the respective track. It is guaranteed that at most two tracks start at each spot. There may be tracks in which starting and finishing spots both coincide. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, print a single integer k (0 ≀ k ≀ 4/7n) β€” the number of spots to be closed. In the next line, print k distinct integers β€” indices of all spots to be closed, in any order. If there are several answers, you may output any of them. Note that you don't have to minimize k. It can be shown that a suitable answer always exists. Example Input 2 4 6 1 2 1 3 2 3 2 4 3 4 3 4 7 6 1 2 1 3 2 4 2 5 3 6 3 7 Output 2 3 4 4 4 5 6 7 Note In the first sample case, closing any two spots is suitable. In the second sample case, closing only the spot 1 is also suitable.
instruction
0
52,396
1
104,792
Tags: constructive algorithms, graphs, greedy Correct Solution: ``` import sys input = sys.stdin.readline for f in range(int(input())): n,m=map(int,input().split()) neig=[0]*n for i in range(n): neig[i]=[0] for i in range(m): a,b=map(int,input().split()) a-=1 b-=1 neig[a][0]+=1 neig[a].append(b) lev=[1]*n for i in range(n): for j in range(1,neig[i][0]+1): x=lev[i]+1 if x==4: x=1 lev[neig[i][j]]=max(lev[neig[i][j]],x) sol=0 s=[] for i in range(n): if lev[i]==3: sol+=1 s.append(i+1) print(sol) print(*s) ```
output
1
52,396
1
104,793
Provide tags and a correct Python 3 solution for this coding contest problem. Arthur owns a ski resort on a mountain. There are n landing spots on the mountain numbered from 1 to n from the top to the foot of the mountain. The spots are connected with one-directional ski tracks. All tracks go towards the foot of the mountain, so there are no directed cycles formed by the tracks. There are at most two tracks leaving each spot, but many tracks may enter the same spot. A skier can start skiing from one spot and stop in another spot if there is a sequence of tracks that lead from the starting spot and end in the ending spot. Unfortunately, recently there were many accidents, because the structure of the resort allows a skier to go through dangerous paths, by reaching high speed and endangering himself and the other customers. Here, a path is called dangerous, if it consists of at least two tracks. Arthur wants to secure his customers by closing some of the spots in a way that there are no dangerous paths in the resort. When a spot is closed, all tracks entering and leaving that spot become unusable. Formally, after closing some of the spots, there should not be a path that consists of two or more tracks. Arthur doesn't want to close too many spots. He will be happy to find any way to close at most 4/7n spots so that the remaining part is safe. Help him find any suitable way to do so. Input The first line contains a single positive integer T β€” the number of test cases. T test case description follows. The first line of each description contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of landing spots and tracks respectively. The following m lines describe the tracks. Each of these lines contains two integers x and y (1 ≀ x < y ≀ n) β€” indices of the starting and finishing spots for the respective track. It is guaranteed that at most two tracks start at each spot. There may be tracks in which starting and finishing spots both coincide. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, print a single integer k (0 ≀ k ≀ 4/7n) β€” the number of spots to be closed. In the next line, print k distinct integers β€” indices of all spots to be closed, in any order. If there are several answers, you may output any of them. Note that you don't have to minimize k. It can be shown that a suitable answer always exists. Example Input 2 4 6 1 2 1 3 2 3 2 4 3 4 3 4 7 6 1 2 1 3 2 4 2 5 3 6 3 7 Output 2 3 4 4 4 5 6 7 Note In the first sample case, closing any two spots is suitable. In the second sample case, closing only the spot 1 is also suitable.
instruction
0
52,397
1
104,794
Tags: constructive algorithms, graphs, greedy Correct Solution: ``` import sys def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + '\n') def wi(n): sys.stdout.write(str(n) + '\n') def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n') def solve(n, m, g): dp = [0] * n ans = [] for i in range(n): for w in g[i]: dp[i] = max(dp[i], dp[w] + 1) if dp[i] >= 2: dp[i] = -1 ans.append(i+1) wi(len(ans)) wia(ans) def main(): for _ in range(ri()): n, m = ria() g = [[] for i in range(n)] for __ in range(m): u, v = ria() g[v-1].append(u-1) solve(n, m, g) if __name__ == '__main__': main() ```
output
1
52,397
1
104,795
Provide tags and a correct Python 3 solution for this coding contest problem. Arthur owns a ski resort on a mountain. There are n landing spots on the mountain numbered from 1 to n from the top to the foot of the mountain. The spots are connected with one-directional ski tracks. All tracks go towards the foot of the mountain, so there are no directed cycles formed by the tracks. There are at most two tracks leaving each spot, but many tracks may enter the same spot. A skier can start skiing from one spot and stop in another spot if there is a sequence of tracks that lead from the starting spot and end in the ending spot. Unfortunately, recently there were many accidents, because the structure of the resort allows a skier to go through dangerous paths, by reaching high speed and endangering himself and the other customers. Here, a path is called dangerous, if it consists of at least two tracks. Arthur wants to secure his customers by closing some of the spots in a way that there are no dangerous paths in the resort. When a spot is closed, all tracks entering and leaving that spot become unusable. Formally, after closing some of the spots, there should not be a path that consists of two or more tracks. Arthur doesn't want to close too many spots. He will be happy to find any way to close at most 4/7n spots so that the remaining part is safe. Help him find any suitable way to do so. Input The first line contains a single positive integer T β€” the number of test cases. T test case description follows. The first line of each description contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of landing spots and tracks respectively. The following m lines describe the tracks. Each of these lines contains two integers x and y (1 ≀ x < y ≀ n) β€” indices of the starting and finishing spots for the respective track. It is guaranteed that at most two tracks start at each spot. There may be tracks in which starting and finishing spots both coincide. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, print a single integer k (0 ≀ k ≀ 4/7n) β€” the number of spots to be closed. In the next line, print k distinct integers β€” indices of all spots to be closed, in any order. If there are several answers, you may output any of them. Note that you don't have to minimize k. It can be shown that a suitable answer always exists. Example Input 2 4 6 1 2 1 3 2 3 2 4 3 4 3 4 7 6 1 2 1 3 2 4 2 5 3 6 3 7 Output 2 3 4 4 4 5 6 7 Note In the first sample case, closing any two spots is suitable. In the second sample case, closing only the spot 1 is also suitable.
instruction
0
52,398
1
104,796
Tags: constructive algorithms, graphs, greedy Correct Solution: ``` from collections import deque import sys input = sys.stdin.buffer.readline def topological_sort(graph: list) -> list: n = len(graph);degree = [0] * n for g in graph: for next_pos in g:degree[next_pos] += 1 ans = [i for i in range(n) if degree[i] == 0];q = deque(ans) while q: pos = q.popleft() for next_pos in graph[pos]: degree[next_pos] -= 1 if degree[next_pos] == 0:q.append(next_pos);ans.append(next_pos) return ans for _ in range(int(input())): n, m = map(int, input().split());info = [list(map(int, input().split())) for i in range(m)];graph = [[] for i in range(n)];inv_graph = [[] for i in range(n)] for a, b in info:a -= 1;b -= 1;graph[a].append(b);inv_graph[b].append(a) tp_sort = topological_sort(graph);ans = [-1] * n for v in tp_sort: tmp = 0 for prv_v in inv_graph[v]:tmp = max(tmp, (ans[prv_v] + 1) % 3) ans[v] = tmp res = [i + 1 for i in range(n) if ans[i] == 2] print(len(res));print(*res) ```
output
1
52,398
1
104,797
Provide tags and a correct Python 3 solution for this coding contest problem. Arthur owns a ski resort on a mountain. There are n landing spots on the mountain numbered from 1 to n from the top to the foot of the mountain. The spots are connected with one-directional ski tracks. All tracks go towards the foot of the mountain, so there are no directed cycles formed by the tracks. There are at most two tracks leaving each spot, but many tracks may enter the same spot. A skier can start skiing from one spot and stop in another spot if there is a sequence of tracks that lead from the starting spot and end in the ending spot. Unfortunately, recently there were many accidents, because the structure of the resort allows a skier to go through dangerous paths, by reaching high speed and endangering himself and the other customers. Here, a path is called dangerous, if it consists of at least two tracks. Arthur wants to secure his customers by closing some of the spots in a way that there are no dangerous paths in the resort. When a spot is closed, all tracks entering and leaving that spot become unusable. Formally, after closing some of the spots, there should not be a path that consists of two or more tracks. Arthur doesn't want to close too many spots. He will be happy to find any way to close at most 4/7n spots so that the remaining part is safe. Help him find any suitable way to do so. Input The first line contains a single positive integer T β€” the number of test cases. T test case description follows. The first line of each description contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of landing spots and tracks respectively. The following m lines describe the tracks. Each of these lines contains two integers x and y (1 ≀ x < y ≀ n) β€” indices of the starting and finishing spots for the respective track. It is guaranteed that at most two tracks start at each spot. There may be tracks in which starting and finishing spots both coincide. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, print a single integer k (0 ≀ k ≀ 4/7n) β€” the number of spots to be closed. In the next line, print k distinct integers β€” indices of all spots to be closed, in any order. If there are several answers, you may output any of them. Note that you don't have to minimize k. It can be shown that a suitable answer always exists. Example Input 2 4 6 1 2 1 3 2 3 2 4 3 4 3 4 7 6 1 2 1 3 2 4 2 5 3 6 3 7 Output 2 3 4 4 4 5 6 7 Note In the first sample case, closing any two spots is suitable. In the second sample case, closing only the spot 1 is also suitable.
instruction
0
52,399
1
104,798
Tags: constructive algorithms, graphs, greedy Correct Solution: ``` import sys from collections import deque input = sys.stdin.buffer.readline t = int(input()) for i in range(t): n, m = [int(x) for x in input().split()] graph = [[] for j in range(n)] in_degree = [0 for j in range(n)] for j in range(m): a, b = [int(x)-1 for x in input().split()] graph[a].append(b) in_degree[b] += 1 q = deque() for j in range(n): if in_degree[j] == 0: q.append(j) s = [] while len(q) > 0: u = q.popleft() s.append(u) for v in graph[u]: in_degree[v] -= 1 if in_degree[v] == 0: q.append(v) depth = [2 for x in range(n)] for u in s: if depth[u] > 0: for v in graph[u]: depth[v] = min(depth[v], depth[u] - 1) sol = [j+1 for j in range(n) if depth[j] == 0] print(len(sol)) print(*sol) ```
output
1
52,399
1
104,799
Provide tags and a correct Python 3 solution for this coding contest problem. Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages. The river banks can be assumed to be vertical straight lines x = a and x = b (0 < a < b). The west village lies in a steppe at point O = (0, 0). There are n pathways leading from the village to the river, they end at points Ai = (a, yi). The villagers there are plain and simple, so their pathways are straight segments as well. The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are m twisted paths leading from this village to the river and ending at points Bi = (b, y'i). The lengths of all these paths are known, the length of the path that leads from the eastern village to point Bi, equals li. The villagers want to choose exactly one point on the left bank of river Ai, exactly one point on the right bank Bj and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |OAi| + |AiBj| + lj, where |XY| is the Euclidean distance between points X and Y) were minimum. The Euclidean distance between points (x1, y1) and (x2, y2) equals <image>. Help them and find the required pair of points. Input The first line contains integers n, m, a, b (1 ≀ n, m ≀ 105, 0 < a < b < 106). The second line contains n integers in the ascending order: the i-th integer determines the coordinate of point Ai and equals yi (|yi| ≀ 106). The third line contains m integers in the ascending order: the i-th integer determines the coordinate of point Bi and equals y'i (|y'i| ≀ 106). The fourth line contains m more integers: the i-th of them determines the length of the path that connects the eastern village and point Bi, and equals li (1 ≀ li ≀ 106). It is guaranteed, that there is such a point C with abscissa at least b, that |BiC| ≀ li for all i (1 ≀ i ≀ m). It is guaranteed that no two points Ai coincide. It is guaranteed that no two points Bi coincide. Output Print two integers β€” the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to n, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to m in the order in which they are given in the input. If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10 - 6 in absolute or relative value. Examples Input 3 2 3 5 -2 -1 4 -1 2 7 3 Output 2 2
instruction
0
52,510
1
105,020
Tags: geometry, ternary search, two pointers Correct Solution: ``` import sys from itertools import * from math import * def solve(): n,m,leftbank,rightbank = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) l = list(map(int, input().split())) smallx = leftbank leftbest, rightbest, distbest = -100, -100, 100000000 for i, bcord, length in zip(count(), b, l): wanty = bcord * smallx / rightbank ll , rr = 0, n - 1 while ll < rr: mm = (ll + rr + 1) // 2 if a[mm] > wanty: rr = mm - 1 else: ll = mm for pos in range(ll - 1, ll + 2): if pos >= 0 and pos < n: first = sqrt(smallx * smallx + a[pos] * a[pos]) second = sqrt((rightbank -leftbank)*(rightbank - leftbank) + (bcord - a[pos])*(bcord - a[pos])) totaldist = first + second + length if totaldist < distbest: distbest = totaldist leftbest = pos rightbest = i print(leftbest + 1, rightbest + 1) if sys.hexversion == 50594544 : sys.stdin = open("test.txt") solve() ```
output
1
52,510
1
105,021
Provide tags and a correct Python 3 solution for this coding contest problem. Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages. The river banks can be assumed to be vertical straight lines x = a and x = b (0 < a < b). The west village lies in a steppe at point O = (0, 0). There are n pathways leading from the village to the river, they end at points Ai = (a, yi). The villagers there are plain and simple, so their pathways are straight segments as well. The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are m twisted paths leading from this village to the river and ending at points Bi = (b, y'i). The lengths of all these paths are known, the length of the path that leads from the eastern village to point Bi, equals li. The villagers want to choose exactly one point on the left bank of river Ai, exactly one point on the right bank Bj and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |OAi| + |AiBj| + lj, where |XY| is the Euclidean distance between points X and Y) were minimum. The Euclidean distance between points (x1, y1) and (x2, y2) equals <image>. Help them and find the required pair of points. Input The first line contains integers n, m, a, b (1 ≀ n, m ≀ 105, 0 < a < b < 106). The second line contains n integers in the ascending order: the i-th integer determines the coordinate of point Ai and equals yi (|yi| ≀ 106). The third line contains m integers in the ascending order: the i-th integer determines the coordinate of point Bi and equals y'i (|y'i| ≀ 106). The fourth line contains m more integers: the i-th of them determines the length of the path that connects the eastern village and point Bi, and equals li (1 ≀ li ≀ 106). It is guaranteed, that there is such a point C with abscissa at least b, that |BiC| ≀ li for all i (1 ≀ i ≀ m). It is guaranteed that no two points Ai coincide. It is guaranteed that no two points Bi coincide. Output Print two integers β€” the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to n, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to m in the order in which they are given in the input. If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10 - 6 in absolute or relative value. Examples Input 3 2 3 5 -2 -1 4 -1 2 7 3 Output 2 2
instruction
0
52,511
1
105,022
Tags: geometry, ternary search, two pointers Correct Solution: ``` import sys from itertools import * from math import * def solve(): n,m,leftbank,rightbank = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) l = list(map(int, input().split())) smallx = leftbank leftbest, rightbest, distbest = -1, -1, 100000000 for i, bcord, length in zip(count(), b, l): wanty = bcord * smallx / rightbank ll , rr = 0, n - 1 while ll < rr: mm = (ll + rr + 1) // 2 if a[mm] > wanty: rr = mm - 1 else: ll = mm for pos in range(ll, ll + 2): if pos >= 0 and pos < n: first = sqrt(smallx * smallx + a[pos] * a[pos]) second = sqrt((rightbank -leftbank)*(rightbank - leftbank) + (bcord - a[pos])*(bcord - a[pos])) totaldist = first + second + length if totaldist < distbest: distbest = totaldist leftbest = pos rightbest = i print(leftbest + 1, rightbest + 1) if sys.hexversion == 50594544 : sys.stdin = open("test.txt") solve() ```
output
1
52,511
1
105,023
Provide tags and a correct Python 3 solution for this coding contest problem. Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages. The river banks can be assumed to be vertical straight lines x = a and x = b (0 < a < b). The west village lies in a steppe at point O = (0, 0). There are n pathways leading from the village to the river, they end at points Ai = (a, yi). The villagers there are plain and simple, so their pathways are straight segments as well. The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are m twisted paths leading from this village to the river and ending at points Bi = (b, y'i). The lengths of all these paths are known, the length of the path that leads from the eastern village to point Bi, equals li. The villagers want to choose exactly one point on the left bank of river Ai, exactly one point on the right bank Bj and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |OAi| + |AiBj| + lj, where |XY| is the Euclidean distance between points X and Y) were minimum. The Euclidean distance between points (x1, y1) and (x2, y2) equals <image>. Help them and find the required pair of points. Input The first line contains integers n, m, a, b (1 ≀ n, m ≀ 105, 0 < a < b < 106). The second line contains n integers in the ascending order: the i-th integer determines the coordinate of point Ai and equals yi (|yi| ≀ 106). The third line contains m integers in the ascending order: the i-th integer determines the coordinate of point Bi and equals y'i (|y'i| ≀ 106). The fourth line contains m more integers: the i-th of them determines the length of the path that connects the eastern village and point Bi, and equals li (1 ≀ li ≀ 106). It is guaranteed, that there is such a point C with abscissa at least b, that |BiC| ≀ li for all i (1 ≀ i ≀ m). It is guaranteed that no two points Ai coincide. It is guaranteed that no two points Bi coincide. Output Print two integers β€” the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to n, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to m in the order in which they are given in the input. If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10 - 6 in absolute or relative value. Examples Input 3 2 3 5 -2 -1 4 -1 2 7 3 Output 2 2
instruction
0
52,512
1
105,024
Tags: geometry, ternary search, two pointers Correct Solution: ``` import sys def pro(): return sys.stdin.readline().strip() def rop(): return map(int, pro().split()) def main(): s = list(rop()) a = list(rop()) q = list(rop()) o = list(rop()) p = -1 t = (1e100, -1, -1) for i in range(s[1]): while not((p == - 1 or s[2] * q[i] - s[3] * a[p] >= 0) and (p + 1 == s[0] or s[2] * q[i] - s[3] * a[p+1] < 0)): p += 1 if p != -1: t = min(t, (o[i] + (s[2] ** 2 + a[p] ** 2) ** 0.5 + ((s[3] - s[2]) ** 2 + (q[i] - a[p]) ** 2) ** 0.5, p, i)) if p + 1 != s[0]: t = min(t, (o[i] + (s[2] ** 2 + a[p + 1] ** 2) ** 0.5 + ((s[3] - s[2]) ** 2 + (q[i] - a[p + 1]) ** 2) ** 0.5, p + 1, i)) print(t[1] + 1, t[2] + 1) main() ```
output
1
52,512
1
105,025
Provide tags and a correct Python 3 solution for this coding contest problem. Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages. The river banks can be assumed to be vertical straight lines x = a and x = b (0 < a < b). The west village lies in a steppe at point O = (0, 0). There are n pathways leading from the village to the river, they end at points Ai = (a, yi). The villagers there are plain and simple, so their pathways are straight segments as well. The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are m twisted paths leading from this village to the river and ending at points Bi = (b, y'i). The lengths of all these paths are known, the length of the path that leads from the eastern village to point Bi, equals li. The villagers want to choose exactly one point on the left bank of river Ai, exactly one point on the right bank Bj and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |OAi| + |AiBj| + lj, where |XY| is the Euclidean distance between points X and Y) were minimum. The Euclidean distance between points (x1, y1) and (x2, y2) equals <image>. Help them and find the required pair of points. Input The first line contains integers n, m, a, b (1 ≀ n, m ≀ 105, 0 < a < b < 106). The second line contains n integers in the ascending order: the i-th integer determines the coordinate of point Ai and equals yi (|yi| ≀ 106). The third line contains m integers in the ascending order: the i-th integer determines the coordinate of point Bi and equals y'i (|y'i| ≀ 106). The fourth line contains m more integers: the i-th of them determines the length of the path that connects the eastern village and point Bi, and equals li (1 ≀ li ≀ 106). It is guaranteed, that there is such a point C with abscissa at least b, that |BiC| ≀ li for all i (1 ≀ i ≀ m). It is guaranteed that no two points Ai coincide. It is guaranteed that no two points Bi coincide. Output Print two integers β€” the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to n, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to m in the order in which they are given in the input. If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10 - 6 in absolute or relative value. Examples Input 3 2 3 5 -2 -1 4 -1 2 7 3 Output 2 2
instruction
0
52,513
1
105,026
Tags: geometry, ternary search, two pointers Correct Solution: ``` import sys from itertools import * from math import * def solve(): n,m,leftbank,rightbank = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) l = list(map(int, input().split())) smallx = leftbank leftbest, rightbest, distbest = -100, -100, 100000000 for i, bcord, length in zip(count(), b, l): wanty = bcord * smallx / rightbank ll , rr = 0, n - 1 while ll < rr: mm = (ll + rr + 1) // 2 if a[mm] > wanty: rr = mm - 1 else: ll = mm for pos in range(ll - 3, ll + 4): if pos >= 0 and pos < n: first = sqrt(smallx * smallx + a[pos] * a[pos]) second = sqrt((rightbank -leftbank)*(rightbank - leftbank) + (bcord - a[pos])*(bcord - a[pos])) totaldist = first + second + length if totaldist < distbest: distbest = totaldist leftbest = pos rightbest = i print(leftbest + 1, rightbest + 1) if sys.hexversion == 50594544 : sys.stdin = open("test.txt") solve() ```
output
1
52,513
1
105,027
Provide tags and a correct Python 3 solution for this coding contest problem. Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages. The river banks can be assumed to be vertical straight lines x = a and x = b (0 < a < b). The west village lies in a steppe at point O = (0, 0). There are n pathways leading from the village to the river, they end at points Ai = (a, yi). The villagers there are plain and simple, so their pathways are straight segments as well. The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are m twisted paths leading from this village to the river and ending at points Bi = (b, y'i). The lengths of all these paths are known, the length of the path that leads from the eastern village to point Bi, equals li. The villagers want to choose exactly one point on the left bank of river Ai, exactly one point on the right bank Bj and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |OAi| + |AiBj| + lj, where |XY| is the Euclidean distance between points X and Y) were minimum. The Euclidean distance between points (x1, y1) and (x2, y2) equals <image>. Help them and find the required pair of points. Input The first line contains integers n, m, a, b (1 ≀ n, m ≀ 105, 0 < a < b < 106). The second line contains n integers in the ascending order: the i-th integer determines the coordinate of point Ai and equals yi (|yi| ≀ 106). The third line contains m integers in the ascending order: the i-th integer determines the coordinate of point Bi and equals y'i (|y'i| ≀ 106). The fourth line contains m more integers: the i-th of them determines the length of the path that connects the eastern village and point Bi, and equals li (1 ≀ li ≀ 106). It is guaranteed, that there is such a point C with abscissa at least b, that |BiC| ≀ li for all i (1 ≀ i ≀ m). It is guaranteed that no two points Ai coincide. It is guaranteed that no two points Bi coincide. Output Print two integers β€” the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to n, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to m in the order in which they are given in the input. If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10 - 6 in absolute or relative value. Examples Input 3 2 3 5 -2 -1 4 -1 2 7 3 Output 2 2
instruction
0
52,514
1
105,028
Tags: geometry, ternary search, two pointers Correct Solution: ``` import sys from itertools import * from math import * def solve(): n,m,leftbank,rightbank = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) l = list(map(int, input().split())) smallx = leftbank smallxsquared = smallx * smallx rightbankminusleftbanksquared = (rightbank -leftbank)*(rightbank - leftbank) leftbest, rightbest, distbest = -1, -1, 100000000 for i, bcord, length in zip(count(), b, l): wanty = bcord * smallx / rightbank ll , rr = 0, n - 1 while ll < rr: mm = (ll + rr + 1) // 2 if a[mm] > wanty: rr = mm - 1 else: ll = mm for pos in range(ll, ll + 2): if pos < n: temp = a[pos] first = sqrt(smallxsquared + temp * temp) temp = bcord - a[pos] second = sqrt(rightbankminusleftbanksquared + temp * temp) totaldist = first + second + length if totaldist < distbest: distbest = totaldist leftbest = pos rightbest = i print(leftbest + 1, rightbest + 1) if sys.hexversion == 50594544 : sys.stdin = open("test.txt") solve() ```
output
1
52,514
1
105,029
Provide tags and a correct Python 3 solution for this coding contest problem. Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages. The river banks can be assumed to be vertical straight lines x = a and x = b (0 < a < b). The west village lies in a steppe at point O = (0, 0). There are n pathways leading from the village to the river, they end at points Ai = (a, yi). The villagers there are plain and simple, so their pathways are straight segments as well. The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are m twisted paths leading from this village to the river and ending at points Bi = (b, y'i). The lengths of all these paths are known, the length of the path that leads from the eastern village to point Bi, equals li. The villagers want to choose exactly one point on the left bank of river Ai, exactly one point on the right bank Bj and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |OAi| + |AiBj| + lj, where |XY| is the Euclidean distance between points X and Y) were minimum. The Euclidean distance between points (x1, y1) and (x2, y2) equals <image>. Help them and find the required pair of points. Input The first line contains integers n, m, a, b (1 ≀ n, m ≀ 105, 0 < a < b < 106). The second line contains n integers in the ascending order: the i-th integer determines the coordinate of point Ai and equals yi (|yi| ≀ 106). The third line contains m integers in the ascending order: the i-th integer determines the coordinate of point Bi and equals y'i (|y'i| ≀ 106). The fourth line contains m more integers: the i-th of them determines the length of the path that connects the eastern village and point Bi, and equals li (1 ≀ li ≀ 106). It is guaranteed, that there is such a point C with abscissa at least b, that |BiC| ≀ li for all i (1 ≀ i ≀ m). It is guaranteed that no two points Ai coincide. It is guaranteed that no two points Bi coincide. Output Print two integers β€” the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to n, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to m in the order in which they are given in the input. If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10 - 6 in absolute or relative value. Examples Input 3 2 3 5 -2 -1 4 -1 2 7 3 Output 2 2
instruction
0
52,515
1
105,030
Tags: geometry, ternary search, two pointers Correct Solution: ``` import sys from itertools import * from math import * def solve(): n,m,leftbank,rightbank = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) l = list(map(int, input().split())) smallx = leftbank smallxsquared = smallx * smallx rightbankminusleftbanksquared = (rightbank -leftbank)*(rightbank - leftbank) leftbest, rightbest, distbest = -1, -1, 100000000 ll = 0 for i, bcord, length in zip(count(), b, l): wanty = bcord * smallx / rightbank while ll < n and a[ll] <= wanty: ll+=1 for pos in range(ll - 1, ll + 1): if pos >= 0 and pos < n: first = sqrt(smallxsquared + a[pos] * a[pos]) second = sqrt(rightbankminusleftbanksquared + (bcord - a[pos]) * (bcord - a[pos])) totaldist = first + second + length if totaldist < distbest: distbest = totaldist leftbest = pos rightbest = i print(leftbest + 1, rightbest + 1) if sys.hexversion == 50594544 : sys.stdin = open("test.txt") solve() ```
output
1
52,515
1
105,031
Provide tags and a correct Python 3 solution for this coding contest problem. Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages. The river banks can be assumed to be vertical straight lines x = a and x = b (0 < a < b). The west village lies in a steppe at point O = (0, 0). There are n pathways leading from the village to the river, they end at points Ai = (a, yi). The villagers there are plain and simple, so their pathways are straight segments as well. The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are m twisted paths leading from this village to the river and ending at points Bi = (b, y'i). The lengths of all these paths are known, the length of the path that leads from the eastern village to point Bi, equals li. The villagers want to choose exactly one point on the left bank of river Ai, exactly one point on the right bank Bj and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |OAi| + |AiBj| + lj, where |XY| is the Euclidean distance between points X and Y) were minimum. The Euclidean distance between points (x1, y1) and (x2, y2) equals <image>. Help them and find the required pair of points. Input The first line contains integers n, m, a, b (1 ≀ n, m ≀ 105, 0 < a < b < 106). The second line contains n integers in the ascending order: the i-th integer determines the coordinate of point Ai and equals yi (|yi| ≀ 106). The third line contains m integers in the ascending order: the i-th integer determines the coordinate of point Bi and equals y'i (|y'i| ≀ 106). The fourth line contains m more integers: the i-th of them determines the length of the path that connects the eastern village and point Bi, and equals li (1 ≀ li ≀ 106). It is guaranteed, that there is such a point C with abscissa at least b, that |BiC| ≀ li for all i (1 ≀ i ≀ m). It is guaranteed that no two points Ai coincide. It is guaranteed that no two points Bi coincide. Output Print two integers β€” the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to n, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to m in the order in which they are given in the input. If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10 - 6 in absolute or relative value. Examples Input 3 2 3 5 -2 -1 4 -1 2 7 3 Output 2 2
instruction
0
52,516
1
105,032
Tags: geometry, ternary search, two pointers Correct Solution: ``` __author__ = 'Michael Ilyin' import math # debug = True debug = False def dist(x1, y1, x2, y2): return math.sqrt(math.pow(math.fabs(x1 - x2), 2) + math.pow(math.fabs(y1 - y2), 2)) def get_y(x1, y1, x2, y2, x): return (((x - x1) * (y2 - y1)) / (x2 - x1)) + y1 if debug: with open("input.txt", "r") as inp: firstLine = inp.readline() secondLine = inp.readline() thirdLine = inp.readline() fourthLine = inp.readline() else: firstLine = input() secondLine = input() thirdLine = input() fourthLine = input() first = firstLine.split() n = float(first[0]) m = float(first[1]) a = float(first[2]) b = float(first[3]) A = [float(x) for x in secondLine.split()] B = [float(x) for x in thirdLine.split()] L = [float(x) for x in fourthLine.split()] if debug: print(A) print(B) print(L) optimalLen = float("inf") optimalBIdx = -1 for i, bi in enumerate(B): d = dist(0, 0, b, bi) + L[i] if d <= optimalLen: optimalLen = d optimalBIdx = i if debug: print(optimalBIdx + 1, optimalLen) intersectY = get_y(0, 0, b, B[optimalBIdx], a) if debug: print(intersectY) pointDist = float("inf") optimalAIdx = -1 for i, ai in enumerate(A): d = dist(a, ai, a, intersectY) if d < pointDist: pointDist = d optimalAIdx = i if debug: print(optimalAIdx + 1, pointDist) optimalLen = float("inf") optimalBIdx = -1 for i, bi in enumerate(B): d = dist(a, A[optimalAIdx], b, bi) + L[i] if d <= optimalLen: optimalLen = d optimalBIdx = i print(optimalAIdx + 1, optimalBIdx + 1) ```
output
1
52,516
1
105,033
Provide tags and a correct Python 3 solution for this coding contest problem. Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages. The river banks can be assumed to be vertical straight lines x = a and x = b (0 < a < b). The west village lies in a steppe at point O = (0, 0). There are n pathways leading from the village to the river, they end at points Ai = (a, yi). The villagers there are plain and simple, so their pathways are straight segments as well. The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are m twisted paths leading from this village to the river and ending at points Bi = (b, y'i). The lengths of all these paths are known, the length of the path that leads from the eastern village to point Bi, equals li. The villagers want to choose exactly one point on the left bank of river Ai, exactly one point on the right bank Bj and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |OAi| + |AiBj| + lj, where |XY| is the Euclidean distance between points X and Y) were minimum. The Euclidean distance between points (x1, y1) and (x2, y2) equals <image>. Help them and find the required pair of points. Input The first line contains integers n, m, a, b (1 ≀ n, m ≀ 105, 0 < a < b < 106). The second line contains n integers in the ascending order: the i-th integer determines the coordinate of point Ai and equals yi (|yi| ≀ 106). The third line contains m integers in the ascending order: the i-th integer determines the coordinate of point Bi and equals y'i (|y'i| ≀ 106). The fourth line contains m more integers: the i-th of them determines the length of the path that connects the eastern village and point Bi, and equals li (1 ≀ li ≀ 106). It is guaranteed, that there is such a point C with abscissa at least b, that |BiC| ≀ li for all i (1 ≀ i ≀ m). It is guaranteed that no two points Ai coincide. It is guaranteed that no two points Bi coincide. Output Print two integers β€” the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to n, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to m in the order in which they are given in the input. If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10 - 6 in absolute or relative value. Examples Input 3 2 3 5 -2 -1 4 -1 2 7 3 Output 2 2
instruction
0
52,517
1
105,034
Tags: geometry, ternary search, two pointers Correct Solution: ``` from math import sqrt,fabs def dist(x1, y1, x2, y2): return sqrt(pow(abs(x1 - x2), 2) + pow(abs(y1 - y2), 2)) def calcOptimumRightPoint(startX, startY): l = float("inf") idx = -1 for i in range(len(B)): d = dist(startX, startY, b, B[i]) + L[i] if d <= l: l = d idx = i return idx n,m,a,b = [int(x) for x in input().split()] A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] L = [int(x) for x in input().split()] optimumRightPoint = calcOptimumRightPoint(0,0) intersectLeft = (a * B[optimumRightPoint]) / b l = float("inf") optimumLeftPoint = -1 for i in range(len(A)): if fabs(intersectLeft-A[i]) < l: l = fabs(intersectLeft-A[i]) optimumLeftPoint = i optimumRightPoint = calcOptimumRightPoint(a, A[optimumLeftPoint]) print(optimumLeftPoint + 1, optimumRightPoint + 1) ```
output
1
52,517
1
105,035
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages. The river banks can be assumed to be vertical straight lines x = a and x = b (0 < a < b). The west village lies in a steppe at point O = (0, 0). There are n pathways leading from the village to the river, they end at points Ai = (a, yi). The villagers there are plain and simple, so their pathways are straight segments as well. The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are m twisted paths leading from this village to the river and ending at points Bi = (b, y'i). The lengths of all these paths are known, the length of the path that leads from the eastern village to point Bi, equals li. The villagers want to choose exactly one point on the left bank of river Ai, exactly one point on the right bank Bj and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |OAi| + |AiBj| + lj, where |XY| is the Euclidean distance between points X and Y) were minimum. The Euclidean distance between points (x1, y1) and (x2, y2) equals <image>. Help them and find the required pair of points. Input The first line contains integers n, m, a, b (1 ≀ n, m ≀ 105, 0 < a < b < 106). The second line contains n integers in the ascending order: the i-th integer determines the coordinate of point Ai and equals yi (|yi| ≀ 106). The third line contains m integers in the ascending order: the i-th integer determines the coordinate of point Bi and equals y'i (|y'i| ≀ 106). The fourth line contains m more integers: the i-th of them determines the length of the path that connects the eastern village and point Bi, and equals li (1 ≀ li ≀ 106). It is guaranteed, that there is such a point C with abscissa at least b, that |BiC| ≀ li for all i (1 ≀ i ≀ m). It is guaranteed that no two points Ai coincide. It is guaranteed that no two points Bi coincide. Output Print two integers β€” the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to n, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to m in the order in which they are given in the input. If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10 - 6 in absolute or relative value. Examples Input 3 2 3 5 -2 -1 4 -1 2 7 3 Output 2 2 Submitted Solution: ``` import sys from math import * def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) n, m, a, b = mints() A = list(mints()) B = list(mints()) l = list(mints()) j = -1 r = (1e100,-1,-1) for i in range(m): while not((j == -1 or a*B[i]-b*A[j] >= 0) \ and (j+1 == n or a*B[i]-b*A[j+1] < 0)): j += 1 #print(j)#,a*B[i]-b*A[j],a*B[i]-b*A[j+1]) if j != -1: r = min(r,(l[i]+sqrt(a*a+A[j]*A[j])+sqrt((b-a)**2+(B[i]-A[j])**2),j,i)) if j+1 != n: r = min(r,(l[i]+sqrt(a*a+A[j+1]*A[j+1])+sqrt((b-a)**2+(B[i]-A[j+1])**2),j+1,i)) print(r[1]+1,r[2]+1) ```
instruction
0
52,518
1
105,036
Yes
output
1
52,518
1
105,037
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages. The river banks can be assumed to be vertical straight lines x = a and x = b (0 < a < b). The west village lies in a steppe at point O = (0, 0). There are n pathways leading from the village to the river, they end at points Ai = (a, yi). The villagers there are plain and simple, so their pathways are straight segments as well. The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are m twisted paths leading from this village to the river and ending at points Bi = (b, y'i). The lengths of all these paths are known, the length of the path that leads from the eastern village to point Bi, equals li. The villagers want to choose exactly one point on the left bank of river Ai, exactly one point on the right bank Bj and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |OAi| + |AiBj| + lj, where |XY| is the Euclidean distance between points X and Y) were minimum. The Euclidean distance between points (x1, y1) and (x2, y2) equals <image>. Help them and find the required pair of points. Input The first line contains integers n, m, a, b (1 ≀ n, m ≀ 105, 0 < a < b < 106). The second line contains n integers in the ascending order: the i-th integer determines the coordinate of point Ai and equals yi (|yi| ≀ 106). The third line contains m integers in the ascending order: the i-th integer determines the coordinate of point Bi and equals y'i (|y'i| ≀ 106). The fourth line contains m more integers: the i-th of them determines the length of the path that connects the eastern village and point Bi, and equals li (1 ≀ li ≀ 106). It is guaranteed, that there is such a point C with abscissa at least b, that |BiC| ≀ li for all i (1 ≀ i ≀ m). It is guaranteed that no two points Ai coincide. It is guaranteed that no two points Bi coincide. Output Print two integers β€” the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to n, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to m in the order in which they are given in the input. If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10 - 6 in absolute or relative value. Examples Input 3 2 3 5 -2 -1 4 -1 2 7 3 Output 2 2 Submitted Solution: ``` import sys from itertools import * from math import * def solve(): n,m,leftbank,rightbank = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) l = list(map(int, input().split())) smallx = leftbank smallxsquared = smallx * smallx rightbankminusleftbanksquared = (rightbank -leftbank)*(rightbank - leftbank) leftbest, rightbest, distbest = -1, -1, 100000000 for i, bcord, length in zip(count(), b, l): wanty = bcord * smallx / rightbank ll , rr = 0, n - 1 while ll < rr: mm = (ll + rr + 1) // 2 if a[mm] > wanty: rr = mm - 1 else: ll = mm for pos in range(ll, ll + 2): if pos < n: first = sqrt(smallxsquared + a[pos] * a[pos]) second = sqrt(rightbankminusleftbanksquared + (bcord - a[pos]) * (bcord - a[pos])) totaldist = first + second + length if totaldist < distbest: distbest = totaldist leftbest = pos rightbest = i print(leftbest + 1, rightbest + 1) if sys.hexversion == 50594544 : sys.stdin = open("test.txt") solve() ```
instruction
0
52,519
1
105,038
Yes
output
1
52,519
1
105,039
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages. The river banks can be assumed to be vertical straight lines x = a and x = b (0 < a < b). The west village lies in a steppe at point O = (0, 0). There are n pathways leading from the village to the river, they end at points Ai = (a, yi). The villagers there are plain and simple, so their pathways are straight segments as well. The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are m twisted paths leading from this village to the river and ending at points Bi = (b, y'i). The lengths of all these paths are known, the length of the path that leads from the eastern village to point Bi, equals li. The villagers want to choose exactly one point on the left bank of river Ai, exactly one point on the right bank Bj and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |OAi| + |AiBj| + lj, where |XY| is the Euclidean distance between points X and Y) were minimum. The Euclidean distance between points (x1, y1) and (x2, y2) equals <image>. Help them and find the required pair of points. Input The first line contains integers n, m, a, b (1 ≀ n, m ≀ 105, 0 < a < b < 106). The second line contains n integers in the ascending order: the i-th integer determines the coordinate of point Ai and equals yi (|yi| ≀ 106). The third line contains m integers in the ascending order: the i-th integer determines the coordinate of point Bi and equals y'i (|y'i| ≀ 106). The fourth line contains m more integers: the i-th of them determines the length of the path that connects the eastern village and point Bi, and equals li (1 ≀ li ≀ 106). It is guaranteed, that there is such a point C with abscissa at least b, that |BiC| ≀ li for all i (1 ≀ i ≀ m). It is guaranteed that no two points Ai coincide. It is guaranteed that no two points Bi coincide. Output Print two integers β€” the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to n, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to m in the order in which they are given in the input. If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10 - 6 in absolute or relative value. Examples Input 3 2 3 5 -2 -1 4 -1 2 7 3 Output 2 2 Submitted Solution: ``` # http://codeforces.com/problemset/problem/250/D from math import sqrt def calcPath(x1, y1, x2, y2): return sqrt(pow(x1-x2,2)+pow(y1-y2,2)) (n,m,a,b) = [int(x) for x in input().strip().split(' ')] A = [int(x) for x in input().strip().split(' ')] B = [int(x) for x in input().strip().split(' ')] L = [int(x) for x in input().strip().split(' ')] i = 0 j = 0 min_path = 0 min_i = 0 min_j = 0 curr_path = 0 flagDec = False res = {} while i<m: while j<n: curr_path = calcPath(0,0,a,A[j]) + calcPath(a,A[j],b,B[i])+L[i] if min_path < curr_path and min_path != 0: i += 1 if flagDec: j -= 1 flagDec = False break min_path = curr_path min_i = i+1 min_j = j+1 j += 1 flagDec = True if m == 1: break print(str(min_j)+" "+str(min_i)) ```
instruction
0
52,520
1
105,040
No
output
1
52,520
1
105,041
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages. The river banks can be assumed to be vertical straight lines x = a and x = b (0 < a < b). The west village lies in a steppe at point O = (0, 0). There are n pathways leading from the village to the river, they end at points Ai = (a, yi). The villagers there are plain and simple, so their pathways are straight segments as well. The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are m twisted paths leading from this village to the river and ending at points Bi = (b, y'i). The lengths of all these paths are known, the length of the path that leads from the eastern village to point Bi, equals li. The villagers want to choose exactly one point on the left bank of river Ai, exactly one point on the right bank Bj and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |OAi| + |AiBj| + lj, where |XY| is the Euclidean distance between points X and Y) were minimum. The Euclidean distance between points (x1, y1) and (x2, y2) equals <image>. Help them and find the required pair of points. Input The first line contains integers n, m, a, b (1 ≀ n, m ≀ 105, 0 < a < b < 106). The second line contains n integers in the ascending order: the i-th integer determines the coordinate of point Ai and equals yi (|yi| ≀ 106). The third line contains m integers in the ascending order: the i-th integer determines the coordinate of point Bi and equals y'i (|y'i| ≀ 106). The fourth line contains m more integers: the i-th of them determines the length of the path that connects the eastern village and point Bi, and equals li (1 ≀ li ≀ 106). It is guaranteed, that there is such a point C with abscissa at least b, that |BiC| ≀ li for all i (1 ≀ i ≀ m). It is guaranteed that no two points Ai coincide. It is guaranteed that no two points Bi coincide. Output Print two integers β€” the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to n, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to m in the order in which they are given in the input. If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10 - 6 in absolute or relative value. Examples Input 3 2 3 5 -2 -1 4 -1 2 7 3 Output 2 2 Submitted Solution: ``` # http://codeforces.com/problemset/problem/250/D # inputFile.readline from math import sqrt def calcPath(x1, y1, x2, y2): return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)) # with open('h.in', 'r') as inputFile, open('h.out', 'w') as outputFile: (n, m, a, b) = [int(x) for x in input().strip().split(' ')] A = [int(x) for x in input().strip().split(' ')] B = [int(x) for x in input().strip().split(' ')] L = [int(x) for x in input().strip().split(' ')] # print(n,m,a,b) # print(A,B,L) i = 0 j = 0 min_path = float("inf") min_i = 0 min_j = 0 # curr_path = 0 flagDec = False if n == 1 and m == 1: print("1 1") else: while i < m: c_min_path = float("-inf") curr_path = float("inf") while j < n: curr_path = calcPath(0, 0, a, A[j]) + calcPath(a, A[j], b, B[i]) + L[i] if c_min_path < curr_path and c_min_path != 0: # res[min_path] = [j, i + 1] if c_min_path < min_path: min_path = c_min_path min_i = i + 1 min_j = j i += 1 if flagDec: j -= 1 flagDec = False # print(j,i,curr_path) break c_min_path = curr_path # print(res) # min_i = i+1 # min_j = j+1 # print(min_j,min_i,min_path) j += 1 flagDec = True if j == n: break # print(str(min_j)+" "+str(min_i)) # print(res) print(str(min_j) + " " + str(min_i)) ```
instruction
0
52,521
1
105,042
No
output
1
52,521
1
105,043
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages. The river banks can be assumed to be vertical straight lines x = a and x = b (0 < a < b). The west village lies in a steppe at point O = (0, 0). There are n pathways leading from the village to the river, they end at points Ai = (a, yi). The villagers there are plain and simple, so their pathways are straight segments as well. The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are m twisted paths leading from this village to the river and ending at points Bi = (b, y'i). The lengths of all these paths are known, the length of the path that leads from the eastern village to point Bi, equals li. The villagers want to choose exactly one point on the left bank of river Ai, exactly one point on the right bank Bj and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |OAi| + |AiBj| + lj, where |XY| is the Euclidean distance between points X and Y) were minimum. The Euclidean distance between points (x1, y1) and (x2, y2) equals <image>. Help them and find the required pair of points. Input The first line contains integers n, m, a, b (1 ≀ n, m ≀ 105, 0 < a < b < 106). The second line contains n integers in the ascending order: the i-th integer determines the coordinate of point Ai and equals yi (|yi| ≀ 106). The third line contains m integers in the ascending order: the i-th integer determines the coordinate of point Bi and equals y'i (|y'i| ≀ 106). The fourth line contains m more integers: the i-th of them determines the length of the path that connects the eastern village and point Bi, and equals li (1 ≀ li ≀ 106). It is guaranteed, that there is such a point C with abscissa at least b, that |BiC| ≀ li for all i (1 ≀ i ≀ m). It is guaranteed that no two points Ai coincide. It is guaranteed that no two points Bi coincide. Output Print two integers β€” the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to n, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to m in the order in which they are given in the input. If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10 - 6 in absolute or relative value. Examples Input 3 2 3 5 -2 -1 4 -1 2 7 3 Output 2 2 Submitted Solution: ``` # http://codeforces.com/problemset/problem/250/D from math import sqrt def calcPath(x1, y1, x2, y2): return sqrt(pow(x1-x2,2)+pow(y1-y2,2)) # with open('h.in', 'r') as inputFile, open('h.out', 'w') as outputFile: (n,m,a,b) = [int(x) for x in input().strip().split(' ')] A = [int(x) for x in input().strip().split(' ')] B = [int(x) for x in input().strip().split(' ')] L = [int(x) for x in input().strip().split(' ')] # print(n,m,a,b) # print(A,B,L) i = 0 res = {} while i<m: min_path = 0 curr_path = 0 j = 0 while j<n: curr_path = calcPath(0,0,a,A[j])+ calcPath(a,A[j],b,B[i])+L[i] if min_path < curr_path and min_path != 0: break min_path = curr_path res[min_path] = [i+1,j+1]; j += 1 i += 1 min_k = min(res.keys()) # print(res) print(str(res[min_k][0])+" "+str(res[min_k][1])) ```
instruction
0
52,522
1
105,044
No
output
1
52,522
1
105,045
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages. The river banks can be assumed to be vertical straight lines x = a and x = b (0 < a < b). The west village lies in a steppe at point O = (0, 0). There are n pathways leading from the village to the river, they end at points Ai = (a, yi). The villagers there are plain and simple, so their pathways are straight segments as well. The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are m twisted paths leading from this village to the river and ending at points Bi = (b, y'i). The lengths of all these paths are known, the length of the path that leads from the eastern village to point Bi, equals li. The villagers want to choose exactly one point on the left bank of river Ai, exactly one point on the right bank Bj and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |OAi| + |AiBj| + lj, where |XY| is the Euclidean distance between points X and Y) were minimum. The Euclidean distance between points (x1, y1) and (x2, y2) equals <image>. Help them and find the required pair of points. Input The first line contains integers n, m, a, b (1 ≀ n, m ≀ 105, 0 < a < b < 106). The second line contains n integers in the ascending order: the i-th integer determines the coordinate of point Ai and equals yi (|yi| ≀ 106). The third line contains m integers in the ascending order: the i-th integer determines the coordinate of point Bi and equals y'i (|y'i| ≀ 106). The fourth line contains m more integers: the i-th of them determines the length of the path that connects the eastern village and point Bi, and equals li (1 ≀ li ≀ 106). It is guaranteed, that there is such a point C with abscissa at least b, that |BiC| ≀ li for all i (1 ≀ i ≀ m). It is guaranteed that no two points Ai coincide. It is guaranteed that no two points Bi coincide. Output Print two integers β€” the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to n, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to m in the order in which they are given in the input. If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10 - 6 in absolute or relative value. Examples Input 3 2 3 5 -2 -1 4 -1 2 7 3 Output 2 2 Submitted Solution: ``` import sys from itertools import * from math import * def solve(): n,m,leftbank,rightbank = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) l = list(map(int, input().split())) dist = rightbank - leftbank smallx = leftbank leftbest, rightbest, distbest = -100, -100, 100000000 for i, bcord, length in zip(count(), b, l): wanty = bcord * smallx / rightbank ll , rr = 0, n - 1 while ll < rr: mm = (ll + rr + 1) // 2 if a[mm] > wanty: rr = mm - 1 else: ll = mm for pos in range(ll - 3, ll + 4): if pos >= 0 and pos < n: first = sqrt(smallx * smallx + a[pos] * a[pos]) second = sqrt((dist - smallx)*(dist - smallx) + (bcord - a[pos])*(bcord - a[pos])) totaldist = first + second + length if totaldist < distbest: distbest = totaldist leftbest = pos rightbest = i print(leftbest + 1, rightbest + 1) if sys.hexversion == 50594544 : sys.stdin = open("test.txt") solve() ```
instruction
0
52,523
1
105,046
No
output
1
52,523
1
105,047
Provide a correct Python 3 solution for this coding contest problem. To get on the Shinkansen, you need two tickets, a "ticket" and a "limited express ticket". These are separate tickets because some of the routes may not use the Shinkansen, but for routes that use only the Shinkansen, one ticket can be used as both a ticket and a limited express ticket. It may also be issued. Automatic ticket gates must read these tickets and open the gate only when the correct ticket is inserted. Create a program that determines whether one "ticket" and one "limited express ticket" or both, or one "boarding / limited express ticket" has been inserted, and determines whether the door of the automatic ticket gate is open or closed. please do it. input The input is given in the following format. b1 b2 b3 The input consists of one line and contains three integers separated by one space. b1 indicates the state in which the "ticket" is inserted, b2 indicates the state in which the "limited express ticket" is inserted, and b3 indicates the state in which the "boarding / limited express ticket" is inserted. The turned-in state is represented by 0 or 1, where 0 indicates a non-charged state, and 1 indicates a loaded state. However, the combination of expected input states is limited to the following cases. Input | Input state | Door operation for input --- | --- | --- 1 0 0 | Insert only "ticket" | Close 0 1 0 | Only "Limited Express Ticket" is introduced | Close 1 1 0 | Introducing "tickets" and "express tickets" | Open 0 0 1 | Introducing "Boarding / Limited Express Tickets" | Open 0 0 0 | No input | Close output Outputs Open or Close, which indicates the opening and closing of the automatic ticket gate, on one line. Example Input 0 0 1 Output Open
instruction
0
52,951
1
105,902
"Correct Solution: ``` b = input() if b[0] == "1" and b[2] == "1" and b[4] == "0": print("Open") elif b[0] == "0" and b[2] == "0" and b[4] == "1": print("Open") else: print("Close") ```
output
1
52,951
1
105,903
Provide a correct Python 3 solution for this coding contest problem. To get on the Shinkansen, you need two tickets, a "ticket" and a "limited express ticket". These are separate tickets because some of the routes may not use the Shinkansen, but for routes that use only the Shinkansen, one ticket can be used as both a ticket and a limited express ticket. It may also be issued. Automatic ticket gates must read these tickets and open the gate only when the correct ticket is inserted. Create a program that determines whether one "ticket" and one "limited express ticket" or both, or one "boarding / limited express ticket" has been inserted, and determines whether the door of the automatic ticket gate is open or closed. please do it. input The input is given in the following format. b1 b2 b3 The input consists of one line and contains three integers separated by one space. b1 indicates the state in which the "ticket" is inserted, b2 indicates the state in which the "limited express ticket" is inserted, and b3 indicates the state in which the "boarding / limited express ticket" is inserted. The turned-in state is represented by 0 or 1, where 0 indicates a non-charged state, and 1 indicates a loaded state. However, the combination of expected input states is limited to the following cases. Input | Input state | Door operation for input --- | --- | --- 1 0 0 | Insert only "ticket" | Close 0 1 0 | Only "Limited Express Ticket" is introduced | Close 1 1 0 | Introducing "tickets" and "express tickets" | Open 0 0 1 | Introducing "Boarding / Limited Express Tickets" | Open 0 0 0 | No input | Close output Outputs Open or Close, which indicates the opening and closing of the automatic ticket gate, on one line. Example Input 0 0 1 Output Open
instruction
0
52,952
1
105,904
"Correct Solution: ``` x=input() if x == '1 0 0': print('Close') elif x == '0 1 0': print('Close') elif x == '0 0 0': print('Close') else: print('Open') ```
output
1
52,952
1
105,905
Provide a correct Python 3 solution for this coding contest problem. To get on the Shinkansen, you need two tickets, a "ticket" and a "limited express ticket". These are separate tickets because some of the routes may not use the Shinkansen, but for routes that use only the Shinkansen, one ticket can be used as both a ticket and a limited express ticket. It may also be issued. Automatic ticket gates must read these tickets and open the gate only when the correct ticket is inserted. Create a program that determines whether one "ticket" and one "limited express ticket" or both, or one "boarding / limited express ticket" has been inserted, and determines whether the door of the automatic ticket gate is open or closed. please do it. input The input is given in the following format. b1 b2 b3 The input consists of one line and contains three integers separated by one space. b1 indicates the state in which the "ticket" is inserted, b2 indicates the state in which the "limited express ticket" is inserted, and b3 indicates the state in which the "boarding / limited express ticket" is inserted. The turned-in state is represented by 0 or 1, where 0 indicates a non-charged state, and 1 indicates a loaded state. However, the combination of expected input states is limited to the following cases. Input | Input state | Door operation for input --- | --- | --- 1 0 0 | Insert only "ticket" | Close 0 1 0 | Only "Limited Express Ticket" is introduced | Close 1 1 0 | Introducing "tickets" and "express tickets" | Open 0 0 1 | Introducing "Boarding / Limited Express Tickets" | Open 0 0 0 | No input | Close output Outputs Open or Close, which indicates the opening and closing of the automatic ticket gate, on one line. Example Input 0 0 1 Output Open
instruction
0
52,953
1
105,906
"Correct Solution: ``` b1,b2,b3=map(int,input().split()) if b1==1 and b2==1 and b3==0: print('Open') elif b1==0 and b2==0 and b3==1: print('Open') else: print('Close') ```
output
1
52,953
1
105,907
Provide a correct Python 3 solution for this coding contest problem. To get on the Shinkansen, you need two tickets, a "ticket" and a "limited express ticket". These are separate tickets because some of the routes may not use the Shinkansen, but for routes that use only the Shinkansen, one ticket can be used as both a ticket and a limited express ticket. It may also be issued. Automatic ticket gates must read these tickets and open the gate only when the correct ticket is inserted. Create a program that determines whether one "ticket" and one "limited express ticket" or both, or one "boarding / limited express ticket" has been inserted, and determines whether the door of the automatic ticket gate is open or closed. please do it. input The input is given in the following format. b1 b2 b3 The input consists of one line and contains three integers separated by one space. b1 indicates the state in which the "ticket" is inserted, b2 indicates the state in which the "limited express ticket" is inserted, and b3 indicates the state in which the "boarding / limited express ticket" is inserted. The turned-in state is represented by 0 or 1, where 0 indicates a non-charged state, and 1 indicates a loaded state. However, the combination of expected input states is limited to the following cases. Input | Input state | Door operation for input --- | --- | --- 1 0 0 | Insert only "ticket" | Close 0 1 0 | Only "Limited Express Ticket" is introduced | Close 1 1 0 | Introducing "tickets" and "express tickets" | Open 0 0 1 | Introducing "Boarding / Limited Express Tickets" | Open 0 0 0 | No input | Close output Outputs Open or Close, which indicates the opening and closing of the automatic ticket gate, on one line. Example Input 0 0 1 Output Open
instruction
0
52,954
1
105,908
"Correct Solution: ``` b_1,b_2,b_3=map(int,input().split()) if (b_1==1): if (b_2==0): if (b_3==0): print("Close") else: if(b_2==1): if (b_3==0): print("Open") elif (b_1==0): if (b_2==1): if (b_3==0): print("Close") elif (b_2==0): if (b_3==1): print("Open") elif (b_3==0): print("Close") else: print("Close") ```
output
1
52,954
1
105,909
Provide a correct Python 3 solution for this coding contest problem. To get on the Shinkansen, you need two tickets, a "ticket" and a "limited express ticket". These are separate tickets because some of the routes may not use the Shinkansen, but for routes that use only the Shinkansen, one ticket can be used as both a ticket and a limited express ticket. It may also be issued. Automatic ticket gates must read these tickets and open the gate only when the correct ticket is inserted. Create a program that determines whether one "ticket" and one "limited express ticket" or both, or one "boarding / limited express ticket" has been inserted, and determines whether the door of the automatic ticket gate is open or closed. please do it. input The input is given in the following format. b1 b2 b3 The input consists of one line and contains three integers separated by one space. b1 indicates the state in which the "ticket" is inserted, b2 indicates the state in which the "limited express ticket" is inserted, and b3 indicates the state in which the "boarding / limited express ticket" is inserted. The turned-in state is represented by 0 or 1, where 0 indicates a non-charged state, and 1 indicates a loaded state. However, the combination of expected input states is limited to the following cases. Input | Input state | Door operation for input --- | --- | --- 1 0 0 | Insert only "ticket" | Close 0 1 0 | Only "Limited Express Ticket" is introduced | Close 1 1 0 | Introducing "tickets" and "express tickets" | Open 0 0 1 | Introducing "Boarding / Limited Express Tickets" | Open 0 0 0 | No input | Close output Outputs Open or Close, which indicates the opening and closing of the automatic ticket gate, on one line. Example Input 0 0 1 Output Open
instruction
0
52,955
1
105,910
"Correct Solution: ``` a,b,c=map(int,input().split()) if a == 1 and b == 0 and c == 0: print("Close") elif a == 0 and b == 1 and c == 0: print("Close") elif a == 0 and b == 0 and c == 0: print("Close") elif a == 0 and b == 0 and c == 1: print("Open") elif a == 1 and b == 1 and c == 0: print("Open") ```
output
1
52,955
1
105,911
Provide a correct Python 3 solution for this coding contest problem. To get on the Shinkansen, you need two tickets, a "ticket" and a "limited express ticket". These are separate tickets because some of the routes may not use the Shinkansen, but for routes that use only the Shinkansen, one ticket can be used as both a ticket and a limited express ticket. It may also be issued. Automatic ticket gates must read these tickets and open the gate only when the correct ticket is inserted. Create a program that determines whether one "ticket" and one "limited express ticket" or both, or one "boarding / limited express ticket" has been inserted, and determines whether the door of the automatic ticket gate is open or closed. please do it. input The input is given in the following format. b1 b2 b3 The input consists of one line and contains three integers separated by one space. b1 indicates the state in which the "ticket" is inserted, b2 indicates the state in which the "limited express ticket" is inserted, and b3 indicates the state in which the "boarding / limited express ticket" is inserted. The turned-in state is represented by 0 or 1, where 0 indicates a non-charged state, and 1 indicates a loaded state. However, the combination of expected input states is limited to the following cases. Input | Input state | Door operation for input --- | --- | --- 1 0 0 | Insert only "ticket" | Close 0 1 0 | Only "Limited Express Ticket" is introduced | Close 1 1 0 | Introducing "tickets" and "express tickets" | Open 0 0 1 | Introducing "Boarding / Limited Express Tickets" | Open 0 0 0 | No input | Close output Outputs Open or Close, which indicates the opening and closing of the automatic ticket gate, on one line. Example Input 0 0 1 Output Open
instruction
0
52,956
1
105,912
"Correct Solution: ``` b1, b2, b3 = map(int,input().split()) if (b1 == 1 and b2 == 1) or b3 == 1 : print('Open') else: print('Close') ```
output
1
52,956
1
105,913