#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}
#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 sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"
using ll = long long;
using ld = long double;
template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}
const int N = 7e4 + 5, M = 2e3 + 1;
int n, m, a[N];
bitset<M> dp[N]; vector<int> adj[N];
void calc_dp(int x){
for(int v : adj[x]) calc_dp(v);
sort(all(adj[x]), [&](int u, int v){
return a[u] < a[v];
});
int ptr = a[x] + 1; dp[x][a[x]] = 1;
for(int v : adj[x]){
if(a[v] <= a[x]) continue;
if(dp[v][ptr]){
ptr = max(ptr, a[v]);
while(ptr <= m && dp[v][ptr]){
dp[x][ptr] = 1, ptr++;
}
}
}
sort(all(adj[x]), [&](int u, int v){
return a[u] > a[v];
});
ptr = a[x] - 1;
for(int v : adj[x]){
if(a[v] >= a[x]) continue;
if(dp[v][ptr]){
ptr = min(ptr, a[v]);
while(ptr >= 1 && dp[v][ptr]){
dp[x][ptr] = 1, --ptr;
}
}
}
}
void Main()
{
cin >> n;
FOR(i, 1, n){
cin >> a[i];
}
FOR(i, 2, n){
int u,v; cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
calc_dp(1);
int l = 1, r = 1;
FOR(i, 1, a[1] - 1) l += dp[1][i];
FOR(i, a[1] + 1, 100) r += dp[1][i];
cout << l * r << el;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
int t = 1; while(t--) Main();
return 0;
}
Compilation message (stderr)
uzastopni.cpp: In function 'int32_t main()':
uzastopni.cpp:103:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | freopen(name".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
uzastopni.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen(name".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |