답안 #466280

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
466280 2021-08-18T12:49:55 Z benson0402 Papričice (COCI20_papricice) C++14
15 / 110
5 ms 5068 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define F first
#define S second
#define eb emplace_back
#define ep emplace
#define mkp make_pair
#define ALL(x) x.begin(), x.end()
#define MEM(x) memset(x, 0, sizeof(x));
#define MEMS(x) memset(x, -1, sizeof(x));

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
/*-------------------------------------------------------------------------*/

const ll MOD = 1e9 + 7;

const int N = 2e5 + 5;
int n;
vector<int> G[N];
set<int> stA, stB;
int sz[N];

void dfs(int u, int p = -1) {
	sz[u] = 1;
	for(auto& v : G[u]) if(v != p) {
		dfs(v, u);
		sz[u] += sz[v];
	}
}
int ans = INF;
void dfs2(int u, int p = -1) {
	stA.ep(sz[u]);
	for(auto& v : G[u]) if(v != p) {
		dfs2(v, u);
	}
	stA.erase(sz[u]);
	// Case1: is Ancestor
	{
		auto it = stA.upper_bound((n + sz[u]) / 2);
		if(it != stA.end()) {
			// if(u == 7) cout << *it << '\n';
			int mx = max({*it - sz[u], sz[u], n - *it});
			int mn = min({*it - sz[u], sz[u], n - *it});
			ans = min(ans, mx - mn);
		}
		if(it != stA.begin()) {
			it = prev(it);
			// if(u == 7) cout << *it << '\n';
			int mx = max({*it - sz[u], sz[u], n - *it});
			int mn = min({*it - sz[u], sz[u], n - *it});
			ans = min(ans, mx - mn);
		}
	}
	// Case2: Not Ancestor
	{
		auto it = stB.upper_bound((n - sz[u]) / 2);
		if(it != stB.end()) {
			int mx = max({*it, sz[u], n - *it - sz[u]});
			int mn = min({*it, sz[u], n - *it - sz[u]});
			ans = min(ans, mx - mn);
		}
		if(it != stB.begin()) {
			it = prev(it);
			int mx = max({*it, sz[u], n - *it - sz[u]});
			int mn = min({*it, sz[u], n - *it - sz[u]});
			ans = min(ans, mx - mn);
		}
	}
	stB.ep(sz[u]);
}


inline void solve() {
	cin >> n;
	for(int i = 0; i < n - 1; ++i) {
		int u, v; cin >> u >> v;
		G[u].eb(v);
		G[v].eb(u);
	}
	dfs(1);
	dfs2(1);
	cout << ans << '\n';
}

signed main() {
    cin.tie(nullptr), cout.tie(nullptr), ios::sync_with_stdio(false);
    solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5016 KB Output is correct
3 Correct 3 ms 5016 KB Output is correct
4 Correct 4 ms 5020 KB Output is correct
5 Correct 4 ms 4924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5016 KB Output is correct
3 Correct 3 ms 5016 KB Output is correct
4 Correct 4 ms 5020 KB Output is correct
5 Correct 4 ms 4924 KB Output is correct
6 Correct 5 ms 5068 KB Output is correct
7 Incorrect 4 ms 5068 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5016 KB Output is correct
3 Correct 3 ms 5016 KB Output is correct
4 Correct 4 ms 5020 KB Output is correct
5 Correct 4 ms 4924 KB Output is correct
6 Correct 5 ms 5068 KB Output is correct
7 Incorrect 4 ms 5068 KB Output isn't correct
8 Halted 0 ms 0 KB -