# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
284290 | BeanZ | Mousetrap (CEOI17_mousetrap) | C++14 | 1140 ms | 81400 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];
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){
dp[u] = 0;
vector<ll> val;
for (auto j : node[u]){
if (j == p || j == ban) continue;
dfs(j, u);
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);
}
}
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);
ll ans = 0;
ll cnt = 0;
while (true){
if (m == t) break;
ll preans = 0;
dfs(m, par[m]);
ll cur = par[m];
preans = dp[m];
while (cur != t){
preans = preans + node[cur].size() - 2;
cur = par[cur];
}
//cout << preans << endl;
ans = max(ans, preans - (cnt > 0));
cnt++;
ban = m;
m = par[m];
}
cout << ans;
}
/*
13 13 1
1 2
2 3
2 4
1 5
5 6
5 7
1 8
8 9
8 10
1 11
11 12
11 13
10 1 4
1 2
2 3
2 4
3 9
3 5
4 7
4 6
6 8
7 10
14 4 1
1 2
2 5
2 6
5 7
5 8
8 10
6 9
1 3
3 14
3 11
11 12
11 13
1 4
12 12 1
1 2
1 3
3 4
4 5
4 6
3 7
7 8
7 9
3 10
10 11
10 12
*/
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... |