結果

問題 No.2316 Freight Train
ユーザー D4Cngo_cppD4Cngo_cpp
提出日時 2023-05-26 21:43:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 412 ms / 2,000 ms
コード長 2,137 bytes
コンパイル時間 1,468 ms
コンパイル使用メモリ 169,100 KB
実行使用メモリ 4,712 KB
最終ジャッジ日時 2023-08-26 10:57:30
合計ジャッジ時間 13,169 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 397 ms
4,656 KB
testcase_04 AC 248 ms
4,376 KB
testcase_05 AC 153 ms
4,376 KB
testcase_06 AC 51 ms
4,376 KB
testcase_07 AC 351 ms
4,380 KB
testcase_08 AC 226 ms
4,660 KB
testcase_09 AC 309 ms
4,380 KB
testcase_10 AC 334 ms
4,380 KB
testcase_11 AC 277 ms
4,576 KB
testcase_12 AC 334 ms
4,384 KB
testcase_13 AC 400 ms
4,688 KB
testcase_14 AC 393 ms
4,604 KB
testcase_15 AC 393 ms
4,548 KB
testcase_16 AC 398 ms
4,552 KB
testcase_17 AC 406 ms
4,552 KB
testcase_18 AC 396 ms
4,552 KB
testcase_19 AC 395 ms
4,540 KB
testcase_20 AC 403 ms
4,648 KB
testcase_21 AC 412 ms
4,600 KB
testcase_22 AC 395 ms
4,712 KB
testcase_23 AC 362 ms
4,700 KB
testcase_24 AC 395 ms
4,540 KB
testcase_25 AC 374 ms
4,644 KB
testcase_26 AC 363 ms
4,660 KB
testcase_27 AC 286 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
using ll = long long;
#define reps(i, a, n) for(ll i = (a); i < (ll)(n); ++i)
#define rep(i,n) reps(i,0,n)
#define all(a) (a).begin(), (a).end();
#define debug(x) cerr << "\033[33m(line:" << __LINE__ << ") " << #x << ": " << x << "\033[m" << endl;

//なんかstd::vector<bool>の挙動を修正できるやつ

/* alias */
using ull = unsigned long long;
using ll = long long;
using vi = vector<int>;
using vl = vector<long>;
using vll = vector<long long>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvll = vector<vll>;
using vs = vector<string>;
using pii = pair<int, int>;
//UnionFind木
// @see https://zenn.dev/reputeless/books/standard-cpp-for-competitive-programming/viewer/union-find
class UnionFind{
public:
    UnionFind() = default;
    // n 要素数
    explicit UnionFind(size_t n)
        : m_parentsOrSize(n, -1){}
    int root(int i){
        if (m_parentsOrSize[i] < 0) return i;
        // 経路圧縮
        return (m_parentsOrSize[i] = root(m_parentsOrSize[i]));
    }
    void unite(int a, int b){
        a = root(a); b = root(b);
        if(a != b){
            // 小さいほうが子になる
            if(-m_parentsOrSize[a] < -m_parentsOrSize[b]) swap(a,b);
            m_parentsOrSize[a] += m_parentsOrSize[b];
            m_parentsOrSize[b] = a;
        }
    }
    bool same(int a, int b){ return (root(a) == root(b));}
    // i が属するグループの要素数を返す
    int size(int i){return -m_parentsOrSize[root(i)];}
private:
    // m_parentsOrSize[i]は i の親,
    // ただし root の場合は (-1 * そのグループに属する要素数)
    vi m_parentsOrSize; // using vi = vector<int>
};

int main(){
    int n, q;
    cin >> n >> q;
    vi a(n);
    UnionFind uf(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
        if(a[i] != -1) {
            a[i]--;
            uf.unite(i,a[i]);
        }
    }

    rep(i, q){
        int x, y;
        cin >> x >> y;
        x--; y--;
        if(uf.same(x,y)) cout << "Yes\n";
        else cout << "No\n";
    }
}
0