#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e6;
int N, R, X;
vector<int> adj[MAXN+10];
vector<int> V;
bool P[MAXN+10];
int val[MAXN+10];
int dfs(int now, int bef)
{
V.push_back(now);
if(now==X) return 1;
for(int nxt : adj[now])
{
if(nxt==bef) continue;
if(dfs(nxt, now)) return 1;
}
V.pop_back();
return 0;
}
void dfs2(int now, int bef)
{
int chd=0;
for(int nxt : adj[now])
{
if(nxt==bef) continue;
if(P[nxt]) continue;
chd++;
}
val[now]=val[bef]+chd;
for(int nxt : adj[now])
{
if(nxt==bef) continue;
if(P[nxt]) continue;
dfs2(nxt, now);
}
}
int dp[MAXN+10];
void dfs3(int now, int bef)
{
int a=0, b=0, cnt=0;
for(int nxt : adj[now])
{
if(nxt==bef) continue;
if(P[nxt]) continue;
dfs3(nxt, now);
cnt++;
if(a<=dp[nxt]) b=a, a=dp[nxt];
else if(b<=dp[nxt]) b=dp[nxt];
}
dp[now]=b;
if(cnt<=1) dp[now]=val[now];
}
int main()
{
scanf("%d%d%d", &N, &R, &X);
for(int i=1; i<N; i++)
{
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(R, R);
for(auto it : V) P[it]=1;
int t=0;
for(int i=1; i<V.size(); i++)
{
dfs2(V[i], V[i]);
if(i+1<V.size())
{
t+=adj[V[i]].size()-2;
val[V[i+1]]=t;
}
}
for(int i=1; i<V.size(); i++)
{
dfs3(V[i], V[i]);
dp[V[i]]=0;
int a=0, b=0, cnt=0;
for(int nxt : adj[V[i]])
{
if(P[nxt]) continue;
if(a<=dp[nxt]) b=a, a=dp[nxt];
else if(b<=dp[nxt]) b=dp[nxt];
}
dp[V[i]]=min(max(a, dp[V[i-1]]), max(b, dp[V[i-1]]+1));
}
printf("%d\n", dp[X]);
//dfs(X, R, adj[X].size());
//printf("%d\n", dp[X]-1);
}
Compilation message
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:80:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for(int i=1; i<V.size(); i++)
| ~^~~~~~~~~
mousetrap.cpp:83:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | if(i+1<V.size())
| ~~~^~~~~~~~~
mousetrap.cpp:90:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for(int i=1; i<V.size(); i++)
| ~^~~~~~~~~
mousetrap.cpp:95:17: warning: unused variable 'cnt' [-Wunused-variable]
95 | int a=0, b=0, cnt=0;
| ^~~
mousetrap.cpp:68:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
68 | scanf("%d%d%d", &N, &R, &X);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
72 | scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Correct |
17 ms |
23788 KB |
Output is correct |
4 |
Correct |
16 ms |
23788 KB |
Output is correct |
5 |
Incorrect |
16 ms |
23788 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
415 ms |
62956 KB |
Output is correct |
2 |
Correct |
391 ms |
58988 KB |
Output is correct |
3 |
Correct |
1024 ms |
64100 KB |
Output is correct |
4 |
Correct |
500 ms |
43884 KB |
Output is correct |
5 |
Correct |
1035 ms |
64108 KB |
Output is correct |
6 |
Correct |
1039 ms |
64016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Correct |
17 ms |
23788 KB |
Output is correct |
4 |
Correct |
16 ms |
23788 KB |
Output is correct |
5 |
Incorrect |
16 ms |
23788 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Correct |
17 ms |
23788 KB |
Output is correct |
4 |
Correct |
16 ms |
23788 KB |
Output is correct |
5 |
Incorrect |
16 ms |
23788 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |