Submission #1083311

# Submission time Handle Problem Language Result Execution time Memory
1083311 2024-09-02T22:25:01 Z f0rizen Uzastopni (COCI15_uzastopni) C++17
160 / 160
71 ms 24144 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>> 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 time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 2 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 628 KB Output is correct
6 Correct 1 ms 452 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 452 KB Output is correct
11 Correct 63 ms 17496 KB Output is correct
12 Correct 62 ms 17492 KB Output is correct
13 Correct 59 ms 17488 KB Output is correct
14 Correct 71 ms 24000 KB Output is correct
15 Correct 71 ms 24088 KB Output is correct
16 Correct 68 ms 24144 KB Output is correct
17 Correct 58 ms 17492 KB Output is correct
18 Correct 56 ms 17492 KB Output is correct
19 Correct 63 ms 19796 KB Output is correct
20 Correct 67 ms 19796 KB Output is correct