#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define mp make_pair
#define pb push_back
#define se second
#define fi first
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
ll set_on(ll n, ll k){
return (n |= 1 << k);
}
ll set_off(ll n, ll k){
return (n &= ~(1UL << k));
}
bool check_bit(ll n, ll k){
int bit = (n >> k) & 1U;
if(bit == 1)
return true;
return false;
}
int n;
vi G[200100];
int ans = 1e9, sum[200100], depth[200100];
pii d1, d2;
bool check(int a, int b){
if(d1.fi == a && d1.se == b) return true;
if(d1.fi == b && d1.se == a) return true;
if(d2.fi == a && d2.se == b) return true;
if(d2.se == a && d2.fi == b) return true;
return false;
}
void get_depth(int at, int prev, int dep){
depth[at] = dep;
for(auto next : G[at]){
if(next == prev) continue;
get_depth(next, at, dep + 1);
}
}
void dfs(int at, int prev){
sum[at] = 1;
for(auto next : G[at]){
if(next == prev) continue;
if(check(at, next)){
dfs(next, at);
continue;
}
else{
dfs(next, at);
sum[at] += sum[next];
}
}
}
bool comp(int& A, int& B){
if(depth[A] >= depth[B]) return false;
return true;
}
void solve(){
cin>>n;
vector<pii> edge;
for(int i = 1; i < n; i++){
int a, b;
cin>>a>>b;
a--; b--;
G[a].pb(b);
G[b].pb(a);
edge.pb(mp(a,b));
}
get_depth(0, -1, 0);
for(int i = 0; i < edge.size(); i++){
for(int j = i + 1; j < edge.size(); j++){
d1 = edge[i];
d2 = edge[j];
dfs(0, -1);
int a = sum[0], b = (depth[edge[i].fi] < depth[edge[i].se]) ? edge[i].se : edge[i].fi;
int c = (depth[edge[j].fi] < depth[edge[j].se]) ? edge[j].se : edge[j].fi;
b = sum[b];
c = sum[c];
int maks = max(a, max(b,c));
int mins = min(a, min(b,c));
ans = min(ans, maks - mins);
}
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
cout<<fixed;
startTime = clock();
int t=1;
// cin>>t;
while(t--)
solve();
return 0;
}
Compilation message
papricice.cpp: In function 'void solve()':
papricice.cpp:103:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for(int i = 0; i < edge.size(); i++){
| ~~^~~~~~~~~~~~~
papricice.cpp:104:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int j = i + 1; j < edge.size(); j++){
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
5072 KB |
Output is correct |
2 |
Correct |
40 ms |
5024 KB |
Output is correct |
3 |
Correct |
38 ms |
4948 KB |
Output is correct |
4 |
Correct |
37 ms |
5020 KB |
Output is correct |
5 |
Correct |
37 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
5072 KB |
Output is correct |
2 |
Correct |
40 ms |
5024 KB |
Output is correct |
3 |
Correct |
38 ms |
4948 KB |
Output is correct |
4 |
Correct |
37 ms |
5020 KB |
Output is correct |
5 |
Correct |
37 ms |
4948 KB |
Output is correct |
6 |
Execution timed out |
1069 ms |
5032 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
5072 KB |
Output is correct |
2 |
Correct |
40 ms |
5024 KB |
Output is correct |
3 |
Correct |
38 ms |
4948 KB |
Output is correct |
4 |
Correct |
37 ms |
5020 KB |
Output is correct |
5 |
Correct |
37 ms |
4948 KB |
Output is correct |
6 |
Execution timed out |
1069 ms |
5032 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |