結果

問題 No.1451 集団登校
ユーザー vwxyzvwxyz
提出日時 2023-05-21 15:58:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,312 ms / 2,000 ms
コード長 3,333 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 11,040 KB
実行使用メモリ 84,888 KB
最終ジャッジ日時 2023-08-23 15:10:23
合計ジャッジ時間 12,469 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,904 KB
testcase_01 AC 19 ms
8,960 KB
testcase_02 AC 19 ms
8,800 KB
testcase_03 AC 19 ms
8,884 KB
testcase_04 AC 19 ms
8,860 KB
testcase_05 AC 20 ms
8,876 KB
testcase_06 AC 20 ms
8,864 KB
testcase_07 AC 19 ms
8,860 KB
testcase_08 AC 1,312 ms
84,888 KB
testcase_09 AC 20 ms
8,960 KB
testcase_10 AC 376 ms
47,464 KB
testcase_11 AC 209 ms
47,892 KB
testcase_12 AC 371 ms
43,940 KB
testcase_13 AC 618 ms
48,560 KB
testcase_14 AC 794 ms
39,348 KB
testcase_15 AC 349 ms
29,880 KB
testcase_16 AC 534 ms
42,060 KB
testcase_17 AC 691 ms
10,676 KB
testcase_18 AC 212 ms
23,116 KB
testcase_19 AC 128 ms
31,808 KB
testcase_20 AC 835 ms
42,280 KB
testcase_21 AC 463 ms
9,580 KB
testcase_22 AC 209 ms
13,608 KB
testcase_23 AC 130 ms
8,944 KB
testcase_24 AC 342 ms
46,716 KB
testcase_25 AC 351 ms
27,864 KB
testcase_26 AC 522 ms
42,360 KB
testcase_27 AC 682 ms
25,008 KB
testcase_28 AC 216 ms
16,308 KB
testcase_29 AC 382 ms
27,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline
from collections import defaultdict

class UnionFind:
    def __init__(self,N,label=None,f=None,weighted=False):
        self.N=N
        self.parents=[None]*self.N
        self.size=[1]*self.N
        self.roots={i for i in range(self.N)}
        self.label=label
        if self.label!=None:
            self.label=[x for x in label]
        self.f=f
        self.weighted=weighted
        if self.weighted:
            self.weight=[0]*self.N

    def Find(self,x):
        stack=[]
        while self.parents[x]!=None:
            stack.append(x)
            x=self.parents[x]
        if self.weighted:
            w=0
            for y in stack[::-1]:
                self.parents[y]=x
                w+=self.weight[y]
                self.weight[y]=w
        else:
            for y in stack[::-1]:
                self.parents[y]=x
        return x

    def Union(self,x,y,w=None):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if root_x==root_y:
            if self.weighted:
                if self.weight[y]-self.weight[x]==w:
                    return True
                else:
                    return False
        else:
            if self.size[root_x]<self.size[root_y]:
                x,y=y,x
                root_x,root_y=root_y,root_x
                if self.weighted:
                    w=-w
            self.parents[root_y]=root_x
            self.size[root_x]+=self.size[root_y]
            self.roots.remove(root_y)
            if self.label!=None:
                self.label[root_x]=self.f(self.label[root_x],self.label[root_y])
            if self.weighted:
                self.weight[root_y]=w+self.weight[x]-self.weight[y]

    def Size(self,x):
        return self.size[self.Find(x)]

    def Same(self,x,y):
        return self.Find(x)==self.Find(y)

    def Label(self,x):
        return self.label[self.Find(x)]

    def Weight(self,x,y):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if root_x!=root_y:
            return None
        return self.weight[y]-self.weight[x]

    def Roots(self):
        return list(self.roots)

    def Linked_Components_Count(self):
        return len(self.roots)

    def Linked_Components(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return linked_components

    def __str__(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return "\n".join(f"{r}: {m}" for r,m in linked_components.items())

N,M=map(int,readline().split())
mod=10**9+7
inve2=(mod+1)//2
dp=[defaultdict(int) for i in range(N)]
for i in range(N):
    dp[i][i]=1
UF=UnionFind(N)
for m in range(M):
    a,b=map(int,readline().split())
    a=UF.Find(a-1);b=UF.Find(b-1)
    size_a,size_b=UF.Size(a),UF.Size(b)
    UF.Union(a,b)
    r=UF.Find(a)
    if size_a==size_b:
        dp_a,dp_b=dp[a],dp[b]
        dp[r]=defaultdict(int)
        for x,p in dp_a.items():
            dp[r][x]+=p*inve2%mod
            dp[r][x]%=mod
        for x,p in dp_b.items():
            dp[r][x]+=p*inve2%mod
            dp[r][x]%=mod
ans_lst=[0]*N
for r in UF.Roots():
    for x,p in dp[r].items():
        ans_lst[x]+=p
print(*ans_lst,sep="\n")
0