結果

問題 No.1293 2種類の道路
ユーザー 沙耶花沙耶花
提出日時 2020-05-05 17:53:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,270 bytes
コンパイル時間 2,493 ms
コンパイル使用メモリ 214,152 KB
実行使用メモリ 16,216 KB
最終ジャッジ日時 2023-09-18 01:31:04
合計ジャッジ時間 6,638 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
11,484 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 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

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);
	}
	
	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[uf1.find(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