Submission #25640

#TimeUsernameProblemLanguageResultExecution timeMemory
25640szawinisUzastopni (COCI15_uzastopni)C++14
160 / 160
206 ms25336 KiB
#include <bits/stdc++.h> using namespace std; const int MAXN = (1e4)+2, MAXK = 102; int n, a[MAXN]; vector<int> g[MAXN], q[MAXK]; vector<pair<int,int> > s[MAXN]; bitset<MAXK> dp[MAXN][MAXK]; void dfs(int u) { for(int v: g[u]) dfs(v); for(int i = 1; i < MAXK; i++) q[i].clear(); for(int v: g[u]) { for(auto p: s[v]) q[p.first].push_back(p.second); } for(int l = MAXK-1; l >= 1; l--) { if(l == a[u]) { dp[u][l] |= dp[u][l+1]; dp[u][l][l] = true; } else for(int r: q[l]) { if(a[u] < l || a[u] > r) { dp[u][l] |= dp[u][r+1]; dp[u][l][r] = true; } } for(int r = MAXK-1; r >= l; r--) { if(dp[u][l][r] && a[u] >= l && a[u] <= r) s[u].emplace_back(l, r); } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]; for(int i = 1, u, v; i < n; i++) { cin >> u >> v; g[u].push_back(v); } dfs(1); cout << s[1].size(); }
#Verdict Execution timeMemoryGrader output
Fetching results...