Pagini recente » Rating Stefan Tifrea (StefanTifrea) | Borderou de evaluare (job #1044186) | Cod sursa (job #231326) | Diferente pentru info-oltenia-2018/individual/clasament/11-12 intre reviziile 3 si 4 | Cod sursa (job #1065482)
#include <stdio.h>
#include <vector>
#include <bitset>
#include <set>
#include <stack>
#include <algorithm>
using namespace std;
const char infile[] = "guvern.in";
const char outfile[] = "guvern.out";
const int MAXN = 200005;
const int oo = 0x3f3f3f3f;
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
int N, v[MAXN], First[MAXN], Last[MAXN], dp[MAXN], Ans, K;
Graph G, T;
set <pair<int, int> > S;
inline void CatchDP(const int &Node) {
stack <int> Stack, DP;
for(It it = T[Node].begin(), fin = T[Node].end(); it != fin ; ++ it) {
int candidate = *it, actDP = 0;
while(!Stack.empty() && First[candidate] <= First[Stack.top()] && Last[Stack.top()] <= Last[candidate]) {
actDP += DP.top();
Stack.pop();
DP.pop();
}
Stack.push(candidate);
DP.push(max(actDP, dp[candidate]));
}
for(;!DP.empty() ; DP.pop())
dp[Node] += DP.top();
++ dp[Node];
Get_max(Ans, dp[Node]);
}
void DFs(const int &Node, const int &Father) {
S.insert(make_pair(v[Node], Node));
First[Node] = ++ K;
for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it) {
if(*it == Father)
continue;
DFs(*it, Node);
}
Last[Node] = ++ K;
S.erase(make_pair(v[Node], Node));
set< pair<int, int> > :: iterator it = S.upper_bound(make_pair(v[Node], 0));
if(it != S.end())
T[it->second].push_back(Node);
CatchDP(Node);
}
int main() {
freopen(infile, "r", stdin);
freopen(outfile, "w", stdout);
scanf("%d", &N);
for(int i = 1 ; i != N ; ++ i) {
int x, y;
scanf("%d %d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
for(int i = 1 ; i <= N ; ++ i)
scanf("%d", &v[i]);
G[0].push_back(1);
v[0] = oo;
DFs(0, -1);
printf("%d\n", Ans - 1);
return 0;
}