This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/0439"
#include <algorithm>
#include <functional>
#include <limits>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;
#include <cstdio>
#include <cstring>
#include <string>
#include <type_traits>
#include "../util/fastio.cpp"
#include "../tree/virtual_tree_helper.cpp"
template <typename T>
using GPQ = priority_queue<T, vector<T>, greater<T>>;
int main() {
Scanner sc;
Printer pr;
int n;
sc.read(n);
vector<vector<int>> color(n);
VirtualTreeHelper vt(n);
for (int i = 0; i < n; ++i) {
int x;
sc.read(x);
color[x - 1].emplace_back(i);
}
for (int i = 0; i < n - 1; ++i) {
int u, v;
sc.read(u, v);
--u;
--v;
vt.add_edge(u, v);
}
vt.build();
vector<int> ans(n, INF<int>);
vector<int> cmp(n), dist(n);
vector<vector<int>> g(n);
vector<int> is_target(n, 0);
for (auto &&vs : color) {
if (vs.empty()) continue;
for (int v : vs) is_target[v] = 1;
auto tree = vt.make(vs);
for (int v : tree.vertices) g[v].clear();
for (int i = 1; i < (int)tree.vertices.size(); ++i) {
int v = tree.vertices[i];
int p = tree.parent[i];
g[v].emplace_back(p);
g[p].emplace_back(v);
}
GPQ<pair<int, int>> q;
for (int i = 0; i < (int)tree.vertices.size(); ++i) {
int v = tree.vertices[i];
if (is_target[v]) {
dist[v] = 0;
cmp[v] = v;
q.emplace(0, v);
} else {
dist[v] = INF<int>;
}
}
while (!q.empty()) {
auto [d, v] = q.top();
q.pop();
if (d != dist[v]) continue;
for (auto &&u : g[v]) {
if (dist[u] > dist[v] + vt.distance(v, u)) {
dist[u] = dist[v] + vt.distance(v, u);
cmp[u] = cmp[v];
q.emplace(dist[u], u);
}
}
}
for (int v : tree.vertices) {
for (auto &&u : g[v]) {
int a = cmp[v], b = cmp[u];
if (a != b) ans[a] = min(ans[a], vt.distance(a, b));
}
}
for (int v : vs) is_target[v] = 0;
}
for (auto &&x : ans) pr.println(x);
return 0;
}#line 1 "test/aoj0439_virtual_tree_helper.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/0439"
#include <algorithm>
#include <functional>
#include <limits>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;
#include <cstdio>
#include <cstring>
#include <string>
#include <type_traits>
#line 1 "util/fastio.cpp"
using namespace std;
extern "C" int fileno(FILE *);
extern "C" int isatty(int);
template<class T, class = void>
struct is_fastio_range : false_type {};
template<class T>
struct is_fastio_range<T, void_t<decltype(declval<T &>().begin()), decltype(declval<T &>().end())>> : true_type {};
template<class T, class = void>
struct has_fastio_value : false_type {};
template<class T>
struct has_fastio_value<T, void_t<decltype(declval<const T &>().value())>> : true_type {};
struct FastIoDigitTable {
char num[40000];
constexpr FastIoDigitTable() : num() {
for (int i = 0; i < 10000; ++i) {
int x = i;
for (int j = 3; j >= 0; --j) {
num[i * 4 + j] = char('0' + x % 10);
x /= 10;
}
}
}
};
struct Scanner {
static constexpr int BUFSIZE = 1 << 17;
static constexpr int OFFSET = 64;
char buf[BUFSIZE + 1];
int idx, size;
bool interactive;
Scanner() : idx(0), size(0), interactive(isatty(fileno(stdin))) {}
inline void load() {
int len = size - idx;
memmove(buf, buf + idx, len);
if (interactive) {
if (fgets(buf + len, BUFSIZE + 1 - len, stdin)) size = len + (int)strlen(buf + len);
else size = len;
} else {
size = len + (int)fread(buf + len, 1, BUFSIZE - len, stdin);
}
idx = 0;
buf[size] = 0;
}
inline void ensure() {
if (idx + OFFSET > size) load();
}
inline void ensure_interactive() {
if (idx == size) load();
}
inline char skip() {
if (interactive) {
ensure_interactive();
while (buf[idx] && buf[idx] <= ' ') {
++idx;
ensure_interactive();
}
return buf[idx++];
}
ensure();
while (buf[idx] && buf[idx] <= ' ') {
++idx;
ensure();
}
return buf[idx++];
}
template<class T, typename enable_if<is_integral<T>::value, int>::type = 0>
void read(T &x) {
if (interactive) {
char c = skip();
bool neg = false;
if constexpr (is_signed<T>::value) {
if (c == '-') {
neg = true;
ensure_interactive();
c = buf[idx++];
}
}
x = 0;
while (c >= '0') {
x = x * 10 + (c & 15);
ensure_interactive();
c = buf[idx++];
}
if constexpr (is_signed<T>::value) {
if (neg) x = -x;
}
return;
}
char c = skip();
bool neg = false;
if constexpr (is_signed<T>::value) {
if (c == '-') {
neg = true;
c = buf[idx++];
}
}
x = 0;
while (c >= '0') {
x = x * 10 + (c & 15);
c = buf[idx++];
}
if constexpr (is_signed<T>::value) {
if (neg) x = -x;
}
}
template<class T, typename enable_if<!is_integral<T>::value && !is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value && has_fastio_value<T>::value, int>::type = 0>
void read(T &x) {
long long v;
read(v);
x = T(v);
}
template<class Head, class Next, class... Tail>
void read(Head &head, Next &next, Tail &...tail) {
read(head);
read(next, tail...);
}
template<class T, class U>
void read(pair<T, U> &p) {
read(p.first, p.second);
}
template<class T, typename enable_if<is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value, int>::type = 0>
void read(T &a) {
for (auto &x : a) read(x);
}
void read(char &c) {
c = skip();
}
void read(string &s) {
s.clear();
if (interactive) {
ensure_interactive();
while (buf[idx] && buf[idx] <= ' ') {
++idx;
ensure_interactive();
}
while (true) {
int start = idx;
while (idx < size && buf[idx] > ' ') ++idx;
s.append(buf + start, idx - start);
if (idx < size) break;
load();
if (size == 0) break;
}
if (idx < size) ++idx;
return;
}
ensure();
while (buf[idx] && buf[idx] <= ' ') {
++idx;
ensure();
}
while (true) {
int start = idx;
while (idx < size && buf[idx] > ' ') ++idx;
s.append(buf + start, idx - start);
if (idx < size) break;
load();
}
if (idx < size) ++idx;
}
};
struct Printer {
static constexpr int BUFSIZE = 1 << 17;
static constexpr int OFFSET = 64;
char buf[BUFSIZE];
int idx;
bool interactive;
inline static constexpr FastIoDigitTable table{};
Printer() : idx(0), interactive(isatty(fileno(stdout))) {}
~Printer() { flush(); }
inline void flush() {
if (idx) {
fwrite(buf, 1, idx, stdout);
idx = 0;
}
}
inline void pc(char c) {
if (idx > BUFSIZE - OFFSET) flush();
buf[idx++] = c;
if (interactive && c == '\n') flush();
}
inline void print_range(const char *s, size_t n) {
size_t pos = 0;
while (pos < n) {
if (idx == BUFSIZE) flush();
size_t chunk = min(n - pos, (size_t)(BUFSIZE - idx));
memcpy(buf + idx, s + pos, chunk);
idx += (int)chunk;
pos += chunk;
}
}
void print(const char *s) {
print_range(s, strlen(s));
}
void print(const string &s) {
print_range(s.data(), s.size());
}
void print(char c) {
pc(c);
}
void print(bool b) {
pc(char('0' + (b ? 1 : 0)));
}
template<class T, typename enable_if<is_integral<T>::value && !is_same<T, bool>::value, int>::type = 0>
void print(T x) {
if (idx > BUFSIZE - 100) flush();
using U = typename make_unsigned<T>::type;
U y;
if constexpr (is_signed<T>::value) {
if (x < 0) {
buf[idx++] = '-';
y = U(0) - static_cast<U>(x);
} else {
y = static_cast<U>(x);
}
} else {
y = x;
}
if (y == 0) {
buf[idx++] = '0';
return;
}
static constexpr int TMP_SIZE = sizeof(U) * 10 / 4;
char tmp[TMP_SIZE];
int pos = TMP_SIZE;
while (y >= 10000) {
pos -= 4;
memcpy(tmp + pos, table.num + (y % 10000) * 4, 4);
y /= 10000;
}
if (y >= 1000) {
memcpy(buf + idx, table.num + (y << 2), 4);
idx += 4;
} else if (y >= 100) {
memcpy(buf + idx, table.num + (y << 2) + 1, 3);
idx += 3;
} else if (y >= 10) {
unsigned q = (unsigned(y) * 205) >> 11;
buf[idx] = char('0' + q);
buf[idx + 1] = char('0' + (unsigned(y) - q * 10));
idx += 2;
} else {
buf[idx++] = char('0' + y);
}
memcpy(buf + idx, tmp + pos, TMP_SIZE - pos);
idx += TMP_SIZE - pos;
}
template<class T, typename enable_if<!is_integral<T>::value && !is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value && has_fastio_value<T>::value, int>::type = 0>
void print(const T &x) {
print(x.value());
}
template<class T, typename enable_if<is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value, int>::type = 0>
void print(const T &a) {
bool first = true;
for (auto &&x : a) {
if (!first) pc(' ');
first = false;
print(x);
}
}
template<class T>
void println(const T &x) {
print(x);
pc('\n');
}
template<class Head, class... Tail>
void println(const Head &head, const Tail &...tail) {
print(head);
((pc(' '), print(tail)), ...);
pc('\n');
}
void println() {
pc('\n');
}
};
template<class T>
Scanner &operator>>(Scanner &in, T &x) {
in.read(x);
return in;
}
template<class T>
Printer &operator<<(Printer &out, const T &x) {
out.print(x);
return out;
}
/**
* @brief 高速入出力(Fast IO)
*/
#line 1 "datastructure/sparsetable.cpp"
template <class F>
struct SparseTable {
using T = typename F::T;
vector<vector<T>> table;
vector<int> u;
SparseTable() = default;
explicit SparseTable(const vector<T> &v){ build(v); }
void build(const vector<T> &v){
int n = v.size(), m = 1;
while((1<<m) <= n) m++;
table.assign(m, vector<T>(n));
u.assign(n+1, 0);
for (int i = 2; i <= n; ++i) {
u[i] = u[i>>1] + 1;
}
for (int i = 0; i < n; ++i) {
table[0][i] = v[i];
}
for (int i = 1; i < m; ++i) {
int x = (1<<(i-1));
for (int j = 0; j < n; ++j) {
table[i][j] = F::f(table[i-1][j], table[i-1][min(j+x, n-1)]);
}
}
}
T query(int a, int b){
int l = b-a;
return F::f(table[u[l]][a], table[u[l]][b-(1<<u[l])]);
}
};
/**
* @brief Sparse Table
*/
#line 2 "tree/auxtree.cpp"
struct F {
using T = pair<int, int>;
static T f(T a, T b) { return min(a, b); }
static T e() { return T{INF<int>, -1}; }
};
class AuxTree {
SparseTable<F> table;
void dfs_euler(int v, int p, int d, int &k, int &l){
id[v] = k;
vs[k] = v;
depth[k++] = d;
dep[v] = d;
fi[v] = l++;
for (auto &&u : G[v]) {
if(u != p){
dfs_euler(u, v, d+1, k, l);
vs[k] = v;
depth[k++] = d;
}
}
}
public:
int n;
vector<vector<int>> G, out;
vector<int> vs, depth, dep, id, fi;
explicit AuxTree(int n) : table(), n(n), G(n), out(n), vs(2*n-1), depth(2*n-1), dep(n), id(n), fi(n) {};
void add_edge(int a, int b){
G[a].emplace_back(b);
G[b].emplace_back(a);
}
void eulertour(int root) {
int k = 0, l = 0;
dfs_euler(root, -1, 0, k, l);
}
void buildLCA(int root = 0){
eulertour(root);
vector<pair<int, int>> v(2*n-1);
for (int i = 0; i < 2*n-1; ++i) {
v[i] = make_pair(depth[i], vs[i]);
}
table.build(v);
}
void make(vector<int> &v){
sort(v.begin(),v.end(), [&](int a, int b){ return fi[a] < fi[b]; });
v.erase(unique(v.begin(), v.end()), v.end());
int k = v.size();
stack<int> s;
s.emplace(v.front());
for (int i = 0; i+1 < k; ++i) {
int w = LCA(v[i], v[i+1]);
if(w != v[i]){
int u = s.top(); s.pop();
while(!s.empty() && dep[w] < dep[s.top()]){
out[s.top()].emplace_back(u);
out[u].emplace_back(s.top());
u = s.top(); s.pop();
}
if(s.empty() || s.top() != w){
s.emplace(w);
v.emplace_back(w);
}
out[w].emplace_back(u);
out[u].emplace_back(w);
}
s.emplace(v[i+1]);
}
while(s.size() > 1){
int u = s.top(); s.pop();
out[s.top()].emplace_back(u);
out[u].emplace_back(s.top());
}
}
void clear(vector<int> &v){
for (auto &&i : v) {
out[i].clear();
out[i].shrink_to_fit();
}
}
int LCA(int u, int v){
if(id[u] > id[v]) swap(u, v);
return table.query(id[u], id[v]+1).second;
}
int distance(int u, int v){
return dep[u]+dep[v]-2*dep[LCA(u, v)];
}
};
/**
* @brief 補助木(Aux Tree)
*/
#line 2 "tree/virtual_tree_helper.cpp"
struct VirtualTree {
int root;
vector<int> vertices;
vector<int> parent;
};
class VirtualTreeHelper {
AuxTree aux;
vector<int> mark, parent_buf;
int stamp = 0;
public:
explicit VirtualTreeHelper(int n) : aux(n), mark(n, 0), parent_buf(n, -1) {}
void add_edge(int u, int v) {
aux.add_edge(u, v);
}
void build(int root = 0) {
aux.buildLCA(root);
}
int lca(int u, int v) {
return aux.LCA(u, v);
}
int distance(int u, int v) {
return aux.distance(u, v);
}
VirtualTree make(vector<int> vertices) {
aux.make(vertices);
sort(vertices.begin(), vertices.end(), [&](int a, int b) { return aux.fi[a] < aux.fi[b]; });
vertices.erase(unique(vertices.begin(), vertices.end()), vertices.end());
VirtualTree res;
res.root = vertices.front();
++stamp;
vector<int> st = {res.root};
mark[res.root] = stamp;
parent_buf[res.root] = -1;
while (!st.empty()) {
int v = st.back();
st.pop_back();
res.vertices.emplace_back(v);
res.parent.emplace_back(parent_buf[v]);
for (auto &&u : aux.out[v]) {
if (mark[u] == stamp) continue;
mark[u] = stamp;
parent_buf[u] = v;
st.emplace_back(u);
}
}
aux.clear(vertices);
return res;
}
};
/**
* @brief Virtual Tree Helper
*/
#line 20 "test/aoj0439_virtual_tree_helper.test.cpp"
template <typename T>
using GPQ = priority_queue<T, vector<T>, greater<T>>;
int main() {
Scanner sc;
Printer pr;
int n;
sc.read(n);
vector<vector<int>> color(n);
VirtualTreeHelper vt(n);
for (int i = 0; i < n; ++i) {
int x;
sc.read(x);
color[x - 1].emplace_back(i);
}
for (int i = 0; i < n - 1; ++i) {
int u, v;
sc.read(u, v);
--u;
--v;
vt.add_edge(u, v);
}
vt.build();
vector<int> ans(n, INF<int>);
vector<int> cmp(n), dist(n);
vector<vector<int>> g(n);
vector<int> is_target(n, 0);
for (auto &&vs : color) {
if (vs.empty()) continue;
for (int v : vs) is_target[v] = 1;
auto tree = vt.make(vs);
for (int v : tree.vertices) g[v].clear();
for (int i = 1; i < (int)tree.vertices.size(); ++i) {
int v = tree.vertices[i];
int p = tree.parent[i];
g[v].emplace_back(p);
g[p].emplace_back(v);
}
GPQ<pair<int, int>> q;
for (int i = 0; i < (int)tree.vertices.size(); ++i) {
int v = tree.vertices[i];
if (is_target[v]) {
dist[v] = 0;
cmp[v] = v;
q.emplace(0, v);
} else {
dist[v] = INF<int>;
}
}
while (!q.empty()) {
auto [d, v] = q.top();
q.pop();
if (d != dist[v]) continue;
for (auto &&u : g[v]) {
if (dist[u] > dist[v] + vt.distance(v, u)) {
dist[u] = dist[v] + vt.distance(v, u);
cmp[u] = cmp[v];
q.emplace(dist[u], u);
}
}
}
for (int v : tree.vertices) {
for (auto &&u : g[v]) {
int a = cmp[v], b = cmp[u];
if (a != b) ans[a] = min(ans[a], vt.distance(a, b));
}
}
for (int v : vs) is_target[v] = 0;
}
for (auto &&x : ans) pr.println(x);
return 0;
}