답안 #497022

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
497022 2021-12-22T08:40:56 Z abc864197532 Balanced Tree (info1cup18_balancedtree) C++17
0 / 100
211 ms 67728 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]);
				upd(v, 0, dp2[u][0]);
				upd(v, 1, dp1[u][1]);
				upd(v, 1, dp2[u][1]);
				upd(v, a[u], dep[u]);
			}
		};
		function<void(int, int)> dfs2 = [&](int v, int pa) {
			if (~pa) {
				dp3[v][t] = dp3[pa][t];
				for (int t : {0, 1}) {
					if (dp1[pa][t] == dp1[v][t]) {
						// from v
						dp3[v][t] = min(dp3[v][t], dp2[pa][t]);
					} else {
						// not from v
						dp3[v][t] = min(dp3[v][t], dp1[pa][t]);
					}
				}
			}
			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]] - dep[i], dp3[i][a[i]] - dep[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 452 KB Execution killed with signal 6
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 460 KB Execution killed with signal 11
2 Incorrect 98 ms 13132 KB Output isn't correct
3 Runtime error 31 ms 5804 KB Execution killed with signal 6
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 588 KB Execution killed with signal 6
2 Runtime error 41 ms 13676 KB Execution killed with signal 6
3 Runtime error 13 ms 5868 KB Execution killed with signal 6
# 결과 실행 시간 메모리 Grader output
1 Runtime error 7 ms 3140 KB Execution killed with signal 6
2 Runtime error 1 ms 588 KB Execution killed with signal 6
3 Runtime error 2 ms 788 KB Execution killed with signal 6
4 Runtime error 1 ms 436 KB Execution killed with signal 6
5 Runtime error 1 ms 580 KB Execution killed with signal 6
6 Runtime error 2 ms 1200 KB Execution killed with signal 6
7 Runtime error 1 ms 556 KB Execution killed with signal 6
8 Runtime error 1 ms 436 KB Execution killed with signal 6
9 Runtime error 1 ms 580 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 452 KB Execution killed with signal 6
3 Runtime error 1 ms 460 KB Execution killed with signal 11
4 Incorrect 98 ms 13132 KB Output isn't correct
5 Runtime error 31 ms 5804 KB Execution killed with signal 6
6 Runtime error 1 ms 588 KB Execution killed with signal 6
7 Runtime error 41 ms 13676 KB Execution killed with signal 6
8 Runtime error 13 ms 5868 KB Execution killed with signal 6
9 Runtime error 7 ms 3140 KB Execution killed with signal 6
10 Runtime error 1 ms 588 KB Execution killed with signal 6
11 Runtime error 2 ms 788 KB Execution killed with signal 6
12 Runtime error 1 ms 436 KB Execution killed with signal 6
13 Runtime error 1 ms 580 KB Execution killed with signal 6
14 Runtime error 2 ms 1200 KB Execution killed with signal 6
15 Runtime error 1 ms 556 KB Execution killed with signal 6
16 Runtime error 1 ms 436 KB Execution killed with signal 6
17 Runtime error 1 ms 580 KB Execution killed with signal 6
18 Runtime error 4 ms 1868 KB Execution killed with signal 6
19 Runtime error 35 ms 13836 KB Execution killed with signal 6
20 Runtime error 1 ms 436 KB Execution killed with signal 6
21 Runtime error 211 ms 67728 KB Execution killed with signal 6
22 Runtime error 1 ms 588 KB Execution killed with signal 6