結果

問題 No.2780 The Bottle Imp
ユーザー やまひろ
提出日時 2024-06-19 02:28:16
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,214 bytes
コンパイル時間 4,378 ms
コンパイル使用メモリ 232,208 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-19 02:28:24
合計ジャッジ時間 8,168 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35 WA * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using ll = long long;
using mint = atcoder::modint1000000007;
const ll inf = 9e18;

struct UnionFind{

    vector<ll> par;
    vector<ll> size;
    
    UnionFind(ll n){
        par.resize(2*(n+1),-1);
        size.resize(2*(n+1),1);
    }

    ll root(ll x){
        if (par[x]==-1) return x;
        return par[x]=root(par[x]);
    }

    void unite(ll u,ll v){
        ll uu=root(u),vv=root(v);
        if(uu==vv){
            return;
        }
        if(size[uu]<size[vv]){
            par[uu]=vv;
            size[vv]+=size[uu];
        }else{
            par[vv]=uu;
            size[uu]+=size[vv];
        }
        return;
    }

    bool same(ll u,ll v){
        return root(u)==root(v);
    }

    ll out_size(ll n){
        return size[root(n)];
    }

};

int main(){

	ios::sync_with_stdio(0);
	cin.tie(0);

    ll n;
    cin >> n;

    UnionFind tree(n);

    for(ll x=0;x<n;x++){
        ll m,y;
        cin >> m;
        for(ll i=0;i<m;i++){
            cin >> y;
            y--;
            tree.unite(i,y);
        }
    }

    if(tree.out_size(tree.root(0))==n) cout << "Yes";
    else cout << "No";
    cout << endl;
    
}
0