#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <array>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define FOR(i,start,end) for(int i=start;i<(int)(end);i++)
#define FORE(i,start,end) for(int i=start;i<=(int)end;i++)
#define RFOR(i,start,end) for(int i = start; i>end; i--)
#define RFORE(i,start,end) for(int i = start; i>=end; i--)
#define all(a) a.begin(), a.end()
#define mt make_tuple
#define mp make_pair
#define v vector
#define sf scanf
#define pf printf
#define dvar(x) cout << #x << " := " << x << "\n"
#define darr(x,n) FOR(i,0,n) cout << #x << "[" << i << "]" << " := " << x[i] << "\n"
typedef long long ll;
typedef long double ld;
typedef pair<int, int > pii;
typedef pair<ll, ll> pll;
//template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void minn(T &a, T b) { a = min(a, b); }
template<class T> void maxx(T &a, T b) { a = max(a, b); }
void io() {
#ifdef LOCAL_PROJECT
freopen("input.in", "r", stdin); freopen("output.out", "w", stdout);
#else
/* online submission */
#endif
ios_base::sync_with_stdio(false); cin.tie(NULL);
}
const ll MOD = 1000000007LL;
const ll PRIME = 105943LL;
const ll INF = 1e18;
/**************************COCI15 R1 P6 uzastopni************************************/
const int SZ = 10000;
typedef array<bitset<100>, 100> dpt;
int N, J[SZ];
v<int> adj[SZ];
v<pii> dp[SZ];
dpt t, cd;
void dfs(int x, int p) {
FOR(i, 0, 100) FOR(j, 0, 100) t[i][j] = 0;
FOR(i, 0, 100) FOR(j, 0, 100) cd[i][j] = 0;
//cout << "wut";
for (auto c : adj[x]) if (c != p) {
dfs(c, x);
for (auto s : dp[c]) t[s.first][s.second] = 1;
}
RFORE(l, 99, J[x] + 1) {
cd[l] = t[l];
FORE(r, l, 99) if (t[l][r] && r + 1 <= 99) {
cd[l] |= cd[r + 1];
}
}
cd[J[x]][J[x]] = 1;
if (J[x] != 99)
cd[J[x]] |= cd[J[x] + 1];
RFORE(l, J[x] - 1, 0) {
FORE(r, l, J[x] - 1) if (t[l][r]) {
cd[l][r] = 1;
cd[l] |= cd[r + 1];
}
}
FOR(l, 0, 100) FOR(r, l, 100) if (cd[l][r] && r < J[x] || J[x] < l)
cd[l][r] = 0;
FOR(l, 0, 100) FOR(r, l, 100) if (cd[l][r])
dp[x].push_back({ l,r });
}
int main() {
io();
cin >> N;
FOR(i, 0, N) {
cin >> J[i];
J[i]--;
}
FOR(i, 0, N - 1) {
int a, b; cin >> a >> b;
adj[a - 1].push_back(b - 1), adj[b - 1].push_back(a - 1);
}
dfs(0, -1);
cout << dp[0].size() << "\n";
}
Compilation message
uzastopni.cpp: In function 'void dfs(int, int)':
uzastopni.cpp:78:45: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
FOR(l, 0, 100) FOR(r, l, 100) if (cd[l][r] && r < J[x] || J[x] < l)
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
888 KB |
Output isn't correct |
2 |
Incorrect |
6 ms |
888 KB |
Output isn't correct |
3 |
Incorrect |
6 ms |
760 KB |
Output isn't correct |
4 |
Incorrect |
7 ms |
888 KB |
Output isn't correct |
5 |
Incorrect |
8 ms |
888 KB |
Output isn't correct |
6 |
Incorrect |
8 ms |
760 KB |
Output isn't correct |
7 |
Incorrect |
8 ms |
888 KB |
Output isn't correct |
8 |
Incorrect |
8 ms |
888 KB |
Output isn't correct |
9 |
Incorrect |
8 ms |
760 KB |
Output isn't correct |
10 |
Incorrect |
8 ms |
888 KB |
Output isn't correct |
11 |
Execution timed out |
588 ms |
1616 KB |
Time limit exceeded |
12 |
Execution timed out |
592 ms |
1756 KB |
Time limit exceeded |
13 |
Execution timed out |
595 ms |
1668 KB |
Time limit exceeded |
14 |
Execution timed out |
572 ms |
3960 KB |
Time limit exceeded |
15 |
Execution timed out |
581 ms |
4004 KB |
Time limit exceeded |
16 |
Execution timed out |
585 ms |
4444 KB |
Time limit exceeded |
17 |
Execution timed out |
608 ms |
1528 KB |
Time limit exceeded |
18 |
Execution timed out |
607 ms |
1696 KB |
Time limit exceeded |
19 |
Execution timed out |
569 ms |
1812 KB |
Time limit exceeded |
20 |
Execution timed out |
568 ms |
1656 KB |
Time limit exceeded |