結果

問題 No.2319 Friends+
ユーザー Yaowei Lyu
提出日時 2023-05-26 21:55:29
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,491 bytes
コンパイル時間 3,908 ms
コンパイル使用メモリ 256,212 KB
実行使用メモリ 39,680 KB
最終ジャッジ日時 2024-12-25 06:35:52
合計ジャッジ時間 41,728 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 37 TLE * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
constexpr i64 mod = 998244353;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<int> p(n + 1);
    for (int i = 1; i <= n; i += 1) {
        cin >> p[i];
    }
    vector<vector<int>> g(n + 1), sg(n + 1);
    vector<multiset<int>> adj(n + 1);
    int sn = 500;
    for (int i = 0, x, y; i < m; i += 1) {
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for (int i = 1; i <= n; i += 1) {
        if (g[i].size() > sn) {
            for (int j : g[i]) {
                sg[j].push_back(i);
                adj[i].insert(p[j]);
            }
        }
    }
    int q;
    cin >> q;
    for (int i = 0, x, y; i < q; i += 1) {
        cin >> x >> y;
        if (p[x] == p[y]) {
            cout << "No\n";
        } else {
            bool ok = false;
            if (g[x].size() <= sn) {
                for (int z : g[x]) {
                    if (p[z] == p[y]) {
                        ok = true;
                    }
                }
            } else {
                ok = adj[x].contains(p[y]);
            }
            if (ok) {
                cout << "Yes\n";
                for (int z : sg[x]) {
                    adj[z].extract(p[x]);
                    adj[z].insert(p[y]);
                }
                p[x] = p[y];
            } else {
                cout << "No\n";
            }
        }
    }
}
0