Submission #1083311

#TimeUsernameProblemLanguageResultExecution timeMemory
1083311f0rizenUzastopni (COCI15_uzastopni)C++17
160 / 160
71 ms24144 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int inf = 1e9 + 7; const ll infll = 1e18; template<typename T> istream &operator>>(istream &is, vector<T> &a) { for (auto &i : a) { is >> i; } return is; } const int N = 100; vector<int> a; vector<vector<int>> g; vector<vector<bitset<N>>> dp; vector<vector<pair<int, int>>> s; void dfs(int v) { for (auto u : g[v]) { dfs(u); } vector<vector<int>> to(N); for (auto u : g[v]) { for (auto [l, r] : s[u]) { to[l].push_back(r); } } for (int i = N - 1; i >= 0; --i) { if (i == a[v]) { dp[v][i][i] = 1; dp[v][i] |= dp[v][i + 1]; continue; } for (auto j : to[i]) { if (!(i <= a[v] && a[v] <= j)) { dp[v][i][j] = 1; dp[v][i] |= dp[v][j + 1]; } } } for (int i = 0; i < N; ++i) { for (int j = i; j < N; ++j) { if (i <= a[v] && a[v] <= j && dp[v][i][j]) { s[v].push_back({i, j}); } } } } int32_t main() { #ifdef LOCAL freopen("/tmp/input.txt", "r", stdin); #else ios::sync_with_stdio(false); cin.tie(nullptr); #endif int n; cin >> n; a.resize(n); cin >> a; for (auto &i : a) { --i; } g.resize(n); for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; --u, --v; g[u].push_back(v); } dp.resize(n, vector<bitset<N>>(N)); s.resize(n); dfs(0); cout << s[0].size() << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...