Submission #319619

# Submission time Handle Problem Language Result Execution time Memory
319619 2020-11-05T21:17:49 Z Blagojce Papričice (COCI20_papricice) C++11
0 / 110
4 ms 5120 KB
#include <bits/stdc++.h> 
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define rfr(i, n, m) for(int i = (n); i >= (m); i --)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
#include <time.h>
#include <cmath>
#include <deque>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int i_inf = 1e9;
const ll inf = 2e18;
const ll mod = 998244353;
const ld eps = 1e-13;
const ld pi  = 3.14159265359;
const int mxn = 2e5+5;
mt19937 _rand(time(NULL));

int n;
vector<int> g[mxn];

int sz[mxn];
set<int> *S[mxn];
int ans = mxn;
void dfs(int u, int p){
	sz[u] = 1;
	int bst = -1;
	int bsz = 0;
	for(auto e : g[u]){
		if(e == p) continue;
		dfs(e, u);
		sz[u] += sz[e];
		if(sz[e] > bsz){
			bsz = sz[e];
			bst = e;
		}
	}
	
	if(sz[u] == 1){
		(*S[u]).insert(0);
	}
	else{
		S[u] = S[bst];
		for(auto e : g[u]){
			if(e == bst) continue;
			for(auto k : *S[e]){
				(*S[u]).insert(k);
			}
		}
	}
	(*S[u]).insert(sz[u]);
	if(u != 0){
		auto it = (*S[u]).lower_bound(sz[u]/2);
		
		if(it != (*S[u]).end()){
			int MAX = max(n-sz[u], max(*it, sz[u]-(*it)));
			int MIN = min(n-sz[u], min(*it, sz[u]-(*it)));
			ans = min(ans, MAX-MIN);
		}
		if(it != (*S[u]).begin()){
			--it;
			if(it != (*S[u]).end()){
				int MAX = max(n-sz[u], max(*it, sz[u]-(*it)));
				int MIN = min(n-sz[u], min(*it, sz[u]-(*it)));
				ans = min(ans, MAX-MIN);
			}
		}
	}

}


int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n;
	fr(i, 0, n) S[i] = new set<int>;
	fr(i, 0, n-1){
		int u, v;
		cin >> u >> v;
		--u, --v;
		g[u].pb(v);
		g[v].pb(u);
	}
	dfs(0, 0); 
	cout<<ans<<endl;
	
}

# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Incorrect 4 ms 5100 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Incorrect 4 ms 5100 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Incorrect 4 ms 5100 KB Output isn't correct
4 Halted 0 ms 0 KB -