結果

問題 No.2316 Freight Train
ユーザー にしろにしろ
提出日時 2023-10-25 01:07:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 498 ms / 2,000 ms
コード長 1,054 bytes
コンパイル時間 4,117 ms
コンパイル使用メモリ 231,556 KB
実行使用メモリ 6,472 KB
最終ジャッジ日時 2023-10-25 01:08:09
合計ジャッジ時間 18,407 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 461 ms
6,208 KB
testcase_04 AC 243 ms
4,888 KB
testcase_05 AC 183 ms
4,888 KB
testcase_06 AC 57 ms
4,372 KB
testcase_07 AC 384 ms
4,372 KB
testcase_08 AC 268 ms
6,472 KB
testcase_09 AC 361 ms
5,416 KB
testcase_10 AC 369 ms
4,888 KB
testcase_11 AC 322 ms
6,208 KB
testcase_12 AC 418 ms
5,944 KB
testcase_13 AC 477 ms
6,472 KB
testcase_14 AC 473 ms
6,472 KB
testcase_15 AC 497 ms
6,472 KB
testcase_16 AC 467 ms
6,472 KB
testcase_17 AC 490 ms
6,472 KB
testcase_18 AC 468 ms
6,472 KB
testcase_19 AC 470 ms
6,472 KB
testcase_20 AC 496 ms
6,472 KB
testcase_21 AC 469 ms
6,472 KB
testcase_22 AC 498 ms
6,472 KB
testcase_23 AC 416 ms
6,472 KB
testcase_24 AC 449 ms
6,472 KB
testcase_25 AC 455 ms
6,472 KB
testcase_26 AC 417 ms
6,472 KB
testcase_27 AC 321 ms
4,372 KB
testcase_28 AC 2 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0;i<(n);++i)

struct UnionFind{
    vector<ll> par;
    vector<ll> size;
    
    UnionFind(ll n){
        par.resize(n+1,-1);
        size.resize(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 RootU=root(u),RootV=root(v);
        if(RootU==RootV){
            return;
        }
        if(size[RootU]<size[RootV]){
            par[RootU]=RootV;
            size[RootV]+=size[RootU];
        }else{
            par[RootV]=RootU;
            size[RootU]+=size[RootV];
        }
        return;
    }

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

int main(){

    ll n,q;
    cin >> n >> q;

    UnionFind uf(n);

    rep(i,n){
        ll p;
        cin >> p;
        if(p!=-1) uf.unite(i+1,p);
    }

    rep(i,q){
        ll a,b;
        cin >> a >> b;
        cout << (uf.same(a,b)?"Yes":"No") << endl;
    }

}
0