답안 #1088104

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1088104 2024-09-13T22:10:12 Z freedommo33 Village (BOI20_village) C++17
100 / 100
68 ms 20360 KB
#include <bits/stdc++.h>
typedef long long ll;
constexpr int M = 1e5+7;
using namespace std;
vector<vector<ll>> g(M);
ll paramin[M];
ll paramax[M];
ll par[M];
ll sajz[M];
ll kolor[M];
ll odl[M];
vector<ll> preorder;
vector<ll> kolory[M];
int odl2[20][20];
void bfs(){
	queue<ll> q;
	q.push(1);
	preorder.push_back(1);
	while(!q.empty()){
		ll p = q.front();
		q.pop();
		for(auto i:g[p]) if(par[i] == 0 && par[p]!=i){
			q.push(i);
			par[i] = p;
			preorder.push_back(i);
		}
	}
}
void bfs2(ll v){
	queue<ll> q;
	q.push(v);
	while(!q.empty()){
		ll p = q.front();
		q.pop();
		for(auto i:g[p]) if(i != v && odl[i]==0){
			odl[i] = odl[p] + 1;
			q.push(i);
		}
	}
}
ll centroid = 0;
void dfs(ll v){
	pair<ll, ll> maks = {0, 0};
	for(auto i:g[v]) if(i!=par[v]){
		if(sajz[i] > maks.first) maks = {sajz[i], i};
	}
	if(maks.first > sajz[1]/2) dfs(maks.second);
	else centroid = v;
}
void dfs2(ll v, ll k){
	kolor[v] = k;
	for(auto i:g[v]) if(i!=centroid && kolor[i]==0) dfs2(i, k);
}
bool czy(vector<ll> a, vector<ll> b) { return a.size() > b.size(); }

void bfs3(ll v){
	queue<ll> q;
	q.push(v);
	while(!q.empty()){
		int p = q.front();
		q.pop();
		for(auto i:g[p]) if(odl2[v][i] == 0 && i != v){
			odl2[v][i] = odl2[v][p] + 1;
			q.push(i);
		}
	}
}
int main(){
	cin.tie(0)->sync_with_stdio(0);
	ll n;
	cin>>n;
	for(int i=1; i<n; i++){
		int a, b;
		cin>>a>>b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	bfs();
	reverse(preorder.begin(), preorder.end());
	ll minwynik = 0;
	for(auto i:preorder){
		sajz[i] = 1;
		for(auto j:g[i]) if(j!=par[i]) sajz[i] += sajz[j];
		ll last = 0;
		ll p1 = 0, p2 = 0;
		for(auto j:g[i]) if(j!=par[i] && paramin[j]==0){
			if(last==0) last = j;
			else{	
				p1 = last, p2 = j;
				minwynik += 4;
				//cout<<"P1: "<<last<<" "<<j<<endl;
				paramin[last] = j;
				paramin[j] = last;
				last = 0;
			}	
		}
		if(last!=0){
			minwynik += 2;
			//cout<<"P2: "<<i<<" "<<last<<endl;
			paramin[i] = last;
			paramin[last] = i;
		}
		else if(p1!=0){
			//cout<<"P3: "<<p1<<" "<<p2<<" "<<i<<endl;
			paramin[p1] = i;
			paramin[p2] = p1;
			paramin[i] = p2;
		}
	}
	if(paramin[1] == 0){
		bool czy = 0;
		for(auto i:g[1]){
			if(paramin[paramin[i]] == i){
				int a = i, b = paramin[a];
				//cout<<"P4: "<<a<<" "<<b<<endl;
				paramin[a] = 1;
				paramin[b] = a;
				paramin[1] = b;
				minwynik+=2;
				czy = 1;
				break;
			}
		}
		if(czy == 0){
			int a = g[1][0], b = paramin[a], c = paramin[b];
			//cout<<"P5: "<<a<<" "<<b<<endl;
			paramin[b] = c;
			paramin[c] = b;
			paramin[1] = a;
			paramin[a] = 1;
			minwynik+=2;
		}
	}
	dfs(1);
	ll aktk = 0;
	for(auto i:g[centroid]) dfs2(i, ++aktk);
	for(int i=1; i<=n; i++) if(i!=centroid) kolory[kolor[i]].push_back(i);
	priority_queue<pair<int, int>> q;
	for(int i=1; i<=aktk; i++){
		//cout<<kolory[i].size()<<" "<<i<<endl;
		q.push({kolory[i].size(), i});
	}
	bfs2(centroid);
	//for(int i=1; i<=n; i++) cout<<i<<": "<<odl[i]<<endl;
	ll makswyn = 0;
	while(q.size() > 1){
		pair<ll, ll> a = q.top();
		q.pop();
		pair<ll, ll> b = q.top();
		q.pop();
		ll aa = kolory[a.second].back();
		kolory[a.second].pop_back();
		ll bb = kolory[b.second].back();
		kolory[b.second].pop_back();
		//cout<<"PARUJEMY: "<<aa<<" "<<bb<<endl;
		makswyn += (odl[aa] + odl[bb])*2;
		paramax[aa] = bb;
		paramax[bb] = aa;
		a.first--, b.first--;
		if(a.first) q.push(a);
		if(b.first) q.push(b);
	}
	if(!q.empty()){
		pair<ll, ll> a = q.top();
		q.pop();
		ll aa = kolory[a.second].back();
		kolory[a.second].pop_back();
		//cout<<"PARUJEMY: "<<centroid<<" "<<aa<<endl;
		makswyn += odl[aa]*2;
		paramax[centroid] = aa;
		paramax[aa] = centroid;
	}
	if(paramax[centroid] == 0){
		ll x = 1;
		if(centroid == 1) x = 2;
		ll a = paramax[x];
		paramax[a] = centroid;
		paramax[centroid] = x;
	}
	cout<<minwynik<<" "<<makswyn<<endl;
	for(int i=1; i<=n; i++) cout<<paramin[i]<<" ";
	cout<<endl;
	for(int i=1; i<=n; i++) cout<<paramax[i]<<" ";
	cout<<endl;
	//for(int i=1; i<=n; i++) bfs3(i);
	//ll aktmin = 0;
	//for(int i=1; i<=n; i++) aktmin += odl2[i][paramin[i]];
	//ll aktmaks = 0;
	//for(int i=1; i<=n; i++) aktmaks += odl2[i][paramax[i]];
	//if(aktmin == minwynik && aktmaks == makswyn) cout<<"OK"<<endl;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5208 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 5132 KB Output is correct
6 Correct 2 ms 5208 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4952 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 4956 KB Output is correct
14 Correct 2 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 5208 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 3 ms 5212 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 3 ms 5212 KB Output is correct
11 Correct 2 ms 5208 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5208 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5212 KB Output is correct
18 Correct 2 ms 5212 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 2 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 3 ms 5212 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 2 ms 5212 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 2 ms 5212 KB Output is correct
29 Correct 3 ms 5208 KB Output is correct
30 Correct 2 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5208 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 5132 KB Output is correct
6 Correct 2 ms 5208 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4952 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 4956 KB Output is correct
14 Correct 2 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 5208 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 4 ms 5212 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 2 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 2 ms 5212 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 2 ms 5212 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 3 ms 5212 KB Output is correct
28 Correct 2 ms 5208 KB Output is correct
29 Correct 2 ms 5212 KB Output is correct
30 Correct 2 ms 5208 KB Output is correct
31 Correct 2 ms 5212 KB Output is correct
32 Correct 2 ms 5212 KB Output is correct
33 Correct 3 ms 5212 KB Output is correct
34 Correct 3 ms 5212 KB Output is correct
35 Correct 2 ms 5212 KB Output is correct
36 Correct 2 ms 5212 KB Output is correct
37 Correct 2 ms 5212 KB Output is correct
38 Correct 2 ms 5212 KB Output is correct
39 Correct 3 ms 5212 KB Output is correct
40 Correct 2 ms 5212 KB Output is correct
41 Correct 2 ms 5212 KB Output is correct
42 Correct 3 ms 5212 KB Output is correct
43 Correct 3 ms 5212 KB Output is correct
44 Correct 2 ms 5212 KB Output is correct
45 Correct 2 ms 5212 KB Output is correct
46 Correct 3 ms 5208 KB Output is correct
47 Correct 2 ms 5212 KB Output is correct
48 Correct 45 ms 16016 KB Output is correct
49 Correct 49 ms 16836 KB Output is correct
50 Correct 48 ms 16784 KB Output is correct
51 Correct 41 ms 14124 KB Output is correct
52 Correct 52 ms 16788 KB Output is correct
53 Correct 46 ms 15556 KB Output is correct
54 Correct 27 ms 11984 KB Output is correct
55 Correct 68 ms 18588 KB Output is correct
56 Correct 56 ms 17864 KB Output is correct
57 Correct 54 ms 17356 KB Output is correct
58 Correct 55 ms 17092 KB Output is correct
59 Correct 49 ms 17104 KB Output is correct
60 Correct 47 ms 20360 KB Output is correct
61 Correct 40 ms 17644 KB Output is correct
62 Correct 39 ms 17096 KB Output is correct
63 Correct 40 ms 16836 KB Output is correct
64 Correct 41 ms 17092 KB Output is correct
65 Correct 40 ms 17348 KB Output is correct
66 Correct 38 ms 16332 KB Output is correct
67 Correct 31 ms 14280 KB Output is correct
68 Correct 36 ms 15332 KB Output is correct
69 Correct 47 ms 17484 KB Output is correct
70 Correct 37 ms 16840 KB Output is correct
71 Correct 28 ms 13776 KB Output is correct
72 Correct 32 ms 14808 KB Output is correct
73 Correct 43 ms 17452 KB Output is correct
74 Correct 36 ms 16324 KB Output is correct
75 Correct 49 ms 17272 KB Output is correct
76 Correct 45 ms 16840 KB Output is correct
77 Correct 43 ms 16836 KB Output is correct
78 Correct 31 ms 13248 KB Output is correct
79 Correct 33 ms 14644 KB Output is correct
80 Correct 48 ms 17392 KB Output is correct
81 Correct 42 ms 17444 KB Output is correct
82 Correct 40 ms 17156 KB Output is correct