結果

問題 No.1293 2種類の道路
ユーザー muminmumin
提出日時 2020-11-21 07:39:41
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 1,398 bytes
コンパイル時間 1,754 ms
コンパイル使用メモリ 177,680 KB
実行使用メモリ 14,152 KB
最終ジャッジ日時 2023-09-30 21:37:26
合計ジャッジ時間 3,874 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 54 ms
10,904 KB
testcase_10 AC 56 ms
10,912 KB
testcase_11 AC 56 ms
10,580 KB
testcase_12 AC 56 ms
10,944 KB
testcase_13 AC 56 ms
10,668 KB
testcase_14 AC 47 ms
14,032 KB
testcase_15 AC 47 ms
14,152 KB
testcase_16 AC 49 ms
12,180 KB
testcase_17 AC 54 ms
12,772 KB
testcase_18 AC 36 ms
13,288 KB
testcase_19 AC 34 ms
14,080 KB
testcase_20 AC 34 ms
14,076 KB
testcase_21 AC 29 ms
4,376 KB
testcase_22 AC 30 ms
4,380 KB
testcase_23 AC 29 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;

struct UnionFind {
    vector<int> par;
    
    UnionFind(int n) : par(n, -1) { }
    void init(int n) { par.assign(n, -1); }
    
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    
    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y); // merge technique
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    
    int size(int x) {
        return -par[root(x)];
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, d, w;
    cin >> n >> d >> w;
    UnionFind uf1(n), uf2(n);
    rep(i,d){
        int a, b;
        cin >> a >> b;
        a--, b--;
        uf1.merge(a, b);
    }
    rep(i,w){
        int a, b;
        cin >> a >> b;
        a--, b--;
        uf2.merge(a, b);
    }

    vector<set<int>> vec(n);
    rep(i,n){
        vec[uf1.root(i)].insert(uf2.root(i));
    }
    vector<ll> cnt(n);
    rep(i,n){
        for(auto v : vec[i]){
            cnt[i] += uf2.size(v);
        }
    }
    ll res = -n;
    rep(i,n) res += cnt[uf1.root(i)];
    cout << res << endl;
}
0