#include <iostream>
#include <bitset>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
bitset <101> dp[10001][101];
int n,a[10001],u,v,res;
vector <int> ke[10001];
void dfs(int u){
dp[u][a[u]][a[u]]=1;
if (ke[u].empty())
return;
int pos=-1,pos2=1e9;
for (int i=0;i<ke[u].size();i++){
int v=ke[u][i];
dfs(v);
if (a[v]<=a[u])
pos=max(pos,i);
if (a[v]>=a[u])
pos2=min(pos2,i);
}
for (int it=pos2;it<ke[u].size();it++){
int v=ke[u][it];
for (int j=a[u];j<=100;j++)
if (!dp[u][a[u]][j])
for (int i=a[u]+1;i<=j;i++)
dp[u][a[u]][j]=dp[u][a[u]][j]|(dp[u][a[u]][i-1]&dp[v][i][j]);
}
for (int it=pos;it>=0;it--){
int v=ke[u][it];
for (int j=1;j<=a[u];j++)
if (!dp[u][j][a[u]])
for (int i=j;i<a[u];i++)
dp[u][j][a[u]]=dp[u][j][a[u]]|(dp[u][i+1][a[u]]&dp[v][j][i]);
}
for (int i=1;i<=a[u];i++)
for (int j=a[u];j<=100;j++)
dp[u][i][j]=dp[u][i][j]|(dp[u][i][a[u]]&dp[u][a[u]][j]);
}
int main(){
ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
cin >> n;
for (int i=1;i<=n;i++)
cin >> a[i];
for (int i=1;i<n;i++){
cin >> u >> v;
ke[u].push_back(v);
}
for (int i=1;i<=n;i++)
if (!ke[i].empty())
sort(ke[i].begin(),ke[i].end(),[](int i, int j){
return a[i]<a[j];
});
dfs(1);
int cnt=0;
for (int i=1;i<=100;i++)
for (int j=1;j<=100;j++)
cnt+=dp[1][i][j];
cout << cnt;
}
Compilation message
uzastopni.cpp: In function 'void dfs(int)':
uzastopni.cpp:15:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for (int i=0;i<ke[u].size();i++){
| ~^~~~~~~~~~~~~
uzastopni.cpp:23:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (int it=pos2;it<ke[u].size();it++){
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
59 ms |
16428 KB |
Output is correct |
12 |
Correct |
56 ms |
16516 KB |
Output is correct |
13 |
Correct |
58 ms |
16500 KB |
Output is correct |
14 |
Correct |
91 ms |
17844 KB |
Output is correct |
15 |
Correct |
99 ms |
17864 KB |
Output is correct |
16 |
Correct |
86 ms |
17828 KB |
Output is correct |
17 |
Correct |
57 ms |
16468 KB |
Output is correct |
18 |
Correct |
57 ms |
16460 KB |
Output is correct |
19 |
Correct |
23 ms |
16436 KB |
Output is correct |
20 |
Correct |
23 ms |
16372 KB |
Output is correct |