#pragma GCC diagnostic warning "-std=c++11"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define MOD 1000000007
#define flush fflush(stdout)
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define pii pair<int,int>
using namespace std;
const int N=1e6+5;
int n,m,T,k,s,t,fix[N],dp[N];
vector<int> g[N];
void solve(int x, int p) {
vector<int> v;
for (auto y:g[x]) {
if (y==p) continue;
solve(y,x); v.pb(dp[y]);
}
int sz=v.size();
sort(allr(v));
if (sz==0) dp[x]=0;
else if (sz==1) dp[x]=1;
else dp[x]=sz+v[1];
}
vector<int> v;
bool found=0;
void dfs(int x, int p) {
cout<<x<<endl;
v.pb(x);
if (x==t) {
found=1; return;
}
for (auto y:g[x]) {
if (y==p) continue;
dfs(y,x);
if (found==1) return;
}
v.pop_back();
}
void test_case() {
scanf("%d%d%d",&n,&t,&s);
for (int i=1; i<=n-1; i++) {
int a,b; scanf("%d%d",&a,&b);
g[a].pb(b); g[b].pb(a);
}
dfs(s,s);
for (auto x:v) fix[x]=1;
int sz=v.size();
int l=0,r=n,ans=0;
while (l<=r) {
int mid=(l+r)/2;
// printf("MID %d\n",mid);
int cnt=0;
for (int i=0; i<sz; i++) {
int x=v[i];
for (auto y:g[x]) {
if (fix[y]) continue;
cnt++;
}
}
int val=1;
bool check=1;
for (int i=0; i<sz; i++) {
int x=v[i];
vector<int> c;
for (auto y:g[x]) {
if (fix[y]) continue;
solve(y,x); c.pb(dp[y]);
// printf("Y %d %d\n",y,dp[y]);
}
sort(allr(c));
for (int j=0; j<c.size(); j++) {
if (c[j]+cnt<=mid) {
break;
}
val--;
}
if (cnt>mid) val-=1e9;
if (val<0) {
check=0; break;
}
val++;
int sz1=c.size(); cnt-=sz1;
}
// printf("check %d\n",check);
if (check) {
ans=mid; r=mid-1;
}
else {
l=mid+1;
}
}
printf("%d\n",ans);
}
main () {
ios :: sync_with_stdio(0);
cin.tie(0); cout.tie(0);
T=1;
while (T--) test_case();
}
Compilation message (stderr)
mousetrap.cpp:1:32: warning: '-std=c++11' is not an option that controls warnings [-Wpragmas]
1 | #pragma GCC diagnostic warning "-std=c++11"
| ^~~~~~~~~~~~
mousetrap.cpp:101:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
101 | main () {
| ^~~~
mousetrap.cpp: In function 'void test_case()':
mousetrap.cpp:46:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%d%d%d",&n,&t,&s);
| ~~~~~^~~~~~~~~~~~~~~~~~~
mousetrap.cpp:48:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | int a,b; scanf("%d%d",&a,&b);
| ~~~~~^~~~~~~~~~~~~~
# | 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... |