結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー suisui
提出日時 2022-08-28 02:33:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 683 ms / 4,000 ms
コード長 3,544 bytes
コンパイル時間 2,803 ms
コンパイル使用メモリ 215,868 KB
実行使用メモリ 49,620 KB
最終ジャッジ日時 2024-04-23 03:58:25
合計ジャッジ時間 21,796 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 15 ms
6,944 KB
testcase_09 AC 21 ms
6,940 KB
testcase_10 AC 20 ms
6,940 KB
testcase_11 AC 25 ms
6,944 KB
testcase_12 AC 17 ms
6,944 KB
testcase_13 AC 415 ms
14,024 KB
testcase_14 AC 475 ms
16,632 KB
testcase_15 AC 477 ms
17,840 KB
testcase_16 AC 261 ms
12,020 KB
testcase_17 AC 459 ms
17,628 KB
testcase_18 AC 375 ms
17,892 KB
testcase_19 AC 433 ms
23,612 KB
testcase_20 AC 406 ms
17,188 KB
testcase_21 AC 425 ms
19,000 KB
testcase_22 AC 475 ms
21,640 KB
testcase_23 AC 669 ms
26,112 KB
testcase_24 AC 683 ms
25,968 KB
testcase_25 AC 682 ms
26,172 KB
testcase_26 AC 681 ms
26,124 KB
testcase_27 AC 677 ms
26,112 KB
testcase_28 AC 678 ms
25,964 KB
testcase_29 AC 676 ms
25,892 KB
testcase_30 AC 681 ms
25,972 KB
testcase_31 AC 674 ms
26,104 KB
testcase_32 AC 678 ms
25,968 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 403 ms
10,992 KB
testcase_35 AC 568 ms
42,224 KB
testcase_36 AC 547 ms
17,376 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 327 ms
6,940 KB
testcase_39 AC 584 ms
49,620 KB
testcase_40 AC 583 ms
24,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n+1); i++)
#define repn(i,a,b) for (int i = (int)(a) ; i < (int)(b+1); i++)
#define repv(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x) ((int)(x).size())
#define cauto const auto&
#define bit(n) (1LL<<(n))
#define uni(v) v.erase( unique(v.begin(), v.end()), v.end() );
using ll = long long;
#ifdef LOCAL
    #include <debug.hpp>
    #define debug(...) debug::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
    #define debug(...) (static_cast<void>(0))
#endif

struct dsu {
  public:
    dsu() : _n(0) {}
    dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }
    
  private:
    int _n;
    std::vector<int> parent_or_size;
};



struct Edge {
    int to;
};
using Graph = vector<vector<Edge>>;
using P = pair<long, long>;

struct LowLink {
    const Graph &G;
    vector<int> used, ord, low;
    vector<int> aps;  
    vector<P> bridges;

    LowLink(const Graph &G_) : G(G_) {
        used.assign(G.size(), 0);
        ord.assign(G.size(), 0);
        low.assign(G.size(), 0);
        int k = 0;
        for (int i = 0; i < (int)G.size(); i++) {
            if (!used[i]) k = dfs(i, k, -1);
        }
        sort(aps.begin(), aps.end());
        sort(bridges.begin(), bridges.end()); 
    }

    int dfs(int id, int k, int par) { 
        used[id] = true;
        ord[id] = k++;
        low[id] = ord[id];
        bool is_aps = false;
        int count = 0;
        for (auto &e : G[id]) {
            if (!used[e.to]) {
                count++;
                k = dfs(e.to, k, id);
                low[id] = min(low[id], low[e.to]);
                if (par != -1 && ord[id] <= low[e.to]) is_aps = true; 
                if (ord[id] < low[e.to]) bridges.emplace_back(min(id, e.to), max(id, e.to)); 
            } else if (e.to != par) {
                low[id] = min(low[id], ord[e.to]);
            }
        }
        if (par == -1 && count >= 2) is_aps = true; 
        if (is_aps) aps.push_back(id);
        return k;
    }
};

int main(){
    int n,m,q;
    cin >> n >> m >> q;
    Graph G(n);
    rep(i,m){
        int a,b;
        cin >> a >> b;
        a--;b--;
        Edge e;
        e.to = b;
        G[a].push_back(e);
        e.to = a;
        G[b].push_back(e);
    }
    LowLink lowlink(G);
    debug(lowlink.bridges);
    debug(lowlink.aps);
    vector<P> ans = lowlink.bridges;
    dsu ds(n);
    for(auto p : ans){
        ds.merge(p.first,p.second);
    }
    rep(i,q){
        int c,d;
        cin >> c >> d;
        c--;d--;
        if(ds.same(c,d)) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}
0