結果

問題 No.922 東北きりきざむたん
ユーザー polylogKpolylogK
提出日時 2019-09-19 17:18:42
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 383 ms / 2,000 ms
コード長 3,518 bytes
コンパイル時間 2,496 ms
コンパイル使用メモリ 192,664 KB
実行使用メモリ 72,276 KB
最終ジャッジ日時 2023-10-13 08:38:40
合計ジャッジ時間 8,668 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 93 ms
31,132 KB
testcase_10 AC 40 ms
11,632 KB
testcase_11 AC 77 ms
25,236 KB
testcase_12 AC 34 ms
30,144 KB
testcase_13 AC 15 ms
10,204 KB
testcase_14 AC 139 ms
45,648 KB
testcase_15 AC 28 ms
31,940 KB
testcase_16 AC 307 ms
54,944 KB
testcase_17 AC 306 ms
54,924 KB
testcase_18 AC 312 ms
54,900 KB
testcase_19 AC 306 ms
54,944 KB
testcase_20 AC 287 ms
54,948 KB
testcase_21 AC 321 ms
56,288 KB
testcase_22 AC 316 ms
56,488 KB
testcase_23 AC 372 ms
56,852 KB
testcase_24 AC 383 ms
56,804 KB
testcase_25 AC 330 ms
57,792 KB
testcase_26 AC 332 ms
57,816 KB
testcase_27 AC 326 ms
57,752 KB
testcase_28 AC 53 ms
38,140 KB
testcase_29 AC 299 ms
72,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cout;
using std::endl;
using std::cin;

template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

class lowest_common_ancestor {
	std::vector<std::vector<size_t>> g, parent;
	std::vector<size_t> depth;
	
	const size_t logN = 20;

	private:
	void dfs(size_t v, int par = -1, size_t d = 0) {
		parent[0][v] = par;
		depth[v] = d;
		
		for(auto e:g[v]) {
			if(e == par) continue;
			
			dfs(e, v, d + 1);
		}
	}
	void build() {
		for(int i = 0; i < g.size(); i++) if(depth[i] == -1) dfs(i);
		
		for(int k = 0; k < logN - 1; k++) {
			for(int i = 0; i < g.size(); i++) {
				if(parent[k][i] == -1) parent[k + 1][i] = -1;
				else parent[k + 1][i] = parent[k][parent[k][i]];
			}
		}
	}

	public:
	lowest_common_ancestor() {}
	lowest_common_ancestor(int n) : g(n), depth(n, -1) {
		parent.assign(logN, std::vector<size_t>(n, -1));
	}
	void add_edge(const size_t & u, const size_t & v) {
		g[u].push_back(v);
		g[v].push_back(u);
	}
	const size_t query(size_t u, size_t v) {
		static bool built = false;
		if(!built) {
			build();
			built = true;
		}

		if(depth[u] > depth[v]) std::swap(u, v);
		for(int k = 0; k < logN; k++) {
			if(((depth[v] - depth[u]) >> k) & 1) v = parent[k][v];
		}
		if(u == v) return u;

		for(int k = logN - 1; k >= 0; k--) {
			if(parent[k][u] == parent[k][v]) continue;

			u = parent[k][u];
			v = parent[k][v];			
		}		
		return parent[0][u];
	}
};

int main() {
	int n, m, q; scanf("%d%d%d", &n, &m, &q);
	lowest_common_ancestor lca(n);
	std::vector<std::vector<int>> g(n);
	for(int i = 0; i < m; i++) {
		int a, b; scanf("%d%d", &a, &b);

		g[a - 1].push_back(b - 1);
		g[b - 1].push_back(a - 1);
		lca.add_edge(a - 1, b - 1);
	}
	
	int cnt = 0;
	std::vector<int> comp(n, -1), dist(n);
	for(int i = 0; i < n; i++) {
		if(comp[i] != -1) continue;
		
		auto kiri = [&](auto && kiri, int v, int par) -> void {
			comp[v] = cnt;
			
			for(auto e: g[v]) {
				if(e == par) continue;

				dist[e] = dist[v] + 1;
				kiri(kiri, e, v);
			}
		};
		kiri(kiri, i, -1);
		cnt++;
	}

	auto calc = [dist, &lca](int a, int b) -> i64 {
		return dist[a] + dist[b] - 2 * dist[lca.query(a, b)];
	};

	i64 ans = 0;
	std::vector<i64> p(n);
	while(q--) {
		int a, b; scanf("%d%d", &a, &b); a--; b--;

		if(comp[a] != comp[b]) {
			p[a]++;
			p[b]++;
		} else {
			ans += calc(a, b);
		}
	}

	std::vector<std::map<int, std::pair<i64, i64>>> dp(n);
	auto dfs = [&](auto && dfs, int v, int par, bool f) -> std::pair<i64, i64> {
		if(dp[v].count(par)) return dp[v][par];

		std::pair<i64, i64> val{0, p[v]};
		if(par == -1 or f) {
			for(auto e: g[v]) {
				if(e == par) continue;
				auto p = dfs(dfs, e, v, f);

				val.first += p.first + p.second;
				val.second += p.second;
			}
		} else {
			auto p = dfs(dfs, par, v, 0);
			auto q = dfs(dfs, v, -1, 0);

			val.first = q.first - (p.first + p.second);
			val.second = q.second - p.second;
		}

		return dp[v][par] = val;
	};

	std::vector<bool> used(cnt, false);
	for(int i = 0; i < n; i++) {
		if(used[comp[i]]) continue;
		used[comp[i]] = true;

		dfs(dfs, i, -1, true);
	}
	
	std::vector<i64> latte(cnt, 1LL << 60);
	for(int i = 0; i < n; i++) latte[comp[i]] = std::min(latte[comp[i]], dfs(dfs, i, -1, 0).first);
	for(auto v: latte) ans += v;

	printf("%lld\n", ans);
	return 0;
}
0