Submission #8886080


Source Code Expand

def main():
    class unionfind():
        #size:要素数,tree:unionfind木
        def __init__(self,size):#self,要素数
            self.size=size
            self.tree=[[i,1] for i in range(self.size)]#root,depth
        
        #rootを探す
        def root(self,index):
            temp_list=[]
            temp=self.tree[index][0]
            while index!=temp:
                temp_list.append(index)
                index=temp
                temp=self.tree[index][0]
            for i in temp_list:
                self.tree[i][0]=index
            return index
        
        #結合
        def unite(self,index1,index2):
            r1=self.root(index1)
            r2=self.root(index2)
            if r1!=r2:
                d1,d2=self.tree[r1][1],self.tree[r2][1]
                if d1<=d2:
                    self.tree[r1][0]=r2
                    self.tree[r2][1]=max(d1+1,d2)
                else:
                    self.tree[r2][0]=r1
                    self.tree[r1][1]=max(d2+1,d1)
    
        #同じか判定
        def same(self,index1,index2):
            r1=self.root(index1)
            r2=self.root(index2)
            return r1==r2
    
        #連結成分の個数
        def component(self):
            return len({self.root(i) for i in range(self.size)})
    
    
    n,m=map(int,input().split())
    ab=[list(map(int,input().split())) for _ in [0]*m]
 
    u=unionfind(n)
    for a,b in ab:
        u.unite(a-1,b-1)
    print(u.component()-1)

main()

Submission Info

Submission Time
Task B - 道路工事
User tmg_dayo
Language PyPy3 (2.4.0)
Score 100
Code Size 1557 Byte
Status AC
Exec Time 772 ms
Memory 82008 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 2
AC × 22
Set Name Test Cases
Sample sample1.txt, sample2.txt
All 0.txt, 1.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 2.txt, 3.txt, 4.txt, 5.txt, 6.txt, 7.txt, 8.txt, 9.txt, sample1.txt, sample2.txt
Case Name Status Exec Time Memory
0.txt AC 166 ms 38384 KB
1.txt AC 163 ms 38256 KB
10.txt AC 163 ms 38256 KB
11.txt AC 169 ms 38256 KB
12.txt AC 169 ms 38256 KB
13.txt AC 167 ms 38256 KB
14.txt AC 166 ms 38256 KB
15.txt AC 626 ms 62552 KB
16.txt AC 193 ms 56884 KB
17.txt AC 187 ms 56884 KB
18.txt AC 194 ms 56884 KB
19.txt AC 772 ms 82008 KB
2.txt AC 165 ms 38256 KB
3.txt AC 168 ms 38256 KB
4.txt AC 168 ms 38256 KB
5.txt AC 172 ms 38256 KB
6.txt AC 164 ms 38256 KB
7.txt AC 165 ms 38256 KB
8.txt AC 167 ms 38256 KB
9.txt AC 164 ms 38256 KB
sample1.txt AC 164 ms 38256 KB
sample2.txt AC 164 ms 38256 KB