#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 1e4 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
const int block_sz = 550;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
int n;
int v[mxN];
ll dp[107][107][107];
ll f[107][107];
vector<int> adj[mxN];
void dfs(int u, int p)
{
for(int v : adj[u]) {
if(v == p) continue;
dfs(v, u);
}
memset(f, 0, sizeof(f));
for(int i = 0; i <= 101; i++)
for(int j = 0; j <= 101; j++) if(i > j) f[i][j] = 1;
for(int v : adj[u]) {
if(v == p) continue;
for(int le = 100; le >= 1; le--) {
for(int i = 1; i + le - 1 <= 100; i++) {
int j = i + le - 1;
for(int k = i; k <= j; k++) {
f[i][j] += 1LL * f[i][k] * dp[v][k + 1][j];
f[i][j] += 1LL * dp[v][i][k] * f[k + 1][j];
}
}
}
}
for(int i = 1; i <= 100; i++) {
for(int j = i; j <= 100; j++) {
if(i <= v[u] && v[u] <= j) {
dp[u][i][j] += 1LL * f[i][v[u] - 1] * f[v[u] + 1][j];
}
}
}
}
void solve()
{
cin >> n;
for(int i = 1; i <= n; i++) cin >> v[i];
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1, -1);
ll res = 0;
for(int l = 1; l <= 100; l++)
for(int r = l; r <= 100; r++) {
res += dp[1][l][r];
}
cout << res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("graph.inp", "r", stdin);
//freopen("graph.out", "w", stdout);
int tc = 1;
//cin >> tc;
while(tc--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
1108 KB |
Output is correct |
2 |
Correct |
21 ms |
1400 KB |
Output is correct |
3 |
Incorrect |
20 ms |
1236 KB |
Output isn't correct |
4 |
Incorrect |
20 ms |
1100 KB |
Output isn't correct |
5 |
Incorrect |
26 ms |
1332 KB |
Output isn't correct |
6 |
Correct |
26 ms |
5144 KB |
Output is correct |
7 |
Correct |
28 ms |
5172 KB |
Output is correct |
8 |
Correct |
25 ms |
5164 KB |
Output is correct |
9 |
Correct |
26 ms |
4036 KB |
Output is correct |
10 |
Correct |
26 ms |
4020 KB |
Output is correct |
11 |
Runtime error |
4 ms |
1984 KB |
Execution killed with signal 11 |
12 |
Runtime error |
4 ms |
2004 KB |
Execution killed with signal 11 |
13 |
Runtime error |
4 ms |
2004 KB |
Execution killed with signal 11 |
14 |
Runtime error |
6 ms |
3668 KB |
Execution killed with signal 11 |
15 |
Runtime error |
6 ms |
3772 KB |
Execution killed with signal 11 |
16 |
Runtime error |
5 ms |
3712 KB |
Execution killed with signal 11 |
17 |
Runtime error |
4 ms |
1988 KB |
Execution killed with signal 11 |
18 |
Runtime error |
5 ms |
1876 KB |
Execution killed with signal 11 |
19 |
Runtime error |
4 ms |
2120 KB |
Execution killed with signal 11 |
20 |
Runtime error |
4 ms |
2132 KB |
Execution killed with signal 11 |