結果
問題 | No.1451 集団登校 |
ユーザー |
|
提出日時 | 2021-03-14 16:34:09 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 228 ms / 2,000 ms |
コード長 | 788 bytes |
コンパイル時間 | 1,879 ms |
コンパイル使用メモリ | 174,620 KB |
実行使用メモリ | 13,980 KB |
最終ジャッジ日時 | 2024-11-06 07:20:24 |
合計ジャッジ時間 | 6,484 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
#include<bits/stdc++.h>using namespace std;using ll=long long;constexpr ll MOD=1e9+7;int dat[100005];ll p[100005];set<int>grp[100005];void UF_init(int N){fill(dat,dat+N,-1);fill(p,p+N,1);for(int i=0;i<N;i++)grp[i].insert(i);}int find(int x){return dat[x]<0?x:dat[x]=find(dat[x]);}void unite(int x,int y){x=find(x);y=find(y);if(x==y)return;if(dat[x]>dat[y])swap(x,y);if(dat[x]==dat[y]){for(int i:grp[x])(p[i]*=500000004)%=MOD;for(int i:grp[y])(p[i]*=500000004)%=MOD;}else{for(int i:grp[y])p[i]=0;}for(int i:grp[y])grp[x].insert(i);grp[y].clear();dat[x]+=dat[y];dat[y]=x;}int main(){int N,M;cin>>N>>M;UF_init(N);while(M--){int A,B;cin>>A>>B;unite(A-1,B-1);}for(int i=0;i<N;i++)cout<<p[i]<<endl;}