#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1000;
const int INF = 987654321;
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][MAXN+10];
void dfs3(int now, int bef)
{
if(now==R) return;
for(int i=0; i<=N; i++) dp[now][i]=INF;
if(P[now])
{
vector<int> V[MAXN+10];
int pnode=-1;
int chd=0;
for(int i=0; i<=N; i++) V[i].push_back(0);
for(int nxt : adj[now])
{
if(nxt==bef) continue;
if(P[nxt])
{
pnode=nxt;
dfs3(nxt, now);
continue;
}
chd++;
dfs3(nxt, now);
for(int i=0; i<=N; i++) V[i].push_back(dp[nxt][i]);
}
assert(pnode!=-1);
for(int i=0; i<=N; i++) sort(V[i].begin(), V[i].end(), greater<int>());
for(int i=0; i<N; i++) for(int j=0; j<V[i].size(); j++)
{
if(i+j<=N) dp[now][i+j]=min(dp[now][i+j], max(V[i+1][j], dp[pnode][i+1]+j));
}
for(int i=chd; i<=N; i++) dp[now][i]=min(dp[now][i], dp[pnode][i-chd+1]);
}
else
{
vector<int> V[MAXN+10];
int chd=0;
for(int nxt : adj[now])
{
if(nxt==bef) continue;
chd++;
dfs3(nxt, now);
for(int i=0; i<=N; i++) V[i].push_back(dp[nxt][i]);
}
for(int i=0; i<=N; i++) sort(V[i].begin(), V[i].end(), greater<int>());
for(int i=0; i<N; i++) for(int j=0; j<V[i].size(); j++)
{
if(i+j<=N) dp[now][i+j]=min(dp[now][i+j], V[i+1][j]);
}
for(int i=chd; i<=N; i++) dp[now][i]=min(dp[now][i], val[now]);
}
//printf("%d : ", now);
//for(int i=0; i<=N; i++) printf("%d ", dp[now][i]); printf("\n");
}
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;
}
}
dfs3(X, X);
printf("%d\n", dp[X][1]);
//dfs(X, R, adj[X].size());
//printf("%d\n", dp[X]-1);
}
Compilation message
mousetrap.cpp: In function 'void dfs3(int, int)':
mousetrap.cpp:78:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i=0; i<N; i++) for(int j=0; j<V[i].size(); j++)
| ~^~~~~~~~~~~~
mousetrap.cpp:97:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for(int i=0; i<N; i++) for(int j=0; j<V[i].size(); j++)
| ~^~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:122:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
122 | for(int i=1; i<V.size(); i++)
| ~^~~~~~~~~
mousetrap.cpp:125:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | if(i+1<V.size())
| ~~~^~~~~~~~~
mousetrap.cpp:110:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
110 | scanf("%d%d%d", &N, &R, &X);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:114:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
114 | scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
620 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |