結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー sotanishysotanishy
提出日時 2022-06-17 22:28:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 224 ms / 4,000 ms
コード長 3,106 bytes
コンパイル時間 2,894 ms
コンパイル使用メモリ 206,824 KB
実行使用メモリ 55,196 KB
最終ジャッジ日時 2024-04-17 14:39:23
合計ジャッジ時間 9,494 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 5 ms
5,376 KB
testcase_09 AC 6 ms
5,376 KB
testcase_10 AC 10 ms
5,376 KB
testcase_11 AC 10 ms
5,376 KB
testcase_12 AC 6 ms
5,376 KB
testcase_13 AC 101 ms
17,504 KB
testcase_14 AC 128 ms
20,952 KB
testcase_15 AC 138 ms
22,956 KB
testcase_16 AC 56 ms
16,036 KB
testcase_17 AC 125 ms
22,272 KB
testcase_18 AC 115 ms
23,088 KB
testcase_19 AC 157 ms
30,140 KB
testcase_20 AC 117 ms
21,724 KB
testcase_21 AC 127 ms
23,920 KB
testcase_22 AC 170 ms
27,392 KB
testcase_23 AC 224 ms
33,524 KB
testcase_24 AC 222 ms
33,136 KB
testcase_25 AC 216 ms
33,728 KB
testcase_26 AC 208 ms
33,424 KB
testcase_27 AC 203 ms
33,504 KB
testcase_28 AC 201 ms
33,384 KB
testcase_29 AC 212 ms
33,324 KB
testcase_30 AC 204 ms
33,528 KB
testcase_31 AC 209 ms
33,532 KB
testcase_32 AC 206 ms
33,524 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 61 ms
14,976 KB
testcase_35 AC 150 ms
52,352 KB
testcase_36 AC 134 ms
27,492 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 33 ms
5,376 KB
testcase_39 AC 155 ms
55,196 KB
testcase_40 AC 129 ms
30,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

class Lowlink {
public:
    Lowlink() = default;
    explicit Lowlink(const std::vector<std::vector<int>>& G) : G(G), ord(G.size(), -1), low(G.size()) {
        for (int i = 0; i < (int) G.size(); ++i) {
            if (ord[i] == -1) dfs(i, -1);
        }
    }

    std::vector<std::pair<int, int>> get_bridges() const {
        return bridge;
    }

    std::vector<int> get_articulation_points() const {
        return articulation;
    }

    bool is_bridge(int u, int v) const {
        if (ord[u] > ord[v]) std::swap(u, v);
        return ord[u] < low[v];
    }

protected:
    std::vector<std::vector<int>> G;
    std::vector<int> ord, low;
    std::vector<std::pair<int, int>> bridge;
    std::vector<int> articulation;

private:
    int k = 0;

    void dfs(int v, int p) {
        ord[v] = k++;
        low[v] = ord[v];
        bool is_articulation = false, checked = false;
        int cnt = 0;
        for (int c : G[v]) {
            if (c == p && !checked) {
                checked = true;
                continue;
            }
            if (ord[c] == -1) {
                ++cnt;
                dfs(c, v);
                low[v] = std::min(low[v], low[c]);
                if (p != -1 && ord[v] <= low[c]) is_articulation = true;
                if (ord[v] < low[c]) bridge.push_back(std::minmax(v, c));
            } else {
                low[v] = std::min(low[v], ord[c]);
            }
        }
        if (p == -1 && cnt > 1) is_articulation = true;
        if (is_articulation) articulation.push_back(v);
    }
};

class UnionFind {
public:
    UnionFind() = default;
    explicit UnionFind(int n) : data(n, -1) {}

    int find(int x) {
        if (data[x] < 0) return x;
        return data[x] = find(data[x]);
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (data[x] > data[y]) std::swap(x, y);
        data[x] += data[y];
        data[y] = x;
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    int size(int x) {
        return -data[find(x)];
    }

private:
    std::vector<int> data;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N, M, Q;
    cin >> N >> M >> Q;
    vector<vector<int>> G(N);
    rep(_,0,M) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    Lowlink low(G);
    UnionFind uf(N);
    rep(i,0,N) for (int j : G[i]) if (low.is_bridge(i, j)) uf.unite(i, j);
    while (Q--) {
        int x, y;
        cin >> x >> y;
        --x, --y;
        cout << (uf.same(x, y) ? "Yes" : "No") << "\n";
    }
}
0