답안 #499070

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
499070 2021-12-27T07:41:05 Z blue Roadside Advertisements (NOI17_roadsideadverts) C++17
100 / 100
53 ms 10428 KB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

using vi = vector<int>;
using pii = pair<int, int>;

const int mx = 50'000;
const int lg = 15;

int N;
int Q;

vector<pii> edge[mx];

vi dist0(mx, 0);
vi par(mx, 0);
vi depth(mx, 0);

int anc[mx][1+lg];

int ct = -1;
vi dfs_ind(mx, -1);



void dfs(int u)
{
    dfs_ind[u] = ++ct;
    for(auto e: edge[u])
    {
        int v = e.first;
        int w = e.second;

        if(v == par[u]) continue;

        depth[v] = depth[u] + 1;
        par[v] = u;
        dist0[v] = dist0[u] + w;

        dfs(v);
    }
}

int getanc(int u, int d)
{
    for(int e = lg; e >= 0; e--)
        if(d & (1 << e))
            u = anc[u][e];

    return u;
}

int lca(int u, int v)
{
    if(depth[v] < depth[u]) swap(u, v);

    for(int e = lg; e >= 0; e--)
        if((depth[v] - depth[u]) & (1 << e))
            v = anc[v][e];

    if(u == v) return u;

    for(int e = lg; e >= 0; e--)
    {
        if(anc[u][e] == anc[v][e]) continue;
        u = anc[u][e];
        v = anc[v][e];
    }

    return anc[u][0];
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> N;
    for(int e = 0; e < N-1; e++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        edge[u].push_back({v, w});
        edge[v].push_back({u, w});
    }

    par[0] = 0;
    dfs(0);


    for(int i = 0; i < N; i++) anc[i][0] = par[i];
    for(int e = 1; e <= lg; e++)
        for(int i = 0; i < N; i++)
            anc[i][e] = anc[ anc[i][e-1] ][e-1];

    int Q;
    cin >> Q;

    for(int j = 0; j < Q; j++)
    {
        vi q(5);
        for(int f = 0; f < 5; f++) cin >> q[f];

        sort(q.begin(), q.end(), [] (int u, int v)
        {
            return (dfs_ind[u] < dfs_ind[v]);
        });

        for(int f = 0; f+1 < 5; f++)
        {
            q.push_back(lca(q[f], q[f+1]));
        }

        sort(q.begin(), q.end(), [] (int u, int v)
        {
            return (dfs_ind[u] < dfs_ind[v]);
        });

        vi st;
        st.push_back(q[0]);

        int ans = 0;

        for(int u: q)
        {
            while(getanc(u, depth[u] - depth[st.back()]) != st.back())
                st.pop_back();

            ans += dist0[u] - dist0[st.back()];

            st.push_back(u);
        }

        cout << ans << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 9376 KB Output is correct
2 Correct 53 ms 10396 KB Output is correct
3 Correct 48 ms 10344 KB Output is correct
4 Correct 43 ms 10348 KB Output is correct
5 Correct 48 ms 10416 KB Output is correct
6 Correct 51 ms 10392 KB Output is correct
7 Correct 45 ms 10308 KB Output is correct
8 Correct 44 ms 10328 KB Output is correct
9 Correct 46 ms 10416 KB Output is correct
10 Correct 46 ms 10428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 8000 KB Output is correct
2 Correct 24 ms 9616 KB Output is correct
3 Correct 27 ms 9636 KB Output is correct
4 Correct 25 ms 9716 KB Output is correct
5 Correct 25 ms 9644 KB Output is correct
6 Correct 24 ms 9740 KB Output is correct
7 Correct 26 ms 9720 KB Output is correct
8 Correct 30 ms 9628 KB Output is correct
9 Correct 24 ms 9712 KB Output is correct
10 Correct 30 ms 9656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2252 KB Output is correct
2 Correct 51 ms 9376 KB Output is correct
3 Correct 53 ms 10396 KB Output is correct
4 Correct 48 ms 10344 KB Output is correct
5 Correct 43 ms 10348 KB Output is correct
6 Correct 48 ms 10416 KB Output is correct
7 Correct 51 ms 10392 KB Output is correct
8 Correct 45 ms 10308 KB Output is correct
9 Correct 44 ms 10328 KB Output is correct
10 Correct 46 ms 10416 KB Output is correct
11 Correct 46 ms 10428 KB Output is correct
12 Correct 22 ms 8000 KB Output is correct
13 Correct 24 ms 9616 KB Output is correct
14 Correct 27 ms 9636 KB Output is correct
15 Correct 25 ms 9716 KB Output is correct
16 Correct 25 ms 9644 KB Output is correct
17 Correct 24 ms 9740 KB Output is correct
18 Correct 26 ms 9720 KB Output is correct
19 Correct 30 ms 9628 KB Output is correct
20 Correct 24 ms 9712 KB Output is correct
21 Correct 30 ms 9656 KB Output is correct
22 Correct 47 ms 9248 KB Output is correct
23 Correct 43 ms 8328 KB Output is correct
24 Correct 47 ms 10112 KB Output is correct
25 Correct 50 ms 10176 KB Output is correct
26 Correct 47 ms 10056 KB Output is correct
27 Correct 48 ms 10120 KB Output is correct
28 Correct 47 ms 10112 KB Output is correct
29 Correct 47 ms 10112 KB Output is correct
30 Correct 46 ms 10112 KB Output is correct
31 Correct 47 ms 10052 KB Output is correct