結果

問題 No.2319 Friends+
ユーザー ruthenruthen
提出日時 2023-05-27 00:36:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,993 bytes
コンパイル時間 2,503 ms
コンパイル使用メモリ 212,732 KB
実行使用メモリ 7,824 KB
最終ジャッジ日時 2023-08-26 14:31:43
合計ジャッジ時間 24,081 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 414 ms
7,632 KB
testcase_05 WA -
testcase_06 AC 404 ms
7,612 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 432 ms
5,792 KB
testcase_19 AC 421 ms
6,680 KB
testcase_20 AC 410 ms
5,720 KB
testcase_21 AC 420 ms
5,688 KB
testcase_22 AC 400 ms
5,792 KB
testcase_23 AC 418 ms
6,332 KB
testcase_24 AC 424 ms
5,988 KB
testcase_25 AC 408 ms
5,980 KB
testcase_26 AC 400 ms
6,056 KB
testcase_27 AC 413 ms
5,664 KB
testcase_28 AC 418 ms
5,624 KB
testcase_29 AC 437 ms
5,760 KB
testcase_30 AC 428 ms
5,620 KB
testcase_31 AC 410 ms
5,676 KB
testcase_32 AC 400 ms
5,620 KB
testcase_33 AC 408 ms
5,792 KB
testcase_34 AC 406 ms
5,660 KB
testcase_35 AC 411 ms
5,788 KB
testcase_36 AC 439 ms
6,784 KB
testcase_37 AC 409 ms
6,860 KB
testcase_38 AC 413 ms
6,672 KB
testcase_39 AC 414 ms
6,724 KB
testcase_40 AC 414 ms
6,848 KB
testcase_41 AC 418 ms
6,716 KB
testcase_42 AC 415 ms
6,844 KB
testcase_43 AC 412 ms
6,732 KB
testcase_44 AC 412 ms
6,728 KB
testcase_45 AC 420 ms
6,848 KB
testcase_46 AC 398 ms
6,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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, M;
    cin >> N >> M;
    vector<int> P(N);
    REP(i, N) {
        cin >> P[i];
        P[i]--;
    }
    UF uf(N);
    vector<int> A(M), B(M);
    REP(i, M) {
        cin >> A[i] >> B[i];
        A[i]--, B[i]--;
        uf.merge(A[i], B[i]);
    }
    vector<int> L(N);
    REP(i, N) L[i] = uf.leader(i);
    show(L);
    show(P);
    vector<map<int, int>> S(N);
    REP(i, N) S[P[i]][L[i]]++;
    int Q;
    cin >> Q;
    REP(qq, Q) {
        int X, Y;
        cin >> X >> Y;
        X--, Y--;
        if (P[X] == P[Y]) {
            cout << "No" << '\n';
            continue;
        }
        int ld = uf.leader(X);
        if (S[P[Y]][ld] > 0) {
            cout << "Yes" << '\n';
            // move
            if (--S[P[X]][ld] == 0) S[P[X]].erase(ld);
            S[P[Y]][ld]++;
            P[X] = P[Y];
        } else {
            cout << "No" << '\n';
        }
        show(P);
    }
    return 0;
}
0