답안 #765134

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
765134 2023-06-24T08:36:51 Z vjudge1 Roadside Advertisements (NOI17_roadsideadverts) C++17
70 / 100
269 ms 58200 KB
#include <bits/stdc++.h>
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
#define ent '\n'
#define f first
#define s second

using namespace std;
typedef long long ll;
typedef pair < int, int> pii;
typedef pair < ll, ll > pll;

const int maxn = 2e6 + 100;
const int mod = 1e9 + 7;
const int INF = 1e9;
const int LOG = 20;
const int K = 400;
const int P = 31;

int n, m;
vector < pii > g[maxn];
int pref[maxn], p[LOG][maxn], tin[maxn], tout[maxn], timer, depth[maxn];
int a, b, c, d, e;
ll ans = 0;
void dfs(int v){
    for(int i = 1;i < LOG;i++){
        p[i][v] = p[i - 1][p[i - 1][v]];
    }
    tin[v] = ++timer;
    for(auto to: g[v]){
        if(to.f == p[0][v])continue;
        p[0][to.f] = v;
        depth[to.f] = depth[v] + 1;
        pref[to.f] = pref[v] + to.s;
        dfs(to.f);
    }
    tout[v] = ++timer;
}

bool upper(int a, int b){return tin[a] <= tin[b] && tout[a] >= tout[b];}

int lca(int a, int b){
    if(upper(a, b))return a;
    if(upper(b, a))return b;
    for(int i = LOG - 1;i >= 0;i--){
        if(!upper(p[i][a], b) && p[i][a] != 0)a = p[i][a];
    }
    return p[0][a];
}

int dfs2(int v, int pa, int cal){
    bool ok = 0;
    if(v == b || v == c || v == d || v == e){
        ans += cal;
        ok = 1;
    }
    for(pii to: g[v]){
        if(to.f == pa)continue;
        int c;
        if(!ok)c = dfs2(to.f, v, to.s + cal);
        else c = dfs2(to.f, v, to.s);
        if(c){
            ok = 1;
        }
    }
    return ok;
}

void solve(){
    cin >> n;
    for(int i = 1;i < n;i++){
        int u, v, w;
        cin >> u >> v >> w;
        u++, v++;
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    dfs(1);
    cin >> m;
    if(m <= 100){
        for(int i = 1;i <= m;i++){
            cin >> a >> b >> c >> d >> e;
            a++, b++, c++, d++, e++;
            dfs2(a, -1, 0);
            cout << ans << ent;
            ans = 0;
        }
        return;
    }
    for(int i = 1;i <= m;i++){
        cin >> a >> b >> c >> d >> e;
        a++, b++, c++, d++, e++;
        vector < pii > v;
        v.push_back({depth[a], a});
        v.push_back({depth[b], b});
        v.push_back({depth[c], c});
        v.push_back({depth[d], d});
        v.push_back({depth[e], e});
        sort(v.begin(), v.end());
        a = v[0].s, b = v[1].s, c = v[2].s, d = v[3].s, e = v[4].s;
        int LCA_DE = lca(d, e);
        ans = (pref[e] - pref[LCA_DE]) + (pref[d] - pref[LCA_DE]);
        int LCA_CD = lca(c, LCA_DE);
        if(upper(c, e) == 0 && upper(c, d) == 0){
            ans += (pref[LCA_DE] - pref[LCA_CD]) + (pref[c] - pref[LCA_CD]);
        } else if(depth[c] < depth[LCA_DE]){
            ans += (pref[LCA_DE] - pref[c]);
        }
        int LCA_BC = lca(b, LCA_CD);
        if(upper(b, c) == 0 && upper(b, d) == 0 && upper(b, e) == 0){
            ans += (pref[LCA_CD] - pref[LCA_BC]) + (pref[b] - pref[LCA_BC]);
        } else if(depth[b] < depth[LCA_CD]){
            ans += (pref[LCA_CD] - pref[b]);
        }
        int LCA_AB = lca(a, LCA_BC);
        if(upper(a, b) == 0 && upper(a, c) == 0 && upper(a, d) == 0 && upper(a, e) == 0){
            ans += (pref[LCA_BC] - pref[LCA_AB]) + (pref[a] - pref[LCA_AB]);
        } else if(depth[a] < depth[LCA_BC]){
            ans += (pref[LCA_BC] - pref[a]);
        }
        cout << ans << ent;
        ans = 0;
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int T = 1;
    // cin >> T;
    for(int i = 1;i <= T;i++){
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 47436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 55888 KB Output is correct
2 Correct 51 ms 57604 KB Output is correct
3 Correct 52 ms 58084 KB Output is correct
4 Correct 62 ms 58200 KB Output is correct
5 Correct 54 ms 58112 KB Output is correct
6 Correct 54 ms 58108 KB Output is correct
7 Correct 54 ms 58104 KB Output is correct
8 Correct 52 ms 58112 KB Output is correct
9 Correct 56 ms 58072 KB Output is correct
10 Correct 52 ms 58060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 147 ms 53984 KB Output is correct
2 Correct 238 ms 57008 KB Output is correct
3 Correct 232 ms 57252 KB Output is correct
4 Correct 222 ms 57248 KB Output is correct
5 Correct 239 ms 57252 KB Output is correct
6 Correct 224 ms 57252 KB Output is correct
7 Correct 222 ms 57252 KB Output is correct
8 Correct 224 ms 57248 KB Output is correct
9 Correct 269 ms 57252 KB Output is correct
10 Correct 224 ms 57252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 47436 KB Output is correct
2 Correct 53 ms 55888 KB Output is correct
3 Correct 51 ms 57604 KB Output is correct
4 Correct 52 ms 58084 KB Output is correct
5 Correct 62 ms 58200 KB Output is correct
6 Correct 54 ms 58112 KB Output is correct
7 Correct 54 ms 58108 KB Output is correct
8 Correct 54 ms 58104 KB Output is correct
9 Correct 52 ms 58112 KB Output is correct
10 Correct 56 ms 58072 KB Output is correct
11 Correct 52 ms 58060 KB Output is correct
12 Correct 147 ms 53984 KB Output is correct
13 Correct 238 ms 57008 KB Output is correct
14 Correct 232 ms 57252 KB Output is correct
15 Correct 222 ms 57248 KB Output is correct
16 Correct 239 ms 57252 KB Output is correct
17 Correct 224 ms 57252 KB Output is correct
18 Correct 222 ms 57252 KB Output is correct
19 Correct 224 ms 57248 KB Output is correct
20 Correct 269 ms 57252 KB Output is correct
21 Correct 224 ms 57252 KB Output is correct
22 Correct 52 ms 56132 KB Output is correct
23 Incorrect 53 ms 54344 KB Output isn't correct
24 Halted 0 ms 0 KB -