这道题由于考场上基本上口胡出来了,就贴个代码吧。
考场没有写出来的原因是因为最后找答案的姿势不够清奇啊~~~
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
struct lpl{
int data, num;
bool operator < (const lpl &a)const{
return data < a.data;
}
}lin[maxn];
int n, a, b, ans, w[maxn], lpd[maxn];
vector<int> point[maxn];
inline void connect(int aaa, int bbb)
{
point[aaa].push_back(bbb); point[bbb].push_back(aaa);
}
inline void workk()
{
memset(lpd, 0, sizeof(lpd));
for(int i = n; i >= 1; --i)
{
lpd[i] += 1;
for(int t = point[i].size() - 1; t >= 0; --t)
{
int now = point[i][t];
if(i > now)
{
lpd[now] += lpd[i];
}
}
}
for(int i = 1; i <= n; ++i) ans = max(ans, lpd[i]);
}
int main()
{
while(~scanf("%d", &n))
{
for(int i = 1; i <= n; ++i) point[i].clear();
for(int i = 1; i <= n; ++i) scanf("%d", &lin[i].data), lin[i].num = i;
sort(lin + 1, lin + 1 + n);
for(int i = 1; i <= n; ++i) w[lin[i].num] = i;
for(int i = 1; i < n; ++i) scanf("%d%d", &a, &b), connect(w[a], w[b]);
ans = 0; workk(); printf("%d
", ans);
}
return 0;
}