답안 #776707

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
776707 2023-07-08T07:35:13 Z Cookie Roadside Advertisements (NOI17_roadsideadverts) C++14
100 / 100
55 ms 13296 KB
#include <bits/stdc++.h>
using namespace std;
#define LIFESUCKS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define ld long double
#define ar array
#include<cstdio>
#define vt vector
#include<fstream>
ifstream fin("measurement.in");
ofstream fout("measurement.out");
#include<fstream>
#define pb push_back
#define all(c) (c).begin(), (c).end()
//#define length(x) (int)(x).size()
#define fi first
#define se second
#define vt vector
using namespace std;
const int mxn = 5e4 + 5;
struct th{
    int val = 0, sm = 0;
};
int n;
vt<pair<int, int>>adj[mxn + 3];
int depth[mxn + 3] = {0};
th up[mxn][17];
ll res = 0;
int num[mxn + 3] = {0};
int cr = 1;
bool cmp(int a, int b){
    return(num[a] < num[b]);
}
void dfs(int s, int pre){
 
    for(auto i: adj[s]){
        if(i.fi == pre)continue;
        num[i.fi] = cr++;
        int v = i.fi, c = i.se;
        depth[v] = depth[s] + 1;
        up[v][0].val = s; up[v][0].sm = c; 
        dfs(v, s);
    }
}
void build(){
    for(int i = 1; i < 17; i++){
        for(int j = 1; j <= n; j++){
            up[j][i].val = up[up[j][i - 1].val][i - 1].val;
            up[j][i].sm = up[j][i - 1].sm + up[up[j][i - 1].val][i - 1].sm;
        }
    }
}
int find(int a, int b, bool ok){
    if(depth[a] < depth[b])swap(a, b);
    int k =depth[a] - depth[b];
    ll curr = 0;
    for(int i = 0; i < 17; i++){
        if(k & (1 << i)){
            curr += up[a][i].sm;
            a = up[a][i].val;
        }
    }
    //cout << curr << " ";
    if(a == b){
        if(ok)return(curr);
        return(a);
    }
    for(int i = 16; i >= 0; i--){
        if(up[a][i].val != up[b][i].val){
            curr += up[a][i].sm + up[b][i].sm;
            a = up[a][i].val; b = up[b][i].val;
        }
    }
    curr += up[a][0].sm + up[b][0].sm;
    if(ok)return(curr);
    return(up[a][0].val);
}
int main()
{
    LIFESUCKS;
    cin >> n;
    
    for(int i = 1; i < n; i++){
        int u, v, c; cin >> u >> v >> c;
        adj[u].pb({v, c});
        adj[v].pb({u, c});
    }
    dfs(0, -1);
    build();
    int a[5];
    int q; cin >> q;
    
    for(int i = 0; i < q; i++){
        for(int j = 0; j < 5; j++)cin >> a[j];
        sort(a, a + 5, cmp);
        int lca = find(a[1], a[0], false);
        
        for(int j = 2; j < 5; j++){
            lca = find(lca, a[j], false);
        }
        res = find(lca, a[0], true);
        
        for(int j = 1; j < 5; j++){
            
            res += find(a[j], find(a[j - 1], a[j], false), true);
            //cout << a[j] << " " << find(a[j], a[j - 1], false) << " " << res << "\n";
        }
        cout << res << "\n";
        
    }
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 11776 KB Output is correct
2 Correct 41 ms 13280 KB Output is correct
3 Correct 42 ms 13296 KB Output is correct
4 Correct 40 ms 13244 KB Output is correct
5 Correct 40 ms 13240 KB Output is correct
6 Correct 41 ms 13296 KB Output is correct
7 Correct 40 ms 13232 KB Output is correct
8 Correct 53 ms 13240 KB Output is correct
9 Correct 40 ms 13268 KB Output is correct
10 Correct 40 ms 13248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 10324 KB Output is correct
2 Correct 23 ms 12640 KB Output is correct
3 Correct 25 ms 12752 KB Output is correct
4 Correct 23 ms 12752 KB Output is correct
5 Correct 34 ms 12764 KB Output is correct
6 Correct 26 ms 12736 KB Output is correct
7 Correct 26 ms 12732 KB Output is correct
8 Correct 23 ms 12756 KB Output is correct
9 Correct 24 ms 12656 KB Output is correct
10 Correct 24 ms 12700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 43 ms 11776 KB Output is correct
3 Correct 41 ms 13280 KB Output is correct
4 Correct 42 ms 13296 KB Output is correct
5 Correct 40 ms 13244 KB Output is correct
6 Correct 40 ms 13240 KB Output is correct
7 Correct 41 ms 13296 KB Output is correct
8 Correct 40 ms 13232 KB Output is correct
9 Correct 53 ms 13240 KB Output is correct
10 Correct 40 ms 13268 KB Output is correct
11 Correct 40 ms 13248 KB Output is correct
12 Correct 21 ms 10324 KB Output is correct
13 Correct 23 ms 12640 KB Output is correct
14 Correct 25 ms 12752 KB Output is correct
15 Correct 23 ms 12752 KB Output is correct
16 Correct 34 ms 12764 KB Output is correct
17 Correct 26 ms 12736 KB Output is correct
18 Correct 26 ms 12732 KB Output is correct
19 Correct 23 ms 12756 KB Output is correct
20 Correct 24 ms 12656 KB Output is correct
21 Correct 24 ms 12700 KB Output is correct
22 Correct 47 ms 11792 KB Output is correct
23 Correct 32 ms 10368 KB Output is correct
24 Correct 55 ms 12824 KB Output is correct
25 Correct 43 ms 12780 KB Output is correct
26 Correct 40 ms 12796 KB Output is correct
27 Correct 41 ms 12768 KB Output is correct
28 Correct 40 ms 12748 KB Output is correct
29 Correct 40 ms 12756 KB Output is correct
30 Correct 41 ms 12804 KB Output is correct
31 Correct 39 ms 12804 KB Output is correct