#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)
{
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);
}
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});
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:64:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int i = 0; i < g[v].size(); i++)
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 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 |
5 ms |
5228 KB |
Output is correct |
7 |
Correct |
5 ms |
5228 KB |
Output is correct |
8 |
Correct |
5 ms |
5228 KB |
Output is correct |
9 |
Correct |
5 ms |
5228 KB |
Output is correct |
10 |
Correct |
5 ms |
5248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 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 |
5 ms |
5228 KB |
Output is correct |
7 |
Correct |
5 ms |
5228 KB |
Output is correct |
8 |
Correct |
5 ms |
5228 KB |
Output is correct |
9 |
Correct |
5 ms |
5228 KB |
Output is correct |
10 |
Correct |
5 ms |
5248 KB |
Output is correct |
11 |
Correct |
412 ms |
24220 KB |
Output is correct |
12 |
Correct |
413 ms |
24204 KB |
Output is correct |
13 |
Correct |
374 ms |
24656 KB |
Output is correct |
14 |
Correct |
408 ms |
24300 KB |
Output is correct |
15 |
Correct |
415 ms |
24172 KB |
Output is correct |
16 |
Correct |
316 ms |
24040 KB |
Output is correct |
17 |
Correct |
325 ms |
24172 KB |
Output is correct |
18 |
Correct |
326 ms |
29376 KB |
Output is correct |
19 |
Correct |
352 ms |
24172 KB |
Output is correct |
20 |
Correct |
339 ms |
24092 KB |
Output is correct |