結果
問題 | No.1293 2種類の道路 |
ユーザー |
![]() |
提出日時 | 2020-11-20 22:11:19 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 211 ms / 2,000 ms |
コード長 | 1,888 bytes |
コンパイル時間 | 2,359 ms |
コンパイル使用メモリ | 102,096 KB |
実行使用メモリ | 24,000 KB |
最終ジャッジ日時 | 2024-07-23 13:07:45 |
合計ジャッジ時間 | 4,399 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 22 |
ソースコード
#include <iostream>#include <cstdio>#include <cmath>#include <ctime>#include <cstdlib>#include <cassert>#include <vector>#include <list>#include <stack>#include <queue>#include <deque>#include <map>#include <set>#include <bitset>#include <string>#include <algorithm>#include <utility>#include <complex>#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)#define chmin(x, y) (x) = min((x), (y))#define chmax(x, y) (x) = max((x), (y))#define all(x) (x).begin(),(x).end()#define inf 1e18using namespace std;typedef long long llint;typedef long long ll;typedef pair<llint, llint> P;struct UnionFind{int size;vector<int> parent;UnionFind(){}UnionFind(int size){this->size = size;parent.resize(size+1);init();}void init(){for(int i = 0; i <= size; i++) parent[i] = i;}int root(int i){if(parent[i] == i) return i;return parent[i] = root(parent[i]);}bool same(int i, int j){return root(i) == root(j);}void unite(int i, int j){int root_i = root(i), root_j = root(j);if(root_i == root_j) return;parent[root_i] = root_j;}};ll n, d, w;vector<ll> G[100005];UnionFind uf(100005);bool used[100005];map<ll, ll> mp;set<ll> S;P dfs(int v){used[v] = true;ll a = 1, b = 0;if(S.count(uf.root(v)) == 0){b += mp[uf.root(v)];S.insert(uf.root(v));}for(auto u : G[v]){if(used[u]) continue;P res = dfs(u);a += res.first, b += res.second;}return P(a, b);}int main(void){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> d >> w;ll u, v;rep(i, 1, d){cin >> u >> v;G[u].push_back(v);G[v].push_back(u);}rep(i, 1, w){cin >> u >> v;uf.unite(u, v);}rep(i, 1, n) mp[uf.root(i)]++;ll ans = 0;rep(i, 1, n){if(!used[i]){S.clear();P res = dfs(i);ans += res.first * res.second;}}ans -= n;cout << ans << endl;return 0;}