結果

問題 No.922 東北きりきざむたん
ユーザー kotamanegikotamanegi
提出日時 2019-11-08 21:59:01
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 215 ms / 2,000 ms
コード長 3,521 bytes
コンパイル時間 1,242 ms
コンパイル使用メモリ 116,948 KB
実行使用メモリ 69,600 KB
最終ジャッジ日時 2023-08-20 13:24:04
合計ジャッジ時間 8,164 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
57,732 KB
testcase_01 AC 12 ms
57,308 KB
testcase_02 AC 14 ms
60,280 KB
testcase_03 AC 13 ms
57,936 KB
testcase_04 AC 13 ms
57,380 KB
testcase_05 AC 14 ms
58,248 KB
testcase_06 AC 15 ms
57,520 KB
testcase_07 AC 14 ms
58,476 KB
testcase_08 AC 14 ms
58,136 KB
testcase_09 AC 83 ms
61,464 KB
testcase_10 AC 61 ms
59,184 KB
testcase_11 AC 75 ms
60,140 KB
testcase_12 AC 52 ms
62,924 KB
testcase_13 AC 33 ms
58,712 KB
testcase_14 AC 126 ms
64,356 KB
testcase_15 AC 48 ms
63,324 KB
testcase_16 AC 187 ms
63,356 KB
testcase_17 AC 190 ms
63,124 KB
testcase_18 AC 194 ms
62,960 KB
testcase_19 AC 190 ms
62,772 KB
testcase_20 AC 189 ms
63,184 KB
testcase_21 AC 208 ms
62,848 KB
testcase_22 AC 215 ms
63,180 KB
testcase_23 AC 199 ms
63,000 KB
testcase_24 AC 197 ms
62,640 KB
testcase_25 AC 170 ms
63,472 KB
testcase_26 AC 171 ms
63,576 KB
testcase_27 AC 172 ms
63,520 KB
testcase_28 AC 122 ms
64,912 KB
testcase_29 AC 202 ms
69,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <bitset>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 1e-5
#define LONG_INF 2000000000000000000LL
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define seg_size 262144
#define REP(a,b) for(long long a = 0;a < b;++a)
vector<int> vertexs[200000];
int union_tree[200000];
int union_find(int now) {
	if (union_tree[now] == now) return now;
	return union_tree[now] = union_find(union_tree[now]);
}
int union_merge(int a, int b) {
	a = union_find(a);
	b = union_find(b);
	union_tree[a] = b;
	return 0;
}
int backing[64][200000];
int depth[200000];
int dfs(int now, int back) {
	backing[0][now] = back;
	REP(q, vertexs[now].size()) {
		int go = vertexs[now][q];
		if (go == back) continue;
		depth[go] = depth[now] + 1;
		dfs(go, now);
		
	}
	return 0;
}
long long cnt[200000];
long long calc(int a,int b) {
	if (depth[a] > depth[b]) swap(a, b);
	long long ans = 0;
	int hoge = depth[b] - depth[a];
	ans = hoge;
	for (int i = 0;hoge != 0; ++i) {
		if (hoge % 2 == 1) {
			b = backing[i][b];
		}
		hoge /= 2;
	}
	while (a != b) {
		for (int i = 0;; ++i) {
			if (backing[i][a] == backing[i][b]) {
				int next_go = max(i - 1, 0);
				ans += (1 << (next_go + 1));
				a = backing[next_go][a];
				b = backing[next_go][b];
				break;
			}
		}
	}
	return ans;
}
long long calcu(long long now, long long back) {
	long long ans = 0;
	long long hoge = 0;
	REP(q, vertexs[now].size()) {
		long long go = vertexs[now][q];
		if (go == back) continue;
		ans += calcu(go, now);
		hoge += cnt[go];
	}
	ans += hoge;
	cnt[now] += hoge;
	return ans;
}
long long final_calc(long long now_ans,long long now,long long now_back,long long based) {
	long long final_ans = now_ans;
	for (int i = 0; i < vertexs[now].size(); ++i) {
		int go = vertexs[now][i];
		if (go == now_back) continue;
		long long next_hoge = now_ans;
		next_hoge -= cnt[go];
		next_hoge += cnt[based] - cnt[go];
		final_ans = min(final_ans, final_calc(next_hoge, go, now, based));
	}
	return final_ans;
}
int main(){
	int n, m, query;
	cin >> n >> m >> query;
	REP(i, n) {
		union_tree[i] = i;
	}
	REP(i, m) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		vertexs[a].push_back(b);
		vertexs[b].push_back(a);
		union_merge(a, b);
	}
	REP(i, 200000) {
		backing[0][i] = -2;
	}
	REP(i, n) {
		if (backing[0][i] == -2) {
			dfs(i, i);
		}
	}
	REP(i, 63) {
		REP(q, n) {
			backing[i + 1][q] = backing[i][backing[i][q]];
		}
	}
	long long ans = 0;
	REP(i, query) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		if (union_find(a) == union_find(b)) {
			ans += calc(a, b);
		}
		else {
			cnt[a]++;
			cnt[b]++;
		}
	}
	//cout << ans << endl;
	set<int> gone;
	REP(i, n) {
		if (gone.find(union_find(i)) != gone.end()) continue;
		gone.insert(union_find(i));
		int now = i;
		long long hoge = calcu(i, -1);
		hoge = final_calc(hoge, i, -1, i);
		ans += hoge;
	}
	cout << ans << endl;
}
0