// COCI15_uzastopni
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;
#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'
#define MAXN 10100
#define MAXA 120
int n, val[MAXN];
vector<int> cld[MAXN];
bitset<MAXA> dp[MAXN][MAXA];
bitset<MAXA> ndp[MAXA];
void dfs(int v)
{
for (int u : cld[v]) dfs(u);
FOR(i, 0, MAXA) ndp[i].reset();
for (int u : cld[v]) FOR(i, 0, MAXA) ndp[i] |= dp[u][i];
for (int l = MAXA - 1; l >= 0; l--)
{
FOR(r, l, MAXA - 1)
{
if (ndp[l][r])
{
ndp[l] |= ndp[r + 1];
}
}
}
for (int l = val[v]; l > 0; l--)
if (l == val[v] || ndp[l][val[v] - 1])
{
dp[v][l] |= ndp[val[v] + 1];
dp[v][l][val[v]] = 1;
}
}
int32_t main(void)
{
fast_io;
cin >> n;
FOR(i, 1, n + 1) cin >> val[i];
FOR(i, 0, n - 1)
{
int x, y;
cin >> x >> y;
cld[x].pb(y);
}
dfs(1);
int ans = 0;
FOR(l, 0, MAXA) ans += dp[1][l].count();
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
2 ms |
716 KB |
Output is correct |
6 |
Correct |
2 ms |
716 KB |
Output is correct |
7 |
Correct |
2 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
716 KB |
Output is correct |
11 |
Correct |
84 ms |
19524 KB |
Output is correct |
12 |
Correct |
84 ms |
19416 KB |
Output is correct |
13 |
Correct |
84 ms |
19528 KB |
Output is correct |
14 |
Correct |
84 ms |
20040 KB |
Output is correct |
15 |
Correct |
85 ms |
20032 KB |
Output is correct |
16 |
Correct |
83 ms |
19992 KB |
Output is correct |
17 |
Correct |
84 ms |
19524 KB |
Output is correct |
18 |
Correct |
84 ms |
19516 KB |
Output is correct |
19 |
Correct |
80 ms |
19344 KB |
Output is correct |
20 |
Correct |
84 ms |
19364 KB |
Output is correct |