結果

問題 No.2319 Friends+
ユーザー ococonomy1ococonomy1
提出日時 2023-05-26 22:25:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 743 ms / 3,000 ms
コード長 4,000 bytes
コンパイル時間 1,543 ms
コンパイル使用メモリ 117,880 KB
実行使用メモリ 59,088 KB
最終ジャッジ日時 2023-08-26 12:26:09
合計ジャッジ時間 13,870 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 743 ms
58,964 KB
testcase_03 AC 693 ms
59,088 KB
testcase_04 AC 659 ms
59,088 KB
testcase_05 AC 662 ms
58,964 KB
testcase_06 AC 706 ms
59,008 KB
testcase_07 AC 127 ms
7,284 KB
testcase_08 AC 128 ms
7,216 KB
testcase_09 AC 126 ms
7,300 KB
testcase_10 AC 121 ms
7,384 KB
testcase_11 AC 121 ms
7,284 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 105 ms
7,832 KB
testcase_19 AC 96 ms
7,756 KB
testcase_20 AC 100 ms
7,236 KB
testcase_21 AC 123 ms
7,348 KB
testcase_22 AC 102 ms
7,348 KB
testcase_23 AC 96 ms
7,524 KB
testcase_24 AC 95 ms
7,424 KB
testcase_25 AC 93 ms
7,352 KB
testcase_26 AC 93 ms
7,404 KB
testcase_27 AC 93 ms
7,420 KB
testcase_28 AC 126 ms
7,436 KB
testcase_29 AC 113 ms
7,264 KB
testcase_30 AC 105 ms
7,224 KB
testcase_31 AC 99 ms
7,236 KB
testcase_32 AC 99 ms
7,264 KB
testcase_33 AC 101 ms
7,480 KB
testcase_34 AC 97 ms
7,268 KB
testcase_35 AC 93 ms
7,212 KB
testcase_36 AC 89 ms
7,260 KB
testcase_37 AC 382 ms
28,932 KB
testcase_38 AC 95 ms
7,268 KB
testcase_39 AC 97 ms
7,340 KB
testcase_40 AC 95 ms
7,376 KB
testcase_41 AC 97 ms
7,216 KB
testcase_42 AC 100 ms
7,256 KB
testcase_43 AC 100 ms
7,336 KB
testcase_44 AC 105 ms
7,216 KB
testcase_45 AC 438 ms
27,808 KB
testcase_46 AC 341 ms
24,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <cassert>
#include <cmath>
#include <tuple>
#include <queue>
#include <bitset>
#include <complex>
#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;
using lg = long long;
using pii = pair<int, int>;
using pll = pair<lg, lg>;
#define TEST clog << "TEST" << endl
#define IINF 2147483647
#define LLINF 9223372036854775807LL
#define AMARI 998244353
//#define AMARI 1000000007
#define TEMOTO ((sizeof(long double) == 16) ? false : true)
#define TIME_LIMIT 1980 * (TEMOTO ? 1 : 1000)
#define el '\n'
#define El '\n'

long long ococo_gcd(long long a, long long b) {
    if (a < b) {
        long long kari = b;
        b = a;
        a = kari;
    }
    long long ans = a;
    while (a % b) {
        long long kari = b;
        b = a % b;
        a = kari;
    }
    return min(a, b);
}

#define MULTI_TEST_CASE false
void solve(void) {
    int n, m;
    cin >> n >> m;
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        p[i]--;
    }
    vector<vector<int>> g(n);
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    int kizyun = sqrt(m);
    vector<int> ooi;
    vector<int> gyaku_o(n);
    int cnt = 0;
    for (int i = 0; i < n; i++)if (g[i].size() >= kizyun) {
        ooi.push_back(i);
        gyaku_o[i] = cnt;
        cnt++;
    }
    vector<vector<int>> assyuku(n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < g[i].size(); j++) {
            vector<int>::iterator it = lower_bound(ooi.begin(), ooi.end(), g[i][j]);
            if (it != ooi.end() && *it == g[i][j]) {
                assyuku[i].push_back(g[i][j]);
            }
        }
    }
    //can_move[i][j] = エトワーニュくんooi[i]がワールドyに行けるかどうか
    vector<vector<int>> can_move(ooi.size(), vector<int>(n,0));
    for (int i = 0; i < ooi.size(); i++) {
        for (int j = 0; j < g[ooi[i]].size(); j++) {
            int fr = ooi[i], to = g[ooi[i]][j];
            can_move[i][p[to]]++;
        }
    }
    int q;
    cin >> q;
    while (q--) {
        if (TEMOTO) {
            for (int i = 0; i < n; i++)clog << p[i] << ' ';
            clog << el;
        }
        int x, y;
        cin >> x >> y;
        x--; y--;
        if (p[x] == p[y]) {
            cout << "No" << el;
            continue;
        }
        //x→yが可能かどうか
        if (g[x].size() >= kizyun) {
            if (can_move[gyaku_o[x]][p[y]] >= 1) {
                cout << "Yes" << el;
                for (int i = 0; i < assyuku[x].size(); i++) {
                    can_move[gyaku_o[assyuku[x][i]]][p[x]]--;
                    can_move[gyaku_o[assyuku[x][i]]][p[y]]++;
                }
                p[x] = p[y];
                continue;
            }
            cout << "No" << El;
            continue;
        }
        if (g[x].size() < kizyun) {
            bool cm = false;
            for (int i = 0; i < g[x].size(); i++) {
                if (p[g[x][i]] == p[y]) {
                    cm = true;
                    break;
                }
            }
            if (cm) {
                cout << "Yes" << el;
                for (int i = 0; i < assyuku[x].size(); i++) {
                    can_move[gyaku_o[assyuku[x][i]]][p[x]]--;
                    can_move[gyaku_o[assyuku[x][i]]][p[y]]++;
                }
                p[x] = p[y];
                continue;
            }
            cout << "No" << el;
            continue;
        }
    }
    return;
}

void calc(void) {
    return;
}

int main(void) {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    calc();
    int t = 1;
    if (MULTI_TEST_CASE)cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
0