Submission #1027601

# Submission time Handle Problem Language Result Execution time Memory
1027601 2024-07-19T07:55:31 Z vjudge1 Mousetrap (CEOI17_mousetrap) C++17
100 / 100
674 ms 214476 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 1'000'000 + 10;
int n, t, m;
vector<int> ad[N];
vector<int> path;
bool inPath[N];
 
bool dfs1(int u, int p = -1) { 
	inPath[u] = u == t;
	for (const auto& v : ad[u]) { 
		if (v == p) continue;
		inPath[u] |= dfs1(v, u);
	}
	if (inPath[u]) path.push_back(u);
	return inPath[u];
}
int f[N];
void dfs2(int u, int p = -1) { 
	vector<int> best {0, 0};
	for (const auto& v : ad[u]) { 
		if (v == p || inPath[v]) continue;
		dfs2(v, u);
		best.push_back(f[v]);
	}
	sort(best.begin(), best.end(), greater<>());
	f[u] = best.size() + best[1] - 2;
	// cerr << u << " ";
  // for (const auto& x : best) cerr << x << " "; cerr << "\n";
}
 
int32_t main() { 
	cin.tie(0)->sync_with_stdio(0);
 
	cin >> n >> t >> m;
	for (int i = 1; i < n; ++i) { 
		int u, v; cin >> u >> v;
		ad[u].push_back(v);
		ad[v].push_back(u);
	}
 
	dfs1(m);
	
	vector<pair<int, int>> value;
	{
		reverse(path.begin(), path.end());
		path.pop_back();
		
		int it = 0;
		for (const auto& u : path) {
			vector<int> best;
			for (const auto& v : ad[u]) { 
				if (inPath[v]) continue;
				dfs2(v, u);
				best.push_back(f[v]);
			}
			sort(best.begin(), best.end(), greater<>());
			for (const auto& x : best) value.push_back({x, it + 1}); 
			it += 1;
		}
	}
	
	for (int i = 0; i < value.size(); ++i) value[i].first += value.size() - i;
 
	auto chk = [&](int mid) {
		int it = 0;
		for (const auto& [val, d] : value) { 
			if (it + val > mid) { 
				if (it >= d) return false;
				it += 1;
			}
		}
		return it <= mid;
	};
 
	int l = 0, r = n * 2, it = 0;
	while (l <= r) { 
		int mid = l + r >> 1;
		if (chk(mid)) r = (it = mid) - 1;
		else l = mid + 1;
	}
	cout << it << "\n";
} 

Compilation message

mousetrap.cpp: In function 'int32_t main()':
mousetrap.cpp:65:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |  for (int i = 0; i < value.size(); ++i) value[i].first += value.size() - i;
      |                  ~~^~~~~~~~~~~~~~
mousetrap.cpp:80:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   80 |   int mid = l + r >> 1;
      |             ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 26460 KB Output is correct
2 Correct 5 ms 26460 KB Output is correct
3 Correct 5 ms 26604 KB Output is correct
4 Correct 7 ms 26600 KB Output is correct
5 Correct 4 ms 26460 KB Output is correct
6 Correct 4 ms 26460 KB Output is correct
7 Correct 6 ms 26460 KB Output is correct
8 Correct 5 ms 26460 KB Output is correct
9 Correct 5 ms 26432 KB Output is correct
10 Correct 5 ms 26600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 228 ms 73272 KB Output is correct
2 Correct 197 ms 68896 KB Output is correct
3 Correct 654 ms 74324 KB Output is correct
4 Correct 230 ms 51288 KB Output is correct
5 Correct 674 ms 74388 KB Output is correct
6 Correct 622 ms 74568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 26460 KB Output is correct
2 Correct 5 ms 26460 KB Output is correct
3 Correct 5 ms 26604 KB Output is correct
4 Correct 7 ms 26600 KB Output is correct
5 Correct 4 ms 26460 KB Output is correct
6 Correct 4 ms 26460 KB Output is correct
7 Correct 6 ms 26460 KB Output is correct
8 Correct 5 ms 26460 KB Output is correct
9 Correct 5 ms 26432 KB Output is correct
10 Correct 5 ms 26600 KB Output is correct
11 Correct 5 ms 26460 KB Output is correct
12 Correct 6 ms 26460 KB Output is correct
13 Correct 4 ms 26584 KB Output is correct
14 Correct 5 ms 26716 KB Output is correct
15 Correct 5 ms 26460 KB Output is correct
16 Correct 4 ms 26460 KB Output is correct
17 Correct 4 ms 26460 KB Output is correct
18 Correct 5 ms 26460 KB Output is correct
19 Correct 4 ms 26604 KB Output is correct
20 Correct 5 ms 26460 KB Output is correct
21 Correct 5 ms 26460 KB Output is correct
22 Correct 6 ms 26460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 26460 KB Output is correct
2 Correct 5 ms 26460 KB Output is correct
3 Correct 5 ms 26604 KB Output is correct
4 Correct 7 ms 26600 KB Output is correct
5 Correct 4 ms 26460 KB Output is correct
6 Correct 4 ms 26460 KB Output is correct
7 Correct 6 ms 26460 KB Output is correct
8 Correct 5 ms 26460 KB Output is correct
9 Correct 5 ms 26432 KB Output is correct
10 Correct 5 ms 26600 KB Output is correct
11 Correct 228 ms 73272 KB Output is correct
12 Correct 197 ms 68896 KB Output is correct
13 Correct 654 ms 74324 KB Output is correct
14 Correct 230 ms 51288 KB Output is correct
15 Correct 674 ms 74388 KB Output is correct
16 Correct 622 ms 74568 KB Output is correct
17 Correct 5 ms 26460 KB Output is correct
18 Correct 6 ms 26460 KB Output is correct
19 Correct 4 ms 26584 KB Output is correct
20 Correct 5 ms 26716 KB Output is correct
21 Correct 5 ms 26460 KB Output is correct
22 Correct 4 ms 26460 KB Output is correct
23 Correct 4 ms 26460 KB Output is correct
24 Correct 5 ms 26460 KB Output is correct
25 Correct 4 ms 26604 KB Output is correct
26 Correct 5 ms 26460 KB Output is correct
27 Correct 5 ms 26460 KB Output is correct
28 Correct 6 ms 26460 KB Output is correct
29 Correct 4 ms 26460 KB Output is correct
30 Correct 228 ms 73344 KB Output is correct
31 Correct 203 ms 73496 KB Output is correct
32 Correct 242 ms 138180 KB Output is correct
33 Correct 322 ms 214476 KB Output is correct
34 Correct 621 ms 74324 KB Output is correct
35 Correct 588 ms 74292 KB Output is correct
36 Correct 610 ms 82888 KB Output is correct
37 Correct 584 ms 83016 KB Output is correct
38 Correct 434 ms 84812 KB Output is correct
39 Correct 519 ms 84812 KB Output is correct
40 Correct 472 ms 84800 KB Output is correct