結果
問題 |
No.2316 Freight Train
|
ユーザー |
|
提出日時 | 2023-05-26 21:53:04 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 507 ms / 2,000 ms |
コード長 | 1,454 bytes |
コンパイル時間 | 2,169 ms |
コンパイル使用メモリ | 196,424 KB |
最終ジャッジ日時 | 2025-02-13 06:30:49 |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; #define REP(i, n) for (int i = 0; i < (n); i++) template<class T> using V = vector<T>; template<class T> ostream &operator<<(ostream &os, const V<T> &v) { os << "[ "; for (auto &vi: v) os << vi << ", "; return os << "]"; } template<class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << "{ " << p.first << ", " << p.second << "}"; } #ifdef LOCAL #define show(x) cerr << __LINE__ << " : " << #x << " = " << x << endl; #else #define show(x) true #endif struct UF { V<int> par; UF() {} UF(int n) : par(n, -1) {} int leader(int x) { return par[x] < 0 ? x : par[x] = leader(par[x]); } bool merge(int x, int y) { x = leader(x), y = leader(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return true; } bool same(int x, int y) { return leader(x) == leader(y); } int size(int x) { return -par[leader(x)]; } }; int main() { int N, Q; cin >> N >> Q; vector<int> P(N); REP(i, N) { cin >> P[i]; if (P[i] != -1) P[i]--; } UF uf(N); REP(i, N) { if (P[i] != -1) { uf.merge(i, P[i]); } } REP(qq, Q) { int a, b; cin >> a >> b; a--, b--; cout << (uf.same(a, b) ? "Yes" : "No") << '\n'; } return 0; }