結果

問題 No.2319 Friends+
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-06-30 01:50:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 743 ms / 3,000 ms
コード長 1,713 bytes
コンパイル時間 2,086 ms
コンパイル使用メモリ 206,764 KB
実行使用メモリ 59,420 KB
最終ジャッジ日時 2023-09-20 20:37:31
合計ジャッジ時間 27,143 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 573 ms
59,252 KB
testcase_03 AC 571 ms
59,420 KB
testcase_04 AC 584 ms
59,340 KB
testcase_05 AC 567 ms
59,344 KB
testcase_06 AC 578 ms
59,284 KB
testcase_07 AC 520 ms
7,664 KB
testcase_08 AC 526 ms
7,584 KB
testcase_09 AC 535 ms
7,500 KB
testcase_10 AC 527 ms
7,500 KB
testcase_11 AC 508 ms
7,500 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 474 ms
7,468 KB
testcase_19 AC 463 ms
7,656 KB
testcase_20 AC 505 ms
7,680 KB
testcase_21 AC 493 ms
7,520 KB
testcase_22 AC 528 ms
7,540 KB
testcase_23 AC 469 ms
7,476 KB
testcase_24 AC 465 ms
7,536 KB
testcase_25 AC 469 ms
7,472 KB
testcase_26 AC 466 ms
7,532 KB
testcase_27 AC 471 ms
7,588 KB
testcase_28 AC 689 ms
7,600 KB
testcase_29 AC 588 ms
7,608 KB
testcase_30 AC 528 ms
7,468 KB
testcase_31 AC 498 ms
7,480 KB
testcase_32 AC 485 ms
7,676 KB
testcase_33 AC 469 ms
7,472 KB
testcase_34 AC 469 ms
7,632 KB
testcase_35 AC 471 ms
7,640 KB
testcase_36 AC 493 ms
7,240 KB
testcase_37 AC 686 ms
27,860 KB
testcase_38 AC 472 ms
7,500 KB
testcase_39 AC 473 ms
7,848 KB
testcase_40 AC 475 ms
7,524 KB
testcase_41 AC 481 ms
7,484 KB
testcase_42 AC 488 ms
7,488 KB
testcase_43 AC 519 ms
7,660 KB
testcase_44 AC 572 ms
7,596 KB
testcase_45 AC 743 ms
27,936 KB
testcase_46 AC 487 ms
24,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;


int main(){

    int N, M, Q, A, B, K;
    cin >> N >> M;
    K = sqrt(M);
    vector<int> P(N), lg;
    vector<bool> large(N);
    for (int i=0; i<N; i++){
        cin >> P[i]; P[i]--;
    }
    vector<vector<int>> E(N), Eg(N);
    for (int i=0; i<M; i++){
        cin >> A >> B; A--; B--;
        if (A == B) continue;
        E[A].push_back(B);
        E[B].push_back(A);
    }

    vector<vector<int>> cnt(N);

    for (int i=0; i<N; i++){
        if (E[i].size() >= K){
            large[i] = 1;
            lg.push_back(i);
        }
    }
    for (auto i : lg){
        cnt[i].resize(N);
        for (auto x : E[i]){
            if (!large[x]) cnt[i][P[x]]++;
            else Eg[i].push_back(x);
        }
    }

    cin >> Q;
    while(Q){
        Q--;
        bool f=0;
        cin >> A >> B; A--; B--;
        if (P[A] == P[B]){
            cout << "No" << endl;
            continue;
        }
        if (!large[A]){
            for (auto x : E[A]){
                if (P[x] == P[B]) f=1;
            }
            if (f){
                for (auto x : E[A]){
                    if (large[x]){
                        cnt[x][P[A]]--;
                        cnt[x][P[B]]++;
                    }
                }
                P[A] = P[B];
                cout << "Yes" << endl;
            }
            else cout << "No" << endl;
        }
        else{
            for (auto x : Eg[A]) if (P[x] == P[B]) f=1;
            if (cnt[A][P[B]] > 0) f=1;
            if (f){
                cout << "Yes" << endl;
                P[A] = P[B];
            }
            else cout << "No" << endl;
        }
    }

    return 0;
}
0