#include <iostream>
#include <bitset>
#include <vector>
#include <algorithm>
using namespace std;
bool dp[10001][101][101];
int n,a[101],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]][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][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][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++)
sort(ke[i].begin(),ke[i].end(),[](int i, int j){
return a[i]<a[j];
});
dfs(1);
int cnt=0,cnt2=0;
for (int i=1;i<=100;i++)
cnt+=dp[1][i][a[1]],cnt2+=dp[1][a[1]][i];
cout << cnt*cnt2;
}
Compilation message
uzastopni.cpp: In function 'void dfs(int)':
uzastopni.cpp:14:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for (int i=0;i<ke[u].size();i++){
| ~^~~~~~~~~~~~~
uzastopni.cpp:22:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int it=pos2;it<ke[u].size();it++){
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
17 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |