Submission #468208

#TimeUsernameProblemLanguageResultExecution timeMemory
468208idk321Mousetrap (CEOI17_mousetrap)C++17
100 / 100
935 ms272200 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1000005; vector<int> adj[N]; int upCost[N]; int downCost[N]; int p[N]; vector<vector<int>> sideNodes; int n, t, m; bool comp(int a, int b) { return downCost[a] < downCost[b]; } void dfs(int node, int par) { p[node] = par; for (int next : adj[node]) { if (next == par) continue; dfs(next, node); } } void calcDown(int node, int depth) { if (adj[node].size() == 1) { downCost[node] = depth; return; } if (adj[node].size() == 2) { downCost[node] = depth + 1; return; } array<int, 2> most{0, 0}; for (int next : adj[node]) { if (next == p[node]) continue; calcDown(next, depth + 1); if (downCost[next] > most[1]) most[1] = downCost[next]; if (most[0] < most[1]) swap(most[0], most[1]); } downCost[node] = adj[node].size() - 2 + most[1]; } void goUp(int node, int prev = -1) { if (node == t) return; goUp(p[node], node); upCost[node] += upCost[p[node]]; if (p[node] != t) { upCost[node] += adj[p[node]].size(); upCost[node] -= 2; } vector<int> curSideNodes; for (int next : adj[node]) { if (next == prev || next == p[node]) continue; curSideNodes.push_back(next); upCost[next] = upCost[node]; calcDown(next, 1); } sort(curSideNodes.rbegin(), curSideNodes.rend(), comp); sideNodes.push_back(curSideNodes); } bool poss(int most) { int already = 0; int canHave = 1; for (auto it = sideNodes.rbegin(); it != sideNodes.rend(); it++) { vector<int>& nodesVector = *it; int blocked = 0; for (int i : nodesVector) { if (already + downCost[i] + upCost[i] + nodesVector.size() - blocked - 1 > most) { if (already < canHave) { already++; blocked++; } else return false; } } canHave++; } return already <= most; } int bs() { int a = 0; int b = 2 * n; int res = -1; while (a <= b) { int mid =(a +b ) / 2; //cout << a<< " " << b << " "<< mid << " " << poss(mid) << "\n"; if (poss(mid)) { res = mid; b = mid - 1; } else a = mid + 1; } return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> t >> m; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } dfs(t, -1); goUp(m); /* for (int i = 1; i <= n; i++ )cout << upCost[i] << " "; cout << "\n"; for (int i = 1; i <= n; i++ )cout << downCost[i] << " "; cout << "\n"; */ cout << bs() << "\n"; } /* 10 1 4 1 2 2 3 2 4 3 9 3 5 4 7 4 6 6 8 7 10 */

Compilation message (stderr)

mousetrap.cpp: In function 'bool poss(int)':
mousetrap.cpp:76:86: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   76 |             if (already + downCost[i] + upCost[i] + nodesVector.size() - blocked - 1 > most) {
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...