#include<bits/stdc++.h>
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
using namespace std;
template <typename T>
using matrix = vector<vector<T>>;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INFL = (1LL<<62)-1;
const int INF = (1<<30)-1;
const double EPS = 1e-7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6+1;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
matrix<int> grafo(n+1);
for(int i = 0; i < n-1; i++){
int a, b;
cin >> a >> b;
grafo[a].push_back(b);
grafo[b].push_back(a);
}
int resp = INF;
vector<set<int>> vs(n+1);
vector<int> sid(n+1);
iota(sid.begin(),sid.end(),0);
function<void(int,set<int>&)> getresp =[&](int c1,set<int>&s){
if(s.size() == 0)
return;
int c2 = n-c1;
auto it = s.lower_bound(c1>>1);
/*cout << s.size() << ": ";
for(int i : s)
cout << i << ' ';
cout << '\n';
cout << "getresp " << c1 << ' ' << c2 << ' ' << "=>" << *it;*/
resp = min(resp,(it==s.end() ? INF : max(c2,max(c1-*it,*it))-min(c2,min(c1-*it,*it))));
if(it!=s.begin()){
it--;
//cout << ' ' << *it << '\n';
resp = min(resp,(it==s.end() ? INF : max(c2,max(c1-*it,*it))-min(c2,min(c1-*it,*it))));
}// else cout << '\n';
//cout << "resp = " << resp << '\n';
};
function<int(int,int)> dfs = [&](int id, int last){
int ret = 1;
vector<int> csize(grafo[id].size());
int mainset = -1;
for(int i = 0; i < grafo[id].size(); i++){
if(grafo[id][i]!=last){
csize[i]=dfs(grafo[id][i],id);
ret+=csize[i];
if(vs[sid[grafo[id][i]]].size() > vs[sid[id]].size())
sid[id] = sid[grafo[id][i]], mainset = i;
}
}
//cout << "->" << id << endl;
if(mainset != -1){
//cout << "calc " << grafo[id][mainset] << '\n';
getresp(csize[mainset],vs[sid[id]]);
/*if(csize[mainset] < 0){
cout << "AAAAAAAAAAAAAAAAAAAAA\n";
}
cout << "insert(" << id << ") => " << csize[mainset] << '\n';*/
vs[sid[id]].insert(csize[mainset]);
}
for(int i = 0; i < grafo[id].size(); i++){
if(grafo[id][i]!=last && i !=mainset){
//cout << "calc " << grafo[id][i] << '\n';
getresp(csize[i],vs[sid[grafo[id][i]]]);
//cout << "calc " << grafo[id][i] << '\n';
getresp(n-csize[i],vs[sid[id]]);
for(int j : vs[sid[grafo[id][i]]]){
//cout << "calc " << grafo[id][i] << '\n';
getresp(n-j,vs[sid[id]]);
/*if(j < 0){
cout << "BBBBBBBBBBBBBBBBBBBBBB\n";
}
cout << "insert(" << id << ") => " << j << '\n';*/
vs[sid[id]].insert(j);
}
/*if(csize[i] < 0){
cout << "csize[" << i << "], ERRO\n";
}
cout << "insert(" << id << ") => " << csize[i] << '\n';*/
vs[sid[id]].insert(csize[i]);
}
}
//cout << "ret(" << id << ") = " << ret << ", resp = " << resp << '\n';
return ret;
};
dfs(1,0);
cout << resp << '\n';
return 0;
}
Compilation message
papricice.cpp: In lambda function:
papricice.cpp:63:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i = 0; i < grafo[id].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
papricice.cpp:81:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int i = 0; i < grafo[id].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Incorrect |
2 ms |
588 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Incorrect |
2 ms |
588 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |