#include <bits/stdc++.h>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define pi pair<int,int>
#define pii pair<int,pi>
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(a) (int) a.size()
#define endl '\n'
#define data "data"
using namespace std;
const ll linf = 1e18;
const int inf = 1e9;
const int MOD = 1e9 + 7, N = 1e4;
const int LIM = 110;
void add(int &a, int b)
{
a += b;
if(a>=MOD)
a-=MOD;
if(a<0)
a += MOD;
}
int modulo(int x)
{
if(x<=1)
return 1;
return (MOD - MOD/x) * modulo(MOD/x) % MOD;
}
int mul(int a, int b)
{
return (1ll *a%MOD * b%MOD) % MOD;
}
int n;
int V[N+3];
vector<int> adj[N+3];
bitset<LIM> dp[N+3][LIM];
vector<pi> S[N+3];
vector<int> tmp[LIM];
void inp(void)
{
cin >> n;
FOR(i, 1, n) cin >> V[i];
FOR(i, 1, n -1)
{
int u, v; cin >> u >> v;
adj[u].pb(v);
}
}
void dfs(int u)
{
FOR(i, 1, 100) tmp[i].clear();
FOR(i, 0, sz(adj[u]) -1)
{
int v = adj[u][i];
dfs(v);
}
FOR(i, 0, sz(adj[u]) -1)
{
int v = adj[u][i];
FOR(j, 0, sz(S[v]) -1)
tmp[S[v][j].fi].pb(S[v][j].se);
}
ROF(L, LIM - 1, 1)
{
if(L == V[u])
{
dp[u][L] |= dp[u][L+1];
dp[u][L][L] = 1;
}
else
{
FOR(i, 0, sz(tmp[L]) - 1)
{
int R = tmp[L][i];
if(L <= V[u] && V[u] <= R) continue;
dp[u][L] |= dp[u][R+1];
dp[u][L][R] = 1;
}
}
ROF(R, LIM - 1, L)
{
if(dp[u][L][R] && L <= V[u] && V[u] <= R)
S[u].pb({L, R});
}
}
}
void solve(void)
{
dfs(1);
cout << sz(S[1]);
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if(fopen(data".inp", "r"))
{
freopen(data".inp","r",stdin);
freopen(data".out","w",stdout);
}
inp();
solve();
return 0;
}
Compilation message (stderr)
uzastopni.cpp: In function 'int main()':
uzastopni.cpp:115:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | freopen(data".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
uzastopni.cpp:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | freopen(data".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |