結果

問題 No.1293 2種類の道路
ユーザー 👑 hitonanodehitonanode
提出日時 2020-11-20 23:40:03
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 1,511 bytes
コンパイル時間 2,176 ms
コンパイル使用メモリ 215,952 KB
実行使用メモリ 19,204 KB
最終ジャッジ日時 2023-09-30 20:09:55
合計ジャッジ時間 4,334 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 61 ms
7,988 KB
testcase_10 AC 62 ms
8,080 KB
testcase_11 AC 61 ms
8,004 KB
testcase_12 AC 60 ms
8,024 KB
testcase_13 AC 59 ms
8,080 KB
testcase_14 AC 70 ms
19,204 KB
testcase_15 AC 70 ms
19,156 KB
testcase_16 AC 58 ms
11,164 KB
testcase_17 AC 70 ms
13,592 KB
testcase_18 AC 55 ms
14,888 KB
testcase_19 AC 52 ms
11,300 KB
testcase_20 AC 52 ms
11,196 KB
testcase_21 AC 29 ms
4,376 KB
testcase_22 AC 29 ms
4,380 KB
testcase_23 AC 28 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)

// UnionFind Tree (0-indexed), based on size of each disjoint set
struct UnionFind {
    std::vector<int> par, cou;
    UnionFind(int N = 0) : par(N), cou(N, 1) { iota(par.begin(), par.end(), 0); }
    int find(int x) { return (par[x] == x) ? x : (par[x] = find(par[x])); }
    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if (x == y) return false;
        if (cou[x] < cou[y]) std::swap(x, y);
        par[y] = x, cou[x] += cou[y];
        return true;
    }
    int count(int x) { return cou[find(x)]; }
    bool same(int x, int y) { return find(x) == find(y); }
};

int main()
{
    int N, D, W;
    cin >> N >> D >> W;
    UnionFind uf1(N);
    while (D--) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        uf1.unite(a, b);
    }
    UnionFind uf2(N);
    while (W--) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        uf2.unite(a, b);
    }
    map<int, set<int>> mp;
    vector<int> sz(N);
    REP(i, N) mp[uf2.find(i)].insert(uf1.find(i)), sz[uf2.find(i)]++;
    lint ret = 0;
    REP(r, N) if (sz[r]) {
        lint s = 0;
        for (auto x : mp[r]) s += uf1.count(x);
        ret += s * sz[r] - sz[r];
    }
    cout << ret << '\n';
}
0