結果

問題 No.2316 Freight Train
ユーザー GlinTFrauleinGlinTFraulein
提出日時 2023-05-26 21:36:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 427 ms / 2,000 ms
コード長 2,009 bytes
コンパイル時間 3,885 ms
コンパイル使用メモリ 263,052 KB
実行使用メモリ 8,132 KB
最終ジャッジ日時 2023-08-26 10:40:19
合計ジャッジ時間 15,322 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 414 ms
7,512 KB
testcase_04 AC 217 ms
5,464 KB
testcase_05 AC 168 ms
5,412 KB
testcase_06 AC 53 ms
4,384 KB
testcase_07 AC 347 ms
4,380 KB
testcase_08 AC 240 ms
7,948 KB
testcase_09 AC 318 ms
6,084 KB
testcase_10 AC 323 ms
5,388 KB
testcase_11 AC 284 ms
7,300 KB
testcase_12 AC 354 ms
6,980 KB
testcase_13 AC 419 ms
7,820 KB
testcase_14 AC 427 ms
7,960 KB
testcase_15 AC 417 ms
7,768 KB
testcase_16 AC 416 ms
7,948 KB
testcase_17 AC 416 ms
7,948 KB
testcase_18 AC 417 ms
7,780 KB
testcase_19 AC 410 ms
7,892 KB
testcase_20 AC 427 ms
8,132 KB
testcase_21 AC 413 ms
7,768 KB
testcase_22 AC 405 ms
7,780 KB
testcase_23 AC 372 ms
7,820 KB
testcase_24 AC 390 ms
7,888 KB
testcase_25 AC 375 ms
8,096 KB
testcase_26 AC 364 ms
7,896 KB
testcase_27 AC 286 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
 
#define elif else if
#define ll long long
#define vll vector<long long>
#define vec vector
#define embk emplace_back
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep3(i, n, k) for (ll i = k; i < n; i++)
#define all(a) a.begin(), a.end()
#define YNeos(bool) (bool ? "Yes" : "No")
 
using namespace std;
using namespace atcoder;
const ll INF = 1LL << 60;
const ll mod = 998244353;
const double pi = acos(-1);

struct UnionFind {
  vector<ll> root, size;
  
  //コンストラクタ(初期化処理)
  UnionFind(ll n) : root(n), size(n, 1) {
    for (int i = 0; i < n; i++) root[i] = i;
  }
  
  //xの根を求める
  ll getroot (ll x) {
    if (root.at(x) == x) return x;
    else return root.at(x) = getroot(root.at(x));  //経路圧縮
  }
  
  //xとyが同一グループか判定
  bool issame(ll x, ll y) {
    return (getroot(x) == getroot(y));
  }
  
  //xとyを繋げる
  void unite(ll x, ll y) {
    ll rootx = getroot(x), rooty = getroot(y);  //xとyの根を求める
    if (rootx == rooty) return;  //根が同じなら何もしない
    
    //union by size
    if (size.at(rootx) < size.at(rooty)) swap(rootx, rooty);  //サイズの大小を保障
    root.at(rooty) = rootx;
    size.at(rootx) += size.at(rooty);
    return;
  }

  //uniteを与えられたグラフに対して一気に行う
  void unite_graph(vector<ll> u, vector<ll> v) {
    for(int i = 0; i < u.size(); i++) unite(u[i], v[i]);
  }
  
  //xに含まれる頂点数を求める
  ll getsize(ll x) {
    return size.at(root.at(x));
  }
  
  //連結成分の数を求める
  ll getunion() {
    ll uni = 0;
    for (int i = 0; i < root.size(); i++) if (root.at(i) == i) uni++;
    return uni;
  }
  
};

int main() {
  ll n, q; cin >> n >> q;
  vll p(n); rep(i, n) cin >> p[i];

  UnionFind UF(n);
  rep(i, n) {
    if (p[i] != -1) UF.unite(i, p[i]-1);
  }
  
  rep(itr, q) {
    ll a, b; cin >> a >> b;
    cout << YNeos(UF.issame(a-1, b-1)) << endl;;
  }
}
0