/*
In the name of Allah
made by: Ahmed_Solyman
*/
#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
#pragma GCC optimize("-Ofast")
#pragma GCC optimize("-O1")
//-------------------------------------------------------------//
typedef long long ll;
typedef unsigned long long ull;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI acos(-1)
#define lb lower_bound
#define ub upper_bound
#define endl '\n'
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define sum_to(n) (n*(n+1))/2
#define pb push_back
#define pf push_front
#define fil(arr,x) memset(arr,x,sizeof(arr))
const ll mod=1e9+7;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
//-------------------------------------------------------------//
ll lcm(ll a,ll b)
{
return (max(a,b)/__gcd(a,b))*min(a,b);
}
void person_bool(bool x)
{
cout<<(x?"YES":"NO")<<endl;
}
const int N=2e5+5;
vector<int>adj[N];
int P[N][18],depth[N],n,subtree[N];
int dfs(int node,int parent){
P[node][0]=parent;
subtree[node]=1;
depth[node]=depth[parent]+1;
for(int k=1;k<18;k++){
P[node][k]=P[P[node][k-1]][k-1];
}
for(auto i:adj[node]){
if(i!=parent){
subtree[node]+=dfs(i,node);
}
}
return subtree[node];
}
int lca(int a,int b){
if(depth[a]<depth[b])swap(a,b);
for(int k=17;k>=0;k--){
if(depth[a]-(1<<k)>=depth[b]){
a=P[a][k];
}
}
if(a==b){
return a;
}
for(int k=17;k>=0;k--){
if(P[a][k]!=P[b][k]){
a=P[a][k];
b=P[b][k];
}
}
return P[a][0];
}
int ans=N;
set<int> solve1(int node,int p){ /// for those whose lca is one of the,
set<int>s;
for(auto i:adj[node]){
if(i!=p){
set<int>u=solve1(i,node);
if(u.size()>s.size())swap(u,s);
for(auto p:u)s.insert(p);
}
}
// n-subtree[node] , subtree[node]-subtree[ch] , subtree[ch]
auto iter=s.lower_bound(subtree[node]>>1);
if(iter!=s.end()){
int ch=*iter;
ans=min(ans,max({n-subtree[node],subtree[node]-ch,ch})-min({n-subtree[node],subtree[node]-ch,ch}));
}
if(iter!=s.begin()){
int ch=*(--iter);
ans=min(ans,max({n-subtree[node],subtree[node]-ch,ch})-min({n-subtree[node],subtree[node]-ch,ch}));
}
s.insert(subtree[node]);
return s;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
#ifndef ONLINE_JUDGE
// freopen("input.in", "r", stdin);
// freopen("output.out", "w", stdout);
#endif
fast
cin>>n;
for(int i=0;i<n-1;i++){
int u,v;cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1,1);
solve1(1,1);
for(int i=2;i<=n;i++){
for(int j=2;j<=n;j++){
if(i==j){
continue;
}
int k=lca(i,j);
if(k==i || k==j){
continue;
}
else{
ans=min(ans,max({n-(subtree[i]+subtree[j]),subtree[i],subtree[j]})-min({n-(subtree[i]+subtree[j]),subtree[i],subtree[j]}));
}
}
}
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8284 KB |
Output is correct |
2 |
Correct |
5 ms |
8284 KB |
Output is correct |
3 |
Correct |
5 ms |
8284 KB |
Output is correct |
4 |
Correct |
5 ms |
8332 KB |
Output is correct |
5 |
Correct |
5 ms |
8284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8284 KB |
Output is correct |
2 |
Correct |
5 ms |
8284 KB |
Output is correct |
3 |
Correct |
5 ms |
8284 KB |
Output is correct |
4 |
Correct |
5 ms |
8332 KB |
Output is correct |
5 |
Correct |
5 ms |
8284 KB |
Output is correct |
6 |
Correct |
384 ms |
8536 KB |
Output is correct |
7 |
Correct |
404 ms |
8284 KB |
Output is correct |
8 |
Correct |
364 ms |
8376 KB |
Output is correct |
9 |
Correct |
371 ms |
8284 KB |
Output is correct |
10 |
Correct |
396 ms |
8384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8284 KB |
Output is correct |
2 |
Correct |
5 ms |
8284 KB |
Output is correct |
3 |
Correct |
5 ms |
8284 KB |
Output is correct |
4 |
Correct |
5 ms |
8332 KB |
Output is correct |
5 |
Correct |
5 ms |
8284 KB |
Output is correct |
6 |
Correct |
384 ms |
8536 KB |
Output is correct |
7 |
Correct |
404 ms |
8284 KB |
Output is correct |
8 |
Correct |
364 ms |
8376 KB |
Output is correct |
9 |
Correct |
371 ms |
8284 KB |
Output is correct |
10 |
Correct |
396 ms |
8384 KB |
Output is correct |
11 |
Execution timed out |
1059 ms |
27224 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |