Submission #851573

# Submission time Handle Problem Language Result Execution time Memory
851573 2023-09-20T07:24:34 Z vjudge1 Papričice (COCI20_papricice) C++17
0 / 110
2 ms 8028 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;

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

    int temp = sub[cent] - sub[ans1] - sub[ans2];
    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 8028 KB Output is correct
2 Incorrect 2 ms 8028 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8028 KB Output is correct
2 Incorrect 2 ms 8028 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8028 KB Output is correct
2 Incorrect 2 ms 8028 KB Output isn't correct
3 Halted 0 ms 0 KB -