結果

問題 No.1293 2種類の道路
ユーザー auauaauaua
提出日時 2020-11-21 16:01:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,737 bytes
コンパイル時間 1,637 ms
コンパイル使用メモリ 174,784 KB
実行使用メモリ 13,496 KB
最終ジャッジ日時 2023-09-30 21:56:35
合計ジャッジ時間 4,524 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 80 ms
13,216 KB
testcase_15 AC 81 ms
13,496 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 70 ms
12,468 KB
testcase_19 AC 68 ms
13,276 KB
testcase_20 AC 67 ms
13,152 KB
testcase_21 AC 78 ms
4,380 KB
testcase_22 AC 77 ms
4,376 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+=uf.size(rd)*uf2.size(rw);
        }
    }
    cout<<ans-n<<endl;
}
0