結果

問題 No.1293 2種類の道路
ユーザー leaf_1415leaf_1415
提出日時 2020-11-20 22:11:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 191 ms / 2,000 ms
コード長 1,888 bytes
コンパイル時間 2,103 ms
コンパイル使用メモリ 101,380 KB
実行使用メモリ 22,716 KB
最終ジャッジ日時 2023-09-30 19:21:15
合計ジャッジ時間 4,774 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,740 KB
testcase_01 AC 4 ms
5,872 KB
testcase_02 AC 4 ms
5,744 KB
testcase_03 AC 3 ms
5,952 KB
testcase_04 AC 4 ms
5,704 KB
testcase_05 AC 4 ms
5,736 KB
testcase_06 AC 4 ms
5,876 KB
testcase_07 AC 4 ms
5,932 KB
testcase_08 AC 4 ms
5,764 KB
testcase_09 AC 97 ms
14,412 KB
testcase_10 AC 99 ms
14,236 KB
testcase_11 AC 96 ms
14,324 KB
testcase_12 AC 95 ms
14,344 KB
testcase_13 AC 96 ms
14,228 KB
testcase_14 AC 185 ms
22,716 KB
testcase_15 AC 191 ms
22,680 KB
testcase_16 AC 98 ms
12,700 KB
testcase_17 AC 120 ms
14,328 KB
testcase_18 AC 84 ms
12,860 KB
testcase_19 AC 40 ms
6,888 KB
testcase_20 AC 40 ms
7,016 KB
testcase_21 AC 36 ms
8,096 KB
testcase_22 AC 36 ms
8,020 KB
testcase_23 AC 36 ms
8,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 1e18

using 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;
}
0