# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1277192 | vuquangsang | Uzastopni (COCI15_uzastopni) | C++20 | 66 ms | 131072 KiB |
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
const int MAX = 105;
int n, a[N];
vector<int> adj[N];
bool dp[N][MAX][MAX], temp[MAX][MAX];
int lim;
void dfs(int u)
{
for(int v : adj[u]) {
dfs(v);
}
memset(temp, 0, sizeof temp);
temp[a[u]][a[u]] = 1;
for(int v : adj[u]) {
for(int x = 1; x <= lim; x++) {
for(int y = x; y <= lim; y++) temp[x][y] |= dp[v][x][y];
}
}
for(int x = 1; x <= lim; x++) for(int y = x; y <= lim; y++) if(temp[x][y]){
for(int len = y + 1; len <= lim; len++) temp[x][len] |= temp[y + 1][len];
}
dp[u][a[u]][a[u]] = 1;
for(int x = 1; x <= lim; x++) for(int y = x; y <= lim; y++) {
if(x == a[u] && temp[x + 1][y]) dp[u][x][y] = 1;
else if(y == a[u] && temp[x][y - 1]) dp[u][x][y] = 1;
else if(temp[x][a[u] - 1] && temp[a[u] + 1][y]) dp[u][x][y] = 1;
}
}
void solve()
{
cin >> n;
lim = 100;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i < n; i++) {
int x, y; cin >> x >> y;
adj[x].push_back(y);
}
dfs(1);
// #define el "\n"
// for(int u = 1; u <= n; u++) {
// for(int x = 1; x <= lim; x++) for(int y = 1; y <= lim; y++) cout << u << " " << x << " " << y << " " << dp[u][x][y] << el; cout << el << el;
// }
int ans = 0;
for(int x = 1; x <= lim; x++) for(int y = x; y <= lim; y++) if(dp[1][x][y]) ans++;
cout << ans;
}
main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define TASK "COCI15_uzastopni"
if(fopen(TASK".INP", "r")) {
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
solve();
cerr << "\nTime" << 0.001 * clock() << "s "; return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |