Submission #723092

# Submission time Handle Problem Language Result Execution time Memory
723092 2023-04-13T08:35:08 Z gagik_2007 Village (BOI20_village) C++17
56 / 100
81 ms 22604 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<int>g[N];
vector<int>res_mn;
ll ans_mn = 0;
bool used[N];
int dist[LG][LG];

void dfs(int v, int par = -1) {
	for (int to : g[v]) {
		if (to != par) {
			dfs(to, v);
		}
	}
	vector<int>leaves;
	for (int 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 (int i = 0; i < leaves.size() - 1; i++) {
			res_mn[leaves[i]] = leaves[i + 1];
		}
		res_mn[leaves[leaves.size() - 1]] = v;
		for (int to : leaves) {
			used[to] = true;
		}
		used[v] = true;
	}
}

void calc_dist(int v, int par, int cur, int u) {
	dist[u][v] = cur;
	for (int to : g[v]) {
		if (to != par) {
			calc_dist(to, v, cur + 1, u);
		}
	}
}

int main() {
	//freopen("in.txt", "r", stdin);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		int x, y;
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	if (n <= 10) {
		vector<int>p;
		for (int v = 1; v <= n; v++) {
			p.push_back(v);
			calc_dist(v, v, 0, v);
		}
		vector<int>mx, mn;
		int mxv = 0, mnv = MOD;
		do {
			int cur_sz = 0;
			bool ok = true;
			for (int v = 1; v <= n; v++) {
				cur_sz += dist[v][p[v - 1]];
				if (p[v - 1] == v) {
					ok = false;
				}
			}
			if (ok) {
				if (cur_sz > mxv) {
					mxv = cur_sz;
					mx = p;
				}
				if (cur_sz < mnv) {
					mnv = cur_sz;
					mn = p;
				}
			}
		} while (next_permutation(p.begin(), p.end()));
		cout << mnv << " " << mxv << endl;
		for (int x : mn) {
			cout << x << " ";
		}
		cout << endl;
		for (int x : mx) {
			cout << x << " ";
		}
		cout << endl;
		return 0;
	}
	int leaf = 0;
	for (int v = 1; v <= n; v++) {
		if (g[v].size() == 1) {
			leaf = v;
			break;
		}
	}
	int root = g[leaf][0];
	res_mn.resize(n + 1, 0);
	dfs(root);
	cout << ans_mn << " ";
	cout << ans_mn << endl;
	for (int v = 1; v <= n; v++) {
		cout << res_mn[v] << " ";
	}
	cout << endl;
	for (int v = 1; v <= n; v++) {
		cout << res_mn[v] << " ";
	}
	cout << endl;
}

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

Compilation message

Village.cpp: In function 'void dfs(int, int)':
Village.cpp:59:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |   for (int i = 0; i < leaves.size() - 1; i++) {
      |                   ~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5028 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5020 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 8 ms 5028 KB Output is correct
10 Correct 63 ms 4948 KB Output is correct
11 Correct 60 ms 5000 KB Output is correct
12 Correct 62 ms 4948 KB Output is correct
13 Correct 61 ms 4948 KB Output is correct
14 Correct 64 ms 4948 KB Output is correct
15 Correct 62 ms 4948 KB Output is correct
16 Correct 63 ms 4948 KB Output is correct
17 Correct 61 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Partially correct 4 ms 4948 KB Partially correct
2 Partially correct 3 ms 4948 KB Partially correct
3 Partially correct 3 ms 4948 KB Partially correct
4 Partially correct 4 ms 4960 KB Partially correct
5 Partially correct 3 ms 5076 KB Partially correct
6 Partially correct 3 ms 5024 KB Partially correct
7 Partially correct 3 ms 5148 KB Partially correct
8 Partially correct 4 ms 5024 KB Partially correct
9 Partially correct 3 ms 5076 KB Partially correct
10 Partially correct 3 ms 5028 KB Partially correct
11 Partially correct 4 ms 5076 KB Partially correct
12 Correct 3 ms 5076 KB Output is correct
13 Partially correct 3 ms 5076 KB Partially correct
14 Partially correct 3 ms 5076 KB Partially correct
15 Partially correct 3 ms 5076 KB Partially correct
16 Partially correct 3 ms 5076 KB Partially correct
17 Partially correct 3 ms 5076 KB Partially correct
18 Partially correct 4 ms 5076 KB Partially correct
19 Partially correct 4 ms 5076 KB Partially correct
20 Partially correct 4 ms 5076 KB Partially correct
21 Partially correct 3 ms 5028 KB Partially correct
22 Partially correct 3 ms 5076 KB Partially correct
23 Partially correct 4 ms 5052 KB Partially correct
24 Partially correct 3 ms 5020 KB Partially correct
25 Partially correct 3 ms 5028 KB Partially correct
26 Partially correct 4 ms 5076 KB Partially correct
27 Partially correct 3 ms 4948 KB Partially correct
28 Partially correct 3 ms 5076 KB Partially correct
29 Partially correct 3 ms 5076 KB Partially correct
30 Partially correct 3 ms 5076 KB Partially correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5028 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5020 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 8 ms 5028 KB Output is correct
10 Correct 63 ms 4948 KB Output is correct
11 Correct 60 ms 5000 KB Output is correct
12 Correct 62 ms 4948 KB Output is correct
13 Correct 61 ms 4948 KB Output is correct
14 Correct 64 ms 4948 KB Output is correct
15 Correct 62 ms 4948 KB Output is correct
16 Correct 63 ms 4948 KB Output is correct
17 Correct 61 ms 4948 KB Output is correct
18 Partially correct 4 ms 4948 KB Partially correct
19 Partially correct 3 ms 4948 KB Partially correct
20 Partially correct 3 ms 4948 KB Partially correct
21 Partially correct 4 ms 4960 KB Partially correct
22 Partially correct 3 ms 5076 KB Partially correct
23 Partially correct 3 ms 5024 KB Partially correct
24 Partially correct 3 ms 5148 KB Partially correct
25 Partially correct 4 ms 5024 KB Partially correct
26 Partially correct 3 ms 5076 KB Partially correct
27 Partially correct 3 ms 5028 KB Partially correct
28 Partially correct 4 ms 5076 KB Partially correct
29 Correct 3 ms 5076 KB Output is correct
30 Partially correct 3 ms 5076 KB Partially correct
31 Partially correct 3 ms 5076 KB Partially correct
32 Partially correct 3 ms 5076 KB Partially correct
33 Partially correct 3 ms 5076 KB Partially correct
34 Partially correct 3 ms 5076 KB Partially correct
35 Partially correct 4 ms 5076 KB Partially correct
36 Partially correct 4 ms 5076 KB Partially correct
37 Partially correct 4 ms 5076 KB Partially correct
38 Partially correct 3 ms 5028 KB Partially correct
39 Partially correct 3 ms 5076 KB Partially correct
40 Partially correct 4 ms 5052 KB Partially correct
41 Partially correct 3 ms 5020 KB Partially correct
42 Partially correct 3 ms 5028 KB Partially correct
43 Partially correct 4 ms 5076 KB Partially correct
44 Partially correct 3 ms 4948 KB Partially correct
45 Partially correct 3 ms 5076 KB Partially correct
46 Partially correct 3 ms 5076 KB Partially correct
47 Partially correct 3 ms 5076 KB Partially correct
48 Partially correct 70 ms 9728 KB Partially correct
49 Partially correct 64 ms 10100 KB Partially correct
50 Partially correct 66 ms 10172 KB Partially correct
51 Partially correct 48 ms 8980 KB Partially correct
52 Partially correct 64 ms 10132 KB Partially correct
53 Partially correct 59 ms 9624 KB Partially correct
54 Partially correct 36 ms 13784 KB Partially correct
55 Partially correct 81 ms 22604 KB Partially correct
56 Partially correct 80 ms 15700 KB Partially correct
57 Partially correct 67 ms 12832 KB Partially correct
58 Partially correct 73 ms 12120 KB Partially correct
59 Partially correct 68 ms 10188 KB Partially correct
60 Correct 48 ms 10548 KB Output is correct
61 Partially correct 58 ms 10660 KB Partially correct
62 Partially correct 56 ms 10528 KB Partially correct
63 Partially correct 54 ms 10188 KB Partially correct
64 Partially correct 68 ms 10392 KB Partially correct
65 Partially correct 61 ms 10608 KB Partially correct
66 Partially correct 49 ms 10236 KB Partially correct
67 Partially correct 37 ms 9128 KB Partially correct
68 Partially correct 50 ms 9664 KB Partially correct
69 Partially correct 55 ms 10596 KB Partially correct
70 Partially correct 50 ms 10224 KB Partially correct
71 Partially correct 38 ms 8876 KB Partially correct
72 Partially correct 42 ms 9500 KB Partially correct
73 Partially correct 60 ms 10568 KB Partially correct
74 Partially correct 50 ms 10100 KB Partially correct
75 Partially correct 69 ms 9988 KB Partially correct
76 Partially correct 65 ms 10016 KB Partially correct
77 Partially correct 59 ms 10316 KB Partially correct
78 Partially correct 43 ms 8620 KB Partially correct
79 Partially correct 49 ms 9040 KB Partially correct
80 Partially correct 66 ms 10000 KB Partially correct
81 Partially correct 56 ms 10572 KB Partially correct
82 Partially correct 55 ms 10428 KB Partially correct