結果

問題 No.1293 2種類の道路
ユーザー auauaauaua
提出日時 2020-11-21 16:02:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 1,741 bytes
コンパイル時間 1,763 ms
コンパイル使用メモリ 176,612 KB
実行使用メモリ 13,344 KB
最終ジャッジ日時 2023-09-30 21:56:55
合計ジャッジ時間 4,541 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 121 ms
9,972 KB
testcase_10 AC 123 ms
9,840 KB
testcase_11 AC 123 ms
10,296 KB
testcase_12 AC 122 ms
9,908 KB
testcase_13 AC 123 ms
9,852 KB
testcase_14 AC 80 ms
13,344 KB
testcase_15 AC 80 ms
13,212 KB
testcase_16 AC 106 ms
11,364 KB
testcase_17 AC 117 ms
12,028 KB
testcase_18 AC 70 ms
12,556 KB
testcase_19 AC 68 ms
13,228 KB
testcase_20 AC 67 ms
13,204 KB
testcase_21 AC 78 ms
4,376 KB
testcase_22 AC 78 ms
4,380 KB
testcase_23 AC 78 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef complex<double> comp;
const ll INF=1e9+7;
const ll inf=INF*INF;
const ll MOD=1e9+7;
const ll mod=MOD;
const ll MAX=200010;
const double PI=acos(-1.0);

//Union-Find木

struct UnionFind {
    vector< int > data;

    UnionFind() = default;

    explicit UnionFind(size_t sz) : data(sz, -1) {}

    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if(x == y) return false;
        if(data[x] > data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return true;
    }

    int find(int k) {
        if(data[k] < 0) return (k);
        return data[k] = find(data[k]);
    }

    int size(int k) {
        return -data[find(k)];
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }
};

signed main(){
    ll n,d,w;cin>>n>>d>>w;
    UnionFind uf(n);
    ll ans=0;
    rep(i,d){
        ll a,b;cin>>a>>b;
        a--;b--;
        uf.unite(a,b);
    }
    UnionFind uf2(n);
    rep(i,w){
        ll a,b;cin>>a>>b;
        a--;b--;
        uf2.unite(a,b);
    }
    vector<set<ll> >st(n);
    rep(i,n){
        ll rd=uf.find(i);
        ll rw=uf2.find(i);
        if(!st[rd].count(rw)){
            st[rd].insert(rw);
            ans+=1LL*uf.size(rd)*uf2.size(rw);
        }
    }
    cout<<ans-n<<endl;
}
0