Submission #851585

# Submission time Handle Problem Language Result Execution time Memory
851585 2023-09-20T07:41:08 Z vjudge1 Papričice (COCI20_papricice) C++17
0 / 110
2 ms 8024 KB
#pragma GCC optimize("unroll-loops,Ofast,O3")
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define spc << " " <<
#define endl "\n"
#define all(x) x.begin(), x.end()
#define ll long long
#define int long long
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define st first
#define nd second
#define inf 1000000009
#define MOD 998244353
using namespace std;




vi edges[200005];
int sub[200005], par[200005];
int n, ans1=0, ans2=0;
int lim;

void subby(int node){
    sub[node]=1;
    for(auto i:edges[node]){
        if(par[node]==i || i==0) continue;
        par[i]=node;
        subby(i);
        sub[node]+=sub[i];
    }
}

int diefes(int node){
    for(auto i:edges[node]){
        if(par[node]==i) continue;
        if(sub[i]>n/2) return diefes(i);
    }
    return node;
}


void dfs(int node){
    int flag=1;
    for(auto i:edges[node]){
        if(par[node]==i) continue;
        if(sub[i]>=lim){
            flag=0;
            dfs(i);
        }
    }
    if(flag){
        if(ans1==0) ans1=node;
        else ans2=node;
    }
}




void solve(){
    cin >> n;
    lim = (n-1)/3+1;
    for(int i=1; i<n; i++){
        int a, b;cin>>a>>b;
        edges[a].pb(b);
        edges[b].pb(a);
    }

    par[1]=0;
    subby(1);

    int cent = diefes(1);

    par[cent]=0;
    subby(cent);
    cerr << "**" spc cent spc "**\n";
    //for(int i=1; i<=n; i++) cerr << i spc sub[i] << endl;

    dfs(cent);
    cerr << ans1 spc ans2 << endl;

    int maxi=0, moxi=0;
    if(ans1==cent){
        for(auto i:edges[cent]){
            if(sub[i]>moxi){
                moxi = sub[i];
                ans1 = i;
            }
        }
        sub[ans1]=moxi;
    }
    if(ans2==0){
        for(auto i:edges[cent]) if(i!=ans1) maxi=max(maxi, sub[i]);
        sub[ans2]=maxi;
    }
    if(moxi/2>maxi){
        sub[ans1]=moxi/2;
        sub[ans2]=moxi/2 + (moxi%2);
    }

    int temp = sub[cent] - sub[ans1] - sub[ans2];
    cerr << sub[ans1] spc sub[ans2] spc temp << endl;
    int res = max(max(sub[ans2], temp), sub[ans1]) - min(min(sub[ans2], temp), sub[ans1]);
    cout << res << endl;

    
}



signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);
    #ifdef Local
    freopen("in","r",stdin);
    freopen("out","w",stdout);
    #endif


    ll t=1;
    //cin >> t;
    while(t--) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8024 KB Output is correct
2 Incorrect 2 ms 8024 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8024 KB Output is correct
2 Incorrect 2 ms 8024 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8024 KB Output is correct
2 Incorrect 2 ms 8024 KB Output isn't correct
3 Halted 0 ms 0 KB -