This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 = st.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 != st.end())
{
int a = _sz[v];
int b = it2 -> first;
int c = n - a - b;
funct(a, b, c);
}
if(it2 != st.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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |