Submission #1083313

# Submission time Handle Problem Language Result Execution time Memory
1083313 2024-09-02T22:26:03 Z f0rizen Uzastopni (COCI15_uzastopni) C++17
160 / 160
71 ms 23952 KB
#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>> R(N);
    for (auto u : g[v]) {
        for (auto [l, r] : s[u]) {
            R[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 : R[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 time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 60 ms 17288 KB Output is correct
12 Correct 58 ms 17492 KB Output is correct
13 Correct 57 ms 17284 KB Output is correct
14 Correct 71 ms 23856 KB Output is correct
15 Correct 71 ms 23888 KB Output is correct
16 Correct 70 ms 23952 KB Output is correct
17 Correct 54 ms 17448 KB Output is correct
18 Correct 56 ms 17488 KB Output is correct
19 Correct 61 ms 19796 KB Output is correct
20 Correct 68 ms 19796 KB Output is correct