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>
#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);
vector<pii> edge(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);
edge[i] = {a,b};
}
function<int(int,int)> sizedfs = [&](int id, int last){
int ret = 1;
for(int i = 0; i < grafo[id].size(); i++){
if(grafo[id][i]!=last)
ret+=sizedfs(grafo[id][i],id);
}
return ret;
};
int c1, c2;
int resp = INF;
function<int(int,int)> dfs = [&](int id, int last){
int ret = 1;
for(int i = 0; i < grafo[id].size(); i++){
if(grafo[id][i]!=last){
int cur =dfs(grafo[id][i],id);
resp = min(resp,max(c2,max(cur,c1-cur))-min(c2,min(cur,c1-cur)));
ret+=cur;
}
}
return ret;
};
for(int i = 0; i < n-1; i++){
c1 = sizedfs(edge[i].ff,edge[i].ss);
c2 = sizedfs(edge[i].ss,edge[i].ff);
dfs(edge[i].ff,edge[i].ss);
swap(c1,c2);
dfs(edge[i].ss,edge[i].ff);
}
cout << resp << '\n';
return 0;
}
Compilation message (stderr)
papricice.cpp: In lambda function:
papricice.cpp:39:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 0; i < grafo[id].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
papricice.cpp: In lambda function:
papricice.cpp:49:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int i = 0; i < grafo[id].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... |