結果

問題 No.1293 2種類の道路
ユーザー 沙耶花沙耶花
提出日時 2020-05-02 16:07:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 1,238 bytes
コンパイル時間 2,587 ms
コンパイル使用メモリ 213,512 KB
実行使用メモリ 14,956 KB
最終ジャッジ日時 2023-09-18 01:31:22
合計ジャッジ時間 5,112 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 128 ms
11,796 KB
testcase_10 AC 127 ms
11,524 KB
testcase_11 AC 133 ms
11,652 KB
testcase_12 AC 127 ms
11,524 KB
testcase_13 AC 129 ms
11,500 KB
testcase_14 AC 87 ms
14,892 KB
testcase_15 AC 85 ms
14,892 KB
testcase_16 AC 113 ms
12,908 KB
testcase_17 AC 121 ms
13,632 KB
testcase_18 AC 76 ms
13,968 KB
testcase_19 AC 73 ms
14,956 KB
testcase_20 AC 72 ms
14,800 KB
testcase_21 AC 76 ms
4,380 KB
testcase_22 AC 76 ms
4,376 KB
testcase_23 AC 77 ms
4,380 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;
		cin>>a>>b;
		a--;b--;
		uf1.unite(a,b);
	}
	
	for(int i=0;i<Mw;i++){
		int a,b;
		cin>>a>>b;
		a--;b--;
		uf2.unite(a,b);
	}
	
	vector<set<int>> S(N);
	
	for(int i=0;i<N;i++){
		S[uf1.find(i)].insert(uf2.find(i));
	}
	
	vector<long long> cnt(N,0);
	for(int i=0;i<N;i++){
		for(auto a:S[i]){
			cnt[i] += (long long)uf2.get_size(a);
		}
	}
	
	long long ans = 0;
	
	for(int i=0;i<N;i++){
		ans += cnt[uf1.find(i)]-1;
	}
	cout<<ans<<endl;
	
	return 0;
}
0