#include <bits/stdc++.h>
using namespace std;
#define FOR(i, j, k) for(int i=(j); i<=(k); i++)
#define FFOR(i, j, k) for(int i=(j); i<(k); i++)
#define DFOR(i, j, k) for(int i=(j); i>=(k); i--)
#define bug(x) cerr<<#x<<" = "<<(x)<<'\n'
#define pb push_back
#define mp make_pair
#define setbit(s, i) (s|=(1LL<<(i)))
#define bit(s, i) (((s)>>(i))&1LL)
#define mask(i) ((1LL<<(i)))
#define builtin_popcount __builtin_popcountll
using ll=long long;
using ld=long double;
template <typename T> inline void read(T &x){
char c;
bool nega=0;
while((!isdigit(c=getchar()))&&(c!='-'));
if(c=='-'){
nega=1;
c=getchar();
}
x=c-48;
while(isdigit(c=getchar())) x=x*10+c-48;
if(nega) x=-x;
}
template <typename T> inline void writep(T x){
if(x>9) writep(x/10);
putchar(x%10+48);
}
template <typename T> inline void write(T x){
if(x<0){
putchar('-');
x=-x;
}
writep(x);
}
template <typename T> inline void writeln(T x){
write(x);
putchar('\n');
}
#define taskname "UZASTOPNI"
int n;
int c[10001];
vector <int> g[10001];
bitset <101> f[10001];
void dfs(int u){
for(int v: g[u]) dfs(v);
f[u][c[u]]=1;
vector <bitset<101>> a[101];
for(int v: g[u]) if(c[v]>c[u]){
bitset<101> temp;
FOR(i, c[v], 100) temp[i]=f[v][i];
FOR(i, c[u]+1, c[v]) if(f[v][i]) a[i].pb(temp);
}
else if(c[v]<c[u]){
bitset<101> temp;
FOR(i, 1, c[v]) temp[i]=f[v][i];
DFOR(i, c[u]-1, c[v]) if(f[v][i]) a[i].pb(temp);
}
FFOR(i, c[u], 100) if(f[u][i]) for(auto &x: a[i+1]) f[u]|=x;
DFOR(i, c[u], 2) if(f[u][i]) for(auto &x: a[i-1]) f[u]|=x;
}
int main(){
#ifdef Uiharu
if(fopen(taskname".in", "r"))
freopen(taskname".in", "r", stdin);
#endif // Uiharu
read(n);
FOR(i, 1, n) read(c[i]);
FFOR(i, 1, n){
int u, v;
read(u);
read(v);
g[u].pb(v);
}
dfs(1);
int cnt_left=1;
int cnt_right=1;
FFOR(i, 1, c[1]) cnt_left+=f[1][i];
FOR(i, c[1]+1, 100) cnt_right+=f[1][i];
writeln(cnt_left*cnt_right);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
640 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
640 KB |
Output is correct |
4 |
Correct |
2 ms |
640 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
768 KB |
Output is correct |
7 |
Correct |
3 ms |
768 KB |
Output is correct |
8 |
Correct |
3 ms |
768 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
14 ms |
1124 KB |
Output is correct |
12 |
Correct |
12 ms |
1024 KB |
Output is correct |
13 |
Correct |
17 ms |
1024 KB |
Output is correct |
14 |
Correct |
32 ms |
24704 KB |
Output is correct |
15 |
Correct |
34 ms |
24616 KB |
Output is correct |
16 |
Correct |
36 ms |
24696 KB |
Output is correct |
17 |
Correct |
15 ms |
1152 KB |
Output is correct |
18 |
Correct |
9 ms |
1024 KB |
Output is correct |
19 |
Correct |
10 ms |
896 KB |
Output is correct |
20 |
Correct |
13 ms |
896 KB |
Output is correct |