Submission #473861


Source Code Expand

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#roitiさんをまるまるパクリ
import time
import sys
import io
import re
import math
import itertools
import collections
#sys.stdin=file('input.txt')
#sys.stdout=file('output.txt','w')
#10**9+7
mod=1000000007
#mod=1777777777
pi=3.141592653589
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
#start = time.clock()
class UnionFind:
    def __init__(self, size):
        self.rank=[0]*size
        self.par=range(size)
        self.g_num=size

    def find(self, x):
    #木の根を求める
        if x==self.par[x]:
        #根
            return x
        #経路圧縮
        self.par[x]=self.find(self.par[x])
        return self.par[x]

    def same(self, x, y):
    #xとが同じ集合に属するか否か
        return self.find(x)==self.find(y)

    def unite(self, x, y):
    #xとyの属する集合を併合
        x,y=self.find(x),self.find(y)
        if x==y:
            return

        self.g_num-=1
        if self.rank[x]>self.rank[y]:
            self.par[y]=x
        else:
            self.par[x]=y
            if self.rank[x]==self.rank[y]:
                self.rank[y]+=1

    def group_num(self):
        return self.g_num


N,Q=map(int,raw_input().split())
uf=UnionFind(N)

for loop in xrange(Q):
    p,a,b=map(int,raw_input().split())
    if p==0:
        uf.unite(a,b)
    else:
        print 'Yes' if uf.same(a,b) else 'No'

Submission Info

Submission Time
Task B - Union Find
User nyon
Language Python (2.7.3)
Score 100
Code Size 1490 Byte
Status AC
Exec Time 1748 ms
Memory 8136 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 1
AC × 19
Set Name Test Cases
Sample 00_sample_01.txt
All 00_sample_01.txt, subtask_01_01.txt, subtask_01_02.txt, subtask_01_03.txt, subtask_01_04.txt, subtask_01_05.txt, subtask_01_06.txt, subtask_01_07.txt, subtask_01_08.txt, subtask_01_09.txt, subtask_01_10.txt, subtask_01_11.txt, subtask_01_12.txt, subtask_01_13.txt, subtask_01_14.txt, subtask_01_15.txt, subtask_01_16.txt, subtask_01_17.txt, subtask_01_18.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 70 ms 4108 KB
subtask_01_01.txt AC 961 ms 4492 KB
subtask_01_02.txt AC 81 ms 8068 KB
subtask_01_03.txt AC 1475 ms 4232 KB
subtask_01_04.txt AC 1555 ms 8072 KB
subtask_01_05.txt AC 174 ms 4172 KB
subtask_01_06.txt AC 187 ms 8072 KB
subtask_01_07.txt AC 1545 ms 4104 KB
subtask_01_08.txt AC 1542 ms 8136 KB
subtask_01_09.txt AC 69 ms 4176 KB
subtask_01_10.txt AC 82 ms 8076 KB
subtask_01_11.txt AC 1511 ms 4108 KB
subtask_01_12.txt AC 1529 ms 8124 KB
subtask_01_13.txt AC 1214 ms 4292 KB
subtask_01_14.txt AC 90 ms 8108 KB
subtask_01_15.txt AC 1533 ms 4112 KB
subtask_01_16.txt AC 1572 ms 7996 KB
subtask_01_17.txt AC 1748 ms 8076 KB
subtask_01_18.txt AC 1723 ms 8076 KB