結果

問題 No.1451 集団登校
ユーザー hiro71687khiro71687k
提出日時 2023-07-18 14:33:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 2,663 bytes
コンパイル時間 4,971 ms
コンパイル使用メモリ 265,968 KB
実行使用メモリ 7,172 KB
最終ジャッジ日時 2023-10-18 17:50:09
合計ジャッジ時間 8,199 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 149 ms
6,380 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 143 ms
6,380 KB
testcase_11 AC 131 ms
6,380 KB
testcase_12 AC 128 ms
6,116 KB
testcase_13 AC 173 ms
7,172 KB
testcase_14 AC 171 ms
7,172 KB
testcase_15 AC 96 ms
5,324 KB
testcase_16 AC 147 ms
6,644 KB
testcase_17 AC 51 ms
4,796 KB
testcase_18 AC 61 ms
4,532 KB
testcase_19 AC 79 ms
5,060 KB
testcase_20 AC 178 ms
7,172 KB
testcase_21 AC 31 ms
4,348 KB
testcase_22 AC 32 ms
4,348 KB
testcase_23 AC 8 ms
4,348 KB
testcase_24 AC 138 ms
6,380 KB
testcase_25 AC 90 ms
5,324 KB
testcase_26 AC 148 ms
6,644 KB
testcase_27 AC 107 ms
5,852 KB
testcase_28 AC 44 ms
4,348 KB
testcase_29 AC 90 ms
5,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using namespace std;
using ll=long long;
using ld=long double;
ld pie=3.141592653589793;
ll mod=1000000007;
ld inf=10000999999999900;
struct unionfind
{
    vector<ll>par,siz;
    unionfind(ll n): par(n,-1),siz(n,1){}
    ll root(ll x){
        if (par[x]==-1)
        {
            return x;
        }else{
            return par[x]=root(par[x]);
        }
    }
    bool issame(ll x,ll y){
        return root(x)==root(y);
    }
    bool unite(ll x,ll y){
        x=root(x);
        y=root(y);
        if (x==y)
        {
            return false;
        }
        if (siz[x]<siz[y])
        {
            swap(x,y);
        }
        par[y]=x;
        siz[x]+=siz[y];
        return true;
    }
    ll size(ll x){
        return siz[root(x)];
    }
};
int main(){
    ll n,m;
    cin >> n >> m;
    vector<ll>a(m),b(m);
    unionfind uf(n);
    for (ll i = 0; i < m; i++)
    {
        cin >> a[i]  >> b[i];
        a[i]--,b[i]--;
    }
    vector<ll>ans(n,1);
    vector<ll>g(n);
    for (ll i = 0; i < n; i++)
    {
        g[i]=i;
    }
    for (ll i = 0; i < m; i++)
    {
        if (uf.issame(a[i],b[i]))
        {
            continue;
        }
        
        ll aa=uf.root(a[i]);
        ll bb=uf.root(b[i]);
        ll x=uf.size(aa);
        ll y=uf.size(bb);
        if (x>y)
        {
            uf.unite(aa,bb);
            ans[uf.root(aa)]=ans[aa];
            g[bb]=uf.root(aa);
            g[aa]=uf.root(aa);
            ans[bb]=0;
        }else if (x==y)
        {
            uf.unite(aa,bb);
            ll r=uf.root(aa);
            if (aa==r)
            {
                g[bb]=aa;
                ans[aa]*=inv_mod(2,mod);
                ans[aa]%=mod;
                ans[bb]*=inv_mod(ans[aa],mod);
                ans[bb]%=mod;
                ans[bb]*=inv_mod(2,mod);
                ans[bb]%=mod;
            }else{
                g[aa]=bb;
                ans[bb]*=inv_mod(2,mod);
                ans[bb]%=mod;
                ans[aa]*=inv_mod(ans[bb],mod);
                ans[aa]%=mod;
                ans[aa]*=inv_mod(2,mod);
                ans[aa]%=mod;
            }
        }else{
            uf.unite(aa,bb);
            ans[uf.root(aa)]=ans[bb];
            g[bb]=uf.root(aa);
            g[aa]=uf.root(aa);
            ans[aa]=0;
        }
    }
    for (ll i = 0; i < n; i++)
    {
        ll now=i;
        ll x=ans[now];
        while (1)
        {
            if (now==g[now])
            {
                break;
            }
            now=g[now];
            x*=ans[now];
            x%=mod;
        }
        cout << x << endl;
    }
}
0