#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
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 |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
2 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
2 ms |
204 KB |
Output is correct |
6 |
Correct |
200 ms |
448 KB |
Output is correct |
7 |
Correct |
215 ms |
436 KB |
Output is correct |
8 |
Correct |
148 ms |
452 KB |
Output is correct |
9 |
Correct |
176 ms |
332 KB |
Output is correct |
10 |
Correct |
213 ms |
452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
2 ms |
204 KB |
Output is correct |
6 |
Correct |
200 ms |
448 KB |
Output is correct |
7 |
Correct |
215 ms |
436 KB |
Output is correct |
8 |
Correct |
148 ms |
452 KB |
Output is correct |
9 |
Correct |
176 ms |
332 KB |
Output is correct |
10 |
Correct |
213 ms |
452 KB |
Output is correct |
11 |
Execution timed out |
1086 ms |
15444 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |