0

I recently tried to submit my solution for one of codeforces problem. I successfully solved the question and tried various test cases and the code was working correctly in my local machine. The time constraint was 2 seconds in the problem. I tried to submit the solution but it said time limit exceed.

HERE IS THE CODE (My implementation)

def find(u):
    if parent[u] == u:
        return u
    parent[u] = find(parent[u])
    return parent[u]

n,m = map(int,input().split())
parent = [i for i in range(n+1)]
ans = [0 for i in range(n+1)]

for i in range(1,m+1):
    grp = [int(i) for i in input().split()][1:]
    if len(grp) == 0:
        continue
    x = find(grp[0])
    for i in grp[1:]:
        parent[find(i)] = x
for i in range(1,n+1):
    ans[find(i)] += 1
ans = " ".join(map(str,[ans[find(u)] for u in range(1, n + 1)]))
print(ans)

Then I did some changes in the code and it was successfully submitted on codeforces.

SECOND IMPLEMENTATION:

from sys import stdin
from sys import stdout

def find(u):
    if parent[u] == u:
        return u
    parent[u] = find(parent[u])
    return parent[u]

n, m = map(int, stdin.readline().split())
parent = [i for i in range(n+1)]
ans = [0 for i in range(1+n)]
for i in range(1,m+1):
    grp= [int(u) for u in stdin.readline().split()][1:]
    if(len(grp)==0):
        continue
    x = find(grp[0])
    for i in grp[1:]:
        parent[find(i)] = x

for i in range(1,n+1):
    ans[find(i)]+=1
pprintit = ' '.join(map(str, [ans[find(u)] for u in range(1, n + 1)]))
stdout.write('%s' % pprintit)

My Question is:

Is stdin faster than input() because just by changing the I/O methods I could submit the solution

I am using python 3.7.

Thank You in advance.

Roosh
  • 27
  • 1
  • 8
  • 1
    Possible duplicate of [sys.stdin.readline() and input(): which one is faster when reading lines of input, and why?](https://stackoverflow.com/questions/22623528/sys-stdin-readline-and-input-which-one-is-faster-when-reading-lines-of-inpu) – Mansur May 29 '19 at 05:57
  • I am still not clear of what should be used and why? – Roosh May 29 '19 at 06:03
  • As the above codes are almost same and the only difference is in the inputs. But still a lot of difference when it comes to time. – Roosh May 29 '19 at 06:07
  • Have you tried to run any type of benchmark? Or maybe CodeForce kind of expects the code to use `stdin`. Not sure if that makes sense, tho. Another thing to mention is that, as far as I understood, `input()` itself uses `stdin` to get input, and `stdout` to print the message. I doesn't answer your question, but these maybe are the culprits. – Mansur May 29 '19 at 06:10
  • @MensurQulami the second implementation takes around 1903 ms and first one 2000+ ms. I think I should be using stdin now onwards. Thankyou for the answer. – Roosh May 29 '19 at 06:13
  • I ll try to compare both using various programs. I think that is the best way to understand it. Thankyou for the idea. – Roosh May 29 '19 at 06:14
  • You may also want to update your post, in case you will find the answer or have any progress. – Mansur May 29 '19 at 06:16
  • Sure I will. Thanks – Roosh May 29 '19 at 06:17

0 Answers0