답안 #445818

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
445818 2021-07-19T17:20:17 Z Jasiekstrz Papričice (COCI20_papricice) C++17
110 / 110
250 ms 38720 KB
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=2e5;
set<int> stmem[N+10];
set<int>* st[N+10];
vector<int> e[N+10];
int siz[N+10];
int f(int a,int b,int c)
{
	vector<int> tmp={a,b,c};
	sort(tmp.begin(),tmp.end());
	return tmp[2]-tmp[0];
}
int dfs(int x,int p,int n)
{
	int ans=n;
	siz[x]=1;
	st[x]=&stmem[x];
	for(auto v:e[x])
	{
		if(v==p)
			continue;
		ans=min(ans,dfs(v,x,n));
		siz[x]+=siz[v];
		if(st[x]->size()<st[v]->size())
			swap(st[x],st[v]);
		for(auto c:*st[v])
		{
			auto it=st[x]->lower_bound((n-c+1)/2);
			if(it!=st[x]->end())
				ans=min(ans,f(c,*it,n-*it-c));
			if(it!=st[x]->begin())
				ans=min(ans,f(c,*prev(it),n-*prev(it)-c));
		}
		for(auto c:*st[v])
			st[x]->insert(c);
		{set<int> ().swap(*st[v]);}
	}
	if(x!=1)
	{
		auto it=st[x]->lower_bound((siz[x]+1)/2);
		if(it!=st[x]->end())
			ans=min(ans,f(n-siz[x],*it,siz[x]-*it));
		if(it!=st[x]->begin())
			ans=min(ans,f(n-siz[x],*prev(it),siz[x]-*prev(it)));
	}
	st[x]->insert(siz[x]);
	return ans;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n;
	for(int i=1;i<n;i++)
	{
		int a,b;
		cin>>a>>b;
		e[a].push_back(b);
		e[b].push_back(a);
	}
	cout<<dfs(1,0,n)<<"\n";
	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14380 KB Output is correct
3 Correct 8 ms 14392 KB Output is correct
4 Correct 8 ms 14412 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14380 KB Output is correct
3 Correct 8 ms 14392 KB Output is correct
4 Correct 8 ms 14412 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
6 Correct 9 ms 14428 KB Output is correct
7 Correct 10 ms 14540 KB Output is correct
8 Correct 9 ms 14464 KB Output is correct
9 Correct 9 ms 14412 KB Output is correct
10 Correct 9 ms 14428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14380 KB Output is correct
3 Correct 8 ms 14392 KB Output is correct
4 Correct 8 ms 14412 KB Output is correct
5 Correct 8 ms 14412 KB Output is correct
6 Correct 9 ms 14428 KB Output is correct
7 Correct 10 ms 14540 KB Output is correct
8 Correct 9 ms 14464 KB Output is correct
9 Correct 9 ms 14412 KB Output is correct
10 Correct 9 ms 14428 KB Output is correct
11 Correct 234 ms 25700 KB Output is correct
12 Correct 250 ms 25776 KB Output is correct
13 Correct 181 ms 26148 KB Output is correct
14 Correct 194 ms 25792 KB Output is correct
15 Correct 230 ms 25680 KB Output is correct
16 Correct 145 ms 25496 KB Output is correct
17 Correct 234 ms 25756 KB Output is correct
18 Correct 217 ms 38720 KB Output is correct
19 Correct 216 ms 25752 KB Output is correct
20 Correct 211 ms 25844 KB Output is correct