Cod sursa(job #2484005)

Utilizator sichetpaulSichet Paul sichetpaul Data 30 octombrie 2019 17:01:52
Problema Guvern Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <bits/stdc++.h>
#define Nmax 200005
using namespace std;

ifstream f("guvern.in");
ofstream g("guvern.out");

int N, ans;
vector<int> G[Nmax];
set<int> s;
int v[Nmax], pos[Nmax], dp[Nmax];
void DFS(int node, int father) {
    s.insert(v[node]);
    dp[node] = 1;
    auto it = s.upper_bound(v[node]);
    if (it != s.end()) dp[node] += dp[pos[*it]];
    ans = max(ans, dp[node]);

    for (auto son: G[node])
      if (son != father) DFS(son, node);
    s.erase(v[node]);
}
int main()
{
    f >> N;
    for (int i = 1; i < N; ++i) {
        int x, y;
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for (int i = 1; i <= N; ++i)
        f >> v[i], pos[v[i]] = i;

    DFS(1, 0);
    g << ans << '\n';

    return 0;
}