Submission #1020246

# Submission time Handle Problem Language Result Execution time Memory
1020246 2024-07-11T18:13:09 Z ali2241 Village (BOI20_village) C++17
0 / 100
700 ms 5208 KB
#include <bits/stdc++.h>
//ali2241

using namespace std;
#define int long long
//using ll = __int128;
using ld = long double;
const int M = 10589;
const int N = 100001;

vector<int> adj[N];
int get (int a, int b, int c = -1) {
	if (a == b) {
		return 0;
	}
	int ans = -1e9;

    for (int i : adj[a]) {
		if (i != c)
		ans = max(ans, get(i, b, a));
	}
	return ans + 1;
}
int fact(int n) {
	if (n == 0)
	return 1;
	return n * fact(n - 1);
}
void fun() {
    int n;
    cin >> n;
    for (int i = 0; i < n - 1; ++i) {
    	int a, b;
    	cin >> a >> b;
    	adj[a].push_back(b);
    	adj[b].push_back(a);
	}
	pair<int, vector<int>> mn = {1e9, {}};
	pair<int, vector<int>> mx = {-1e9, {}};
	vector<int> per;
	for (int i = 1; i <= n; ++i) {
		per.push_back(i);
	}
	for (int i = 0; i < fact(n); ++i) {
		int ths = 0;
		bool b = 0;
		for (int i = 0; i < n; ++i) {
			if (i + 1 == per[i]) {
				b = 1;
				continue;
			}
			ths += get(i + 1, per[i]);
		}
		
		if (ths < mn.first and !b) {
			mn = {ths, per};
		}
		if (ths > mx.first and !b) {
			mx = {ths, per};
		}
		next_permutation(per.begin(), per.end());
	}
	cout << mn.first << " " << mx.first << "\n";
	for (int i = 0; i < n; ++i) {
		cout << mn.second[i] << " ";
	}
	cout << "\n";
	for (int i = 0; i < n; ++i) {
		cout << mx.second[i] << " ";
	}
}

int32_t main() {
//    freopen("output.txt", "w", stdout);
//    freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(0);
    int tc = 1;
    while (tc--)
        fun();
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2648 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 1 ms 2808 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 3 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 8 ms 2652 KB Output is correct
9 Correct 85 ms 2652 KB Output is correct
10 Execution timed out 1022 ms 2784 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 5208 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2648 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 1 ms 2808 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 3 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 8 ms 2652 KB Output is correct
9 Correct 85 ms 2652 KB Output is correct
10 Execution timed out 1022 ms 2784 KB Time limit exceeded
11 Halted 0 ms 0 KB -