#include <bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define FF first
#define SS second
#define pb push_back
#define sz(x) (int)x.size()
#define PI acos(-1.0)
#define lb lower_bound
#define ub upper_bound
#define all(a) (a).begin(), (a).end()
#define odd(x) __builtin_parity((int)x)
#define cntbit(x) __builtin_popcount(x)
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ii, int> i3;
const int N = 1e4+7, M=1e2+1;
const int MOD = 1e9+7;
const int oo = 1e15+7;
int n, m, k, t;
int a[N];
vector<int> adj[N], q[M];
vector<ii> segment[N];
bitset<M> mark[N][M];
void dfs(int u, int p=-1) {
for(int v:adj[u]) if(v!=p) dfs(v, u);
for(int i=1;i<M;i++) q[i].clear();
for(int v:adj[u]) if(v!=p)
for(ii s:segment[v]) q[s.FF].pb(s.SS);
for(int l=M-1;l>0;l--) {
if(l==a[u]) {
mark[u][l]|=mark[u][l+1];
mark[u][l].set(l);
} else {
for(int r:q[l]) if(r<a[u]||l>a[u]) {
mark[u][l]|=mark[u][r+1];
mark[u][l].set(r);
}
}
for(int r=M-1;r>=l;r--)
if(mark[u][l].test(r)&&r>=a[u]&&l<=a[u])
segment[u].pb(ii(l, r));
}
}
signed main() {
//freopen("test.inp", "r", stdin);
//freopen("test.out", "w", stdout);
//fastIO;
scanf("%d", &n);
int u, v;
for(int i=1;i<=n;i++) scanf("%d", &a[i]);
for(int i=1;i<n;i++) {
scanf("%d%d", &u, &v);
adj[u].pb(v), adj[v].pb(u);
}
dfs(1);
printf("%d", sz(segment[1]));
return 0;
}
/* stuff you should look for
- int overflow, array bounds
- special cases (n=1?)
- do smth instead of do nothing and stay organized
- WRITE STUFF DOWN
- DONT JUST STICK ON ONE APPROACH
*/
Compilation message
uzastopni.cpp:19:20: warning: overflow in conversion from 'double' to 'int' changes value from '1.000000000000007e+15' to '2147483647' [-Woverflow]
19 | const int oo = 1e15+7;
| ~~~~^~
uzastopni.cpp: In function 'int main()':
uzastopni.cpp:53:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
uzastopni.cpp:55:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | for(int i=1;i<=n;i++) scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
uzastopni.cpp:57:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
844 KB |
Output is correct |
2 |
Correct |
1 ms |
844 KB |
Output is correct |
3 |
Correct |
2 ms |
844 KB |
Output is correct |
4 |
Correct |
1 ms |
844 KB |
Output is correct |
5 |
Correct |
2 ms |
844 KB |
Output is correct |
6 |
Correct |
2 ms |
972 KB |
Output is correct |
7 |
Correct |
2 ms |
844 KB |
Output is correct |
8 |
Correct |
2 ms |
972 KB |
Output is correct |
9 |
Correct |
2 ms |
844 KB |
Output is correct |
10 |
Correct |
2 ms |
844 KB |
Output is correct |
11 |
Correct |
110 ms |
17180 KB |
Output is correct |
12 |
Correct |
84 ms |
17128 KB |
Output is correct |
13 |
Correct |
97 ms |
17256 KB |
Output is correct |
14 |
Correct |
92 ms |
23688 KB |
Output is correct |
15 |
Correct |
108 ms |
23620 KB |
Output is correct |
16 |
Correct |
92 ms |
23624 KB |
Output is correct |
17 |
Correct |
81 ms |
17252 KB |
Output is correct |
18 |
Correct |
100 ms |
17220 KB |
Output is correct |
19 |
Correct |
82 ms |
19760 KB |
Output is correct |
20 |
Correct |
89 ms |
19756 KB |
Output is correct |