#include<bits/stdc++.h>
using namespace std;
set <pair <int, int> > st, st1;
const int N = 2e5;
vector <int> g[N];
int _sz[N];
void build(int v, int p = -1)
{
_sz[v] = 1;
for(int i = 0; i < g[v].size(); i++)
{
int to = g[v][i];
if(to != p)
{
build(to, v);
_sz[v] += _sz[to];
}
}
}
int n;
int ans = 1e9;
void funct(int a, int b, int c)
{
int t = max(abs(a - b), max(abs(b - c), abs(a - c)));
ans = min(ans, t);
}
void dfs(int v, int p = -1)
{
st.insert({n - _sz[v], v});
for(int i = 0; i < g[v].size(); i++)
{
int to = g[v][i];
if(to != p)
{
dfs(to, v);
}
}
st.erase({n - _sz[v], v});
int sz1 = (n - _sz[v]) / 2;
set <pair <int, int> > :: iterator it1, it2;
it1 = st.lower_bound({sz1, 0});
it2 = st1.lower_bound({sz1, 0});
if(it1 != st.end())
{
int a = _sz[v];
int b = it1 -> first;
int c = n - a - b;
funct(a, b,c);
}
if(it1 != st.begin())
{
it1--;
int a = _sz[v];
int b = it1 -> first;
int c = n - a - b;
funct(a, b,c);
}
if(it2 != st1.end())
{
int a = _sz[v];
int b = it2 -> first;
int c = n - a - b;
funct(a, b, c);
}
if(it2 != st1.begin())
{
it2--;
int a = _sz[v];
int b = it2 -> first;
int c = n - a - b;
funct(a, b, c);
}
st1.insert({_sz[v], v});
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 0; i < n - 1; i++)
{
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
build(0);
dfs(0);
cout << ans;
return 0;
}
Compilation message
papricice.cpp: In function 'void build(int, int)':
papricice.cpp:10:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | for(int i = 0; i < g[v].size(); i++)
| ~~^~~~~~~~~~~~~
papricice.cpp: In function 'void dfs(int, int)':
papricice.cpp:30:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i = 0; i < g[v].size(); i++)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
6 ms |
5228 KB |
Output is correct |
7 |
Incorrect |
5 ms |
5228 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
6 ms |
5228 KB |
Output is correct |
7 |
Incorrect |
5 ms |
5228 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |