답안 #723388

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
723388 2023-04-13T16:32:47 Z gagik_2007 Village (BOI20_village) C++17
100 / 100
256 ms 157852 KB
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <chrono>
#include <ctime>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <iomanip>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <functional>
#include <random>
#include <cassert>
using namespace std;

typedef long long ll;
typedef long double ld;

#define ff first
#define ss second

ll ttt;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll N = 200007;
const ll M = 307;
const ll LG = 31;
ll n, m, k;
vector<ll>g[N];
vector<ll>res_mn, res_mx;
ll ans_mn = 0, ans_mx = 0;
bool used[N];
ll sz[N];
stack<ll>st[N];
bool useds[N];

bool comp(ll x, ll y) {
	return st[x].size() < st[y].size();
}

multiset<ll, decltype(&comp)> s(&comp);

void dfs(ll v, ll par = -1) {
	for (ll to : g[v]) {
		if (to != par) {
			dfs(to, v);
		}
	}
	vector<ll>leaves;
	for (ll to : g[v]) {
		if (to != par) {
			if (!used[to]) {
				leaves.push_back(to);
			}
		}
	}
	if (leaves.size() != 0) {
		ans_mn += leaves.size() * 2;
		res_mn[v] = leaves[0];
		for (ll i = 0; i < leaves.size() - 1; i++) {
			res_mn[leaves[i]] = leaves[i + 1];
		}
		res_mn[leaves[leaves.size() - 1]] = v;
		for (ll to : leaves) {
			used[to] = true;
		}
		used[v] = true;
	}
}

void calc_sz(ll v, ll par = -1) {
	sz[v] = 1;
	for (ll to : g[v]) {
		if (to != par) {
			calc_sz(to, v);
			sz[v] += sz[to];
		}
	}
	if (par != -1) {
		ans_mx += 2 * min(ll(sz[v]), n - sz[v]);
	}
}

ll centroid(ll v, ll par = -1) {
	for (ll to : g[v]) {
		if (to != par) {
			if (sz[to] > n / 2) {
				return centroid(to, v);
			}
		}
	}
	return v;
}

void inch_tenum_es_qci_stacki_mej(ll v, ll par, ll cur) {
	if (!useds[v])st[cur].push(v);
	for (ll to : g[v]) {
		if (to != par) {
			inch_tenum_es_qci_stacki_mej(to, v, cur);
		}
	}
}

int main() {
	//freopen("in.txt", "r", stdin);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	ll leaf = 0;
	for (ll i = 0; i < n - 1; i++) {
		ll x, y;
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	for (ll v = 1; v <= n; v++) {
		if (g[v].size() == 1) {
			leaf = v;
			break;
		}
	}
	ll root = g[leaf][0];
	res_mn.resize(n + 1, 0);
	res_mx.resize(n + 1, 0);
	dfs(root);
	calc_sz(1);
	ll c = centroid(1);
	if (n % 2 != 0) {
		ll cur_stack = 0;
		for (ll to : g[c]) {
			inch_tenum_es_qci_stacki_mej(to, c, cur_stack++);
		}
		ll maxim = 0;
		for (ll i = 0; i < cur_stack; i++) {
			if (comp(maxim, i)) {
				maxim = i;
			}
			//cout << i << ": " << st[i].size() << endl;
		}
		ll v1 = st[maxim].top();
		st[maxim].pop();
		ll smaxim = 0;
		if (maxim == 0) {
			smaxim = 1;
		}
		for (ll i = 0; i < cur_stack; i++) {
			if (comp(smaxim, i) && i != maxim) {
				smaxim = i;
			}
			//cout << i << ": " << st[i].size() << endl;
		}
		ll v2 = st[smaxim].top();
		st[smaxim].pop();
		res_mx[c] = v1;
		res_mx[v1] = v2;
		res_mx[v2] = c;
		useds[c] = useds[v2] = useds[v1] = true;
		for (ll i = 0; i < cur_stack; i++) {
			while (!st[i].empty()) {
				st[i].pop();
			}
		}
	}
	ll cur_stack = 0;
	for (ll to : g[c]) {
		inch_tenum_es_qci_stacki_mej(to, c, cur_stack++);
	}
	ll minim = cur_stack;
	if (!useds[c]) {
		st[minim].push(c);
		cur_stack++;
	}
	for (ll i = 0; i < cur_stack; i++) {
		if(st[i].size()!=0)s.insert(i);
	}
	while (!s.empty()) {
		//cout << s.size() << endl;
		ll bg = *s.rbegin();
		s.erase(--s.end());
		ll sbg = *s.rbegin();
		s.erase(--s.end());
		ll v1 = st[bg].top();
		ll v2 = st[sbg].top();
		res_mx[v1] = v2;
		res_mx[v2] = v1;
		st[bg].pop();
		st[sbg].pop();
		if (st[bg].size() != 0) {
			s.insert(bg);
		}
		if (st[sbg].size() != 0) {
			s.insert(sbg);
		}
		//cout << bg << " " << sbg << endl;
	}
	cout << ans_mn << " " << ans_mx << endl;
	for (ll v = 1; v <= n; v++) {
		cout << res_mn[v] << " ";
	}
	cout << endl;
	for (ll v = 1; v <= n; v++) {
		cout << res_mx[v] << " ";
	}
	cout << endl;
}

/// ---- - --------  ------ -------- -- - - -
/// Just a reminder. Ubuntu password is I O I
/// ---- - --------  ------ -------- -- - - -

Compilation message

Village.cpp: In function 'void dfs(ll, ll)':
Village.cpp:67:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |   for (ll i = 0; i < leaves.size() - 1; i++) {
      |                  ~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 139600 KB Output is correct
2 Correct 99 ms 139548 KB Output is correct
3 Correct 85 ms 139596 KB Output is correct
4 Correct 83 ms 139596 KB Output is correct
5 Correct 88 ms 139588 KB Output is correct
6 Correct 82 ms 139556 KB Output is correct
7 Correct 93 ms 139596 KB Output is correct
8 Correct 95 ms 139624 KB Output is correct
9 Correct 83 ms 139544 KB Output is correct
10 Correct 85 ms 139596 KB Output is correct
11 Correct 83 ms 139580 KB Output is correct
12 Correct 90 ms 139548 KB Output is correct
13 Correct 91 ms 139644 KB Output is correct
14 Correct 83 ms 139588 KB Output is correct
15 Correct 86 ms 139556 KB Output is correct
16 Correct 81 ms 139620 KB Output is correct
17 Correct 88 ms 139584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 139668 KB Output is correct
2 Correct 82 ms 139596 KB Output is correct
3 Correct 80 ms 139608 KB Output is correct
4 Correct 84 ms 139724 KB Output is correct
5 Correct 97 ms 139648 KB Output is correct
6 Correct 83 ms 139640 KB Output is correct
7 Correct 82 ms 139724 KB Output is correct
8 Correct 87 ms 139636 KB Output is correct
9 Correct 82 ms 139656 KB Output is correct
10 Correct 85 ms 139652 KB Output is correct
11 Correct 87 ms 139684 KB Output is correct
12 Correct 95 ms 139672 KB Output is correct
13 Correct 83 ms 139724 KB Output is correct
14 Correct 87 ms 139660 KB Output is correct
15 Correct 101 ms 139656 KB Output is correct
16 Correct 88 ms 139696 KB Output is correct
17 Correct 82 ms 139728 KB Output is correct
18 Correct 88 ms 139704 KB Output is correct
19 Correct 87 ms 139712 KB Output is correct
20 Correct 96 ms 139672 KB Output is correct
21 Correct 88 ms 139732 KB Output is correct
22 Correct 84 ms 139652 KB Output is correct
23 Correct 85 ms 139724 KB Output is correct
24 Correct 87 ms 139644 KB Output is correct
25 Correct 87 ms 139636 KB Output is correct
26 Correct 86 ms 139732 KB Output is correct
27 Correct 89 ms 139636 KB Output is correct
28 Correct 85 ms 139676 KB Output is correct
29 Correct 101 ms 139620 KB Output is correct
30 Correct 85 ms 139724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 139600 KB Output is correct
2 Correct 99 ms 139548 KB Output is correct
3 Correct 85 ms 139596 KB Output is correct
4 Correct 83 ms 139596 KB Output is correct
5 Correct 88 ms 139588 KB Output is correct
6 Correct 82 ms 139556 KB Output is correct
7 Correct 93 ms 139596 KB Output is correct
8 Correct 95 ms 139624 KB Output is correct
9 Correct 83 ms 139544 KB Output is correct
10 Correct 85 ms 139596 KB Output is correct
11 Correct 83 ms 139580 KB Output is correct
12 Correct 90 ms 139548 KB Output is correct
13 Correct 91 ms 139644 KB Output is correct
14 Correct 83 ms 139588 KB Output is correct
15 Correct 86 ms 139556 KB Output is correct
16 Correct 81 ms 139620 KB Output is correct
17 Correct 88 ms 139584 KB Output is correct
18 Correct 88 ms 139668 KB Output is correct
19 Correct 82 ms 139596 KB Output is correct
20 Correct 80 ms 139608 KB Output is correct
21 Correct 84 ms 139724 KB Output is correct
22 Correct 97 ms 139648 KB Output is correct
23 Correct 83 ms 139640 KB Output is correct
24 Correct 82 ms 139724 KB Output is correct
25 Correct 87 ms 139636 KB Output is correct
26 Correct 82 ms 139656 KB Output is correct
27 Correct 85 ms 139652 KB Output is correct
28 Correct 87 ms 139684 KB Output is correct
29 Correct 95 ms 139672 KB Output is correct
30 Correct 83 ms 139724 KB Output is correct
31 Correct 87 ms 139660 KB Output is correct
32 Correct 101 ms 139656 KB Output is correct
33 Correct 88 ms 139696 KB Output is correct
34 Correct 82 ms 139728 KB Output is correct
35 Correct 88 ms 139704 KB Output is correct
36 Correct 87 ms 139712 KB Output is correct
37 Correct 96 ms 139672 KB Output is correct
38 Correct 88 ms 139732 KB Output is correct
39 Correct 84 ms 139652 KB Output is correct
40 Correct 85 ms 139724 KB Output is correct
41 Correct 87 ms 139644 KB Output is correct
42 Correct 87 ms 139636 KB Output is correct
43 Correct 86 ms 139732 KB Output is correct
44 Correct 89 ms 139636 KB Output is correct
45 Correct 85 ms 139676 KB Output is correct
46 Correct 101 ms 139620 KB Output is correct
47 Correct 85 ms 139724 KB Output is correct
48 Correct 203 ms 148220 KB Output is correct
49 Correct 188 ms 149196 KB Output is correct
50 Correct 161 ms 149072 KB Output is correct
51 Correct 153 ms 146860 KB Output is correct
52 Correct 185 ms 148960 KB Output is correct
53 Correct 206 ms 147964 KB Output is correct
54 Correct 164 ms 148808 KB Output is correct
55 Correct 199 ms 157852 KB Output is correct
56 Correct 196 ms 153248 KB Output is correct
57 Correct 256 ms 150896 KB Output is correct
58 Correct 202 ms 150432 KB Output is correct
59 Correct 165 ms 149196 KB Output is correct
60 Correct 189 ms 152940 KB Output is correct
61 Correct 183 ms 150052 KB Output is correct
62 Correct 166 ms 149324 KB Output is correct
63 Correct 168 ms 148812 KB Output is correct
64 Correct 176 ms 149468 KB Output is correct
65 Correct 210 ms 149556 KB Output is correct
66 Correct 195 ms 148884 KB Output is correct
67 Correct 165 ms 147280 KB Output is correct
68 Correct 163 ms 147968 KB Output is correct
69 Correct 182 ms 149728 KB Output is correct
70 Correct 163 ms 148976 KB Output is correct
71 Correct 151 ms 146544 KB Output is correct
72 Correct 152 ms 147484 KB Output is correct
73 Correct 192 ms 149596 KB Output is correct
74 Correct 167 ms 148692 KB Output is correct
75 Correct 190 ms 149524 KB Output is correct
76 Correct 203 ms 148856 KB Output is correct
77 Correct 167 ms 149212 KB Output is correct
78 Correct 140 ms 146072 KB Output is correct
79 Correct 143 ms 147084 KB Output is correct
80 Correct 214 ms 149548 KB Output is correct
81 Correct 178 ms 149400 KB Output is correct
82 Correct 185 ms 149124 KB Output is correct