#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define F first
#define S second
#define mp make_pair
using namespace std;
const int tam=200005;
vi G[tam];
int sz[tam];
multiset<int> todos;
int res=1e9;
void init(int nodo, int ant){
sz[nodo]=1;
for(auto x:G[nodo]){
if(x!=ant){
init(x,nodo);
sz[nodo]+=sz[x];
}
}
}
int n;
void dfs(int nodo, int ant){
for(auto it : G[nodo]){
if(it==ant)continue;
int tami=sz[it];
int falta=n-tami;
auto it2=todos.lower_bound((n-sz[it])/2);
if(it2!=todos.end()){
int val=(*it2);
int ahora=max({val,falta-val,tami})-min({val,falta-val,tami});
res=min(res,ahora);
}
if(it2!=todos.begin()){
it2--;
int val=(*it2);
int ahora=max({val,falta-val,tami})-min({val,falta-val,tami});
res=min(res,ahora);
}
dfs(it,nodo);
}
if(ant!=-1){
todos.insert(sz[nodo]);
}
}
int main(){
fast
int a,b;
cin>>n;
for(int i=1;i<n;i++){
cin>>a>>b;
G[a].pb(b);
G[b].pb(a);
}
for(int i=1;i<=10;i++){
todos.clear();
int cual=rand()%n+1;
init(cual,-1);
dfs(cual,-1);
}
cout<<res<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4932 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4932 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
6 ms |
5160 KB |
Output is correct |
7 |
Correct |
5 ms |
5164 KB |
Output is correct |
8 |
Correct |
6 ms |
5204 KB |
Output is correct |
9 |
Correct |
6 ms |
5144 KB |
Output is correct |
10 |
Correct |
6 ms |
5116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4932 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
6 ms |
5160 KB |
Output is correct |
7 |
Correct |
5 ms |
5164 KB |
Output is correct |
8 |
Correct |
6 ms |
5204 KB |
Output is correct |
9 |
Correct |
6 ms |
5144 KB |
Output is correct |
10 |
Correct |
6 ms |
5116 KB |
Output is correct |
11 |
Execution timed out |
1076 ms |
21680 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |