/**Lucky Boy**/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define maxc 1000000007
#define maxn 10004
#define maxm 101
#define pii pair <int,int>
#define Task "UZASTOPNI"
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);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
using namespace std;
int n,val[maxn];
vector <int> a[maxn];
bitset <maxm> l[maxn],r[maxn];
bool dd[maxm];
bool comp(int x,int y)
{
return val[x] < val[y];
}
void Dfs(int u)
{
dd[val[u]] = 1;
l[u].set(val[u],1);
r[u].set(val[u],1);
for (int v : a[u])
if (!dd[val[v]])
{
Dfs(v);
}
for (int v : a[u])
{
if (dd[val[v]] || val[v] < val[u]) continue;
FOR(i,val[u] + 1,val[v])
if (r[u][i-1] && l[v][i])
{
r[u].set(val[v],1);
break;
}
if (r[u][val[v] - 1])
{
r[u] |= r[v];
}
}
reverse(a[u].begin(),a[u].end());
for (int v : a[u])
{
if (dd[val[v]] || val[v] > val[u]) continue;
FOR(i,val[v],val[u] - 1)
if (r[v][i] && l[u][i+1])
{
l[u].set(val[v],1);
break;
}
if (l[u][val[v] + 1])
{
l[u] |= l[v];
}
}
dd[val[u]] = 0;
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//freopen(Task".inp", "r",stdin);
//freopen(Task".out", "w",stdout);
cin >> n;
FOR(i,1,n) cin >> val[i];
FOR(i,2,n)
{
int x,y;
cin >> x >> y;
a[x].pb(y);
}
FOR(i,1,n) sort(a[i].begin(),a[i].end(),comp);
Dfs(1);
int ll = l[1].count(),
rr = r[1].count();
cout << ll * rr;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
640 KB |
Output is correct |
5 |
Correct |
3 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
3 ms |
640 KB |
Output is correct |
11 |
Correct |
4 ms |
1152 KB |
Output is correct |
12 |
Correct |
7 ms |
1152 KB |
Output is correct |
13 |
Correct |
7 ms |
1152 KB |
Output is correct |
14 |
Correct |
7 ms |
1024 KB |
Output is correct |
15 |
Correct |
9 ms |
1024 KB |
Output is correct |
16 |
Correct |
8 ms |
1024 KB |
Output is correct |
17 |
Correct |
6 ms |
1152 KB |
Output is correct |
18 |
Correct |
6 ms |
1152 KB |
Output is correct |
19 |
Correct |
7 ms |
868 KB |
Output is correct |
20 |
Correct |
5 ms |
768 KB |
Output is correct |