結果
| 問題 |
No.1293 2種類の道路
|
| コンテスト | |
| ユーザー |
hitonanode
|
| 提出日時 | 2020-11-20 23:40:03 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 79 ms / 2,000 ms |
| コード長 | 1,511 bytes |
| コンパイル時間 | 2,382 ms |
| コンパイル使用メモリ | 209,608 KB |
| 最終ジャッジ日時 | 2025-01-16 03:26:28 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 22 |
ソースコード
#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';
}
hitonanode