結果

問題 No.922 東北きりきざむたん
ユーザー kwm_tkwm_t
提出日時 2023-05-21 15:11:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 183 ms / 2,000 ms
コード長 4,855 bytes
コンパイル時間 5,090 ms
コンパイル使用メモリ 273,988 KB
実行使用メモリ 35,060 KB
最終ジャッジ日時 2023-08-23 15:09:25
合計ジャッジ時間 10,938 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,356 KB
testcase_04 AC 2 ms
4,356 KB
testcase_05 AC 2 ms
4,356 KB
testcase_06 AC 2 ms
4,356 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 49 ms
19,708 KB
testcase_10 AC 26 ms
7,744 KB
testcase_11 AC 43 ms
15,952 KB
testcase_12 AC 30 ms
21,808 KB
testcase_13 AC 13 ms
7,752 KB
testcase_14 AC 78 ms
29,496 KB
testcase_15 AC 26 ms
23,140 KB
testcase_16 AC 127 ms
31,072 KB
testcase_17 AC 130 ms
31,004 KB
testcase_18 AC 131 ms
31,048 KB
testcase_19 AC 125 ms
31,144 KB
testcase_20 AC 131 ms
30,984 KB
testcase_21 AC 148 ms
31,068 KB
testcase_22 AC 148 ms
31,000 KB
testcase_23 AC 183 ms
30,792 KB
testcase_24 AC 183 ms
30,692 KB
testcase_25 AC 154 ms
32,084 KB
testcase_26 AC 154 ms
32,244 KB
testcase_27 AC 146 ms
32,192 KB
testcase_28 AC 48 ms
27,364 KB
testcase_29 AC 141 ms
35,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
class Tree {
public:
	Tree(int n, int root) : n(n), root(root) {
		edge.resize(n);
		for (int i = 0; i < MAXLOGV; i++) parent[i].resize(n);
		depth.resize(n, -1);
	}
	// uとvをつなぐ
	// lcaを求めることが主目的なので無向グラフとしている
	void unite(int u, int v) {
		edge[u].push_back(v);
		edge[v].push_back(u);
	}
	// initする
	// コンストラクタだけじゃなくてこれも呼ばないとlcaが求められないぞ
	void init() {
		rep(i, n) {
			if (-1 != depth[i])continue;
			dfs(i, -1, 0);
		}
		for (int k = 0; k + 1 < MAXLOGV; k++) {
			for (int v = 0; v < n; v++) {
				if (parent[k][v] < 0) parent[k + 1][v] = -1;
				else parent[k + 1][v] = parent[k][parent[k][v]];
			}
		}
	}
	// uとvのlcaを求める
	int lca(int u, int v) const {
		if (depth[u] > depth[v]) swap(u, v);
		for (int k = 0; k < MAXLOGV; k++) {
			if ((depth[v] - depth[u]) >> k & 1) {
				v = parent[k][v];
			}
		}
		if (u == v) return u;
		for (int k = MAXLOGV - 1; k >= 0; k--) {
			if (parent[k][u] != parent[k][v]) {
				u = parent[k][u];
				v = parent[k][v];
			}
		}
		return parent[0][u];
	}
	// uのn個親を求める
	int pare(int v, int n) {
		if (depth[v] < n)return -1;
		n = min(n, depth[v]);
		int idx = MAXLOGV;
		while (n) {
			for (int i = idx - 1; i >= 0; --i) {
				if (n < (1 << i))continue;
				if (-1 == parent[i][v])continue;
				n -= (1 << i);
				v = parent[i][v];
				idx = i;
				break;
			}
		}
		return v;
	}
	// uからvに向かってd進んだ頂点を返す
	int JumpOnTree(int u, int v, int d) {
		if (0 == d)return u;
		int distuv = dist(u, v);
		if (distuv < d)return -1;
		int l = lca(u, v);
		if (l == u)return pare(v, distuv - d);
		if (l == v)return pare(u, d);
		int distlu = dist(l, u);
		if (distlu >= d)return pare(u, d);
		return pare(v, distuv - d);
	}
	// uとvの距離を求める
	// edgeを定義しないといけない時はこれじゃダメ
	int dist(int u, int v) const {
		int p = lca(u, v);
		return (depth[u] - depth[p]) + (depth[v] - depth[p]);
	}
	//頂点wが頂点u,vのパス上に存在するか
	bool on_path(int u, int v, int w) {
		return (dist(u, w) + dist(v, w) == dist(u, v));
	}
	int dfs(int v, int p, int d) {
		int ret = 1;
		parent[0][v] = p;
		depth[v] = d;
		for (int next : edge[v]) {
			if (next == p) continue;
			auto get = dfs(next, v, d + 1);
			ret += get;
		}
		return ret;
	}
	static const int MAXLOGV = 25;
	// グラフの隣接リスト表現
	vector<vector<int>>edge;
	// 頂点の数
	int n;
	// 根ノードの番号
	int root;
	// 親ノード
	vector<int> parent[MAXLOGV];
	// 根からの深さ
	vector<int> depth;
};
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, m, q; cin >> n >> m >> q;
	vector<vector<int>>to(n);
	dsu uf(n);
	Tree tree(n, 0);
	rep(i, m) {
		int a, b; cin >> a >> b; a--, b--;
		to[a].push_back(b);
		to[b].push_back(a);
		uf.merge(a, b);
		tree.unite(a, b);
	}
	tree.init();
	long long ans = 0;
	vector<int>cnt(n);
	rep(i, q) {
		int a, b; cin >> a >> b, a--, b--;
		if (uf.same(a, b)) {
			ans += tree.dist(a, b);
		}
		else {
			cnt[a]++;
			cnt[b]++;
		}
	}
	vector<pair<long long, int>>memo(n);
	vector<long long>result(n);
	auto gs = uf.groups();
	for (auto g : gs) {
		int root = g[0];
		// pre
		auto dfs = [&](auto self, int v, int p = -1)->pair<long long, int> {
			P ret = { 0,cnt[v] };
			for (auto nv : to[v]) {
				if (p == nv)continue;
				auto get = self(self, nv, v);
				ret.first += get.first + get.second;
				ret.second += get.second;
			}
			return memo[v] = ret;
		};
		dfs(dfs, root);
		int sz = memo[root].second;
		result[root] = memo[root].first;
		// main
		auto dfs2 = [&](auto self, int v, int p = -1)->void {
			if (-1 != p) {
				result[v] = result[p];
				result[v] += sz - 2 * memo[v].second;
			}
			for (auto nv : to[v]) {
				if (p == nv)continue;
				self(self, nv, v);
			}
		};
		dfs2(dfs2, root);
		long long val = LINF;
		for (int v : g) {
			chmin(val, result[v]);
		}
		ans += val;
	}
	cout << ans << endl;
	return 0;
}
0