結果

問題 No.1293 2種類の道路
ユーザー furafura
提出日時 2020-11-20 22:02:27
言語 C++17
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 1,058 bytes
コンパイル時間 2,034 ms
使用メモリ 9,708 KB
最終ジャッジ日時 2023-02-23 18:21:50
合計ジャッジ時間 3,657 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 2 ms
6,204 KB
testcase_01 AC 1 ms
6,196 KB
testcase_02 AC 1 ms
6,252 KB
testcase_03 AC 2 ms
6,192 KB
testcase_04 AC 2 ms
6,276 KB
testcase_05 AC 1 ms
6,200 KB
testcase_06 AC 1 ms
6,172 KB
testcase_07 AC 1 ms
6,184 KB
testcase_08 AC 1 ms
6,188 KB
testcase_09 AC 46 ms
7,632 KB
testcase_10 AC 47 ms
7,596 KB
testcase_11 AC 49 ms
7,560 KB
testcase_12 AC 46 ms
7,632 KB
testcase_13 AC 48 ms
7,672 KB
testcase_14 AC 29 ms
7,556 KB
testcase_15 AC 27 ms
7,636 KB
testcase_16 AC 43 ms
8,128 KB
testcase_17 AC 41 ms
8,128 KB
testcase_18 AC 29 ms
8,700 KB
testcase_19 AC 30 ms
9,640 KB
testcase_20 AC 31 ms
9,708 KB
testcase_21 AC 28 ms
6,204 KB
testcase_22 AC 27 ms
6,272 KB
testcase_23 AC 28 ms
6,164 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