結果
問題 | No.2316 Freight Train |
ユーザー | MAHAYANA |
提出日時 | 2023-10-25 23:51:31 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 488 ms / 2,000 ms |
コード長 | 1,415 bytes |
コンパイル時間 | 1,882 ms |
コンパイル使用メモリ | 173,176 KB |
実行使用メモリ | 6,512 KB |
最終ジャッジ日時 | 2023-10-25 23:51:47 |
合計ジャッジ時間 | 15,785 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
4,348 KB |
testcase_01 | AC | 1 ms
4,348 KB |
testcase_02 | AC | 2 ms
4,348 KB |
testcase_03 | AC | 444 ms
6,512 KB |
testcase_04 | AC | 235 ms
4,928 KB |
testcase_05 | AC | 170 ms
4,664 KB |
testcase_06 | AC | 56 ms
4,348 KB |
testcase_07 | AC | 375 ms
5,192 KB |
testcase_08 | AC | 260 ms
5,720 KB |
testcase_09 | AC | 351 ms
5,456 KB |
testcase_10 | AC | 354 ms
5,456 KB |
testcase_11 | AC | 310 ms
5,720 KB |
testcase_12 | AC | 379 ms
5,984 KB |
testcase_13 | AC | 463 ms
6,512 KB |
testcase_14 | AC | 453 ms
6,512 KB |
testcase_15 | AC | 451 ms
6,512 KB |
testcase_16 | AC | 463 ms
6,512 KB |
testcase_17 | AC | 453 ms
6,512 KB |
testcase_18 | AC | 467 ms
6,512 KB |
testcase_19 | AC | 453 ms
6,512 KB |
testcase_20 | AC | 488 ms
6,512 KB |
testcase_21 | AC | 452 ms
6,512 KB |
testcase_22 | AC | 470 ms
6,512 KB |
testcase_23 | AC | 403 ms
6,512 KB |
testcase_24 | AC | 441 ms
6,512 KB |
testcase_25 | AC | 417 ms
6,512 KB |
testcase_26 | AC | 409 ms
6,512 KB |
testcase_27 | AC | 312 ms
4,928 KB |
testcase_28 | AC | 2 ms
4,348 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,m,n) for(int i=m; i<n; ++i) #define repl(i,m,n) for(ll i=m; i<n; ++i) struct UnionFind{ vector<int> size, parent; UnionFind(){} UnionFind(int n){ size.resize(n,0); parent.resize(n,0); for(int i=0; i<n; ++i){ make_tree(i); } } void make_tree(int x){ parent[x] = x; size[x] = 1; } bool is_same(int x, int y){ return find_root(x) == find_root(y); } bool unite(int x, int y){ x = find_root(x); y = find_root(y); if(x == y) return false; if(size[x] > size[y]){ parent[y] = x; size[x] += size[y]; }else{ parent[x] = y; size[y] += size[x]; } return true; } int find_root(int x){ if(x != parent[x]) parent[x] = find_root(parent[x]); return parent[x]; } int tree_size(int x){ return size[find_root(x)]; } }; int main(){ int N, Q; cin >> N >> Q; UnionFind uf(N); rep(i, 0, N){ int P; cin >> P; if(P == -1) continue; uf.unite(i+1, P); } vector<int> A(Q), B(Q); rep(i, 0, Q) cin >> A[i] >> B[i]; rep(i, 0, Q){ bool f = uf.is_same(A[i], B[i]); cout << (f ? "Yes" : "No") << endl; } return 0; }