結果
| 問題 | No.1293 2種類の道路 | 
| コンテスト | |
| ユーザー |  mumin | 
| 提出日時 | 2020-11-21 07:39:41 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 56 ms / 2,000 ms | 
| コード長 | 1,398 bytes | 
| コンパイル時間 | 1,836 ms | 
| コンパイル使用メモリ | 179,608 KB | 
| 実行使用メモリ | 14,336 KB | 
| 最終ジャッジ日時 | 2024-07-23 15:24:41 | 
| 合計ジャッジ時間 | 4,189 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 22 | 
ソースコード
#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;
}
            
            
            
        