結果

問題 No.1451 集団登校
ユーザー shiomusubi496shiomusubi496
提出日時 2021-03-14 18:20:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 267 ms / 2,000 ms
コード長 1,011 bytes
コンパイル時間 1,782 ms
コンパイル使用メモリ 175,520 KB
実行使用メモリ 34,328 KB
最終ジャッジ日時 2023-08-06 04:48:21
合計ジャッジ時間 6,425 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 239 ms
34,328 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 155 ms
13,772 KB
testcase_11 AC 137 ms
13,468 KB
testcase_12 AC 141 ms
12,732 KB
testcase_13 AC 224 ms
18,036 KB
testcase_14 AC 258 ms
18,224 KB
testcase_15 AC 127 ms
12,060 KB
testcase_16 AC 203 ms
16,760 KB
testcase_17 AC 50 ms
4,380 KB
testcase_18 AC 73 ms
8,344 KB
testcase_19 AC 86 ms
9,432 KB
testcase_20 AC 267 ms
18,828 KB
testcase_21 AC 30 ms
4,376 KB
testcase_22 AC 39 ms
5,428 KB
testcase_23 AC 8 ms
4,376 KB
testcase_24 AC 147 ms
13,624 KB
testcase_25 AC 122 ms
11,704 KB
testcase_26 AC 195 ms
16,388 KB
testcase_27 AC 140 ms
10,640 KB
testcase_28 AC 57 ms
6,840 KB
testcase_29 AC 126 ms
11,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr ll mod=1000000007;
struct UnionFind{
    vector<int> par;
    vector<ll> ans;
    vector<set<int>> st;
    UnionFind(int n):par(n,-1),ans(n,1),st(n){
        for(int i=0;i<n;i++) st[i].insert(i);
    }
    int find(int x){
        return par[x]<0 ? x : par[x]=find(par[x]);
    }
    void unite(int x,int y){
        x=find(x); y=find(y);
        if(x==y)return;
        if(par[x]>par[y]) swap(x,y);
        if(par[x]==par[y]){
            for(int i:st[x]) ans[i]=ans[i]*500000004%mod;
            for(int i:st[y]) ans[i]=ans[i]*500000004%mod;
        }else{
            for(int i:st[y]) ans[i]=0;
        }
        for(int i:st[y]) st[x].insert(i);
        par[x]+=par[y]; par[y]=x;
    }
    int get(int x){
        return ans[x];
    }
};
int main(){
    int N,M; cin>>N>>M;
    UnionFind UF(N);
    for(int i=0;i<M;i++){
        int A,B; cin>>A>>B;
        UF.unite(A-1, B-1);
    }
    for(int i=0;i<N;i++) cout<<UF.get(i)<<endl;
}
0