Submission #497044

# Submission time Handle Problem Language Result Execution time Memory
497044 2021-12-22T08:58:49 Z abc864197532 Balanced Tree (info1cup18_balancedtree) C++17
0 / 100
615 ms 524292 KB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define X first
#define Y second
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T i, U ...j) {
	cout << i << ' ', abc(j...);
}
template <typename T> void printv(T l, T r) {
	for (; l != r; ++l) 
		cout << *l << " \n"[l + 1 == r];
}
#ifdef Doludu
#define test(x...) abc("[" + string(#x) + "]", x);
#else
#define test(x...) void(0);
#endif
const int N = 100000;

int main () {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		vector <vector <int>> adj(n);
		for (int i = 0, u, v; i < n - 1; ++i) {
			cin >> u >> v, --u, --v;
			adj[u].pb(v), adj[v].pb(u);
		}
		vector <int> a(n);
		for (int i = 0; i < n; ++i)
			cin >> a[i], assert(a[i] != -1);
		if (count(all(a), 0) == 1 || count(all(a), 1) == 1) {
			cout << -1 << endl;
			continue;
		}
		vector <vector <int>> dp1(n, vector <int>(2, n + 1));
		vector <vector <int>> dp2(n, vector <int>(2, n + 1));
		auto upd = [&](int i, int j, int v) {
			dp1[i][j] = min(dp1[i][j], v);
		};
		function<void(int, int)> dfs = [&](int v, int pa) {
			for (int u : adj[v]) if (u != pa) {
				dfs(u, v);
				upd(v, 0, dp1[u][0] + 1);
				upd(v, 1, dp1[u][1] + 1);
				upd(v, a[u], 1);
			}
		};
		function<void(int, int)> dfs2 = [&](int v, int pa) {
			if (~pa) {
				dp2[v][t] = dp2[pa][t] + 1;
				for (int t : {0, 1}) {
					dp2[v][t] = min(dp2[v][t], dp1[pa][t] + 1);
				}
				dp2[v][a[pa]] = min(dp2[v][a[pa]], 1);
			}
			for (int u : adj[v]) if (u != pa) {
				dfs2(u, v);
			}
		};
		dfs(0, -1);
		dfs2(0, -1);
		int ans = 0;
		for (int i = 0; i < n; ++i) {
			ans = max(ans, min(dp1[i][a[i]], dp2[i][a[i]]));
		}
		cout << ans << endl;
		printv(all(a));
	}
}
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 460 KB Execution killed with signal 6
2 Runtime error 1 ms 460 KB Execution killed with signal 6
# Verdict Execution time Memory Grader output
1 Runtime error 615 ms 524292 KB Execution killed with signal 9
2 Incorrect 91 ms 9144 KB Output isn't correct
3 Runtime error 20 ms 4244 KB Execution killed with signal 6
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 576 KB Execution killed with signal 6
2 Runtime error 32 ms 12584 KB Execution killed with signal 6
3 Runtime error 20 ms 5452 KB Execution killed with signal 6
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 3148 KB Execution killed with signal 6
2 Runtime error 1 ms 588 KB Execution killed with signal 6
3 Runtime error 1 ms 844 KB Execution killed with signal 6
4 Runtime error 1 ms 460 KB Execution killed with signal 6
5 Runtime error 2 ms 576 KB Execution killed with signal 6
6 Runtime error 4 ms 1228 KB Execution killed with signal 6
7 Runtime error 1 ms 588 KB Execution killed with signal 6
8 Runtime error 1 ms 460 KB Execution killed with signal 6
9 Runtime error 1 ms 588 KB Execution killed with signal 6
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 460 KB Execution killed with signal 6
2 Runtime error 1 ms 460 KB Execution killed with signal 6
3 Runtime error 615 ms 524292 KB Execution killed with signal 9
4 Incorrect 91 ms 9144 KB Output isn't correct
5 Runtime error 20 ms 4244 KB Execution killed with signal 6
6 Runtime error 1 ms 576 KB Execution killed with signal 6
7 Runtime error 32 ms 12584 KB Execution killed with signal 6
8 Runtime error 20 ms 5452 KB Execution killed with signal 6
9 Runtime error 8 ms 3148 KB Execution killed with signal 6
10 Runtime error 1 ms 588 KB Execution killed with signal 6
11 Runtime error 1 ms 844 KB Execution killed with signal 6
12 Runtime error 1 ms 460 KB Execution killed with signal 6
13 Runtime error 2 ms 576 KB Execution killed with signal 6
14 Runtime error 4 ms 1228 KB Execution killed with signal 6
15 Runtime error 1 ms 588 KB Execution killed with signal 6
16 Runtime error 1 ms 460 KB Execution killed with signal 6
17 Runtime error 1 ms 588 KB Execution killed with signal 6
18 Runtime error 4 ms 1868 KB Execution killed with signal 6
19 Runtime error 33 ms 12616 KB Execution killed with signal 6
20 Runtime error 1 ms 460 KB Execution killed with signal 6
21 Runtime error 231 ms 61252 KB Execution killed with signal 6
22 Runtime error 2 ms 584 KB Execution killed with signal 6