結果

問題 No.1293 2種類の道路
ユーザー IKyoproIKyopro
提出日時 2020-11-21 11:50:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,672 bytes
コンパイル時間 2,092 ms
コンパイル使用メモリ 211,480 KB
実行使用メモリ 10,172 KB
最終ジャッジ日時 2023-09-30 21:42:54
合計ジャッジ時間 4,173 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 51 ms
10,164 KB
testcase_15 AC 53 ms
10,160 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 43 ms
9,332 KB
testcase_19 AC 57 ms
10,128 KB
testcase_20 AC 56 ms
10,172 KB
testcase_21 AC 29 ms
4,376 KB
testcase_22 AC 29 ms
4,380 KB
testcase_23 AC 29 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template<class T> bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b){if(a<b) {a = b; return true;} return false;}
#define rep(i,n) for(int i=0;i<(n);i++)
#define drep(i,n) for(int i=(n)-1;i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define debug(x)  cerr << #x << " = " << (x) << endl;

class UnionFind{
private:
    vec<int> p;
    vec<ll> s;
    int cnt;
public:
    UnionFind(){}
    UnionFind(int N):cnt(N),s(N,1),p(N){
        iota(p.begin(),p.end(),0);
    }
    int find(int x){
        if(p[x]==x) return x;
        else return p[x] = find(p[x]);
    }
    void unite(int x,int y){
        x = find(x); y = find(y);
        if(x==y) return;
        if(s[x]>s[y]) swap(x,y);
        p[x] = y;
        s[y] += s[x];
        cnt--;
    }
    bool is_same_set(int x,int y) {return find(x)==find(y);}
    int size(int x) {return s[find(x)];}
    int compnents_number(){return cnt;}
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,D,W;
    cin >> N >> D >> W;
    UnionFind uf0(N),uf1(N);
    rep(i,D){
        int a,b;
        cin >> a >> b;
        a--,b--;
        uf0.unite(a,b);
    }
    rep(i,W){
        int a,b;
        cin >> a >> b;
        a--,b--;
        uf1.unite(a,b);
    }
    ll ans = 0;
    set<pair<int,int>> s;
    rep(i,N){
        int a = uf0.find(i),b = uf1.find(i);
        if(s.count({a,b})) continue;
        ans += uf0.size(i)*uf1.size(i);
        s.insert({a,b});
    }
    cout << ans-N << "\n";
}
0