Submission #342371

# Submission time Handle Problem Language Result Execution time Memory
342371 2021-01-02T02:05:29 Z limabeans Papričice (COCI20_papricice) C++17
50 / 110
1000 ms 31212 KB
#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl







const int maxn = 2e5 + 10;
const int LOG = 20;

int n;
vector<int> g[maxn];
int par[LOG+1][maxn];
int dep[maxn];
int siz[maxn];

void hack(int at, int p=0) {
    for (int i=1; i<LOG; i++) {
	par[i][at] = par[i-1][par[i-1][at]];
    }

    siz[at] = 1;
    
    for (int to: g[at]) {
	if (to==p) continue;
	dep[to]=1+dep[at];
	par[0][to]=at;
	hack(to, at);
	siz[at] += siz[to];
    }
}


int lca(int u, int v) {
    if (dep[u] < dep[v]) swap(u,v);
    int dx = dep[u]-dep[v];
    for (int i=0; i<LOG; i++) {
	if (dx>>i&1) {
	    u = par[i][u];
	}
    }
    if (u==v) return u;

    for (int j=LOG-1; j>=0; j--) {
	if (par[j][u]!=par[j][v]) {
	    u = par[j][u];
	    v = par[j][v];
	}
    }

    return par[0][u];
}

int get(vector<int> a) {
    sort(a.begin(),a.end());
    return a[2]-a[0];
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

    cin>>n;
    for (int i=0; i<n-1; i++) {
	int u,v; cin>>u>>v;
	g[u].push_back(v);
	g[v].push_back(u);
    }

    assert(n>=3);
    hack(1);

    int lo = n;

    for (int i=1; i<=n; i++) {
	for (int j=i+1; j<=n; j++) {
	    int mid = lca(i,j);
	    if (mid==i) {
		lo = min(lo, get({siz[j], siz[i]-siz[j], n-siz[i]}));
	    } else if (mid==j) {
		lo = min(lo, get({siz[i], siz[j]-siz[i], n-siz[j]}));
	    } else {
		lo = min(lo, get({siz[i], siz[j], n-siz[i]-siz[j]}));
	    }
	}
    }


    cout<<lo<<endl;    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5228 KB Output is correct
2 Correct 6 ms 5228 KB Output is correct
3 Correct 6 ms 5248 KB Output is correct
4 Correct 6 ms 5240 KB Output is correct
5 Correct 6 ms 5228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5228 KB Output is correct
2 Correct 6 ms 5228 KB Output is correct
3 Correct 6 ms 5248 KB Output is correct
4 Correct 6 ms 5240 KB Output is correct
5 Correct 6 ms 5228 KB Output is correct
6 Correct 243 ms 5356 KB Output is correct
7 Correct 248 ms 5356 KB Output is correct
8 Correct 214 ms 5356 KB Output is correct
9 Correct 227 ms 5356 KB Output is correct
10 Correct 250 ms 5484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5228 KB Output is correct
2 Correct 6 ms 5228 KB Output is correct
3 Correct 6 ms 5248 KB Output is correct
4 Correct 6 ms 5240 KB Output is correct
5 Correct 6 ms 5228 KB Output is correct
6 Correct 243 ms 5356 KB Output is correct
7 Correct 248 ms 5356 KB Output is correct
8 Correct 214 ms 5356 KB Output is correct
9 Correct 227 ms 5356 KB Output is correct
10 Correct 250 ms 5484 KB Output is correct
11 Execution timed out 1094 ms 31212 KB Time limit exceeded
12 Halted 0 ms 0 KB -