/*
ID: noszaly1
TASK: {TASK}
LANG: C++11
*/
//Noszály Áron 10o Debreceni Fazekas Mihály Gimnázium
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cassert>
#include<cassert>
#include<unordered_map>
#include<unordered_set>
#include<functional>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<sstream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<numeric>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define xx first
#define yy second
#define sz(x) (int)(x).size()
#define gc getchar
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mp make_pair
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double PI=acos(-1);
template<typename T> T getint() {
T val=0;
char c;
bool neg=false;
while((c=gc()) && !(c>='0' && c<='9')) {
neg|=c=='-';
}
do {
val=(val*10)+c-'0';
} while((c=gc()) && (c>='0' && c<='9'));
return val*(neg?-1:1);
}
const int N=1000001;
int n,t,m;
vector<int> adj[N];
int dp[N], dpo[N];
int par1[N], b1[N];
bool ut[N];
vector<int> lst;
void dfs1(int x) {
if(x==t) {
int akt=x;
ut[m]=true;
while(akt!=m) {
lst.pb(akt);
ut[akt]=true;
akt=par1[akt];
}
lst.pb(m);
reverse(all(lst));
return ;
}
b1[x]=1;
for(auto i:adj[x]) {
if(!b1[i]) {
par1[i]=x;
dfs1(i);
}
}
b1[x]=2;
}
int par2[N], b2[N];
void dfs_calc(int x) {
vector<int> sub;
b2[x]=1;
for(auto i:adj[x]) {
if(!b2[i]) {
sub.pb(i);
par2[i]=x;
dfs_calc(i);
}
}
b2[x]=2;
if(sz(sub)==0) {
dp[x]=1 ;
}else if(sz(sub)==1) {
dp[x]=2;
}else {
int mx=-1, mx2=-1;
for(auto i:sub) {
if(dp[i]>=mx) {
mx2=mx;
mx=dp[i];
}else if(dp[i]>mx2) {
mx2=dp[i];
}
}
dp[x]=sz(sub)-2+mx2+2;
}
}
void dfs2(int x) {
if(x==t) return ;
b2[x]=1;
for(auto i:adj[x]) {
if(b2[i]) continue ;
par2[i]=x;
if(!ut[i]) {
dfs_calc(i);
}else {
dfs2(i);
}
}
b2[x]=2;
}
int main() {
IO;
cin>>n>>t>>m;
for(int i=0;i<n-1;++i) {
int a,b;
cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
dfs1(m);
dfs2(m);
int mx=0;
int cnt=0;
for(int i=sz(lst)-2;i>=0;i--) {
for(auto j:adj[lst[i]]) {
if(!ut[j]) cnt++;
}
for(auto j:adj[lst[i]]) {
if(!ut[j]) {
dp[j]+=cnt-1;
mx=max(mx, dp[j]);
}
}
}
int L=0, R=3000000;
while(L<R) {
int mid=(L+R+1)/2;
int cnt=0, cans=0;
bool ok=true;
for(auto j:lst) {
if(j==t) break ;
cnt++;
int curr=0;
for(auto k:adj[j]) {
if(!ut[k] && dp[k]+cans-curr>=mid) {
cnt--;
cans++;
curr++;
}
if(cnt<0) break;
}
if(cnt<0) {
ok=false;
break ;
}
}
if(!ok) {
L=mid;
}else {
R=mid-1;
}
}
int mid=L;
cnt=0; int cans=0;
for(auto j:lst) {
if(j==t) break ;
cnt++;
int curr=0;
for(auto k:adj[j]) {
if(!ut[k] && dp[k]+cans-curr>=mid) {
cnt--;
cans++;
curr++;
}
}
}
cout<<max(L,cans)<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
23940 KB |
Output is correct |
3 |
Correct |
27 ms |
24016 KB |
Output is correct |
4 |
Correct |
22 ms |
24016 KB |
Output is correct |
5 |
Correct |
23 ms |
24016 KB |
Output is correct |
6 |
Correct |
28 ms |
24016 KB |
Output is correct |
7 |
Correct |
24 ms |
24072 KB |
Output is correct |
8 |
Correct |
23 ms |
24092 KB |
Output is correct |
9 |
Correct |
31 ms |
24184 KB |
Output is correct |
10 |
Correct |
22 ms |
24184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
599 ms |
75172 KB |
Output is correct |
2 |
Correct |
460 ms |
82120 KB |
Output is correct |
3 |
Correct |
1739 ms |
101516 KB |
Output is correct |
4 |
Correct |
764 ms |
101516 KB |
Output is correct |
5 |
Correct |
1713 ms |
121340 KB |
Output is correct |
6 |
Correct |
1548 ms |
134352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
23940 KB |
Output is correct |
3 |
Correct |
27 ms |
24016 KB |
Output is correct |
4 |
Correct |
22 ms |
24016 KB |
Output is correct |
5 |
Correct |
23 ms |
24016 KB |
Output is correct |
6 |
Correct |
28 ms |
24016 KB |
Output is correct |
7 |
Correct |
24 ms |
24072 KB |
Output is correct |
8 |
Correct |
23 ms |
24092 KB |
Output is correct |
9 |
Correct |
31 ms |
24184 KB |
Output is correct |
10 |
Correct |
22 ms |
24184 KB |
Output is correct |
11 |
Correct |
26 ms |
134352 KB |
Output is correct |
12 |
Correct |
26 ms |
134352 KB |
Output is correct |
13 |
Correct |
22 ms |
134352 KB |
Output is correct |
14 |
Correct |
23 ms |
134352 KB |
Output is correct |
15 |
Correct |
27 ms |
134352 KB |
Output is correct |
16 |
Correct |
30 ms |
134352 KB |
Output is correct |
17 |
Correct |
29 ms |
134352 KB |
Output is correct |
18 |
Correct |
25 ms |
134352 KB |
Output is correct |
19 |
Correct |
27 ms |
134352 KB |
Output is correct |
20 |
Correct |
27 ms |
134352 KB |
Output is correct |
21 |
Correct |
28 ms |
134352 KB |
Output is correct |
22 |
Correct |
39 ms |
134352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
23940 KB |
Output is correct |
3 |
Correct |
27 ms |
24016 KB |
Output is correct |
4 |
Correct |
22 ms |
24016 KB |
Output is correct |
5 |
Correct |
23 ms |
24016 KB |
Output is correct |
6 |
Correct |
28 ms |
24016 KB |
Output is correct |
7 |
Correct |
24 ms |
24072 KB |
Output is correct |
8 |
Correct |
23 ms |
24092 KB |
Output is correct |
9 |
Correct |
31 ms |
24184 KB |
Output is correct |
10 |
Correct |
22 ms |
24184 KB |
Output is correct |
11 |
Correct |
599 ms |
75172 KB |
Output is correct |
12 |
Correct |
460 ms |
82120 KB |
Output is correct |
13 |
Correct |
1739 ms |
101516 KB |
Output is correct |
14 |
Correct |
764 ms |
101516 KB |
Output is correct |
15 |
Correct |
1713 ms |
121340 KB |
Output is correct |
16 |
Correct |
1548 ms |
134352 KB |
Output is correct |
17 |
Correct |
26 ms |
134352 KB |
Output is correct |
18 |
Correct |
26 ms |
134352 KB |
Output is correct |
19 |
Correct |
22 ms |
134352 KB |
Output is correct |
20 |
Correct |
23 ms |
134352 KB |
Output is correct |
21 |
Correct |
27 ms |
134352 KB |
Output is correct |
22 |
Correct |
30 ms |
134352 KB |
Output is correct |
23 |
Correct |
29 ms |
134352 KB |
Output is correct |
24 |
Correct |
25 ms |
134352 KB |
Output is correct |
25 |
Correct |
27 ms |
134352 KB |
Output is correct |
26 |
Correct |
27 ms |
134352 KB |
Output is correct |
27 |
Correct |
28 ms |
134352 KB |
Output is correct |
28 |
Correct |
39 ms |
134352 KB |
Output is correct |
29 |
Correct |
22 ms |
134352 KB |
Output is correct |
30 |
Correct |
614 ms |
146784 KB |
Output is correct |
31 |
Correct |
638 ms |
160148 KB |
Output is correct |
32 |
Correct |
694 ms |
236892 KB |
Output is correct |
33 |
Correct |
748 ms |
307904 KB |
Output is correct |
34 |
Correct |
1783 ms |
307904 KB |
Output is correct |
35 |
Correct |
1706 ms |
307904 KB |
Output is correct |
36 |
Correct |
1759 ms |
307904 KB |
Output is correct |
37 |
Correct |
1903 ms |
307904 KB |
Output is correct |
38 |
Correct |
1460 ms |
307904 KB |
Output is correct |
39 |
Correct |
1554 ms |
307904 KB |
Output is correct |
40 |
Correct |
1554 ms |
307904 KB |
Output is correct |