답안 #710681

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
710681 2023-03-15T15:19:48 Z aVe Papričice (COCI20_papricice) C++14
0 / 110
7 ms 4948 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define mp make_pair
#define pb push_back
#define se second
#define fi first
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
clock_t startTime;
double getCurrentTime() {
	return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
ll set_on(ll n, ll k){
	return (n |= 1 << k);
}
ll set_off(ll n, ll k){
	return (n &= ~(1UL << k));
}
bool check_bit(ll n, ll k){
	int bit = (n >> k) & 1U;
	if(bit == 1)
		return true;
	return false;
}
int n;
vi G[200100];
int ans = 1e9, sum[200100], depth[200100];
vi segtree, euler, first;
vector<bool> vis;
void dfs2(int at){
	vis[at] = true;
	first[at] = euler.size();
	euler.pb(at);
	for(auto next : G[at]){
		if(!vis[next]){
			dfs2(next);
			euler.pb(at);
		}
	}
}
void build(int node, int l, int r){
	if(l == r){
		segtree[node] = euler[l];
	}
	else{
		int mid = (l + r) / 2;
		build(node * 2, l, mid);
		build(node * 2 + 1 ,mid + 1, r);
		int left = segtree[node * 2];
		int right= segtree[node * 2 + 1];
		segtree[node] = (depth[left] < depth[right]) ? left : right;
	}
}
void LCA(int root = 0){
	vis.assign(n, false);
	first.resize(n);
	euler.reserve(2 * n);
	dfs2(root);
	segtree.resize(4 * euler.size());
	build(1, 0, euler.size()-1);
}
int query(int node, int l, int r, int L, int R){
	if(r < L || l > R)	return -1;
	if(L <= l && r <= R)	return segtree[node];
	
	int mid = (l + r) / 2;
	int left = query(node * 2, l, mid, L, R);
	int right = query(node * 2 + 1, mid + 1, r, L, R);
	if(left == -1)	return right;
	if(right == -1)	return left;
	return (depth[left] < depth[right]) ? left : right;
}
int lca(int u, int v){
	int left = first[u], right = first[v];
	if(left > right)
		swap(left, right);
		
	return query(1, 0, euler.size()-1, left, right);
}
void get_depth(int at, int prev, int dep){
	depth[at] = dep;
	for(auto next : G[at]){
		if(next == prev)	continue;
		get_depth(next, at, dep + 1);
	}
}
void dfs(int at, int prev){
	sum[at] = 1;
	for(auto next : G[at]){
		if(next == prev)	continue;
		dfs(next, at);
		sum[at] += sum[next];
	}
}
bool comp(int& A, int& B){
	if(depth[A] >= depth[B])	return false;
	return true;
}
void solve(){
	cin>>n;
	vector<pii> edge;
	for(int i = 1; i < n; i++){
		int a, b;
		cin>>a>>b;
		a--; b--;
		G[a].pb(b);
		G[b].pb(a);
		edge.pb(mp(a,b));
	}
	LCA();
	get_depth(0, -1, 0);
	dfs(0, -1);
	for(int i = 0; i < edge.size(); i++){
		for(int j = i + 1; j < edge.size(); j++){
			int a = sum[0];
			int b= sum[edge[i].se];
			int c = sum[edge[j].se];
			if(lca(edge[i].se, edge[j].se) == 0){
				a -= c;
				a -= b;
			}
			else if(lca(edge[i].se, edge[j].se) == edge[i].se){
				a -= b;
				b -= c;
			}
			else if(lca(edge[i].se, edge[j].se) == edge[j].se){
				a -= c;
				c -= b;
			}
			int maks = max(a, max(b,c));
			int mins = min(a, min(b,c));
			ans = min(ans, maks - mins);
		}
	}
	cout<<ans<<endl;
	
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.precision(10);
	cout<<fixed;
	startTime = clock();
	int t=1;
//	cin>>t;
	while(t--)
		solve();

	return 0;
}




Compilation message

papricice.cpp: In function 'void solve()':
papricice.cpp:142:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |  for(int i = 0; i < edge.size(); i++){
      |                 ~~^~~~~~~~~~~~~
papricice.cpp:143:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  143 |   for(int j = i + 1; j < edge.size(); j++){
      |                      ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4948 KB Output is correct
2 Incorrect 7 ms 4948 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4948 KB Output is correct
2 Incorrect 7 ms 4948 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4948 KB Output is correct
2 Incorrect 7 ms 4948 KB Output isn't correct
3 Halted 0 ms 0 KB -