This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifdef LC
#include "pch.h"
#else
#include <bits/stdc++.h>
#endif
using namespace std;
using ll = long long;
#define int ll
#define all(x) x.begin(), x.end()
#define x first
#define y second
#define mp make_pair
#define mt make_tuple
const int N = 1e5 + 10;
int n;
vector<int> g[N];
int c[N];
int dist[N];
int who[N];
int check(int value) {
queue<int> q;
fill_n(dist, n, N);
fill_n(who, n, -1);
for (int i = 0; i < n; ++i) {
if (c[i] == value) {
dist[i] = 0;
who[i] = i;
q.push(i);
}
}
if (q.empty()) {
return 0;
}
while (q.size()) {
int v = q.front(); q.pop();
for (int u : g[v]) {
if (dist[u] > dist[v] + 1) {
who[u] = who[v];
dist[u] = dist[v] + 1;
q.push(u);
}
}
}
int answer = 0;
for (int i = 0; i < n; ++i) {
for (int j : g[i]) {
if (who[i] != who[j]) {
answer = max(answer, dist[i] + 1 + dist[j]);
}
}
}
return answer;
}
void solve() {
cin >> n;
fill_n(g, n, vector<int>());
for (int v, u, i = 0; i < n - 1; ++i) {
cin >> v >> u;
--v, --u;
g[v].push_back(u);
g[u].push_back(v);
}
for (int i = 0; i < n; ++i) {
cin >> c[i];
}
cout << max(check(0), check(1)) << "\n";
for (int i = 0; i < n; ++i) {
cout << c[i] << " ";
}
cout << "\n";
}
signed main() {
#ifdef LC
assert(freopen("input.txt", "r", stdin));
#endif
ios::sync_with_stdio(0); cin.tie(0);
int tn;
cin >> tn;
while (tn--) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |