Submission #672919

# Submission time Handle Problem Language Result Execution time Memory
672919 2022-12-19T03:01:34 Z vuavisao Roadside Advertisements (NOI17_roadsideadverts) C++14
100 / 100
51 ms 11384 KB
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
using namespace std;

const int N = 5e4 + 10;

int n, q;
vector<pair<int, int>> g[N];

int Lg, dist[N], parent[20][N];
int s_dist[N];

int cnt, in[N], out[N];

void dfs(int u);
void dfs(int u) {
    in[u] = ++ cnt;
    for(const auto& psy : g[u]) {
        int v = psy.first, w = psy.second;
        if(v == parent[0][u]) continue;
        parent[0][v] = u;
        dist[v] = dist[u] + 1;
        s_dist[v] = s_dist[u] + w;
        dfs(v);
    }
    out[u] = cnt;
}

int lca(int u, int v);
int lca(int u, int v) {
    if(dist[u] < dist[v]) swap(u, v);
    int delta = dist[u] - dist[v];
    for(int i = Lg; i >= 0; -- i) {
        if(delta >> i & 1) u = parent[i][u];
    }
    if(u == v) return u;
    for(int i = Lg; i >= 0; -- i) {
        if(parent[i][u] == parent[i][v]) continue;
        u = parent[i][u];
        v = parent[i][v];
    }
    return parent[0][u];
}

int dfs_finally(int u) {
    int s = 0;
    for(const auto& psy : g[u]) {
        int v = psy.first, w = psy.second;
        s += dfs_finally(v) + w;
    }
    return s;
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if (fopen("NOI17_roadsideadverts.inp", "r")) {
        freopen("NOI17_roadsideadverts.inp", "r", stdin);
        freopen("NOI17_roadsideadverts.out", "w", stdout);
    }
    cin >> n;
    for(int i = 2; i <= n; ++ i) {
        int u, v, w; cin >> u >> v >> w;
        ++ u, ++ v;
        g[u].push_back(make_pair(v, w));
        g[v].push_back(make_pair(u, w));
    }
    dfs(1);
    Lg = __lg(n);
    for(int j = 1; j <= Lg; ++ j) {
        for(int i = 1; i <= n; ++ i) {
            parent[j][i] = parent[j - 1][parent[j - 1][i]];
        }
    }
    for(int u = 1; u <= n; ++ u) {
        g[u].clear();
    }
    cin >> q;
    while(q-- > 0) {
        vector<int> gr(5, 0);
        for(int i = 0; i < 5; ++ i) {
            cin >> gr[i]; ++ gr[i];
        }
        sort(gr.begin(), gr.end(), [&] (int lhs, int rhs) {
            return in[lhs] > in[rhs];
        });
        auto inside = [&] (int u, int v) {
            /// u inside v
            return in[v] <= in[u] && out[u] <= out[v];
        };
        for(int i = 1; i < 5; ++ i) {
            gr.push_back(lca(gr[i], gr[i - 1]));
        }
        sort(gr.begin(), gr.end(), [&] (int lhs, int rhs) {
            return in[lhs] > in[rhs];
        });
        gr.resize(unique(gr.begin(), gr.end()) - gr.begin());
        stack<int> stk = {};
        for(const auto& u : gr) {
            while(!stk.empty() && inside(stk.top(), u)) {
                int v = stk.top(); stk.pop();
                int w = s_dist[v] - s_dist[u];
                g[u].push_back(make_pair(v, w));
            }
            stk.push(u);
        }
        cout << dfs_finally(stk.top()) << '\n';
        for(const auto& u : gr) {
            g[u].clear();
        }
    }
    return 0;
}

/// Code by vuavisao

Compilation message

roadsideadverts.cpp: In function 'int32_t main()':
roadsideadverts.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         freopen("NOI17_roadsideadverts.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
roadsideadverts.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |         freopen("NOI17_roadsideadverts.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 9088 KB Output is correct
2 Correct 41 ms 10872 KB Output is correct
3 Correct 38 ms 10856 KB Output is correct
4 Correct 50 ms 11004 KB Output is correct
5 Correct 39 ms 10956 KB Output is correct
6 Correct 38 ms 10896 KB Output is correct
7 Correct 39 ms 10952 KB Output is correct
8 Correct 39 ms 10872 KB Output is correct
9 Correct 40 ms 10936 KB Output is correct
10 Correct 38 ms 10964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 7160 KB Output is correct
2 Correct 29 ms 10888 KB Output is correct
3 Correct 25 ms 10956 KB Output is correct
4 Correct 27 ms 10916 KB Output is correct
5 Correct 26 ms 10972 KB Output is correct
6 Correct 25 ms 10880 KB Output is correct
7 Correct 24 ms 10920 KB Output is correct
8 Correct 24 ms 10972 KB Output is correct
9 Correct 24 ms 10956 KB Output is correct
10 Correct 24 ms 10956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 40 ms 9088 KB Output is correct
3 Correct 41 ms 10872 KB Output is correct
4 Correct 38 ms 10856 KB Output is correct
5 Correct 50 ms 11004 KB Output is correct
6 Correct 39 ms 10956 KB Output is correct
7 Correct 38 ms 10896 KB Output is correct
8 Correct 39 ms 10952 KB Output is correct
9 Correct 39 ms 10872 KB Output is correct
10 Correct 40 ms 10936 KB Output is correct
11 Correct 38 ms 10964 KB Output is correct
12 Correct 20 ms 7160 KB Output is correct
13 Correct 29 ms 10888 KB Output is correct
14 Correct 25 ms 10956 KB Output is correct
15 Correct 27 ms 10916 KB Output is correct
16 Correct 26 ms 10972 KB Output is correct
17 Correct 25 ms 10880 KB Output is correct
18 Correct 24 ms 10920 KB Output is correct
19 Correct 24 ms 10972 KB Output is correct
20 Correct 24 ms 10956 KB Output is correct
21 Correct 24 ms 10956 KB Output is correct
22 Correct 50 ms 10212 KB Output is correct
23 Correct 36 ms 8328 KB Output is correct
24 Correct 41 ms 11264 KB Output is correct
25 Correct 42 ms 11340 KB Output is correct
26 Correct 41 ms 11276 KB Output is correct
27 Correct 41 ms 11352 KB Output is correct
28 Correct 42 ms 11264 KB Output is correct
29 Correct 51 ms 11264 KB Output is correct
30 Correct 43 ms 11384 KB Output is correct
31 Correct 42 ms 11272 KB Output is correct