結果

問題 No.1293 2種類の道路
ユーザー 沙耶花沙耶花
提出日時 2020-06-06 09:51:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 1,201 bytes
コンパイル時間 2,249 ms
コンパイル使用メモリ 209,976 KB
実行使用メモリ 9,544 KB
最終ジャッジ日時 2023-09-18 01:38:03
合計ジャッジ時間 4,686 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 65 ms
6,532 KB
testcase_10 AC 65 ms
6,268 KB
testcase_11 AC 64 ms
6,364 KB
testcase_12 AC 66 ms
6,396 KB
testcase_13 AC 65 ms
6,324 KB
testcase_14 AC 55 ms
9,452 KB
testcase_15 AC 57 ms
9,544 KB
testcase_16 AC 59 ms
7,572 KB
testcase_17 AC 63 ms
8,204 KB
testcase_18 AC 46 ms
8,908 KB
testcase_19 AC 59 ms
9,424 KB
testcase_20 AC 61 ms
9,364 KB
testcase_21 AC 32 ms
4,380 KB
testcase_22 AC 32 ms
4,376 KB
testcase_23 AC 32 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 10000000000000000

struct unionfind{
	vector<int> data;
	vector<int> size;
	unionfind(int n){
		for(int i=0;i<n;i++){
			data.push_back(i);
			size.push_back(1);
		}
	}
	
	int find(int x){
		if(data[x]==x)return x;
		return data[x]=find(data[x]);
	}
	
	bool unite(int x,int y){
		x=find(x);y=find(y);
		if(x==y)return false;
		if(size[x]>size[y])swap(x,y);
		data[x]=y;
		size[y]+=size[x];
		return true;
	}
	
	bool check(int x,int y){
		return (find(x)==find(y));
	}
	
	int get_size(int x){
		int X = find(x);
		return size[X];
	}
};

int main(){
	
	int N,Md,Mw;
	cin>>N>>Md>>Mw;
	
	unionfind uf1(N),uf2(N);
	
	for(int i=0;i<Md;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		a--;b--;
		uf1.unite(a,b);
	}
	
	for(int i=0;i<Mw;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		a--;b--;
		uf2.unite(a,b);
	}
	
	set<pair<int,int>> P;
	long long ans = 0;
	
	for(int x=0;x<N;x++){
		pair<int,int> t = make_pair(uf1.find(x),uf2.find(x));
		if(P.count(t))continue;
		P.insert(t);
		ans += (long long)uf1.get_size(x)*uf2.get_size(x);
	}
	ans-=N;
	
	cout<<ans<<endl;
	
	return 0;
}
0