結果

問題 No.922 東北きりきざむたん
ユーザー startcppstartcpp
提出日時 2019-11-08 22:50:02
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 400 ms / 2,000 ms
コード長 2,966 bytes
コンパイル時間 752 ms
コンパイル使用メモリ 66,300 KB
実行使用メモリ 34,556 KB
最終ジャッジ日時 2023-10-13 04:27:33
合計ジャッジ時間 6,516 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
22,772 KB
testcase_01 AC 6 ms
22,784 KB
testcase_02 AC 7 ms
22,732 KB
testcase_03 AC 7 ms
22,788 KB
testcase_04 AC 7 ms
22,824 KB
testcase_05 AC 7 ms
23,032 KB
testcase_06 AC 7 ms
22,944 KB
testcase_07 AC 7 ms
22,820 KB
testcase_08 AC 7 ms
22,840 KB
testcase_09 AC 60 ms
27,148 KB
testcase_10 AC 57 ms
24,016 KB
testcase_11 AC 59 ms
25,692 KB
testcase_12 AC 22 ms
26,272 KB
testcase_13 AC 22 ms
23,628 KB
testcase_14 AC 87 ms
28,172 KB
testcase_15 AC 12 ms
25,964 KB
testcase_16 AC 188 ms
29,680 KB
testcase_17 AC 200 ms
29,536 KB
testcase_18 AC 195 ms
29,572 KB
testcase_19 AC 201 ms
29,496 KB
testcase_20 AC 200 ms
29,496 KB
testcase_21 AC 216 ms
29,464 KB
testcase_22 AC 211 ms
29,400 KB
testcase_23 AC 383 ms
30,396 KB
testcase_24 AC 400 ms
30,356 KB
testcase_25 AC 390 ms
30,464 KB
testcase_26 AC 392 ms
30,548 KB
testcase_27 AC 396 ms
30,488 KB
testcase_28 AC 69 ms
26,072 KB
testcase_29 AC 214 ms
34,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//考察10分、実装10分、デバッグたくさん (同じ木の間の移動を見落とし、その他実装上の見落とし)
#include <iostream>
#include <vector>
#include <algorithm>
#define rep(i, n) for(i = 0; i < n; i++)
#define int long long
using namespace std;

int n, m, q;
vector<int> et[100000];
int vcost[100000];

bool used[100000];
int treeSum[100000];
int parent[100000];
int depth[100000];
int dfs(int p, int v, int dep) {
	int i;
	
	used[v] = true;
	parent[v] = p;
	depth[v] = dep;
	int ret = vcost[v];
	rep(i, et[v].size()) {
		int nv = et[v][i];
		if (nv == p) continue;
		ret += dfs(v, nv, dep + 1);
	}
	return treeSum[v] = ret;
}

int calcInitCost(int p, int v) {
	int i;
	
	int ret = 0;
	rep(i, et[v].size()) {
		int nv = et[v][i];
		if (nv == p) continue;
		ret += calcInitCost(v, nv);
	}
	ret += treeSum[v];
	return ret;
}

int sumcost[100000];
int rt;
int dfs2(int p, int v, int sumCost) {
	int i;
	
	sumcost[v] = sumCost;
	if (p != -1 && et[v].size() == 1) { return sumCost; }
	
	int ret = sumCost;
	rep(i, et[v].size()) {
		int nv = et[v][i];
		if (nv == p) continue;
		int res = dfs2(v, nv, sumCost + treeSum[rt] - 2 * treeSum[nv]);
		ret = min(ret, res);
	}
	return ret;
}

//
struct UF {
	int par[100000];
	UF() { int i; rep(i, 100000) par[i] = i; }
	int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); }
	void unit(int x, int y) { x = root(x); y = root(y); par[x] = y; }
	bool same(int x, int y) { return root(x) == root(y); }
};

int parents[20][100000];
UF uf;

int lca(int a, int b) {
	if (depth[a] < depth[b]) {
		swap(a, b);
	}
	
	int i;
	int diff = depth[a] - depth[b];
	rep(i, 20) {
		if ((diff >> i) & 1) {
			a = parents[i][a];
		}
	}
	
	for (i = 19; i >= 0; i--) {
		if (parents[i][a] != parents[i][b]) {
			a = parents[i][a];
			b = parents[i][b];
		}
	}
	if (a == b) return a;
	return parent[a];
}

signed main() {
	int i;
	
	cin >> n >> m >> q;
	rep(i, m) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		et[u].push_back(v);
		et[v].push_back(u);
		uf.unit(u, v);
	}
	
	rep(i, n) {
		if (used[i]) continue;
		dfs(-1, i, 0);
	}
	
	rep(i, n) parents[0][i] = parent[i];
	
	rep(i, 19) {
		int j;
		rep(j, n) {
			parents[i + 1][j] = parents[i][parents[i][j]];
		}
	}
	
	int ans1 = 0;
	rep(i, q) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		
		if (uf.same(a, b)) {
			int l = lca(a, b);
			ans1 += depth[a] + depth[b] - 2 * depth[l];
			continue;
		}
		
		vcost[a]++;
		vcost[b]++;
	}
	
	//rep(i, n) { cout << "vcost[" << i + 1 << "] = " << vcost[i] << endl; }
	
	rep(i, n) { used[i] = false; }
	
	int ans2 = 0;
	rep(i, n) {
		if (used[i]) continue;
		
		//cout << "i = " << i << endl;
		dfs(-1, i, 0);
		
		int initCost = calcInitCost(-1, i);
		initCost -= treeSum[i];
		
		rt = i;
		int res = dfs2(-1, i, initCost);
		ans2 += res;
	}
	
	/*rep(i, n) {
		cout << "v = " << i + 1 << ", treeSum = " << treeSum[i] << ", sumcost = " << sumcost[i] << endl;
	}*/
	cout << ans1 + ans2 << endl;
	return 0;
}
0