結果

問題 No.1293 2種類の道路
ユーザー furafura
提出日時 2020-11-20 22:02:27
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 1,058 bytes
コンパイル時間 2,327 ms
コンパイル使用メモリ 209,076 KB
実行使用メモリ 9,732 KB
最終ジャッジ日時 2023-09-30 19:16:38
合計ジャッジ時間 4,408 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 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 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 55 ms
7,520 KB
testcase_10 AC 55 ms
7,636 KB
testcase_11 AC 56 ms
7,676 KB
testcase_12 AC 55 ms
7,540 KB
testcase_13 AC 55 ms
7,520 KB
testcase_14 AC 33 ms
7,720 KB
testcase_15 AC 31 ms
7,668 KB
testcase_16 AC 50 ms
8,036 KB
testcase_17 AC 48 ms
8,108 KB
testcase_18 AC 33 ms
8,708 KB
testcase_19 AC 35 ms
9,732 KB
testcase_20 AC 35 ms
9,660 KB
testcase_21 AC 32 ms
4,380 KB
testcase_22 AC 32 ms
4,376 KB
testcase_23 AC 32 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

class union_find{
	int n;
	vector<int> p;
public:
	union_find(int n):n(n),p(n,-1){}
	int find(int u){ return p[u]<0?u:p[u]=find(p[u]); }
	void unite(int u,int v){
		u=find(u); v=find(v);
		if(u!=v){
			if(p[v]<p[u]) swap(u,v);
			p[u]+=p[v]; p[v]=u; n--;
		}
	}
	bool is_same(int u,int v){ return find(u)==find(v); }
	int size()const{ return n; }
	int size(int u){ return -p[find(u)]; }
};

int main(){
	int n,m1,m2; scanf("%d%d%d",&n,&m1,&m2);

	union_find U1(n),U2(n);
	rep(i,m1){
		int u,v; scanf("%d%d",&u,&v); u--; v--;
		U1.unite(u,v);
	}
	rep(i,m2){
		int u,v; scanf("%d%d",&u,&v); u--; v--;
		U2.unite(u,v);
	}

	vector<vector<int>> L(n);
	rep(u,n) L[U1.find(u)].emplace_back(U2.find(u));

	vector<int> cnt(n);
	rep(u,n){
		sort(L[u].begin(),L[u].end());
		L[u].erase(unique(L[u].begin(),L[u].end()),L[u].end());
		for(int v:L[u]) cnt[u]+=U2.size(v);
	}

	lint ans=0;
	rep(u,n) ans+=cnt[U1.find(u)]-1;
	printf("%lld\n",ans);

	return 0;
}
0