Submission #587474

# Submission time Handle Problem Language Result Execution time Memory
587474 2022-07-01T23:50:46 Z gg123_pe Roadside Advertisements (NOI17_roadsideadverts) C++14
100 / 100
113 ms 9980 KB
#include <bits/stdc++.h>
using namespace std; 
 
typedef long long ll; 
#define f(i,a,b) for(ll i = a; i < b; i++)
#define fa(i,a,b) for(ll i = a; i >= b; i--)

const int N = 50005; 
const ll inf = 2e18; 


int n, a[6], d[N], tin[N], tout[N], c, p[N][16]; 
vector <pair<int,int>> adj[N]; 
vector <int> aux; 

void dfs(int u, int f){
    p[u][0] = f; 
    f(i,1,16) p[u][i] = p[p[u][i-1]][i-1];

    tin[u] = c++; 

    for(auto p: adj[u]){
        int v = p.first, w = p.second; 
        if(v == f) continue; 
        d[v] = d[u] + w; 
        dfs(v, u); 
    }
    tout[u] = c++;
}

bool is(int u, int v){
    return tin[u] <= tin[v] and tout[u] >= tout[v]; 
}

int lca(int u, int v){
    if(is(u, v)) return u; 
    if(is(v, u)) return v; 


    fa(i,15,0){
        if(p[u][i] == 0 or is(p[u][i], v)) continue; 
        u = p[u][i];
    }
    return p[u][0];
}
int main(){
    cin >> n; 

    f(i,1,n){
        int u, v, w; 
        cin >> u >> v >> w; 

        u++, v++; 
        adj[u].push_back({v, w}); 
        adj[v].push_back({u, w}); 
    }

    dfs(1, 0); 

    int qe; cin >> qe; 


    while(qe--){
        bool fe = 0; 
        vector <pair<int,int>> v; 
        set <int> s; 

        f(i,1,6){
            cin >> a[i]; 
            a[i]++;
            if(a[i] == 1) fe = 1; 
            v.push_back({tin[a[i]], a[i]});
            s.insert(a[i]);
        }

        if(!fe){
            v.push_back({tin[1], 1}); 
            s.insert(1);
        }

        sort(v.begin(), v.end()); 

        int ans = 0, l = v.size(); 

        f(i,1,l){
            //cout << v[i].second << " " << v[i-1].second << " " << lca(v[i].second, v[i-1].second) << "\n";
            s.insert(lca(v[i].second, v[i-1].second));
        }

        v.clear(); 
        for(int x: s) v.push_back({tin[x], x}); 

        sort(v.begin(), v.end()); 

        stack <int> q; 
        q.push(1);

        l = v.size();

        f(i,1,l){
            int vi = v[i].second; 
            while(1){
                int x = q.top();
                if(is(x, vi)){
                    //cout << x << " " << vi << " " << d[vi] - d[x] << "\n";
                    ans += d[vi] - d[x];
                    if(x == 1){
                        aux.push_back(vi);
                    }
                    q.push(vi);
                    break; 
                }
                q.pop();
            }
        }
        
        if(aux.size() == 1 and !fe){
            ans -= d[aux[0]];  
        }
        aux.clear();
        cout << ans << "\n"; 
    }
    return 0; 
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 8416 KB Output is correct
2 Correct 108 ms 9872 KB Output is correct
3 Correct 80 ms 9932 KB Output is correct
4 Correct 84 ms 9980 KB Output is correct
5 Correct 81 ms 9880 KB Output is correct
6 Correct 90 ms 9932 KB Output is correct
7 Correct 96 ms 9908 KB Output is correct
8 Correct 93 ms 9932 KB Output is correct
9 Correct 90 ms 9900 KB Output is correct
10 Correct 86 ms 9920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 6980 KB Output is correct
2 Correct 53 ms 9412 KB Output is correct
3 Correct 68 ms 9504 KB Output is correct
4 Correct 59 ms 9456 KB Output is correct
5 Correct 58 ms 9504 KB Output is correct
6 Correct 54 ms 9420 KB Output is correct
7 Correct 52 ms 9504 KB Output is correct
8 Correct 66 ms 9416 KB Output is correct
9 Correct 66 ms 9444 KB Output is correct
10 Correct 57 ms 9488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1364 KB Output is correct
2 Correct 94 ms 8416 KB Output is correct
3 Correct 108 ms 9872 KB Output is correct
4 Correct 80 ms 9932 KB Output is correct
5 Correct 84 ms 9980 KB Output is correct
6 Correct 81 ms 9880 KB Output is correct
7 Correct 90 ms 9932 KB Output is correct
8 Correct 96 ms 9908 KB Output is correct
9 Correct 93 ms 9932 KB Output is correct
10 Correct 90 ms 9900 KB Output is correct
11 Correct 86 ms 9920 KB Output is correct
12 Correct 47 ms 6980 KB Output is correct
13 Correct 53 ms 9412 KB Output is correct
14 Correct 68 ms 9504 KB Output is correct
15 Correct 59 ms 9456 KB Output is correct
16 Correct 58 ms 9504 KB Output is correct
17 Correct 54 ms 9420 KB Output is correct
18 Correct 52 ms 9504 KB Output is correct
19 Correct 66 ms 9416 KB Output is correct
20 Correct 66 ms 9444 KB Output is correct
21 Correct 57 ms 9488 KB Output is correct
22 Correct 84 ms 8532 KB Output is correct
23 Correct 83 ms 7228 KB Output is correct
24 Correct 104 ms 9568 KB Output is correct
25 Correct 113 ms 9556 KB Output is correct
26 Correct 87 ms 9600 KB Output is correct
27 Correct 95 ms 9700 KB Output is correct
28 Correct 103 ms 9580 KB Output is correct
29 Correct 98 ms 9600 KB Output is correct
30 Correct 94 ms 9624 KB Output is correct
31 Correct 92 ms 9520 KB Output is correct