#include<iostream>
#include<bitset>
#include<vector>
#include<algorithm>
std::vector<int> val;
std::vector<std::vector<int>> child;
using set=std::bitset<101>;
struct sets{ // half-open range
set left,right;
};
sets solve(int node){
sets ans;
ans.left[val[node]]=1;ans.right[val[node]+1]=1;
std::vector<int> lnodes,rnodes;
for(int c:child[node])
if(val[c]<val[node])
lnodes.push_back(c);
else
rnodes.push_back(c);
std::sort(begin(lnodes),end(lnodes),[](int a,int b){return val[a]>val[b];});
std::sort(begin(rnodes),end(rnodes),[](int a,int b){return val[a]<val[b];});
for(int c:lnodes){
sets t=solve(c);
if((t.right&ans.left).any())
ans.left|=t.left;
}
for(int c:rnodes){
sets t=solve(c);
if((t.left&ans.right).any())
ans.right|=t.right;
}
return ans;
}
int main(){
std::ios::sync_with_stdio(0);std::cin.tie(0);
int nnode;std::cin>>nnode;
val.resize(nnode);
for(int& x:val){
std::cin>>x;
--x;
}
child.resize(nnode);
for(int _=nnode-1;_-->0;){
int a,b;std::cin>>a>>b;--a;--b;
child[a].push_back(b);
}
sets a=solve(0);
std::cout<<a.left.count()*a.right.count()<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
9 ms |
888 KB |
Output is correct |
12 |
Correct |
8 ms |
896 KB |
Output is correct |
13 |
Correct |
6 ms |
896 KB |
Output is correct |
14 |
Correct |
10 ms |
2944 KB |
Output is correct |
15 |
Correct |
11 ms |
2944 KB |
Output is correct |
16 |
Correct |
12 ms |
3072 KB |
Output is correct |
17 |
Correct |
9 ms |
896 KB |
Output is correct |
18 |
Correct |
10 ms |
896 KB |
Output is correct |
19 |
Correct |
6 ms |
768 KB |
Output is correct |
20 |
Correct |
8 ms |
768 KB |
Output is correct |