結果

問題 No.1451 集団登校
ユーザー shiomusubi496shiomusubi496
提出日時 2021-03-15 15:49:30
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 774 ms / 2,000 ms
コード長 1,110 bytes
コンパイル時間 114 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 59,512 KB
最終ジャッジ日時 2024-04-24 18:41:28
合計ジャッジ時間 10,464 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,880 KB
testcase_01 AC 30 ms
10,880 KB
testcase_02 AC 34 ms
10,752 KB
testcase_03 AC 31 ms
10,880 KB
testcase_04 AC 30 ms
10,752 KB
testcase_05 AC 30 ms
10,880 KB
testcase_06 AC 30 ms
10,880 KB
testcase_07 AC 31 ms
10,880 KB
testcase_08 AC 712 ms
59,512 KB
testcase_09 AC 30 ms
10,880 KB
testcase_10 AC 360 ms
38,400 KB
testcase_11 AC 258 ms
38,400 KB
testcase_12 AC 333 ms
35,456 KB
testcase_13 AC 624 ms
42,768 KB
testcase_14 AC 732 ms
39,492 KB
testcase_15 AC 347 ms
29,396 KB
testcase_16 AC 510 ms
38,332 KB
testcase_17 AC 363 ms
12,416 KB
testcase_18 AC 209 ms
22,016 KB
testcase_19 AC 140 ms
27,264 KB
testcase_20 AC 774 ms
40,384 KB
testcase_21 AC 249 ms
11,392 KB
testcase_22 AC 166 ms
15,360 KB
testcase_23 AC 85 ms
11,136 KB
testcase_24 AC 334 ms
37,504 KB
testcase_25 AC 347 ms
27,760 KB
testcase_26 AC 527 ms
38,496 KB
testcase_27 AC 516 ms
25,180 KB
testcase_28 AC 196 ms
17,756 KB
testcase_29 AC 360 ms
26,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod=1000000007
class UnionFind:
    def __init__(self,n):
        self.par=[-1 for i in range(n)]
        self.ans=[1 for i in range(n)]
        self.st=[{i} for i in range(n)]
    def find(self,x):
        if self.par[x]<0:
            return x
        else:
            self.par[x]=self.find(self.par[x])
            return self.par[x]
    def unite(self,x,y):
        x=self.find(x)
        y=self.find(y)
        if x!=y:
            if self.par[x]>self.par[y]:
                x,y=y,x
            if self.par[x]==self.par[y]:
                for i in self.st[x]:
                    self.ans[i]*=500000004
                    self.ans[i]%=mod
                for i in self.st[y]:
                    self.ans[i]*=500000004
                    self.ans[i]%=mod
            else:
                for i in self.st[y]:
                    self.ans[i]=0
            self.st[x]|=self.st[y]
            self.par[x]+=self.par[y]
            self.par[y]=x
N,M=map(int,input().split())
UF=UnionFind(N)
for i in range(M):
    A,B=map(int,input().split())
    UF.unite(A-1,B-1)
for i in range(N):
    print(UF.ans[i])
0