Buggy python submission (verdict: Runtime Error):
def main():
    n, m = map(int,raw_input().split())
    A = [[] for i in xrange(n)]
    for i in xrange(m):
        s, t = map(int,raw_input().split())
        A[s].append(t)
        A[t].append(s)

    q = input()
    a =[0]*q
    for i in xrange(q):
        s, t = map(int,raw_input().split())
        c = [-1]*n
        dfs(s,t,c,A)
        if c[t] == -1:
            a[i] = 'no'
        else:
            a[i] = 'yes'

    for i in a:
        print i
        
def dfs(u,t,color,A):
    color[u] = 0
    for i in A[u]:
        if i == t:
            color[t] = 1
            break
        elif (color[i] == -1):
            dfs(i,t,color,A)
    color[u] = 1

if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    n, m = map(int,raw_input().split())
    A = [[] for i in xrange(n)]
    for i in xrange(m):
        s, t = map(int,raw_input().split())
        A[s].append(t)
        A[t].append(s)

    g = [-1]*n
    x = 0
    for i in xrange(n):
        if g[i] == -1:
            g = bfs(n,A,i,g,x)
            x += 1
            
    q = input()
    a =[0]*q
    for i in xrange(q):
        s, t = map(int,raw_input().split())
        if g[s] == g[t]:
            a[i] = 'yes'
        else:
            a[i] = 'no'
    for i in a:
        print i
        
def bfs(n,A,s,color,x):
    Q = deque([s])
    while(len(Q)!=0):
        u = Q.popleft()
        for i in A[u]:
            if color[i] == -1:
                Q.append(i)
        color[u] = x
    return color

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, m = map(int,raw_input().split())
    A = [[] for i in xrange(n)]
    for i in xrange(m):
        s, t = map(int,raw_input().split())
        A[s].append(t)
        A[t].append(s)

    q = input()
    a =[0]*q
    for i in xrange(q):
        s, t = map(int,raw_input().split())
        c = bfs(n,A,s,t)
        if c[t] == -1:
            a[i] = 'no'
        else:
            a[i] = 'yes'

    for i in a:
        print i
        
def bfs(n,A,s,t):
    color = [-1]*n
    Q = []
    Q.append(s)
    while(True):
        if len(Q)==0:
            break
        u = Q.pop(0)
        for i in A[u]:
            if i == t:
                color[t] = 1
                break
            elif color[i] == -1:
                color[i] = 0
                Q.append(i)
        color[u] = 1
    return color

if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    n, m = map(int,raw_input().split())
    A = [[] for i in xrange(n)]
    for i in xrange(m):
        s, t = map(int,raw_input().split())
        A[s].append(t)
        A[t].append(s)

    g = [-1]*n
    x = 0
    for i in xrange(n):
        if g[i] == -1:
            g = bfs(n,A,i,g,x)
            x += 1
            
    q = input()
    a =[0]*q
    for i in xrange(q):
        s, t = map(int,raw_input().split())
        if g[s] == g[t]:
            a[i] = 'yes'
        else:
            a[i] = 'no'
    for i in a:
        print i
        
def bfs(n,A,s,color,x):
    Q = deque([s])
    while(len(Q)!=0):
        u = Q.popleft()
        for i in A[u]:
            if color[i] == -1:
                Q.append(i)
        color[u] = x
    return color

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

def main():
    n, m = map(int,raw_input().split())
    A = [[] for i in xrange(n)]
    for i in xrange(m):
        s, t = map(int,raw_input().split())
        A[s].append(t)
        A[t].append(s)

    q = input()
    a =[0]*q
    for i in xrange(q):
        s, t = map(int,raw_input().split())
        c = bfs(n,A,s,t)
        if c[t] == -1:
            a[i] = 'no'
        else:
            a[i] = 'yes'

    for i in a:
        print i
        
def bfs(n,A,s,t):
    color = [-1]*n
    Q = deque([s])
    while(True):
        if len(Q)==0:
            break
        u = Q.popleft()
        for i in A[u]:
            if i == t:
                color[t] = 1
                break
            elif color[i] == -1:
                color[i] = 0
                Q.append(i)
        color[u] = 1
    return color

if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    n, m = map(int,raw_input().split())
    A = [[] for i in xrange(n)]
    for i in xrange(m):
        s, t = map(int,raw_input().split())
        A[s].append(t)
        A[t].append(s)

    g = [-1]*n
    x = 0
    for i in xrange(n):
        if g[i] == -1:
            g = bfs(n,A,i,g,x)
            x += 1
            
    q = input()
    a =[0]*q
    for i in xrange(q):
        s, t = map(int,raw_input().split())
        if g[s] == g[t]:
            a[i] = 'yes'
        else:
            a[i] = 'no'
    for i in a:
        print i
        
def bfs(n,A,s,color,x):
    Q = deque([s])
    while(len(Q)!=0):
        u = Q.popleft()
        for i in A[u]:
            if color[i] == -1:
                Q.append(i)
        color[u] = x
    return color

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

def main():
    n, m = map(int,raw_input().split())
    A = [[] for i in xrange(n)]
    for i in xrange(m):
        s, t = map(int,raw_input().split())
        A[s].append(t)
        A[t].append(s)

    g = [-1]*n
    p = 1
    res = set(range(n))
    for i in xrange(n):
        if i in res:
            color = bfs(n,A,i,-1)
            c = []
            for j in xrange(n):
                if color[j] == 1:
                    c.append(i)
                    g[j] = p
            res = res - set(c)
            p += 1
            
    q = input()
    a =[0]*q
    for i in xrange(q):
        s, t = map(int,raw_input().split())
        if g[s] == g[t]:
            a[i] = 'yes'
        else:
            a[i] = 'no'
    for i in a:
        print i
        
def bfs(n,A,s,t):
    color = [-1]*n
    Q = deque([s])
    while(True):
        if len(Q)==0:
            break
        u = Q.popleft()
        for i in A[u]:
            if i == t:
                color[t] = 1
                break
            elif color[i] == -1:
                color[i] = 0
                Q.append(i)
        color[u] = 1
    return color

if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque

def main():
    n, m = map(int,raw_input().split())
    A = [[] for i in xrange(n)]
    for i in xrange(m):
        s, t = map(int,raw_input().split())
        A[s].append(t)
        A[t].append(s)

    g = [-1]*n
    x = 0
    for i in xrange(n):
        if g[i] == -1:
            g = bfs(n,A,i,g,x)
            x += 1
            
    q = input()
    a =[0]*q
    for i in xrange(q):
        s, t = map(int,raw_input().split())
        if g[s] == g[t]:
            a[i] = 'yes'
        else:
            a[i] = 'no'
    for i in a:
        print i
        
def bfs(n,A,s,color,x):
    Q = deque([s])
    while(len(Q)!=0):
        u = Q.popleft()
        for i in A[u]:
            if color[i] == -1:
                Q.append(i)
        color[u] = x
    return color

if __name__ == '__main__':
    main()