Jumping Monkey
Problem Description
There is a tree with n nodes and n−1 edges that make all nodes connected. Each node i has a distinct weight ai. A monkey is jumping on the tree. In one jump, the monkey can jump from node u to node v if and only if av is the greatest of all nodes on the shortest path from node u to node v. The monkey will stop jumping once no more nodes can be reached.
For each integer k∈[1,n], calculate the maximum number of nodes the monkey can reach if he starts from node k.
Input
The first line of the input contains an integer T (1 ≤ T ≤ 10^4), representing the number of test cases.
The first line of each test case contains an integer n (1 ≤ n ≤ 10^5), representing the number of nodes.
Each of the following n−1 lines contains two integers u,v (1 ≤ u,v ≤ n), representing an edge connecting node u and node v. It is guaranteed that the input will form a tree.
The next line contains n distinct integers a1,a2,…,an (1 ≤ ai ≤ 10^9), representing the weight of each node.
It is guaranteed that the sum of n over all test cases does not exceed 8 × 10^5.
Output
For each test case, output n lines. The k-th line contains an integer representing the answer when the monkey starts from node k.
Sample Input
2
3
1 2
2 3
1 2 3
5
1 2
1 3
2 4
2 5
1 4 2 5 3
Sample Output
3
2
1
4
2
3
1
3
Analysis of title
不管我们从哪个点开始出发,最后都可以到达树中权值最大的点。所以就可以以这个树中权值最大的点作为根,建一棵新书。那么,其他的节点在这棵新书中的深度(根节点深度为一),就是我们要输出的答案。
接下来就是建树了,首先需要将所有的节点根据权值大小进行排序,且,我们在排序之后依旧要知道该节点的初始编号,那我们就需要用一个结构体来保存每个节点的权值和初始编号。排序之后,由于按权值从大到小的顺序建树比较困难,所以我们采取从小到大的方式遍历节点进行建树。对于每个结点,我们都要进行遍历,当我们遍历到节点A时,判断与A相连的节点权值是否比其权值要小,如果C节点与A相连且权值比A小的话,将C节点的祖宗节点加入新树,即将A节点作为C节点祖宗节点的的父节点(可以使用并查集)(ps,当我们第一次将C节点加入新树时,我们是将A节点作为C节点的祖宗节点,对于之后我们遍历的每个节点,如果又有一个B节点与C节点相连,那么B节点的权值一定比A节点的权值要大,所以,我们每次需要查找C节点的祖宗节点,只有这样我们才能保证每个节点在新树中的深度才是最后的答案)。
ACcode
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
//#define int long long
#define ull unsigned long long
const int N = 1e6 + 10;
struct node
{
int i, x;
bool operator<(const node p)
{
return x < p.x;
}
} a[100010];
int p[N], b[N];
int ans[N];
int n, m, t;
int h[N], e[N], ne[N], idx;
int h1[N], e1[N], ne1[N], idx1;
int vis[N];
inline int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = x * 10 + c - '0', c = getchar();
return x * f;
}
inline void add(int a, int b)
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
inline void add1(int a, int b)
{
e1[idx1] = b;
ne1[idx1] = h1[a];
h1[a] = idx1++;
}
inline int find(int x)
{
if (p[x] != x)
p[x] = find(p[x]);
return p[x];
}
void dfs(int u)
{
vis[u] = 1;
for (int i = h1[u]; i != -1; i = ne1[i])
{
int j = e1[i];
if (!vis[j])
{
ans[j] = ans[u] + 1;
dfs(j);
}
}
}
int main()
{
t = read();
int x, y;
while (t--)
{
cin >> n;
idx = 0;
idx1 = 0;
for (int i = 0; i <= n * 2; i++)
{
h[i] = -1;
h1[i] = -1;
e[i] = 0;
ne[i] = 0;
e1[i] = 0;
ne1[i] = 0;
vis[i] = 0;
}
for (int i = 1; i < n; i++)
{
x = read();
y = read();
add(x, y);
add(y, x);
}
for (int i = 1; i <= n; i++)
{
a[i].x = read();
a[i].i = i;
b[i] = a[i].x;
p[i] = i;
}
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++)
{
for (int j = h[a[i].i]; j != -1; j = ne[j])
{
int t = e[j];
if (b[t] < a[i].x)
{
int u = find(t);
int v = a[i].i;
p[u] = v;
add1(u, v);
add1(v, u);
}
}
}
for (int i = 1; i <= n; i++)
ans[i] = 1;
dfs(a[n].i);
for (int i = 1; i <= n; i++)
printf("%d\n", ans[i]);
}
}