結果

問題 No.922 東北きりきざむたん
ユーザー polylogKpolylogK
提出日時 2019-09-19 17:12:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,835 bytes
コンパイル時間 1,887 ms
コンパイル使用メモリ 180,288 KB
実行使用メモリ 12,900 KB
最終ジャッジ日時 2023-10-13 08:38:29
合計ジャッジ時間 9,032 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 58 ms
9,736 KB
testcase_10 AC 1,109 ms
5,116 KB
testcase_11 AC 135 ms
8,420 KB
testcase_12 AC 11 ms
8,508 KB
testcase_13 AC 9 ms
4,596 KB
testcase_14 AC 57 ms
12,900 KB
testcase_15 AC 7 ms
8,564 KB
testcase_16 TLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

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...));
}

int main() {
	int n, m, q; scanf("%d%d%d", &n, &m, &q);
	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);
	}
	
	int cnt = 0;
	std::vector<int> comp(n, -1);
	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;

				kiri(kiri, e, v);
			}
		};
		kiri(kiri, i, -1);
		cnt++;
	}

	auto calc = [g](int v, int u) -> i64 {
		std::queue<int> qu({v});
		std::vector<int> min_cost(g.size(), 1 << 30); min_cost[v] = 0;
		while(!qu.empty()) {
			auto v = qu.front(); qu.pop();

			for(auto e: g[v]) {
				if(min_cost[e] <= min_cost[v] + 1) continue;

				min_cost[e] = min_cost[v] + 1;
				qu.push(e);
			}
		}
		return min_cost[u];
	};

	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<i64> latte(cnt, 1LL << 60); 
	for(int i = 0; i < n; i++) {
		i64 tmp = 0;
		auto dfs = [&](auto dfs, int v, int par, i64 d) -> void {
			tmp += p[v] * d;
			for(auto e: g[v]) {
				if(e == par) continue;

				dfs(dfs, e, v, d + 1);
			}
		};
		dfs(dfs, i, -1, 0);
		
		latte[comp[i]] = std::min(latte[comp[i]], tmp);
	}

	for(auto v: latte) ans += v;
	printf("%lld\n", ans);
	return 0;
}
0