Submission #891173

# Submission time Handle Problem Language Result Execution time Memory
891173 2023-12-22T09:56:50 Z nahco314 File Paths (BOI15_fil) C++14
100 / 100
188 ms 34640 KB
#include <bits/stdc++.h>
using namespace std;

#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

vector<int> get_divs(int n) {
    vector<int> res;
    for (int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            res.push_back(n / i);
            res.push_back(i);
        }
    }
    return res;
}

int n, m, k, s;
vector<vector<pair<int, int>>> g;
vector<vector<pair<int, int>>> files;

vector<vector<bool>> is_child;
vector<bool> ans;
vector<int> pd;

unordered_set<int> ds;

vector<int> makes;

void pre_dfs(int v, int p, int d, vector<int>& st) {
    st.push_back(v);
    pd[v] = d;
    for (int p : st) {
        is_child[p][v] = true;
    }
    for (auto [nxt, l] : g[v]) {
        if (nxt == p) continue;
        pre_dfs(nxt, v, d + l, st);
    }
    st.pop_back();
}

void dfs(int v, int p, int d) {
    vector<int> added_makes;
    for (int i = 0; i < n + 1; i++) {
        if (!is_child[v][i]) continue;
        int num = pd[i] - pd[v] + s;
        // cerr << "  make loop: " << v << " " << i << " " << num << endl;
        makes[num]++;
        added_makes.push_back(num);
    }

    ds.insert(d);

    for (auto [f_n, l] : files[v]) {
        int remain = k - d - l;

        if (remain == 0) {
            // cerr << "ans0 just: " << f_n << endl;
            ans[f_n] = true;
            continue;
        }

        for (int div : get_divs(remain)) {
            if (makes[div]) {
                // cerr << "ans1 loop: " << f_n << " " << div << endl;
                ans[f_n] = true;
            }
        }

        for (int i = 0; i < n + 1; i++) {
            int all_cost = d + l + pd[i] + s;
            int over = all_cost - k;
            if (ds.count(over) > 0) {
                // cerr << "ans2 first jump: " << f_n << " " << i << endl;
                ans[f_n] = true;
            }
        }
    }

    for (auto [nxt, l] : g[v]) {
        if (nxt == p) continue;
        dfs(nxt, v, d + l);
    }

    for (int num : added_makes) {
        makes[num]--;
    }

    ds.erase(d);
}

int main() {
    cin >> n >> m >> k >> s;
    ans = vector<bool>(m, false);
    g = vector<vector<pair<int, int>>>(n + 1);
    files = vector<vector<pair<int, int>>>(n + 1);
    for (int i = 0; i < n; i++) {
        int p, l;
        cin >> p >> l;
        g[p].emplace_back(i + 1, l + 1);
        g[i + 1].emplace_back(p, l + 1);
    }
    for (int i = 0; i < m; i++) {
        int p, l;
        cin >> p >> l;
        files[p].emplace_back(i, l + 1);
    }
    s++;

    vector<int> st;
    pd = vector<int>(n + 1);
    is_child = vector<vector<bool>>(n + 1, vector<bool>(n + 1, false));
    pre_dfs(0, -1, 0, st);

    makes = vector<int>(2000020, 0);

    dfs(0, -1, 0);

    for (int i = 0; i < m; i++) {
        if (ans[i]) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
}

Compilation message

fil.cpp: In function 'void pre_dfs(int, int, int, std::vector<int>&)':
fil.cpp:37:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   37 |     for (auto [nxt, l] : g[v]) {
      |               ^
fil.cpp: In function 'void dfs(int, int, int)':
fil.cpp:56:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   56 |     for (auto [f_n, l] : files[v]) {
      |               ^
fil.cpp:82:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   82 |     for (auto [nxt, l] : g[v]) {
      |               ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8284 KB Output is correct
2 Correct 4 ms 8360 KB Output is correct
3 Correct 6 ms 8396 KB Output is correct
4 Correct 7 ms 8780 KB Output is correct
5 Correct 10 ms 8284 KB Output is correct
6 Correct 9 ms 8396 KB Output is correct
7 Correct 10 ms 9048 KB Output is correct
8 Correct 6 ms 8284 KB Output is correct
9 Correct 6 ms 8284 KB Output is correct
10 Correct 2 ms 8028 KB Output is correct
11 Correct 6 ms 8284 KB Output is correct
12 Correct 6 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 9820 KB Output is correct
2 Correct 99 ms 9860 KB Output is correct
3 Correct 101 ms 9820 KB Output is correct
4 Correct 101 ms 9820 KB Output is correct
5 Correct 178 ms 29648 KB Output is correct
6 Correct 184 ms 30244 KB Output is correct
7 Correct 173 ms 15444 KB Output is correct
8 Correct 188 ms 15440 KB Output is correct
9 Correct 135 ms 9816 KB Output is correct
10 Correct 101 ms 9936 KB Output is correct
11 Correct 96 ms 9808 KB Output is correct
12 Correct 142 ms 29272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8284 KB Output is correct
2 Correct 4 ms 8360 KB Output is correct
3 Correct 6 ms 8396 KB Output is correct
4 Correct 7 ms 8780 KB Output is correct
5 Correct 10 ms 8284 KB Output is correct
6 Correct 9 ms 8396 KB Output is correct
7 Correct 10 ms 9048 KB Output is correct
8 Correct 6 ms 8284 KB Output is correct
9 Correct 6 ms 8284 KB Output is correct
10 Correct 2 ms 8028 KB Output is correct
11 Correct 6 ms 8284 KB Output is correct
12 Correct 6 ms 8796 KB Output is correct
13 Correct 101 ms 9820 KB Output is correct
14 Correct 99 ms 9860 KB Output is correct
15 Correct 101 ms 9820 KB Output is correct
16 Correct 101 ms 9820 KB Output is correct
17 Correct 178 ms 29648 KB Output is correct
18 Correct 184 ms 30244 KB Output is correct
19 Correct 173 ms 15444 KB Output is correct
20 Correct 188 ms 15440 KB Output is correct
21 Correct 135 ms 9816 KB Output is correct
22 Correct 101 ms 9936 KB Output is correct
23 Correct 96 ms 9808 KB Output is correct
24 Correct 142 ms 29272 KB Output is correct
25 Correct 83 ms 9816 KB Output is correct
26 Correct 80 ms 9924 KB Output is correct
27 Correct 80 ms 9820 KB Output is correct
28 Correct 85 ms 9816 KB Output is correct
29 Correct 169 ms 29780 KB Output is correct
30 Correct 163 ms 29948 KB Output is correct
31 Correct 187 ms 15540 KB Output is correct
32 Correct 186 ms 15492 KB Output is correct
33 Correct 96 ms 9816 KB Output is correct
34 Correct 75 ms 9936 KB Output is correct
35 Correct 101 ms 9968 KB Output is correct
36 Correct 183 ms 34640 KB Output is correct