Pagini recente » Cod sursa (job #1171475) | Cod sursa (job #803648) | Cod sursa (job #1726974) | Cod sursa (job #488909) | Cod sursa (job #1065484)
#include <fstream>
#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;
class Scanner { /// hope you don't mind
public:
Scanner(string file, int buffer_size = 1000 * MAXN):
buffer_size_(buffer_size) {
file_ = fopen(file.c_str(), "r");
buffer_ = new char[buffer_size];
pointer_ = buffer_ + buffer_size_;
}
Scanner& operator>>(int &object) {
object = 0;
while (next() < '0' or next() > '9')
advance();
while (next() >= '0' and next() <= '9') {
object = object * 10 + next() - '0';
advance();
}
return *this;
}
private:
char next() {
if (pointer_ == buffer_ + buffer_size_) {
pointer_ = buffer_;
fread(buffer_, 1, buffer_size_, file_);
}
return *pointer_;
}
void advance() {
++pointer_;
}
FILE *file_;
int buffer_size_;
char *buffer_, *pointer_;
};
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() {
Scanner fin(infile);
ofstream fout(outfile);
fin >> N;
for(int i = 1 ; i != N ; ++ i) {
int x, y;
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
for(int i = 1 ; i <= N ; ++ i)
fin >> v[i];
G[0].push_back(1);
v[0] = oo;
DFs(0, -1);
fout << Ans - 1 << '\n';
fout.close();
return 0;
}