#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,popcnt,sse4")
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int mxn = 1e4+10;
int N;
int arr[mxn];
vector<int> tree[mxn];
vector<int> lp[mxn],rp[mxn];
vector<int> lop[110],rop[110];
bitset<mxn> dl,dr;
void dfs(int now){
for(auto &i:lop)i.clear();
for(auto &i:rop)i.clear();
for(auto nxt:tree[now]){
dfs(nxt);
}
for(auto nxt:tree[now]){
for(auto &i:lp[nxt]){
for(auto &j:rp[nxt]){
if(i>arr[now])rop[i].push_back(j);
if(j<arr[now]){
lop[j].push_back(i);
}
}
}
}
dl.reset();
dr.reset();
dl[arr[now]] = dr[arr[now]] = 1;
for(int i = arr[now];i+1<110;i++){
if(dr[i]){
rp[now].push_back(i);
for(auto &j:rop[i+1])dr[j] = 1;
}
}
for(int i = arr[now];i>0;i--){
if(dl[i]){
lp[now].push_back(i);
for(auto &j:lop[i-1])dl[j] = 1;
}
}
return;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N;
for(int i = 1;i<=N;i++)cin>>arr[i];
for(int i = 1;i<N;i++){
int a,b;
cin>>a>>b;
tree[a].push_back(b);
}
dfs(1);
/*
for(int i = 1;i<=N;i++){
cout<<i<<":"<<endl;
for(auto &j:lp[i])cout<<j<<',';cout<<endl;
for(auto &j:rp[i])cout<<j<<',';cout<<endl;
}
*/
cout<<(lp[1].size())*(rp[1].size())<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1172 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
5 |
Correct |
1 ms |
1268 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
7 ms |
1884 KB |
Output is correct |
12 |
Incorrect |
7 ms |
1884 KB |
Output isn't correct |
13 |
Correct |
7 ms |
1884 KB |
Output is correct |
14 |
Correct |
22 ms |
5980 KB |
Output is correct |
15 |
Incorrect |
28 ms |
6124 KB |
Output isn't correct |
16 |
Correct |
25 ms |
6104 KB |
Output is correct |
17 |
Correct |
8 ms |
1884 KB |
Output is correct |
18 |
Correct |
7 ms |
1884 KB |
Output is correct |
19 |
Incorrect |
6 ms |
1884 KB |
Output isn't correct |
20 |
Incorrect |
6 ms |
1884 KB |
Output isn't correct |