message
stringlengths
2
22.8k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
16
109k
cluster
float64
1
1
__index_level_0__
int64
32
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place. Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination. The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once. Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him. Input The first line contains integer n (2 โ‰ค n โ‰ค 105). Next line contains n distinct integers a1, a2, ..., an (1 โ‰ค ai โ‰ค 107). Output Output two integers โ€” the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible. Examples Input 3 2 3 5 Output 22 3 Note Consider 6 possible routes: * [2, 3, 5]: total distance traveled: |2 โ€“ 0| + |3 โ€“ 2| + |5 โ€“ 3| = 5; * [2, 5, 3]: |2 โ€“ 0| + |5 โ€“ 2| + |3 โ€“ 5| = 7; * [3, 2, 5]: |3 โ€“ 0| + |2 โ€“ 3| + |5 โ€“ 2| = 7; * [3, 5, 2]: |3 โ€“ 0| + |5 โ€“ 3| + |2 โ€“ 5| = 8; * [5, 2, 3]: |5 โ€“ 0| + |2 โ€“ 5| + |3 โ€“ 2| = 9; * [5, 3, 2]: |5 โ€“ 0| + |3 โ€“ 5| + |2 โ€“ 3| = 8. The average travel distance is <image> = <image> = <image>. Submitted Solution: ``` #!/usr/bin/python3 import sys from fractions import Fraction n = int(sys.stdin.readline()) a = [int(x) for x in sys.stdin.readline().strip().split()] a.sort() s1 = sum(a) p = [0 for i in range(n)] c = [1 for i in range(n)] for i in range(1, n): p[i] = (a[i] - a[i-1]) * c[i-1] + p[i-1] c[i] += c[i-1] s2 = 2*sum(p) ans = Fraction(s1 + s2, n) print("{0} {1}".format(ans.numerator, ans.denominator)) ```
instruction
0
92,999
1
185,998
Yes
output
1
92,999
1
185,999
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place. Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination. The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once. Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him. Input The first line contains integer n (2 โ‰ค n โ‰ค 105). Next line contains n distinct integers a1, a2, ..., an (1 โ‰ค ai โ‰ค 107). Output Output two integers โ€” the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible. Examples Input 3 2 3 5 Output 22 3 Note Consider 6 possible routes: * [2, 3, 5]: total distance traveled: |2 โ€“ 0| + |3 โ€“ 2| + |5 โ€“ 3| = 5; * [2, 5, 3]: |2 โ€“ 0| + |5 โ€“ 2| + |3 โ€“ 5| = 7; * [3, 2, 5]: |3 โ€“ 0| + |2 โ€“ 3| + |5 โ€“ 2| = 7; * [3, 5, 2]: |3 โ€“ 0| + |5 โ€“ 3| + |2 โ€“ 5| = 8; * [5, 2, 3]: |5 โ€“ 0| + |2 โ€“ 5| + |3 โ€“ 2| = 9; * [5, 3, 2]: |5 โ€“ 0| + |3 โ€“ 5| + |2 โ€“ 3| = 8. The average travel distance is <image> = <image> = <image>. Submitted Solution: ``` from math import gcd n = int(input()) a = sorted(map(int, input().split())) ans = 0 for i in range(n): ans += 2 * (i + i - n + 1) * a[i] ans += sum(a) tmp = gcd(ans, n) print(ans // tmp, n // tmp) ```
instruction
0
93,000
1
186,000
Yes
output
1
93,000
1
186,001
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place. Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination. The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once. Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him. Input The first line contains integer n (2 โ‰ค n โ‰ค 105). Next line contains n distinct integers a1, a2, ..., an (1 โ‰ค ai โ‰ค 107). Output Output two integers โ€” the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible. Examples Input 3 2 3 5 Output 22 3 Note Consider 6 possible routes: * [2, 3, 5]: total distance traveled: |2 โ€“ 0| + |3 โ€“ 2| + |5 โ€“ 3| = 5; * [2, 5, 3]: |2 โ€“ 0| + |5 โ€“ 2| + |3 โ€“ 5| = 7; * [3, 2, 5]: |3 โ€“ 0| + |2 โ€“ 3| + |5 โ€“ 2| = 7; * [3, 5, 2]: |3 โ€“ 0| + |5 โ€“ 3| + |2 โ€“ 5| = 8; * [5, 2, 3]: |5 โ€“ 0| + |2 โ€“ 5| + |3 โ€“ 2| = 9; * [5, 3, 2]: |5 โ€“ 0| + |3 โ€“ 5| + |2 โ€“ 3| = 8. The average travel distance is <image> = <image> = <image>. Submitted Solution: ``` #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed Jun 3 11:57:46 2020 @author: shailesh """ from math import gcd def reduce_fraction(x,y): d = gcd(x,y) x = x//d y = y//d return x,y N = int(input()) A = [int(i) for i in input().split()] A.sort() d0 = A[0] sum_val = 0 for i in range(N-1): m_bf = i+2 m_af = N - i - 1 d = A[i+1]-A[i] # d = 1 sum_val +=m_af*(2*m_bf - 1)*d # print(A[i],A[i+1],sum_val) numerator = N*d0 + sum_val denominator = N numerator,denominator = reduce_fraction(numerator,denominator) print(numerator,denominator) #from itertools import permutations #perms = list(permutations([2,3,5])) # #perms = [(0,) + perm for perm in perms] # #d = {} #d['02'] = 0 #d['23'] = 0 #d['35'] = 0 #for perm in perms: # for i in range(len(perm)-1): # # start_end = [perm[i],perm[i+1]] # start_end.sort() # rng = range(start_end[0],start_end[1]+1) # if 0 in rng and 2 in rng: # d['02'] +=1 # if 2 in rng and 3 in rng: # d['23'] += 1 # if 3 in rng and 5 in rng: # d['35'] +=1 ```
instruction
0
93,001
1
186,002
Yes
output
1
93,001
1
186,003
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place. Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination. The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once. Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him. Input The first line contains integer n (2 โ‰ค n โ‰ค 105). Next line contains n distinct integers a1, a2, ..., an (1 โ‰ค ai โ‰ค 107). Output Output two integers โ€” the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible. Examples Input 3 2 3 5 Output 22 3 Note Consider 6 possible routes: * [2, 3, 5]: total distance traveled: |2 โ€“ 0| + |3 โ€“ 2| + |5 โ€“ 3| = 5; * [2, 5, 3]: |2 โ€“ 0| + |5 โ€“ 2| + |3 โ€“ 5| = 7; * [3, 2, 5]: |3 โ€“ 0| + |2 โ€“ 3| + |5 โ€“ 2| = 7; * [3, 5, 2]: |3 โ€“ 0| + |5 โ€“ 3| + |2 โ€“ 5| = 8; * [5, 2, 3]: |5 โ€“ 0| + |2 โ€“ 5| + |3 โ€“ 2| = 9; * [5, 3, 2]: |5 โ€“ 0| + |3 โ€“ 5| + |2 โ€“ 3| = 8. The average travel distance is <image> = <image> = <image>. Submitted Solution: ``` from fractions import Fraction while(1): try: n=int(input()) a=list(map(int,input().split())) s=sum(a) a.sort() for i in range(0,n): s+=2*(2*i+1-n)*a[i] f=Fraction(s,n) print(f.numerator,end=' ') print(f.denominator) except EOFError: break ```
instruction
0
93,002
1
186,004
Yes
output
1
93,002
1
186,005
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place. Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination. The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once. Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him. Input The first line contains integer n (2 โ‰ค n โ‰ค 105). Next line contains n distinct integers a1, a2, ..., an (1 โ‰ค ai โ‰ค 107). Output Output two integers โ€” the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible. Examples Input 3 2 3 5 Output 22 3 Note Consider 6 possible routes: * [2, 3, 5]: total distance traveled: |2 โ€“ 0| + |3 โ€“ 2| + |5 โ€“ 3| = 5; * [2, 5, 3]: |2 โ€“ 0| + |5 โ€“ 2| + |3 โ€“ 5| = 7; * [3, 2, 5]: |3 โ€“ 0| + |2 โ€“ 3| + |5 โ€“ 2| = 7; * [3, 5, 2]: |3 โ€“ 0| + |5 โ€“ 3| + |2 โ€“ 5| = 8; * [5, 2, 3]: |5 โ€“ 0| + |2 โ€“ 5| + |3 โ€“ 2| = 9; * [5, 3, 2]: |5 โ€“ 0| + |3 โ€“ 5| + |2 โ€“ 3| = 8. The average travel distance is <image> = <image> = <image>. Submitted Solution: ``` import os import sys from io import BytesIO, IOBase from itertools import permutations from math import gcd def main(): n = int(input()) a = list(map(int, input().split())) cnt = 0 asum = 0 for i in permutations(a): print(i) asum += i[0] cnt += 1 for j in range(1, n): asum += abs(i[j-1] - i[j]) print(asum // gcd(asum, cnt), cnt // gcd(asum, cnt)) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main() ```
instruction
0
93,003
1
186,006
No
output
1
93,003
1
186,007
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place. Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination. The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once. Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him. Input The first line contains integer n (2 โ‰ค n โ‰ค 105). Next line contains n distinct integers a1, a2, ..., an (1 โ‰ค ai โ‰ค 107). Output Output two integers โ€” the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible. Examples Input 3 2 3 5 Output 22 3 Note Consider 6 possible routes: * [2, 3, 5]: total distance traveled: |2 โ€“ 0| + |3 โ€“ 2| + |5 โ€“ 3| = 5; * [2, 5, 3]: |2 โ€“ 0| + |5 โ€“ 2| + |3 โ€“ 5| = 7; * [3, 2, 5]: |3 โ€“ 0| + |2 โ€“ 3| + |5 โ€“ 2| = 7; * [3, 5, 2]: |3 โ€“ 0| + |5 โ€“ 3| + |2 โ€“ 5| = 8; * [5, 2, 3]: |5 โ€“ 0| + |2 โ€“ 5| + |3 โ€“ 2| = 9; * [5, 3, 2]: |5 โ€“ 0| + |3 โ€“ 5| + |2 โ€“ 3| = 8. The average travel distance is <image> = <image> = <image>. Submitted Solution: ``` from fractions import Fraction n = int(input()) l = [int(x) for x in input().split()] f = Fraction(sum([(3-2*n+4*i)*l[i] for i in range(n)]),n) print(f.numerator,f.denominator) ```
instruction
0
93,004
1
186,008
No
output
1
93,004
1
186,009
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place. Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination. The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once. Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him. Input The first line contains integer n (2 โ‰ค n โ‰ค 105). Next line contains n distinct integers a1, a2, ..., an (1 โ‰ค ai โ‰ค 107). Output Output two integers โ€” the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible. Examples Input 3 2 3 5 Output 22 3 Note Consider 6 possible routes: * [2, 3, 5]: total distance traveled: |2 โ€“ 0| + |3 โ€“ 2| + |5 โ€“ 3| = 5; * [2, 5, 3]: |2 โ€“ 0| + |5 โ€“ 2| + |3 โ€“ 5| = 7; * [3, 2, 5]: |3 โ€“ 0| + |2 โ€“ 3| + |5 โ€“ 2| = 7; * [3, 5, 2]: |3 โ€“ 0| + |5 โ€“ 3| + |2 โ€“ 5| = 8; * [5, 2, 3]: |5 โ€“ 0| + |2 โ€“ 5| + |3 โ€“ 2| = 9; * [5, 3, 2]: |5 โ€“ 0| + |3 โ€“ 5| + |2 โ€“ 3| = 8. The average travel distance is <image> = <image> = <image>. Submitted Solution: ``` #from bisect import bisect_left as bl #c++ lowerbound bl(array,element) #from bisect import bisect_right as br #c++ upperbound br(array,element) #from __future__ import print_function, division #while using python2 from itertools import accumulate # from collections import defaultdict, Counter import math def modinv(n,p): return pow(n,p-2,p) def main(): #sys.stdin = open('input.txt', 'r') #sys.stdout = open('output.txt', 'w') n = int(input()) arr = [int(x) for x in input().split()] den = 2 ** n - 2 num = 0 s = sum(arr) pre_sum = list(accumulate(arr)) for i in range(n): left = 0 right = 0 if i > 0: left = pre_sum[i-1] if i < n-1: right = s - pre_sum[i] if right > 0: right -= (arr[i] * (n-i-1)) if left > 0: left = (i * arr[i]) - left num += (left + arr[i] + right)*(n-1) # print(c) g = math.gcd(num, den) print(num//g, den//g) #------------------ Python 2 and 3 footer by Pajenegod and c1729----------------------------------------- py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange import os, sys from io import IOBase, BytesIO BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = "x" in file.mode or "w" in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b"\n") + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: self.write = lambda s:self.buffer.write(s.encode('ascii')) self.read = lambda:self.buffer.read().decode('ascii') self.readline = lambda:self.buffer.readline().decode('ascii') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') if __name__ == '__main__': main() ```
instruction
0
93,005
1
186,010
No
output
1
93,005
1
186,011
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place. Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination. The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once. Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him. Input The first line contains integer n (2 โ‰ค n โ‰ค 105). Next line contains n distinct integers a1, a2, ..., an (1 โ‰ค ai โ‰ค 107). Output Output two integers โ€” the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible. Examples Input 3 2 3 5 Output 22 3 Note Consider 6 possible routes: * [2, 3, 5]: total distance traveled: |2 โ€“ 0| + |3 โ€“ 2| + |5 โ€“ 3| = 5; * [2, 5, 3]: |2 โ€“ 0| + |5 โ€“ 2| + |3 โ€“ 5| = 7; * [3, 2, 5]: |3 โ€“ 0| + |2 โ€“ 3| + |5 โ€“ 2| = 7; * [3, 5, 2]: |3 โ€“ 0| + |5 โ€“ 3| + |2 โ€“ 5| = 8; * [5, 2, 3]: |5 โ€“ 0| + |2 โ€“ 5| + |3 โ€“ 2| = 9; * [5, 3, 2]: |5 โ€“ 0| + |3 โ€“ 5| + |2 โ€“ 3| = 8. The average travel distance is <image> = <image> = <image>. Submitted Solution: ``` import math n = int(input()) l = [int(x) for x in input().split()] a1 = sum(l) a2 = n a3 = 0 temp = 0 for i in range(n): temp += l[n-i-1] a3-=(a1-temp) a3+=(n-i-1)*(l[n-i-1]) a1 = a1+a3+a3 a4 = math.gcd(a1, a2) print(a1//a4, a2//a4) ```
instruction
0
93,006
1
186,012
No
output
1
93,006
1
186,013
Provide tags and a correct Python 2 solution for this coding contest problem. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5.
instruction
0
93,235
1
186,470
Tags: dfs and similar, graphs, greedy Correct Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations raw_input = stdin.readline pr = stdout.write def in_arr(): return map(int,raw_input().split()) def pr_num(n): stdout.write(str(n)+'\n') def pr_arr(arr): for i in arr: stdout.write(str(i)+' ') stdout.write('\n') range = xrange # not for python 3.0+ n,m,s=in_arr() d=[[] for i in range(n+1)] for i in range(m): u,v=in_arr() d[u].append(v) good=[False]*(n+1) q=[s] good[s]=True while q: x=q.pop() for i in d[x]: if not good[i]: good[i]=True q.append(i) arr=[] for i in range(1,n+1): if not good[i]: vis=list(good) q=[i] c=1 vis[i]=1 while q: x=q.pop() for j in d[x]: if not vis[j]: vis[j]=True q.append(j) c+=1 arr.append((c,i)) arr.sort(reverse=True) ans=0 for _,i in arr: if not good[i]: good[i]=True q=[i] ans+=1 while q: x=q.pop() for j in d[x]: if not good[j]: good[j]=1 q.append(j) pr_num(ans) ```
output
1
93,235
1
186,471
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5.
instruction
0
93,236
1
186,472
Tags: dfs and similar, graphs, greedy Correct Solution: ``` from collections import defaultdict import sys sys.setrecursionlimit(1000000) n, m, s = map(int, input().split()) s = s - 1 def read_graph(): g = defaultdict(list) for _ in range(m): (u, v) = map(lambda x: int(x) - 1, input().split()) if v != s: g[u].append(v) return g G = read_graph() vis = defaultdict(lambda: False) topo = [] def dfs(u): # print(u) for v in G[u]: if not vis[v]: vis[v] = True dfs(v) topo.append(u) for i in range(n): if not vis[i]: vis[i] = True dfs(i) vis.clear() vis[s] = True dfs(s) ans = 0 for i in topo[::-1]: if not vis[i]: vis[i] = True ans += 1 dfs(i) print(ans) ```
output
1
93,236
1
186,473
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5.
instruction
0
93,237
1
186,474
Tags: dfs and similar, graphs, greedy Correct Solution: ``` import sys n, m, s = map(int, input().split()) adj = [[] for _ in range(500005)] ar = [] vis = [0] * 500005 sys.setrecursionlimit(6000) def dfs(s): vis[s] = 1 for i in range(len(adj[s])): if(vis[adj[s][i]] == 0): dfs(adj[s][i]) ar.append(s) for i in range(m): u, v = map(int, input().split()) adj[u].append(v) dfs(s) for i in range(n): if(vis[i + 1] == 0): dfs(i + 1) res = 0 vis = [0] * 500005 for i in range(n - 1, -1, -1): if(vis[ar[i]] == 0): if(s != ar[i]): res += 1 dfs(ar[i]) print(res) ```
output
1
93,237
1
186,475
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5.
instruction
0
93,238
1
186,476
Tags: dfs and similar, graphs, greedy Correct Solution: ``` from __future__ import print_function from queue import Queue from random import shuffle import sys import math import os.path sys.setrecursionlimit(100000) # LOG def log(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) # INPUT def ni(): return map(int, input().split()) def nio(offset): return map(lambda x: int(x) + offset, input().split()) def nia(): return list(map(int, input().split())) # CONVERT def toString(aList, sep=" "): return sep.join(str(x) for x in aList) def toMapInvertIndex(aList): return {k: v for v, k in enumerate(aList)} # SORT def sortId(arr): return sorted(range(len(arr)), key=lambda k: arr[k]) # MAIN n,m,s = ni() s -= 1 adj = [[] for _ in range(n)] for i in range(m): u,v = nio(-1) if (v != s): adj[u].append(v) stack = [] visited= [False]*n def dfs(x): global visited global stack visited[x] = True for y in adj[x]: if not visited[y]: dfs(y) stack.append(x) for i in range(n): if not visited[i]: dfs(i) # log(adj) # log(visited) # log(stack) count = -1 def loang(x): global visited visited[x] = False for y in adj[x]: if visited[y]: loang(y) for x in stack[::-1]: if visited[x]: count += 1 loang(x) print(count) ```
output
1
93,238
1
186,477
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5.
instruction
0
93,239
1
186,478
Tags: dfs and similar, graphs, greedy Correct Solution: ``` import sys sys.setrecursionlimit(10**4) from collections import deque n, m, s = map(int, input().split()) s -= 1 graph = [[] for _ in range(n)] for _ in range(m): u, v = map(int, input().split()) graph[u-1].append(v-1) seen = [False]*n li = deque() def visit(node, t=True): if not seen[node]: seen[node] = True for c_node in graph[node]: visit(c_node) if t: li.appendleft(node) for i in range(n): visit(i) seen = [False]*n cnt = 0 visit(s, False) for i in list(li): if seen[i]: continue visit(i, False) cnt += 1 print(cnt) ```
output
1
93,239
1
186,479
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5.
instruction
0
93,240
1
186,480
Tags: dfs and similar, graphs, greedy Correct Solution: ``` from collections import defaultdict import sys def dfs(u): avail[u] = False for v in g[u]: if avail[v]: dfs(v) topo.append(u) sys.setrecursionlimit(6000) n, m, s = map(int, input().split()) g = [[] for _ in range(n)] for _ in range(m): u, v = map(int, input().split()) g[u - 1].append(v - 1) avail, topo = [True] * n, [] for i,a in enumerate(avail): if a: dfs(i) avail, res = [True] * n, 0 dfs(s - 1) for i in reversed(topo): if avail[i]: res += 1 dfs(i) print(res) ```
output
1
93,240
1
186,481
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5.
instruction
0
93,241
1
186,482
Tags: dfs and similar, graphs, greedy Correct Solution: ``` def main(): import sys sys.setrecursionlimit(10**5) from collections import deque n, m, s = map(int, input().split()) s -= 1 graph = [[] for _ in range(n)] for _ in range(m): u, v = map(int, input().split()) graph[u-1].append(v-1) seen = [False]*n li = deque() def visit(node): if not seen[node]: seen[node] = True for c_node in graph[node]: visit(c_node) li.appendleft(node) def visit2(node): if not seen[node]: seen[node] = True for c_node in graph[node]: visit2(c_node) for i in range(n): visit(i) seen = [False]*n cnt = 0 visit2(s) for i in li: if seen[i]: continue visit2(i) cnt += 1 print(cnt) if __name__ == "__main__": try: main() except: print('error!') exit(0) ```
output
1
93,241
1
186,483
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5.
instruction
0
93,242
1
186,484
Tags: dfs and similar, graphs, greedy Correct Solution: ``` import sys input = sys.stdin.readline n, m, s = map(int, input().split()) s -= 1 adj = [[] for _ in range(n)] rev = [[] for _ in range(n)] for u, v in (map(int, input().split()) for _ in range(m)): adj[u-1].append(v-1) rev[v-1].append(u-1) group = [-1]*n g = 0 group[s] = g stack = [s] while stack: v = stack.pop() for dest in adj[v]: if group[dest] != -1: continue group[dest] = g stack.append(dest) for i in range(n): if group[i] != -1: continue g += 1 group[i] = g stack = [i] while stack: v = stack.pop() for dest in adj[v]: if group[dest] == 0 or group[dest] == g: continue group[dest] = g stack.append(dest) print(len(set(group)) - 1) ```
output
1
93,242
1
186,485
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5.
instruction
0
93,243
1
186,486
Tags: dfs and similar, graphs, greedy Correct Solution: ``` import sys sys.setrecursionlimit(100001) from collections import * d=deque() def dfs2(start): vis[start]=1 for child in adj[start]: if vis[child]==0: dfs2(child) def dfs(start): vis[start]=1 for child in adj[start]: if vis[child]==0: dfs(child) d.appendleft(start) n,m,k=map(int,input().split()) adj=[] for i in range(n): adj.append([]) for i in range(m): a,b=map(int,input().split()) adj[a-1].append(b-1) vis=[0]*n for i in range(n): if vis[i]==0: dfs(i) vis=[0]*n dfs2(k-1) cnt=0 for i in d: if vis[i]==0: dfs2(i) cnt+=1 print(cnt) ```
output
1
93,243
1
186,487
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5. Submitted Solution: ``` import sys def d(u): rt[u] = False for v in q[u]: if rt[v]: d(v) topo.append(u) sys.setrecursionlimit(6000) n, m, s = map(int, input().split()) q = [[] for _ in range(n)] for _ in range(m): u, v = map(int, input().split()) q[u - 1].append(v - 1) rt, topo = [True] * n, [] for i,a in enumerate(rt): if a: d(i) rt, res = [True] * n, 0 d(s - 1) for i in reversed(topo): if rt[i]: res += 1 d(i) print(res) ```
instruction
0
93,244
1
186,488
Yes
output
1
93,244
1
186,489
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5. Submitted Solution: ``` import sys n, m, s = map(int, input().split()) adj = [[] for i in range(500005)] ar = [] vis = [0 for i in range(500005)] sys.setrecursionlimit(6000) def dfs(s): vis[s] = 1 for i in range(len(adj[s])): if(vis[adj[s][i]] == 0): dfs(adj[s][i]) ar.append(s) for i in range(m): u, v = map(int, input().split()) adj[u].append(v) dfs(s) for i in range(n): if(vis[i + 1] == 0): dfs(i + 1) res = 0 vis = [0 for i in range(500005)] for i in range(n - 1, -1, -1): if(vis[ar[i]] == 0): if(s != ar[i]): res += 1 dfs(ar[i]) print(res) ```
instruction
0
93,245
1
186,490
Yes
output
1
93,245
1
186,491
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5. Submitted Solution: ``` from collections import deque import heapq import sys input = sys.stdin.readline def bfs1(s): q = deque() q.append(s) visit = [0] * (n + 1) visit[s] = 1 while q: i = q.popleft() for j in G[i]: if not visit[j]: visit[j] = 1 q.append(j) return visit def bfs2(t): q = deque() q.append(t) visit0 = [0] * (n + 1) visit0[t] = 1 c = 0 while q: i = q.popleft() c += 1 for j in G[i]: if not visit0[j] and not visit[j]: visit0[j] = 1 q.append(j) return c def bfs3(t): q = deque() q.append(t) visit[t] = 1 while q: i = q.popleft() for j in G[i]: if not visit[j]: visit[j] = 1 q.append(j) return n, m, s = map(int, input().split()) G = [[] for _ in range(n + 1)] for _ in range(m): u, v = map(int, input().split()) G[u].append(v) visit = bfs1(s) h = [] for i in range(1, n + 1): if not visit[i]: c = bfs2(i) heapq.heappush(h, (-c, i)) ans = 0 while h: c, i = heapq.heappop(h) if not visit[i]: ans += 1 bfs3(i) print(ans) ```
instruction
0
93,246
1
186,492
Yes
output
1
93,246
1
186,493
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5. Submitted Solution: ``` import sys N = 5000 + 5 adj = [[] for i in range(N)] mark = [0 for i in range(N)] topo = [] sys.setrecursionlimit(6000) #fin = open("999E.inp", "r") #fou = open("999E.out", "w") #n, m, s = map(int, fin.readline().split()) n, m, s = map(int, input().split()) for i in range(m): #u, v = map(int, fin.readline().split()) u, v = map(int, input().split()) adj[u].append(v) def topoSort(u): mark[u] = 1 for j in range(len(adj[u])): v = adj[u][j] if (mark[v] == 0): topoSort(v) topo.append(u) def dfs(u): mark[u] = 1 for j in range(len(adj[u])): v = adj[u][j] if (mark[v] == 0): dfs(v) for i in range(1, n+1): if (mark[i] == 0): topoSort(i) topo.reverse() for i in range(1, n+1): mark[i] = 0 dfs(s) res = 0 for i in range(n): v = topo[i] if (mark[v] == 0): res += 1 dfs(v) #fou.write(str(res)) print(res) ```
instruction
0
93,247
1
186,494
Yes
output
1
93,247
1
186,495
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5. Submitted Solution: ``` def parent(i,par,ind): pp=i if(par[i]==ind): return 1 i=par[i] while(1): if(par[i]==ind): return 1 if(par[i]==i): par[i]=ind break i=par[i] if(i==pp): break return 0 n,m,ind=[int(x) for x in input().split()] par={} for i in range(1,n+1): par[i]=i for i in range(m): p,q=[int(x) for x in input().split()] par[q]=p c=0 for i in range(1,n+1): #print(c,par) if(i==ind): continue if(parent(i,par,ind)): par[i]=ind else: c+=1 par[i]=ind print(c) ```
instruction
0
93,248
1
186,496
No
output
1
93,248
1
186,497
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5. Submitted Solution: ``` from collections import deque import sys input = sys.stdin.readline def bfs1(s): q = deque() q.append(s) group[s] = s while q: i = q.popleft() for j in G[i]: if not group[j]: unite(s, j) group[j] = s q.append(j) return def bfs2(t): q = deque() q.append(t) group[t] = t while q: i = q.popleft() for j in G[i]: if not group[j]: unite(t, j) group[j] = t q.append(j) elif not group[j] == s: unite(i, j) return def get_root(s): v = [] while not s == root[s]: v.append(s) s = root[s] for i in v: root[i] = s return s def unite(s, t): rs, rt = get_root(s), get_root(t) if not rs ^ rt: return if rank[s] == rank[t]: rank[rs] += 1 if rank[s] >= rank[t]: root[rt] = rs size[rs] += size[rt] else: root[rs] = rt size[rt] += size[rs] return def same(s, t): return True if get_root(s) == get_root(t) else False n, m, s = map(int, input().split()) G = [[] for _ in range(n + 1)] for _ in range(m): u, v = map(int, input().split()) G[u].append(v) group = [0] * (n + 1) root = [i for i in range(n + 1)] rank = [1 for _ in range(n + 1)] size = [1 for _ in range(n + 1)] bfs1(s) for i in range(2, n + 1): if not group[i]: bfs2(i) ans = len(set([get_root(i) for i in range(1, n + 1)])) - 1 print(ans) ```
instruction
0
93,249
1
186,498
No
output
1
93,249
1
186,499
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5. Submitted Solution: ``` n,m,s=map(int,input().split()) s-=1 edge=[[]for _ in range(n)] to=[0]*n for _ in range(m): a,b=map(int,input().split()) a-=1 b-=1 to[b]+=1 edge[a].append(b) to=[(to[i],i)for i in range(n)] to.sort() visited=[False]*n visited[s]=True queue=[s] for node in queue: for mode in edge[node]: if visited[mode]:continue visited[mode]=True queue.append(mode) ans=0 for _,node in to: if visited[node]:continue visited[node]=True ans+=1 queue=[node] for node in queue: for mode in edge[node]: if visited[mode]:continue visited[mode]=True queue.append(mode) print(ans) ```
instruction
0
93,250
1
186,500
No
output
1
93,250
1
186,501
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way. What is the minimum number of new roads that need to be built to make all the cities reachable from the capital? New roads will also be one-way. Input The first line of input consists of three integers n, m and s (1 โ‰ค n โ‰ค 5000, 0 โ‰ค m โ‰ค 5000, 1 โ‰ค s โ‰ค n) โ€” the number of cities, the number of roads and the index of the capital. Cities are indexed from 1 to n. The following m lines contain roads: road i is given as a pair of cities u_i, v_i (1 โ‰ค u_i, v_i โ‰ค n, u_i โ‰  v_i). For each pair of cities (u, v), there can be at most one road from u to v. Roads in opposite directions between a pair of cities are allowed (i.e. from u to v and from v to u). Output Print one integer โ€” the minimum number of extra roads needed to make all the cities reachable from city s. If all the cities are already reachable from s, print 0. Examples Input 9 9 1 1 2 1 3 2 3 1 5 5 6 6 1 1 8 9 8 7 1 Output 3 Input 5 4 5 1 2 2 3 3 4 4 1 Output 1 Note The first example is illustrated by the following: <image> For example, you can add roads (6, 4), (7, 9), (1, 7) to make all the cities reachable from s = 1. The second example is illustrated by the following: <image> In this example, you can add any one of the roads (5, 1), (5, 2), (5, 3), (5, 4) to make all the cities reachable from s = 5. Submitted Solution: ``` import sys input = sys.stdin.readline def kosaraju(g): # 1. For each vertex u of the graph, mark u as unvisited. Let l be empty. size = len(g) vis = [False]*size # vertexes that have been visited l = [0]*size T = size t = [[] for _ in range(size)] # transpose graph def visit(u): nonlocal T, vis stk = [] if not vis[u]: stk.append(u) while stk: x = stk[-1] if not vis[x]: vis[x] = True for y in g[x]: if not vis[y]: stk.append(y) t[y].append(x) else: stk.pop() T -= 1 l[T] = x # 2. For each vertex u of the graph do visit(u) for u in range(len(g)): visit(u) c = [0]*size def assign(u, root): nonlocal vis stk = [u] while stk: x = stk.pop() if vis[x]: vis[x] = False c[x] = root for y in t[x]: stk.append(y) # 3: For each element u of l in order, do assign(u, u) for u in l: assign(u, u) return c def condensed(g,s): c = kosaraju(g) size = len(g) label = 0 new_c = [-1]*(size) ref = dict() for i in range(size): if c[i] in ref: new_c[i] = ref[c[i]] else: ref[c[i]] = label new_c[i] = label label += 1 new_g = [set() for _ in range(label)] for i in range(size): new_i = new_c[i] for j in g[i]: new_j = new_c[j] if new_i != new_j: new_g[new_i].add(new_j) return new_g, new_c[s] n,m,s = map(int,input().split()) G = [[] for _ in range(n)] for _ in range(m): x,y = map(int,input().split()) G[x-1].append(y-1) G,s = condensed(G,s-1) indeg = [0]*len(G) for i in range(len(G)): for j in G[i]: indeg[j] += 1 ans = 0 for i in range(len(G)): if indeg[i] == 0: ans += 1 if indeg[s] == 0: ans -= 1 print(ans) ```
instruction
0
93,251
1
186,502
No
output
1
93,251
1
186,503
Provide a correct Python 3 solution for this coding contest problem. There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City A_i and City B_i. Snuke can perform the following operation zero or more times: * Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities. After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times). What is the minimum number of roads he must build to achieve the goal? Constraints * 2 \leq N \leq 100,000 * 1 \leq M \leq 100,000 * 1 \leq A_i < B_i \leq N * No two roads connect the same pair of cities. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output Print the answer. Example Input 3 1 1 2 Output 1
instruction
0
93,252
1
186,504
"Correct Solution: ``` class UnionFind: def __init__(self, n): self.g = [i for i in range(n)] self.n = n def whichclass(self, x): if self.g[x]==x: return x else: self.g[x] = self.whichclass(self.g[x]) return self.g[x] def union(self, x, y): cx = self.whichclass(x) cy = self.whichclass(y) if cx==cy: return minc = min(cx, cy) maxc = max(cx, cy) self.g[maxc] = minc def __getitem__(self, x): return self.whichclass(x) N, M = map(int, input().split()) UF = UnionFind(N) for i in range(M): a, b = map(int, input().split()) a, b = a-1, b-1 UF.union(a, b) count = [0 for i in range(N)] for i in range(N): count[UF[i]] = 1 print(sum(count)-1) ```
output
1
93,252
1
186,505
Provide a correct Python 3 solution for this coding contest problem. There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City A_i and City B_i. Snuke can perform the following operation zero or more times: * Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities. After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times). What is the minimum number of roads he must build to achieve the goal? Constraints * 2 \leq N \leq 100,000 * 1 \leq M \leq 100,000 * 1 \leq A_i < B_i \leq N * No two roads connect the same pair of cities. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output Print the answer. Example Input 3 1 1 2 Output 1
instruction
0
93,253
1
186,506
"Correct Solution: ``` N, M = map(int, input().split()) F = [[] for _ in range(N)] P = [0] * N for i in range(M): A, B = map(int, input().split()) F[A-1].append(B-1) F[B-1].append(A-1) c=0 l = [] for i in range(N): if P[i] == 0: c+=1 l.append(i) while len(l)>0: p=l.pop() P[p] = c for j in F[p]: if P[j] == 0: l.append(j) print(c-1) ```
output
1
93,253
1
186,507
Provide a correct Python 3 solution for this coding contest problem. There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City A_i and City B_i. Snuke can perform the following operation zero or more times: * Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities. After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times). What is the minimum number of roads he must build to achieve the goal? Constraints * 2 \leq N \leq 100,000 * 1 \leq M \leq 100,000 * 1 \leq A_i < B_i \leq N * No two roads connect the same pair of cities. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output Print the answer. Example Input 3 1 1 2 Output 1
instruction
0
93,254
1
186,508
"Correct Solution: ``` N,M=map(int,input().split()) road=[[] for _ in range(N)] for i in range(M): a,b=map(int,input().split()) road[a-1].append(b-1) road[b-1].append(a-1) ischecked=[0]*(N) ans=0 for i in range(N): if ischecked[i]==1: continue ischecked[i]=1 q=[i] while q: now=q.pop() nex=road[now] for j in nex: if ischecked[j]!=0: continue q.append(j) ischecked[j]=1 ans+=1 print(ans-1) ```
output
1
93,254
1
186,509
Provide a correct Python 3 solution for this coding contest problem. There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City A_i and City B_i. Snuke can perform the following operation zero or more times: * Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities. After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times). What is the minimum number of roads he must build to achieve the goal? Constraints * 2 \leq N \leq 100,000 * 1 \leq M \leq 100,000 * 1 \leq A_i < B_i \leq N * No two roads connect the same pair of cities. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output Print the answer. Example Input 3 1 1 2 Output 1
instruction
0
93,255
1
186,510
"Correct Solution: ``` N, M = map(int, input().split()) cla = [i for i in range(N)] count = set() def root(a): change = [] while cla[a] != a: change.append(a) a = cla[a] for i in change: cla[i] = a return a for _ in range(M): a, b = map(int, input().split()) a -= 1 b -= 1 if root(a) != root(b): cla[root(b)] = root(a) for i in range(N): count |= {root(i)} print(len(count) - 1) ```
output
1
93,255
1
186,511
Provide a correct Python 3 solution for this coding contest problem. There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City A_i and City B_i. Snuke can perform the following operation zero or more times: * Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities. After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times). What is the minimum number of roads he must build to achieve the goal? Constraints * 2 \leq N \leq 100,000 * 1 \leq M \leq 100,000 * 1 \leq A_i < B_i \leq N * No two roads connect the same pair of cities. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output Print the answer. Example Input 3 1 1 2 Output 1
instruction
0
93,256
1
186,512
"Correct Solution: ``` N, M = map(int, input().split()) P = [-1 for i in range(N)] def par(a): L = [] while P[a] >= 0: L.append(a) a = P[a] for l in L: P[l] = a return a def unite(a, b): if par(a) != par(b): if P[par(b)] >= P[par(a)]: if P[par(b)] == P[par(a)]: P[par(a)] -= 1 P[par(b)] = par(a) else: P[par(a)] = par(b) ans = N - 1 for _ in range(M): a, b = map(int, input().split()) a, b = a-1, b-1 if par(a) == par(b): continue unite(a, b) ans -= 1 print(ans) ```
output
1
93,256
1
186,513
Provide a correct Python 3 solution for this coding contest problem. There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City A_i and City B_i. Snuke can perform the following operation zero or more times: * Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities. After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times). What is the minimum number of roads he must build to achieve the goal? Constraints * 2 \leq N \leq 100,000 * 1 \leq M \leq 100,000 * 1 \leq A_i < B_i \leq N * No two roads connect the same pair of cities. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output Print the answer. Example Input 3 1 1 2 Output 1
instruction
0
93,257
1
186,514
"Correct Solution: ``` N,M = map(int,input().split()) par = [0]+[-1 for i in range(N)] def find(x): if par[x] == -1: return x else: par[x] = find(par[x]) #็ตŒ่ทฏๅœง็ธฎ return par[x] def same(x,y): return find(x) == find(y) def unite(x,y): x = find(x) y = find(y) if x == y: return 0 par[x] = y for i in range(M): a,b = map(int,input().split()) unite(a,b) print(len([i for i in par if i == -1])-1) ```
output
1
93,257
1
186,515
Provide a correct Python 3 solution for this coding contest problem. There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City A_i and City B_i. Snuke can perform the following operation zero or more times: * Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities. After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times). What is the minimum number of roads he must build to achieve the goal? Constraints * 2 \leq N \leq 100,000 * 1 \leq M \leq 100,000 * 1 \leq A_i < B_i \leq N * No two roads connect the same pair of cities. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output Print the answer. Example Input 3 1 1 2 Output 1
instruction
0
93,258
1
186,516
"Correct Solution: ``` import sys input=sys.stdin.readline n,m=map(int,input().split()) def unite(x,y): if same(x,y)==False: rx=root(x);ry=root(y) if rank[rx]>rank[ry]: parent[ry]=rx size[rx]+=size[ry] else: parent[rx]=ry size[ry]+=size[rx] if rank[rx]==rank[ry]: rank[ry]+=1 def root(x): if x!=parent[x]: return root(parent[x]) return x def same(x,y): return root(x)==root(y) parent=list(range(n)) rank=[0]*n size=[1]*n for i in range(m): a,b=map(int,input().split()) unite(a-1,b-1) kind=set() for i in range(n): r=parent[root(i)] kind.add(r) print(len(kind)-1) ```
output
1
93,258
1
186,517
Provide a correct Python 3 solution for this coding contest problem. There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City A_i and City B_i. Snuke can perform the following operation zero or more times: * Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities. After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times). What is the minimum number of roads he must build to achieve the goal? Constraints * 2 \leq N \leq 100,000 * 1 \leq M \leq 100,000 * 1 \leq A_i < B_i \leq N * No two roads connect the same pair of cities. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output Print the answer. Example Input 3 1 1 2 Output 1
instruction
0
93,259
1
186,518
"Correct Solution: ``` import sys class UnionFind(): def __init__(self, n): self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x N, M = map(int, input().split()) uf = UnionFind(N) for _ in range(M): a, b = map(lambda x: int(x) - 1, sys.stdin.readline().split()) uf.union(a, b) print(sum(p < 0 for p in uf.parents) - 1) ```
output
1
93,259
1
186,519
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City A_i and City B_i. Snuke can perform the following operation zero or more times: * Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities. After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times). What is the minimum number of roads he must build to achieve the goal? Constraints * 2 \leq N \leq 100,000 * 1 \leq M \leq 100,000 * 1 \leq A_i < B_i \leq N * No two roads connect the same pair of cities. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output Print the answer. Example Input 3 1 1 2 Output 1 Submitted Solution: ``` from collections import deque N, M = map(int, input().split()) AB = [[] for _ in range(N)] for _ in range(M): a,b = map(lambda x: int(x)-1, input().split()) AB[a].append(b) AB[b].append(a) visited = [0] * N ans = [] for i in range(N): if visited[i]: continue group = [i] d = deque(AB[i]) while d: s = d.popleft() if visited[s]: continue visited[s] = 1 group.append(s) for k in AB[s]: d.append(k) ans.append(len(group)) print(len(ans)-1) ```
instruction
0
93,260
1
186,520
Yes
output
1
93,260
1
186,521
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City A_i and City B_i. Snuke can perform the following operation zero or more times: * Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities. After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times). What is the minimum number of roads he must build to achieve the goal? Constraints * 2 \leq N \leq 100,000 * 1 \leq M \leq 100,000 * 1 \leq A_i < B_i \leq N * No two roads connect the same pair of cities. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output Print the answer. Example Input 3 1 1 2 Output 1 Submitted Solution: ``` def find(x): if par[x] < 0: return x else: par[x] = find(par[x]) return par[x] def same(x, y): return find(x) == find(y) def unite(x, y): x = find(x) y = find(y) if x == y: return if par[x] > par[y]: x, y = y, x par[x] += par[y] par[y] = x N, M = map(int, input().split()) par = [-1 for i in range(N)] for i in range(M): a, b = map(int, input().split()) unite(a - 1, b - 1) cnt = -1 num = {0} for i in par: if i <= -1: cnt += 1 print(cnt) ```
instruction
0
93,261
1
186,522
Yes
output
1
93,261
1
186,523
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City A_i and City B_i. Snuke can perform the following operation zero or more times: * Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities. After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times). What is the minimum number of roads he must build to achieve the goal? Constraints * 2 \leq N \leq 100,000 * 1 \leq M \leq 100,000 * 1 \leq A_i < B_i \leq N * No two roads connect the same pair of cities. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output Print the answer. Example Input 3 1 1 2 Output 1 Submitted Solution: ``` #Union Find #xใฎๆ นใ‚’ๆฑ‚ใ‚ใ‚‹ def find(x): if par[x] < 0: return x else: tank = [] while par[x] >= 0: tank.append(x) x = par[x] for elt in tank: par[elt] = x return x #xใจyใฎๅฑžใ™ใ‚‹้›†ๅˆใฎไฝตๅˆ def unite(x,y): x = find(x) y = find(y) if x == y: return False else: #sizeใฎๅคงใใ„ใปใ†ใŒx if par[x] > par[y]: x,y = y,x par[x] += par[y] par[y] = x return True n,m = map(int, input().split()) par = [-1] * n for i in range(m): a,b = map(int,input().split()) unite(a-1,b-1) ans = set([]) for i in range(n): ans.add(find(i)) print(len(ans)-1) ```
instruction
0
93,262
1
186,524
Yes
output
1
93,262
1
186,525
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City A_i and City B_i. Snuke can perform the following operation zero or more times: * Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities. After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times). What is the minimum number of roads he must build to achieve the goal? Constraints * 2 \leq N \leq 100,000 * 1 \leq M \leq 100,000 * 1 \leq A_i < B_i \leq N * No two roads connect the same pair of cities. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output Print the answer. Example Input 3 1 1 2 Output 1 Submitted Solution: ``` n,m=map(int,input().split()) par=[i for i in range(n)] size=[1]*n def find(x): if par[x]==x: return x else: par[x]=find(par[x]) return par[x] def union(a,b): x=find(a) y=find(b) if x!=y: if size[x]<size[y]: par[x]=par[y] size[y]+=size[x] else: par[y]=par[x] size[x]+=size[y] else: return for i in range(m): a,b=map(int,input().split()) union(a-1,b-1) ans={find(i) for i in range(n)} print(len(ans)-1) ```
instruction
0
93,263
1
186,526
Yes
output
1
93,263
1
186,527
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City A_i and City B_i. Snuke can perform the following operation zero or more times: * Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities. After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times). What is the minimum number of roads he must build to achieve the goal? Constraints * 2 \leq N \leq 100,000 * 1 \leq M \leq 100,000 * 1 \leq A_i < B_i \leq N * No two roads connect the same pair of cities. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output Print the answer. Example Input 3 1 1 2 Output 1 Submitted Solution: ``` N, M = (int(i) for i in input().split()) AB = [input().split() for i in range(M)] route = set() for ab in AB: route.add(ab[0]) route.add(ab[1]) if(len(route) == N): break print(N - len(route)) ```
instruction
0
93,264
1
186,528
No
output
1
93,264
1
186,529
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City A_i and City B_i. Snuke can perform the following operation zero or more times: * Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities. After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times). What is the minimum number of roads he must build to achieve the goal? Constraints * 2 \leq N \leq 100,000 * 1 \leq M \leq 100,000 * 1 \leq A_i < B_i \leq N * No two roads connect the same pair of cities. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output Print the answer. Example Input 3 1 1 2 Output 1 Submitted Solution: ``` from typing import List, Dict MAX_N = 10 ** 5 visited = [False] * MAX_N cluster = [0] * MAX_N edges: Dict[int, List[int]] = {} def dfs(node: int, cluster_num: int) -> None: cluster[node] = cluster_num visited[node] = True if node not in edges: return for v in edges[node]: if visited[v]: continue dfs(v, cluster_num) def connect_cities(node_num: int, edge_num: int) -> int: i = 0 for s in range(node_num): if visited[s]: continue dfs(s, i) i += 1 return i - 1 if __name__ == "__main__": N, M = map(int, input().split()) for _ in range(M): a, b = map(int, input().split()) a, b = a - 1, b - 1 edges.setdefault(a, []) edges[a].append(b) edges.setdefault(b, []) edges[b].append(a) ans = connect_cities(N, M) print(ans) ```
instruction
0
93,265
1
186,530
No
output
1
93,265
1
186,531
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City A_i and City B_i. Snuke can perform the following operation zero or more times: * Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities. After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times). What is the minimum number of roads he must build to achieve the goal? Constraints * 2 \leq N \leq 100,000 * 1 \leq M \leq 100,000 * 1 \leq A_i < B_i \leq N * No two roads connect the same pair of cities. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output Print the answer. Example Input 3 1 1 2 Output 1 Submitted Solution: ``` def s(way): global w global wc for i in way: c[i] = 1 if wc[i] == 0: wc[i] =1 for j in w[i]: c[j]=1 s(w[j]) n,m = map(int,input().split()) a = [0 for i in range(m)] b = [0 for i in range(m)] w = [[] for i in range(n)] wc = [0 for i in range(n)] c = [0 for i in range(n)] for i in range(m): a[i],b[i] = map(int,input().split()) w[a[i]-1].append(b[i]-1) w[b[i]-1].append(a[i]-1) ans =-1 for i in range(n): if c[i] == 0: c[i] = 1 wc[i] = 1 ans += 1 s(w[i]) print(ans) ```
instruction
0
93,266
1
186,532
No
output
1
93,266
1
186,533
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City A_i and City B_i. Snuke can perform the following operation zero or more times: * Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities. After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times). What is the minimum number of roads he must build to achieve the goal? Constraints * 2 \leq N \leq 100,000 * 1 \leq M \leq 100,000 * 1 \leq A_i < B_i \leq N * No two roads connect the same pair of cities. * All values in input are integers. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output Print the answer. Example Input 3 1 1 2 Output 1 Submitted Solution: ``` n,m=map(int,input().split()) if n>m: print(n-1-m) else: print(0) ```
instruction
0
93,267
1
186,534
No
output
1
93,267
1
186,535
Provide a correct Python 3 solution for this coding contest problem. Snuke's town has a subway system, consisting of N stations and M railway lines. The stations are numbered 1 through N. Each line is operated by a company. Each company has an identification number. The i-th ( 1 \leq i \leq M ) line connects station p_i and q_i bidirectionally. There is no intermediate station. This line is operated by company c_i. You can change trains at a station where multiple lines are available. The fare system used in this subway system is a bit strange. When a passenger only uses lines that are operated by the same company, the fare is 1 yen (the currency of Japan). Whenever a passenger changes to a line that is operated by a different company from the current line, the passenger is charged an additional fare of 1 yen. In a case where a passenger who changed from some company A's line to another company's line changes to company A's line again, the additional fare is incurred again. Snuke is now at station 1 and wants to travel to station N by subway. Find the minimum required fare. Constraints * 2 \leq N \leq 10^5 * 0 \leq M \leq 2ร—10^5 * 1 \leq p_i \leq N (1 \leq i \leq M) * 1 \leq q_i \leq N (1 \leq i \leq M) * 1 \leq c_i \leq 10^6 (1 \leq i \leq M) * p_i \neq q_i (1 \leq i \leq M) Input The input is given from Standard Input in the following format: N M p_1 q_1 c_1 : p_M q_M c_M Output Print the minimum required fare. If it is impossible to get to station N by subway, print `-1` instead. Examples Input 3 3 1 2 1 2 3 1 3 1 2 Output 1 Input 8 11 1 3 1 1 4 2 2 3 1 2 5 1 3 4 3 3 6 3 3 7 3 4 8 4 5 6 1 6 7 5 7 8 5 Output 2 Input 2 0 Output -1
instruction
0
93,391
1
186,782
"Correct Solution: ``` import sys,heapq from collections import defaultdict,deque input = sys.stdin.readline n,m = map(int,input().split()) #+่ทฏ็ทš*chg : ใใฎ่ทฏ็ทšใƒ›ใƒผใƒ  chg = 10**6 edge=defaultdict(set) used =defaultdict(bool) for i in range(m): p,q,c = map(int,input().split()) edge[p].add(p+c*chg) edge[p+c*chg].add(p) edge[p+c*chg].add(q+c*chg) edge[q+c*chg].add(p+c*chg) edge[q].add(q+c*chg) edge[q+c*chg].add(q) used[p] = False used[q] = False used[p+c*chg] = False used[q+c*chg] = False edgelist = deque() edgelist.append((1,0)) res = float("inf") while len(edgelist): x,cost = edgelist.pop() used[x] = True if x == n: res = cost break for e in edge[x]: if used[e]: continue #ใƒ›ใƒผใƒ โ†’ๆ”นๆœญ if x <= 10**5 and chg <= e: edgelist.appendleft((e,cost+1)) else: edgelist.append((e,cost)) if res == float("inf"): print(-1) else: print(res) ```
output
1
93,391
1
186,783
Provide a correct Python 3 solution for this coding contest problem. Snuke's town has a subway system, consisting of N stations and M railway lines. The stations are numbered 1 through N. Each line is operated by a company. Each company has an identification number. The i-th ( 1 \leq i \leq M ) line connects station p_i and q_i bidirectionally. There is no intermediate station. This line is operated by company c_i. You can change trains at a station where multiple lines are available. The fare system used in this subway system is a bit strange. When a passenger only uses lines that are operated by the same company, the fare is 1 yen (the currency of Japan). Whenever a passenger changes to a line that is operated by a different company from the current line, the passenger is charged an additional fare of 1 yen. In a case where a passenger who changed from some company A's line to another company's line changes to company A's line again, the additional fare is incurred again. Snuke is now at station 1 and wants to travel to station N by subway. Find the minimum required fare. Constraints * 2 \leq N \leq 10^5 * 0 \leq M \leq 2ร—10^5 * 1 \leq p_i \leq N (1 \leq i \leq M) * 1 \leq q_i \leq N (1 \leq i \leq M) * 1 \leq c_i \leq 10^6 (1 \leq i \leq M) * p_i \neq q_i (1 \leq i \leq M) Input The input is given from Standard Input in the following format: N M p_1 q_1 c_1 : p_M q_M c_M Output Print the minimum required fare. If it is impossible to get to station N by subway, print `-1` instead. Examples Input 3 3 1 2 1 2 3 1 3 1 2 Output 1 Input 8 11 1 3 1 1 4 2 2 3 1 2 5 1 3 4 3 3 6 3 3 7 3 4 8 4 5 6 1 6 7 5 7 8 5 Output 2 Input 2 0 Output -1
instruction
0
93,392
1
186,784
"Correct Solution: ``` import sys input = sys.stdin.readline from collections import defaultdict """ (้ง…ใ€ไผš็คพ)ใ‚’้ ‚็‚นใซใ‚ฐใƒฉใƒ•ใ‚’ๆŒใคใ€‚้ ‚็‚นๆ•ฐO(M)ใ€‚ ใใฎใพใพ่พบใ‚’่ฒผใ‚‹ใจ่พบใŒๅคšใใชใ‚Šใ™ใŽใ‚‹ใ€‚ (้ง…ใ€ไผš็คพ) -> (้ง…ใ€็„กๅฑžๆ€ง) -> (้ง…ใ€ไผš็คพ) """ L = 32 mask = (1 << L) - 1 N,M = map(int,input().split()) graph = defaultdict(list) for _ in range(M): p,q,c = map(int,input().split()) p <<= L q <<= L graph[p].append(p+c) graph[p+c].append(p) graph[q].append(q+c) graph[q+c].append(q) graph[p+c].append(q+c) graph[q+c].append(p+c) INF = 10 ** 9 dist = defaultdict(lambda: INF) start = 1 << L goal = N << L q = [start] # 0 ใŒไผš็คพใซๅฑžใ—ใฆใ„ใชใ„็Šถๆ…‹ dist[start] = 0 d = 0 while q: qq = [] while q: x = q.pop() if x & mask == 0: for y in graph[x]: if dist[y] <= d + 1: continue dist[y] = d + 1 qq.append(y) else: for y in graph[x]: if dist[y] <= d: continue dist[y] = d q.append(y) d += 1 q = qq answer = dist[goal] if answer == INF: answer = -1 print(answer) ```
output
1
93,392
1
186,785
Provide a correct Python 3 solution for this coding contest problem. Snuke's town has a subway system, consisting of N stations and M railway lines. The stations are numbered 1 through N. Each line is operated by a company. Each company has an identification number. The i-th ( 1 \leq i \leq M ) line connects station p_i and q_i bidirectionally. There is no intermediate station. This line is operated by company c_i. You can change trains at a station where multiple lines are available. The fare system used in this subway system is a bit strange. When a passenger only uses lines that are operated by the same company, the fare is 1 yen (the currency of Japan). Whenever a passenger changes to a line that is operated by a different company from the current line, the passenger is charged an additional fare of 1 yen. In a case where a passenger who changed from some company A's line to another company's line changes to company A's line again, the additional fare is incurred again. Snuke is now at station 1 and wants to travel to station N by subway. Find the minimum required fare. Constraints * 2 \leq N \leq 10^5 * 0 \leq M \leq 2ร—10^5 * 1 \leq p_i \leq N (1 \leq i \leq M) * 1 \leq q_i \leq N (1 \leq i \leq M) * 1 \leq c_i \leq 10^6 (1 \leq i \leq M) * p_i \neq q_i (1 \leq i \leq M) Input The input is given from Standard Input in the following format: N M p_1 q_1 c_1 : p_M q_M c_M Output Print the minimum required fare. If it is impossible to get to station N by subway, print `-1` instead. Examples Input 3 3 1 2 1 2 3 1 3 1 2 Output 1 Input 8 11 1 3 1 1 4 2 2 3 1 2 5 1 3 4 3 3 6 3 3 7 3 4 8 4 5 6 1 6 7 5 7 8 5 Output 2 Input 2 0 Output -1
instruction
0
93,393
1
186,786
"Correct Solution: ``` import sys int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def MI1(): return map(int1, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] from collections import deque def solve(): dist = [inf] * len(to) q = deque() q.append((0, 0)) dist[0] = 0 while q: d, i = q.popleft() if d > dist[i]: continue if i < n: for j in to[i]: if dist[j] <= d + 1: continue dist[j] = d + 1 q.append((d + 1, j)) else: for j in to[i]: if j == n - 1: print(dist[i]) exit() if dist[j] <= d: continue dist[j] = d q.appendleft((d, j)) print(-1) inf=10**9 n,m=MI() to=[] uctoi={} for u in range(n): to.append([]) uctoi[u,0]=u for _ in range(m): u,v,c=MI() u,v=u-1,v-1 if (u,c) not in uctoi: i=uctoi[u,c]=len(to) to.append([]) to[i].append(u) to[u].append(i) else:i=uctoi[u,c] if (v,c) not in uctoi: j=uctoi[v,c]=len(to) to.append([]) to[j].append(v) to[v].append(j) else:j=uctoi[v,c] to[i].append(j) to[j].append(i) #print(to) #print(fin) solve() ```
output
1
93,393
1
186,787
Provide a correct Python 3 solution for this coding contest problem. Snuke's town has a subway system, consisting of N stations and M railway lines. The stations are numbered 1 through N. Each line is operated by a company. Each company has an identification number. The i-th ( 1 \leq i \leq M ) line connects station p_i and q_i bidirectionally. There is no intermediate station. This line is operated by company c_i. You can change trains at a station where multiple lines are available. The fare system used in this subway system is a bit strange. When a passenger only uses lines that are operated by the same company, the fare is 1 yen (the currency of Japan). Whenever a passenger changes to a line that is operated by a different company from the current line, the passenger is charged an additional fare of 1 yen. In a case where a passenger who changed from some company A's line to another company's line changes to company A's line again, the additional fare is incurred again. Snuke is now at station 1 and wants to travel to station N by subway. Find the minimum required fare. Constraints * 2 \leq N \leq 10^5 * 0 \leq M \leq 2ร—10^5 * 1 \leq p_i \leq N (1 \leq i \leq M) * 1 \leq q_i \leq N (1 \leq i \leq M) * 1 \leq c_i \leq 10^6 (1 \leq i \leq M) * p_i \neq q_i (1 \leq i \leq M) Input The input is given from Standard Input in the following format: N M p_1 q_1 c_1 : p_M q_M c_M Output Print the minimum required fare. If it is impossible to get to station N by subway, print `-1` instead. Examples Input 3 3 1 2 1 2 3 1 3 1 2 Output 1 Input 8 11 1 3 1 1 4 2 2 3 1 2 5 1 3 4 3 3 6 3 3 7 3 4 8 4 5 6 1 6 7 5 7 8 5 Output 2 Input 2 0 Output -1
instruction
0
93,394
1
186,788
"Correct Solution: ``` # -*- coding: utf-8 -*- import bisect import heapq import math import random import sys from collections import Counter, defaultdict, deque from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal from functools import lru_cache, reduce from itertools import combinations, combinations_with_replacement, product, permutations from operator import add, mul, sub sys.setrecursionlimit(10000) def read_int(): return int(input()) def read_int_n(): return list(map(int, input().split())) def read_float(): return float(input()) def read_float_n(): return list(map(float, input().split())) def read_str(): return input().strip() def read_str_n(): return list(map(str, input().split())) def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, 'sec') return ret return wrap def bfs01(g, s): d = {} for v in g.keys(): d[v] = sys.maxsize d[s] = 0 if s not in d: return d q = deque() q.append((d[s], s)) NM = 1 << 30 while q: _, u = q.popleft() for v in g[u]: if v > NM: c = 0 else: c = 1 alt = c + d[u] if d[v] > alt: d[v] = alt if c == 0: q.appendleft((d[v], v)) else: q.append((d[v], v)) return d @mt def slv(N, M): g = defaultdict(list) NM = 30 for _ in range(M): p, q, c = input().split() p = int(p) q = int(q) c = int(c) cp = (c << NM) + p cq = (c << NM) + q g[cp].append(cq) g[cq].append(cp) g[cp].append(p) g[cq].append(q) g[p].append((cp)) g[q].append((cq)) d = bfs01(g, 1) return -1 if N not in d or d[N] == sys.maxsize else d[N] def main(): N, M = read_int_n() print(slv(N, M)) if __name__ == '__main__': main() ```
output
1
93,394
1
186,789
Provide a correct Python 3 solution for this coding contest problem. Snuke's town has a subway system, consisting of N stations and M railway lines. The stations are numbered 1 through N. Each line is operated by a company. Each company has an identification number. The i-th ( 1 \leq i \leq M ) line connects station p_i and q_i bidirectionally. There is no intermediate station. This line is operated by company c_i. You can change trains at a station where multiple lines are available. The fare system used in this subway system is a bit strange. When a passenger only uses lines that are operated by the same company, the fare is 1 yen (the currency of Japan). Whenever a passenger changes to a line that is operated by a different company from the current line, the passenger is charged an additional fare of 1 yen. In a case where a passenger who changed from some company A's line to another company's line changes to company A's line again, the additional fare is incurred again. Snuke is now at station 1 and wants to travel to station N by subway. Find the minimum required fare. Constraints * 2 \leq N \leq 10^5 * 0 \leq M \leq 2ร—10^5 * 1 \leq p_i \leq N (1 \leq i \leq M) * 1 \leq q_i \leq N (1 \leq i \leq M) * 1 \leq c_i \leq 10^6 (1 \leq i \leq M) * p_i \neq q_i (1 \leq i \leq M) Input The input is given from Standard Input in the following format: N M p_1 q_1 c_1 : p_M q_M c_M Output Print the minimum required fare. If it is impossible to get to station N by subway, print `-1` instead. Examples Input 3 3 1 2 1 2 3 1 3 1 2 Output 1 Input 8 11 1 3 1 1 4 2 2 3 1 2 5 1 3 4 3 3 6 3 3 7 3 4 8 4 5 6 1 6 7 5 7 8 5 Output 2 Input 2 0 Output -1
instruction
0
93,395
1
186,790
"Correct Solution: ``` from collections import deque,defaultdict import sys input = sys.stdin.readline N,M=map(int,input().split()) mod =10**7 table=defaultdict(set) visit=defaultdict(int) for i in range(M): a,b,c=map(int,input().split()) table[a].add(a+c*mod) table[b].add(b+c*mod) table[a+c*mod].add(a) table[b+c*mod].add(b) table[a+c*mod].add(b+c*mod) table[b+c*mod].add(a+c*mod) visit[a]=0 visit[b]=0 visit[b+c*mod]=0 visit[a+c*mod]=0 H=deque() H.append((1,0)) visit[1]=1 ans=mod while H: #print(H) x,cost=H.popleft() visit[x] = 1 if x==N: ans=min(ans,cost) break for y in table[x]: if visit[y]==1: continue if x<=10**5 and mod<=y: H.append((y,cost+1)) else: H.appendleft((y,cost)) if ans==mod: print(-1) else: print(ans) ```
output
1
93,395
1
186,791
Provide a correct Python 3 solution for this coding contest problem. Snuke's town has a subway system, consisting of N stations and M railway lines. The stations are numbered 1 through N. Each line is operated by a company. Each company has an identification number. The i-th ( 1 \leq i \leq M ) line connects station p_i and q_i bidirectionally. There is no intermediate station. This line is operated by company c_i. You can change trains at a station where multiple lines are available. The fare system used in this subway system is a bit strange. When a passenger only uses lines that are operated by the same company, the fare is 1 yen (the currency of Japan). Whenever a passenger changes to a line that is operated by a different company from the current line, the passenger is charged an additional fare of 1 yen. In a case where a passenger who changed from some company A's line to another company's line changes to company A's line again, the additional fare is incurred again. Snuke is now at station 1 and wants to travel to station N by subway. Find the minimum required fare. Constraints * 2 \leq N \leq 10^5 * 0 \leq M \leq 2ร—10^5 * 1 \leq p_i \leq N (1 \leq i \leq M) * 1 \leq q_i \leq N (1 \leq i \leq M) * 1 \leq c_i \leq 10^6 (1 \leq i \leq M) * p_i \neq q_i (1 \leq i \leq M) Input The input is given from Standard Input in the following format: N M p_1 q_1 c_1 : p_M q_M c_M Output Print the minimum required fare. If it is impossible to get to station N by subway, print `-1` instead. Examples Input 3 3 1 2 1 2 3 1 3 1 2 Output 1 Input 8 11 1 3 1 1 4 2 2 3 1 2 5 1 3 4 3 3 6 3 3 7 3 4 8 4 5 6 1 6 7 5 7 8 5 Output 2 Input 2 0 Output -1
instruction
0
93,396
1
186,792
"Correct Solution: ``` # coding: utf-8 # Your code here! import sys readline = sys.stdin.readline read = sys.stdin.read n,m = map(int, readline().split()) g = {} for _ in range(m): p,q,c = map(int, readline().split()) pc = ((p-1)<<20)+c qc = ((q-1)<<20)+c pp = (p-1)<<20 qq = (q-1)<<20 if pc not in g: g[pc] = [] if qc not in g: g[qc] = [] if pp not in g: g[pp] = [] if qq not in g: g[qq] = [] g[pc].append(qc) g[pc].append(pp) g[qc].append(pc) g[qc].append(qq) g[pp].append(pc) g[qq].append(qc) if 0 not in g: g[0] = [] from collections import deque q = deque([(0,0)]) res = {0:0} mask = (1<<20)-1 while q: vl,dv = q.popleft() if res[vl] < dv: continue if (vl>>20) == n-1: res[(n-1)<<20] = dv+1 break for tl in g[vl]: ndv = dv + (vl&mask==0 or tl&mask==0) if tl not in res or res[tl] > ndv: res[tl] = ndv if vl&mask==0 or tl&mask==0: q.append((tl,ndv)) else: q.appendleft((tl,ndv)) if (n-1)<<20 in res: print(res[(n-1)<<20]//2) else: print(-1) ```
output
1
93,396
1
186,793
Provide a correct Python 3 solution for this coding contest problem. Snuke's town has a subway system, consisting of N stations and M railway lines. The stations are numbered 1 through N. Each line is operated by a company. Each company has an identification number. The i-th ( 1 \leq i \leq M ) line connects station p_i and q_i bidirectionally. There is no intermediate station. This line is operated by company c_i. You can change trains at a station where multiple lines are available. The fare system used in this subway system is a bit strange. When a passenger only uses lines that are operated by the same company, the fare is 1 yen (the currency of Japan). Whenever a passenger changes to a line that is operated by a different company from the current line, the passenger is charged an additional fare of 1 yen. In a case where a passenger who changed from some company A's line to another company's line changes to company A's line again, the additional fare is incurred again. Snuke is now at station 1 and wants to travel to station N by subway. Find the minimum required fare. Constraints * 2 \leq N \leq 10^5 * 0 \leq M \leq 2ร—10^5 * 1 \leq p_i \leq N (1 \leq i \leq M) * 1 \leq q_i \leq N (1 \leq i \leq M) * 1 \leq c_i \leq 10^6 (1 \leq i \leq M) * p_i \neq q_i (1 \leq i \leq M) Input The input is given from Standard Input in the following format: N M p_1 q_1 c_1 : p_M q_M c_M Output Print the minimum required fare. If it is impossible to get to station N by subway, print `-1` instead. Examples Input 3 3 1 2 1 2 3 1 3 1 2 Output 1 Input 8 11 1 3 1 1 4 2 2 3 1 2 5 1 3 4 3 3 6 3 3 7 3 4 8 4 5 6 1 6 7 5 7 8 5 Output 2 Input 2 0 Output -1
instruction
0
93,397
1
186,794
"Correct Solution: ``` import sys from collections import deque, defaultdict def bfs01(s, t, links): q = deque([(0, s, -1)]) # cost, station, last-company visited = set() while q: d, v, e = q.popleft() if v == t: return d if (v, e) in visited: continue visited.add((v, e)) if e == 0: lv = links[v] for c in lv: for u in lv[c]: if (u, c) in visited: continue q.append((d + 1, u, c)) else: for u in links[v][e]: if (u, e) in visited: continue q.appendleft((d, u, e)) if (v, 0) not in visited: q.appendleft((d, v, 0)) return -1 readline = sys.stdin.buffer.readline read = sys.stdin.buffer.read n, m = map(int, readline().split()) links = [defaultdict(set) for _ in range(n)] pqc = list(map(int, read().split())) for p, q, c in zip(pqc[0::3], pqc[1::3], pqc[2::3]): p -= 1 q -= 1 links[p][c].add(q) links[q][c].add(p) print(bfs01(0, n - 1, links)) ```
output
1
93,397
1
186,795
Provide a correct Python 3 solution for this coding contest problem. Snuke's town has a subway system, consisting of N stations and M railway lines. The stations are numbered 1 through N. Each line is operated by a company. Each company has an identification number. The i-th ( 1 \leq i \leq M ) line connects station p_i and q_i bidirectionally. There is no intermediate station. This line is operated by company c_i. You can change trains at a station where multiple lines are available. The fare system used in this subway system is a bit strange. When a passenger only uses lines that are operated by the same company, the fare is 1 yen (the currency of Japan). Whenever a passenger changes to a line that is operated by a different company from the current line, the passenger is charged an additional fare of 1 yen. In a case where a passenger who changed from some company A's line to another company's line changes to company A's line again, the additional fare is incurred again. Snuke is now at station 1 and wants to travel to station N by subway. Find the minimum required fare. Constraints * 2 \leq N \leq 10^5 * 0 \leq M \leq 2ร—10^5 * 1 \leq p_i \leq N (1 \leq i \leq M) * 1 \leq q_i \leq N (1 \leq i \leq M) * 1 \leq c_i \leq 10^6 (1 \leq i \leq M) * p_i \neq q_i (1 \leq i \leq M) Input The input is given from Standard Input in the following format: N M p_1 q_1 c_1 : p_M q_M c_M Output Print the minimum required fare. If it is impossible to get to station N by subway, print `-1` instead. Examples Input 3 3 1 2 1 2 3 1 3 1 2 Output 1 Input 8 11 1 3 1 1 4 2 2 3 1 2 5 1 3 4 3 3 6 3 3 7 3 4 8 4 5 6 1 6 7 5 7 8 5 Output 2 Input 2 0 Output -1
instruction
0
93,398
1
186,796
"Correct Solution: ``` from collections import defaultdict,deque import sys sys.setrecursionlimit(10**6) input=sys.stdin.readline N,M=map(int,input().split()) if M==0: print(-1) exit() G=defaultdict(set) for i in range(M): a,b,c=map(int,input().split()) G[a+(c<<30)].add(b+(c<<30)) G[b+(c<<30)].add(a+(c<<30)) G[a].add(a+(c<<30)) G[b].add(b+(c<<30)) G[a+(c<<30)].add(a) G[b+(c<<30)].add(b) def BFS(x): d={} for k in G.keys(): d[k]=float("inf") stack=deque([(x,0)]) if x not in d: return -1 while stack: s,m=stack.popleft() if s==N: return m if d[s]>m: d[s]=m if s>=(1<<30): for i in G[s]: if d[i]>10**30: stack.appendleft((i,m)) else: for i in G[s]: if d[i]>10**30: stack.append((i,m+1)) return -1 print(BFS(1)) ```
output
1
93,398
1
186,797
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Snuke's town has a subway system, consisting of N stations and M railway lines. The stations are numbered 1 through N. Each line is operated by a company. Each company has an identification number. The i-th ( 1 \leq i \leq M ) line connects station p_i and q_i bidirectionally. There is no intermediate station. This line is operated by company c_i. You can change trains at a station where multiple lines are available. The fare system used in this subway system is a bit strange. When a passenger only uses lines that are operated by the same company, the fare is 1 yen (the currency of Japan). Whenever a passenger changes to a line that is operated by a different company from the current line, the passenger is charged an additional fare of 1 yen. In a case where a passenger who changed from some company A's line to another company's line changes to company A's line again, the additional fare is incurred again. Snuke is now at station 1 and wants to travel to station N by subway. Find the minimum required fare. Constraints * 2 \leq N \leq 10^5 * 0 \leq M \leq 2ร—10^5 * 1 \leq p_i \leq N (1 \leq i \leq M) * 1 \leq q_i \leq N (1 \leq i \leq M) * 1 \leq c_i \leq 10^6 (1 \leq i \leq M) * p_i \neq q_i (1 \leq i \leq M) Input The input is given from Standard Input in the following format: N M p_1 q_1 c_1 : p_M q_M c_M Output Print the minimum required fare. If it is impossible to get to station N by subway, print `-1` instead. Examples Input 3 3 1 2 1 2 3 1 3 1 2 Output 1 Input 8 11 1 3 1 1 4 2 2 3 1 2 5 1 3 4 3 3 6 3 3 7 3 4 8 4 5 6 1 6 7 5 7 8 5 Output 2 Input 2 0 Output -1 Submitted Solution: ``` def main(): N,M=map(int,input().split()) if M == 0: print(-1) exit() INF = 10**6 pqc = [tuple(map(lambda x:int(x)-1,input().split())) for _ in range(M)] to1d={v:{} for v in range(N)} count=2 for p,q,c in pqc: if not to1d[p].get(c): to1d[p][c] = count count += 1 if not to1d[q].get(c): to1d[q][c] = count count += 1 G = [{} for _ in range(N+count)] for p,q,c in pqc: v1,v2 = to1d[p][c],to1d[q][c] G[v1][v2] = G[v2][v1] = 0 for i in range(N): if len(to1d[i])<=1: continue for c in to1d[i].keys(): v = to1d[i][c] G[v][count] = 1 G[count][v] = 0 count += 1 def dijkstra(start = 0, goal = 1): from heapq import heappop, heappush NN = len(G) d = [INF]*NN d[start] = 0 que = [] heappush(que, start) while que: p = divmod(heappop(que),NN) v = p[1] if d[v] < p[0]: continue for u in G[v].keys(): if d[u] > d[v] + G[v][u]: d[u] = d[v] + G[v][u] heappush(que, d[u]*NN+u) if v == goal: return d[goal] return d[goal] for c in to1d[0].keys(): v = to1d[0][c] G[0][v] = 1 for c in to1d[N-1].keys(): v = to1d[N-1][c] G[v][1] = 0 ans = dijkstra(0,1) print(ans if ans < INF else -1) if __name__=="__main__": main() ```
instruction
0
93,399
1
186,798
Yes
output
1
93,399
1
186,799