結果
問題 | No.2316 Freight Train |
ユーザー |
![]() |
提出日時 | 2024-12-28 18:07:30 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 403 ms / 2,000 ms |
コード長 | 1,841 bytes |
コンパイル時間 | 3,391 ms |
コンパイル使用メモリ | 250,464 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-28 18:07:46 |
合計ジャッジ時間 | 14,261 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 |
ソースコード
#include<bits/stdc++.h>using namespace std;using ll = long long;#define rep(i,m,n) for(int i=m; i<int(n); ++i)#define repll(i,m,n) for(ll i=m; i<ll(n); ++i)#define all(a) (a).begin(), (a).end()#define rall(a) (a).rbegin(), (a).rend()#define fi first#define se secondtemplate<typename T> bool chmin(T& a, T b){ if(a > b){a = b; return true;} return false; }template<typename T> bool chmax(T& a, T b){ if(a < b){a = b; return true;} return false; }template<typename T> T gcd(T a, T b){ return a % b ? gcd(b, a % b) : b; }template<typename T> T lcm(T a, T b){ return a / gcd(a, b) * b; }struct UnionFind{vector<int> siz, parent;UnionFind(){}UnionFind(int n){siz.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;siz[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(siz[x] > siz[y]){parent[y] = x;siz[x] += siz[y];}else{parent[x] = y;siz[y] += siz[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 siz[find_root(x)];}};int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int N, Q;cin >> N >> Q;UnionFind UF(N+1);rep(i, 1, N+1){int P;cin >> P;if(P != -1) UF.unite(i, P);}rep(q, 0, Q){int A, B;cin >> A >> B;cout << (UF.is_same(A, B) ? "Yes" : "No") << endl;}return 0;}