답안 #497025

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
497025 2021-12-22T08:46:30 Z abc864197532 Balanced Tree (info1cup18_balancedtree) C++17
0 / 100
212 ms 61228 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, 1 << 30));
		vector <vector <int>> dp2(n, vector <int>(2, 1 << 30));
		vector <vector <int>> dp3(n, vector <int>(2, 1 << 30));
		vector <int> dep(n, 0);
		auto upd = [&](int i, int j, int v) {
			if (dp1[i][j] >= v)
				dp2[i][j] = dp1[i][j], dp1[i][j] = v;
			else if (dp2[i][j] >= v)
				dp2[i][j] = v;
		};
		function<void(int, int)> dfs = [&](int v, int pa) {
			for (int u : adj[v]) if (u != pa) {
				dep[u] = dep[v] + 1;
				dfs(u, v);
				upd(v, 0, dp1[u][0] + 1);
				upd(v, 0, dp2[u][0] + 1);
				upd(v, 1, dp1[u][1] + 1);
				upd(v, 1, dp2[u][1] + 1);
				upd(v, a[u], 1);
			}
		};
		function<void(int, int)> dfs2 = [&](int v, int pa) {
			if (~pa) {
				dp3[v][t] = dp3[pa][t] + 1;
				for (int t : {0, 1}) {
					if (dp1[pa][t] == dp1[v][t]) {
						// from v
						dp3[v][t] = min(dp3[v][t], dp2[pa][t] + 1);
					} else {
						// not from v
						dp3[v][t] = min(dp3[v][t], dp1[pa][t] + 1);
					}
				}
				dp3[v][a[pa]] = min(dp3[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]], dp3[i][a[i]]));
		}
		cout << ans << endl;
		printv(all(a));
	}
}
# 결과 실행 시간 메모리 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
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 460 KB Execution killed with signal 11
2 Incorrect 125 ms 11900 KB Output isn't correct
3 Runtime error 24 ms 5084 KB Execution killed with signal 6
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 460 KB Execution killed with signal 6
2 Runtime error 35 ms 12348 KB Execution killed with signal 6
3 Runtime error 12 ms 5232 KB Execution killed with signal 6
# 결과 실행 시간 메모리 Grader output
1 Runtime error 9 ms 2860 KB Execution killed with signal 6
2 Runtime error 1 ms 588 KB Execution killed with signal 6
3 Runtime error 2 ms 716 KB Execution killed with signal 6
4 Runtime error 1 ms 460 KB Execution killed with signal 6
5 Runtime error 1 ms 588 KB Execution killed with signal 6
6 Runtime error 2 ms 1052 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
# 결과 실행 시간 메모리 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 1 ms 460 KB Execution killed with signal 11
4 Incorrect 125 ms 11900 KB Output isn't correct
5 Runtime error 24 ms 5084 KB Execution killed with signal 6
6 Runtime error 1 ms 460 KB Execution killed with signal 6
7 Runtime error 35 ms 12348 KB Execution killed with signal 6
8 Runtime error 12 ms 5232 KB Execution killed with signal 6
9 Runtime error 9 ms 2860 KB Execution killed with signal 6
10 Runtime error 1 ms 588 KB Execution killed with signal 6
11 Runtime error 2 ms 716 KB Execution killed with signal 6
12 Runtime error 1 ms 460 KB Execution killed with signal 6
13 Runtime error 1 ms 588 KB Execution killed with signal 6
14 Runtime error 2 ms 1052 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 1612 KB Execution killed with signal 6
19 Runtime error 31 ms 12332 KB Execution killed with signal 6
20 Runtime error 1 ms 460 KB Execution killed with signal 6
21 Runtime error 212 ms 61228 KB Execution killed with signal 6
22 Runtime error 1 ms 588 KB Execution killed with signal 6