Submission #1094481

# Submission time Handle Problem Language Result Execution time Memory
1094481 2024-09-29T16:48:35 Z mat_jur Village (BOI20_village) C++17
100 / 100
69 ms 17504 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";}
#define debug(X) cerr << "["#X"]: " << X << '\n';
#else 
#define cerr if(0)cout
#define debug(X) ;
#endif
using ll = long long;
#define all(v) (v).begin(), (v).end()
#define ssize(x) int(x.size())
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back

void dfs(int v, int p, vector<int> &match, const vector<vector<int>> &g) {
	for (int u : g[v]) {
		if (u == p) continue;
		dfs(u, v, match, g);
	}

	if (match[v] != -1) return;

	for (int u : g[v]) {
		if (match[u] == -1) {
			match[v] = u;
			match[u] = v;
		}
	}
}

vector<int> matching(const vector<vector<int>> &g) {
	int n = ssize(g);
	vector<int> match(n, -1);

	dfs(0, -1, match, g);

	return match;
}

void dfs_cent(int v, int p, vector<int> &s, const vector<vector<int>> &g) {
	s[v] = 1;
	for (int u : g[v]) {
		if (u == p) continue;
		dfs_cent(u, v, s, g);
		s[v] += s[u];
	}
}

int centroid(const vector<vector<int>> &g) {
	int n = ssize(g);
	vector<int> s(n);

	dfs_cent(0, -1, s, g);

//	debug(s);
	
	int c = 0;
	int p = -1;

	bool is_centroid = false;

	while (!is_centroid) {
		is_centroid = true;
		for (int u : g[c]) {
			if (u == p) continue;
			if (s[u] > n/2) {
				p = c;
				c = u;
				is_centroid = false;
				break;
			}
		}
	}

	return c;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr);

	int n;
	cin >> n;
	vector<vector<int>> g(n);

	for (int i = 0; i < n-1; ++i) {
		int u, v;
		cin >> u >> v;
		g[u-1].eb(v-1);
		g[v-1].eb(u-1);
	}

	vector<int> match = matching(g);

	int mn = n;

	for (int v = 0; v < n; ++v) {
		if (match[v] != -1) continue;
		mn++;
		int u = g[v][0];
		match[v] = match[u];
		match[u] = v;
	}

	int c = centroid(g);

	vector<int> match_mx(n, -1);

	vector<vector<int>> sub(ssize(g[c]));
	ll mx = 0;

	vector<int> dist(n);
	function<void(int, int, int)> add_sub = [&](int v, int p, int idx) {
		mx += dist[v];
		sub[idx].eb(v);
		for (int u : g[v]) {
			if (u == p) continue;
			dist[u] = dist[v] + 1;
			add_sub(u, v, idx);
		}
	};


	for (int i = 0; i < ssize(g[c]); ++i) {
		dist[g[c][i]] = 1;
		add_sub(g[c][i], c, i);
	}

	debug(c);

	debug(sub);

	sort(all(sub), [&](const vector<int> &a, const vector<int> &b) {
		return ssize(a) > ssize(b);
	});

	if (n%2 == 0) {
		int u = sub[0].back();
		sub[0].pop_back();
		int i = 0;
		while (i+1 < ssize(sub) && ssize(sub[i]) < ssize(sub[i+1])) {
			swap(sub[i], sub[i+1]);
			++i;
		}
		match_mx[c] = u;
		match_mx[u] = c;
	}

	debug(sub);

	priority_queue<pair<int, int>> q;
	for (int i = 0; i < ssize(sub); ++i) {
		if (ssize(sub[i])) q.push(mp(ssize(sub[i]), i));
	}

	while (ssize(q)) {
		debug(sub);
		debug(q.top());
		int a = q.top().se;
		q.pop();
		debug(q.top());
		int b = q.top().se;
		q.pop();
		int u = sub[a].back(), v = sub[b].back();
		match_mx[u] = v;
		match_mx[v] = u;
		sub[a].pop_back(); sub[b].pop_back();
		if (ssize(sub[a])) q.push(mp(ssize(sub[a]), a));
		if (ssize(sub[b])) q.push(mp(ssize(sub[b]), b));
	}
	
	if (n%2 == 1) {
		int u = g[c][0];
		match_mx[c] = match_mx[u];
		match_mx[u] = c;
	}

//	debug(match_mx);
	cout << mn << ' ' << 2*mx << '\n';
	for (int x : match) cout << x+1 << ' ';
	cout << '\n';
	for (int x : match_mx) cout << x+1 << ' ';
	cout << '\n';

	/*for (int i = 0; i < n; ++i) {
		if (match_mx[i] == -1) {
			cout << "WA";
			return 0;	
		}
	}

	cout << "OK";*/
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 452 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 452 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 460 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 344 KB Output is correct
38 Correct 1 ms 344 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 43 ms 9200 KB Output is correct
49 Correct 47 ms 10160 KB Output is correct
50 Correct 46 ms 10116 KB Output is correct
51 Correct 40 ms 7972 KB Output is correct
52 Correct 48 ms 9932 KB Output is correct
53 Correct 49 ms 9204 KB Output is correct
54 Correct 32 ms 8588 KB Output is correct
55 Correct 61 ms 17504 KB Output is correct
56 Correct 69 ms 13604 KB Output is correct
57 Correct 62 ms 12224 KB Output is correct
58 Correct 54 ms 11212 KB Output is correct
59 Correct 46 ms 10184 KB Output is correct
60 Correct 58 ms 16448 KB Output is correct
61 Correct 43 ms 11680 KB Output is correct
62 Correct 42 ms 10452 KB Output is correct
63 Correct 48 ms 10128 KB Output is correct
64 Correct 44 ms 10300 KB Output is correct
65 Correct 42 ms 10564 KB Output is correct
66 Correct 42 ms 9956 KB Output is correct
67 Correct 36 ms 8608 KB Output is correct
68 Correct 37 ms 8776 KB Output is correct
69 Correct 41 ms 10472 KB Output is correct
70 Correct 38 ms 9728 KB Output is correct
71 Correct 27 ms 7236 KB Output is correct
72 Correct 33 ms 8220 KB Output is correct
73 Correct 43 ms 10640 KB Output is correct
74 Correct 43 ms 9716 KB Output is correct
75 Correct 51 ms 9932 KB Output is correct
76 Correct 53 ms 9900 KB Output is correct
77 Correct 49 ms 9864 KB Output is correct
78 Correct 27 ms 6728 KB Output is correct
79 Correct 35 ms 7884 KB Output is correct
80 Correct 53 ms 9796 KB Output is correct
81 Correct 47 ms 9956 KB Output is correct
82 Correct 60 ms 10188 KB Output is correct