Submission #626700

#TimeUsernameProblemLanguageResultExecution timeMemory
626700MilosMilutinovicUzastopni (COCI15_uzastopni)C++14
160 / 160
313 ms19136 KiB
/** * author: wxhtzdy * created: 11.08.2022 17:31:40 **/ #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } vector<vector<int>> g(n); for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; --u; --v; g[u].push_back(v); g[v].push_back(u); } const int MAX = 105; vector<vector<bitset<MAX>>> dp(n, vector<bitset<MAX>>(MAX)); function<void(int, int)> Dfs = [&](int v, int pr) { dp[v][a[v]][a[v]] = 1; for (int u : g[v]) { if (u == pr) { continue; } Dfs(u, v); for (int l = 1; l < MAX; l++) { for (int r = l; r < MAX; r++) { if (l <= a[v] && a[v] <= r) { continue; } if (dp[u][l][r]) { dp[v][l][r] = 1; } } } } for (int l = MAX - 1; l >= 1; l--) { for (int r = l; r < MAX; r++) { if (dp[v][l][r]) { dp[v][l] |= dp[v][r + 1]; } } } for (int l = 1; l < MAX; l++) { for (int r = l; r < MAX; r++) { if ((r < a[v] || a[v] < l) && dp[v][l][r]) { dp[v][l][r] = 0; } } } }; Dfs(0, 0); int ans = 0; for (int i = 1; i < MAX; i++) { for (int j = i; j < MAX; j++) { if (dp[0][i][j]) { ans += 1; } } } cout << ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...