結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー milanis48663220milanis48663220
提出日時 2022-06-17 22:27:31
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 397 ms / 4,000 ms
コード長 4,490 bytes
コンパイル時間 1,648 ms
コンパイル使用メモリ 130,980 KB
実行使用メモリ 44,580 KB
最終ジャッジ日時 2024-04-17 14:38:17
合計ジャッジ時間 12,284 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 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 10 ms
5,376 KB
testcase_09 AC 15 ms
5,376 KB
testcase_10 AC 12 ms
5,376 KB
testcase_11 AC 17 ms
5,376 KB
testcase_12 AC 12 ms
5,376 KB
testcase_13 AC 265 ms
15,376 KB
testcase_14 AC 278 ms
17,820 KB
testcase_15 AC 284 ms
21,936 KB
testcase_16 AC 189 ms
22,164 KB
testcase_17 AC 274 ms
18,720 KB
testcase_18 AC 232 ms
22,332 KB
testcase_19 AC 243 ms
26,608 KB
testcase_20 AC 251 ms
20,688 KB
testcase_21 AC 246 ms
20,508 KB
testcase_22 AC 257 ms
23,160 KB
testcase_23 AC 380 ms
28,020 KB
testcase_24 AC 389 ms
27,924 KB
testcase_25 AC 384 ms
28,024 KB
testcase_26 AC 393 ms
27,892 KB
testcase_27 AC 384 ms
27,892 KB
testcase_28 AC 397 ms
28,020 KB
testcase_29 AC 390 ms
28,016 KB
testcase_30 AC 387 ms
27,996 KB
testcase_31 AC 383 ms
28,020 KB
testcase_32 AC 383 ms
27,892 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 295 ms
23,540 KB
testcase_35 AC 338 ms
41,384 KB
testcase_36 AC 326 ms
21,496 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 243 ms
5,376 KB
testcase_39 AC 352 ms
44,580 KB
testcase_40 AC 324 ms
23,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

using P = pair<int, int>;

struct edge{
    int from, to;
    edge(int from, int to): from(from), to(to) {}
};

/**
 * dfs木上の後退辺に対応するpathをimos法で塗っていく
 * 塗られなかった辺が橋となる
 * verified by ARC039 D
 */
class TwoEdgeConnectedComponents{
    public:
        TwoEdgeConnectedComponents(vector<edge> v, int size){
            G = vector<vector<int>>(size);
            used = vector<bool>(size, false);
            col = vector<int>(size, 0);
            depth = vector<int>(size, 0);
            for(edge e : v){
                int from = e.from;
                int to = e.to;
                G[from].push_back(to);
                G[to].push_back(from);
            }
            N = size;
        }

        vector<vector<int>> G;
        int N;
        vector<edge> run(){
            vector<edge> bridges;
            dfs(0, -1, 0);
            clear();
            paint(0, -1, bridges);
            return bridges;
        }
    private:
        vector<bool> used;
        vector<int> col;
        vector<int> depth;
        void dfs(int u, int p, int d){
            used[u] = true;
            depth[u] = d;
            for(int to : G[u]){
                if(to == p) continue;
                if(used[to] && depth[to] < depth[u]){
                    col[u]++;
                    col[to]--;
                }else if(!used[to]){
                    dfs(to, u, d+1);
                }
            }
        }

        void paint(int u, int p, vector<edge> &v){
            used[u] = true;
            for(int to : G[u]){
                if(to == p) continue;
                if(!used[to]){
                    paint(to, u, v);
                    col[u] += col[to];
                    if(col[to] == 0){
                        v.push_back((edge){u, to});
                    }
                }
            }
        }
        void clear(){
            for(int i = 0; i < N; i++){
                used[i] = false;
            }
        }
};

struct UnionFind {
    vector<int> data;
    UnionFind(int size) : data(size, -1) {}
    bool unionSet(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
        if (data[y] < data[x]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool findSet(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m, q; cin >> n >> m >> q;
    vector<edge> edges;
    UnionFind uf(n+1);
    for(int i = 0; i < m; i++){
        int u, v; cin >> u >> v;
        uf.unionSet(u, v);
        edges.push_back(edge(u, v));
    }
    for(int i = 1; i <= n; i++){
        if(uf.root(i) == i) {
            edges.push_back(edge(0, i));
        }
    }
    auto bridges = TwoEdgeConnectedComponents(edges, n+1).run();
    UnionFind ok(n+1);
    for(edge e: bridges){
        if(e.from == 0 || e.to == 0) continue;
        ok.unionSet(e.from, e.to);
    }
    while(q--){
        int x, y; cin >> x >> y;
        if(ok.findSet(x, y)) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
}
0