# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1277289 | vuquangsang | Uzastopni (COCI15_uzastopni) | C++20 | 96 ms | 18324 KiB |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 2;
int n, a[N], lim;
vector<int> adj[N];
bitset<105> dp[N][105], temp[105];
void dfs(int u)
{
for(int v : adj[u]) dfs(v);
for(int x = 1; x <= lim; x++) temp[x].reset();
temp[a[u]][a[u]] = 1;
for(int v : adj[u]) {
for(int x = 1; x <= lim; x++) temp[x] |= dp[v][x];
}
dp[u][a[u]][a[u]] = 1;
for(int x = 1; x <= lim; x++) for(int y = x; y <= lim; y++) if(temp[x][y]) temp[x] |= temp[y + 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;
if(y == a[u] && temp[x][y - 1]) dp[u][x][y] = 1;
if(temp[x][a[u] - 1] && temp[a[u] + 1][y]) dp[u][x][y] = 1;
}
}
void solve()
{
cin >> n; 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);
}
lim = 100;
dfs(1);
int ans = 0;
for(int x = 1; x <= lim; x++) for(int y = x; y <= lim; y++) ans += dp[1][x][y];
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... |