Submission #703417

# Submission time Handle Problem Language Result Execution time Memory
703417 2023-02-27T09:42:13 Z 406 Mousetrap (CEOI17_mousetrap) C++17
0 / 100
2 ms 1028 KB
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int N = 2000;
const long long INF = 1ll << 60;
int F[N], n, T, M, ch[N], pr[N], bala[N];
vector<int> adj[N];

void dfs2(int v, int p) {
	for (auto u: adj[v]) if (u != p) {
		bala[u] = bala[v] + ch[v] - 1;
		dfs2(u, v);
	}
}

void dfs(int v, int p) {
	pr[v] = p;
	vector<int> tmp;
	for (auto u: adj[v]) if (u != p) {
		dfs(u, v);
		tmp.push_back(F[u]);
		ch[v]++;
	}
	sort(tmp.rbegin(), tmp.rend());
	if (tmp.size() <= 1)
		F[v] = tmp.size();
	else
		F[v] = tmp[1] + tmp.size();
}

bool check(int O) {
	int m = M;
	int child = -1;
	int cur = 1;
	while (m != T) {
		for (auto u: adj[m]) if (u != pr[m] && u != child) {
			int x = F[u] + bala[u] + (m == M);				
			//F[u] + 1 + bala[u] //m = tmp_m
			//F[u] + bala[u] //else
			if (x >= O) {
				cur--;
				O--;
			}
		}
		cur++;
		if (cur < 0) return false;
		m = pr[m];
	}
	return true;
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> T >> M;
	T--, M--;
	for (int i = 1; i < n; i++) {
		int u, v; cin >> u >> v; u--, v--;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	if (M == T) {
		cout << 0 << '\n';
		return 0;
	}
	dfs(T, T);
	ch[T] = 1;
	dfs2(T, T);

	for (int i = 0; i < n; i++) {
		sort(adj[i].begin(), adj[i].end(), [&](const int x, const int y) {return F[x] + bala[x] > F[y] + bala[y];});
	}

	int l = 0, r = INF;
	while (r - l > 1) {
		int m = l + r >> 1;
		if (check(m)) r = m;
		else l = m;
	}

	cout << l << '\n';
	return 0;
}

Compilation message

mousetrap.cpp: In function 'int main()':
mousetrap.cpp:76:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   76 |   int m = l + r >> 1;
      |           ~~^~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 1028 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -