結果

問題 No.2316 Freight Train
ユーザー Pon9078Pon9078
提出日時 2023-05-26 22:35:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 453 ms / 2,000 ms
コード長 2,321 bytes
コンパイル時間 1,105 ms
コンパイル使用メモリ 85,040 KB
実行使用メモリ 4,760 KB
最終ジャッジ日時 2023-08-26 12:41:37
合計ジャッジ時間 13,182 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 444 ms
4,684 KB
testcase_04 AC 234 ms
4,380 KB
testcase_05 AC 168 ms
4,380 KB
testcase_06 AC 55 ms
4,376 KB
testcase_07 AC 373 ms
4,380 KB
testcase_08 AC 252 ms
4,696 KB
testcase_09 AC 326 ms
4,380 KB
testcase_10 AC 355 ms
4,380 KB
testcase_11 AC 308 ms
4,384 KB
testcase_12 AC 370 ms
4,380 KB
testcase_13 AC 452 ms
4,628 KB
testcase_14 AC 449 ms
4,580 KB
testcase_15 AC 449 ms
4,692 KB
testcase_16 AC 453 ms
4,692 KB
testcase_17 AC 449 ms
4,580 KB
testcase_18 AC 451 ms
4,760 KB
testcase_19 AC 453 ms
4,628 KB
testcase_20 AC 449 ms
4,752 KB
testcase_21 AC 451 ms
4,752 KB
testcase_22 AC 453 ms
4,628 KB
testcase_23 AC 402 ms
4,636 KB
testcase_24 AC 432 ms
4,632 KB
testcase_25 AC 407 ms
4,756 KB
testcase_26 AC 401 ms
4,632 KB
testcase_27 AC 314 ms
4,384 KB
testcase_28 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>

using namespace std;
typedef long long ll;
#define loop1(n) for(int loopt = 0; loopt < (n); loopt++)
#define loop2(i,n) for((i) = 0; (i) < (n); (i)++)
#define loop3(i,k,n) for((i) = (k); (i) < (n); (i)++)
#define GET_NAME_LOOP(_2,_3,NAME,...) NAME
#define loop(n,...) GET_NAME_LOOP(__VA_ARGS__,loop3,loop2,loop1)(n,__VA_ARGS__)

#define rloop1(n) for(int loopt = (n)-1; loopt >= 0; loopt--)
#define rloop2(i,n) for((i) = (n)-1; (i) >= 0; (i)--)
#define rloop3(i,k,n) for((i) = (k)-1; (i) >= (n); (i)--)
#define GET_NAME_RLOOP(_2,_3,NAME,...) NAME
#define rloop(n,...) GET_NAME_RLOOP(__VA_ARGS__,rloop3,rloop2,rloop1)(n,__VA_ARGS__)

#define outone cout << "First" << endl
#define outtwo cout << "Second" << endl
#define outyes cout << "Yes" << endl
#define outno cout << "No" << endl
#define outans cout << ans << endl
#define all(i) i.begin(),i.end()
//const ll mod =  998244353L;
//const ll mod = 1e9+7L;
//const double PI =3.14159265359d;
//const double E =2.718281828d;
class UnionFind{
    public:
    vector<int> par;
    vector<int> size;

    void init(int n){
        par.resize(n,-1);
        size.resize(n,1);
    }

    int find_root(int base){
        int now = base;
        while(true){
            if(par[now] == -1) break;
            now = par[now];
        }
        if(par[base] != -1) par[base] = now;
        return now;
    }

    void unite(int u, int v){
        int rootU = find_root(u);
        int rootV = find_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 is_same(int u, int v){
        if(find_root(u) == find_root(v)) return true;
        else return false;
    }
};
int main() {
    int n,q;
    cin >> n >> q;
    UnionFind uf;
    uf.init(n);
    int i;
    loop(i,0,n){
        int p;
        cin >> p;
        p--;
        if(p != -2) uf.unite(i,p);
    }
    while(q--){
        int a,b;
        cin >>a >> b;
        a--; b--;
        if(uf.is_same(a,b)) outyes;
        else outno;   
    }
}
0