結果

問題 No.2316 Freight Train
ユーザー i_am_noobi_am_noob
提出日時 2023-06-29 19:18:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 805 bytes
コンパイル時間 2,143 ms
コンパイル使用メモリ 199,104 KB
実行使用メモリ 5,088 KB
最終ジャッジ日時 2023-09-20 14:24:12
合計ジャッジ時間 8,931 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 77 ms
4,932 KB
testcase_04 AC 37 ms
4,380 KB
testcase_05 AC 30 ms
4,376 KB
testcase_06 AC 9 ms
4,380 KB
testcase_07 AC 49 ms
4,376 KB
testcase_08 AC 54 ms
4,788 KB
testcase_09 AC 54 ms
4,376 KB
testcase_10 AC 52 ms
4,376 KB
testcase_11 AC 58 ms
4,644 KB
testcase_12 AC 65 ms
4,496 KB
testcase_13 AC 80 ms
4,904 KB
testcase_14 AC 79 ms
4,908 KB
testcase_15 AC 79 ms
4,960 KB
testcase_16 AC 79 ms
5,084 KB
testcase_17 AC 78 ms
5,004 KB
testcase_18 AC 78 ms
4,856 KB
testcase_19 AC 78 ms
4,860 KB
testcase_20 AC 78 ms
4,856 KB
testcase_21 AC 79 ms
5,088 KB
testcase_22 AC 79 ms
4,904 KB
testcase_23 AC 58 ms
5,008 KB
testcase_24 AC 68 ms
4,840 KB
testcase_25 AC 63 ms
4,848 KB
testcase_26 AC 58 ms
4,896 KB
testcase_27 AC 33 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pii=pair<int,int>;

#define all(a) a.begin(),a.end()
#define pb push_back
#define sz(a) ((int)a.size())

const int N=200005;
int par[N],siz[N];
void init_dsu(int n){for(int i=0; i<n; ++i) par[i]=i,siz[i]=1;}
int Find(int x){return x==par[x]?x:par[x]=Find(par[x]);}
bool Union(int x, int y){
    x=Find(x),y=Find(y);
    if(x==y) return 0;
    if(siz[x]<siz[y]) swap(x,y);
    siz[x]+=siz[y],par[y]=x;
    return 1;
}

int n,q;

signed main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    cin >> n >> q;
    init_dsu(n);
    for(int i=0; i<n; ++i){
        int x; cin >> x;
        if(x!=-1) Union(i,x-1);
    }
    while(q--){
        int x,y; cin >> x >> y; x--,y--;
        cout << (Find(x)==Find(y)?"Yes":"No") << "\n";
    }
}
0