# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
284539 | BeanZ | Mousetrap (CEOI17_mousetrap) | C++14 | 1991 ms | 209560 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define endl '\n'
const int N = 3e6 + 5;
const int mod = 998244353;
ll n, t, m, ban = 0;
vector<ll> node[1000006];
ll dp[1000006], par[1000006], f[1000006], sz[1000006];
void dfsFirst(ll u, ll v){
par[u] = v;
for (auto j : node[u]){
if (j == v) continue;
dfsFirst(j, u);
f[u] |= f[j];
}
if (u == m) f[u] = 1;
}
void dfs(ll u, ll p, ll v){
dp[u] = 0;
sz[u] = v;
vector<ll> val;
for (auto j : node[u]){
if (j == p) continue;
dfs(j, u, v + node[j].size() - 2);
if (!f[j]) val.push_back(dp[j] + 1);
}
if (val.size()){
sort(val.begin(), val.end());
dp[u]++;
if (val.size() > 1) dp[u] += val[val.size() - 2];
dp[u] += max(0, (ll)val.size() - 2);
}
}
bool chk(ll mid){
//cout << endl << endl << endl;
//cout << mid << endl;
ll cur = m;
ll turn = 1, used = 0;
ll ban = 0;
while (cur != t){
ll cost = sz[par[cur]];
vector<ll> val;
for (auto j : node[cur]){
if (j == par[cur] || j == ban) continue;
val.push_back(dp[j]);
}
sort(val.begin(), val.end());
for (int i = val.size() - 1; i >= 0; i--){
//cout << (val[i] + 1 + cost + used + i) << " " << cur << endl;
if ((val[i] + 1 + cost + used + i) > mid){
if ((used + 1) > mid) return false;
if (turn){
turn--;
used++;
} else {
return false;
}
}
}
turn++;
ban = cur;
cur = par[cur];
}
return true;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("MOUSETRAP.INP", "r")){
freopen("MOUSETRAP.INP", "r", stdin);
freopen("MOUSETRAP.OUT", "w", stdout);
}
cin >> n >> t >> m;
for (int i = 1; i < n; i++){
ll u, v;
cin >> u >> v;
node[u].push_back(v);
node[v].push_back(u);
}
dfsFirst(t, t);
dfs(t, t, 0);
ll l = 0, h = 1e9;
while (l <= h){
ll mid = (l + h) >> 1;
if (chk(mid)) h = mid - 1;
else l = mid + 1;
}
cout << l;
}
/*
10 2 10
2 1
3 2
4 2
5 3
6 3
7 4
8 4
9 5
10 5
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |