#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;
//ifstream cin("C:\\Users\\Leonardo\\Downloads\\contest1_testdata\\papricice\\papricice.in.2b");
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);
resp = min(resp,(it==s.end() ? INF : max(c2,max(c1-*it,*it))-min(c2,min(c1-*it,*it))));
if(it!=s.begin()){
it--;
resp = min(resp,(it==s.end() ? INF : max(c2,max(c1-*it,*it))-min(c2,min(c1-*it,*it))));
}
};
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;
}
}
if(mainset != -1){
getresp(csize[mainset],vs[sid[id]]);
vs[sid[id]].insert(csize[mainset]);
}
for(int i = 0; i < grafo[id].size(); i++){
if(grafo[id][i]!=last && i !=mainset){
getresp(csize[i],vs[sid[grafo[id][i]]]);
getresp(n-csize[i],vs[sid[id]]);
for(int j : vs[sid[grafo[id][i]]]){
getresp(n-j,vs[sid[id]]);
}
for(int j : vs[sid[grafo[id][i]]]){
vs[sid[id]].insert(j);
}
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:59:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i = 0; i < grafo[id].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
papricice.cpp:72:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | 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 |
332 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 |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
3 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
588 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 |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
3 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
588 KB |
Output is correct |
11 |
Correct |
190 ms |
30212 KB |
Output is correct |
12 |
Correct |
213 ms |
32160 KB |
Output is correct |
13 |
Correct |
160 ms |
26948 KB |
Output is correct |
14 |
Correct |
169 ms |
28608 KB |
Output is correct |
15 |
Correct |
214 ms |
31820 KB |
Output is correct |
16 |
Correct |
113 ms |
24276 KB |
Output is correct |
17 |
Correct |
181 ms |
30336 KB |
Output is correct |
18 |
Correct |
184 ms |
43000 KB |
Output is correct |
19 |
Correct |
175 ms |
29804 KB |
Output is correct |
20 |
Correct |
200 ms |
32340 KB |
Output is correct |