답안 #307551

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
307551 2020-09-28T15:17:08 Z talant117408 공장들 (JOI14_factories) C++17
100 / 100
4848 ms 194892 KB
#include "factories.h"
#include <bits/stdc++.h> 

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;

#define pb push_back
#define mp make_pair

const int NN = 5e5+7;
vector <vector <pii>> graph(NN);
vector <vector <ll>> dist(NN, vector <ll> (20));
vector <int> saizu(NN), par(NN), vis(NN), lvl(NN);
vector <ll> ans(NN, 2e18);
int timer;
stack <int> st;

void find_size(int v, int p, int l){
    saizu[v] = 1;
    
    for(auto to : graph[v]){
        if(!vis[to.first] && to.first != p){
            if(l) dist[to.first][l-1] = dist[v][l-1]+to.second;
            find_size(to.first, v, l);
            saizu[v] += saizu[to.first];
        }
    }
}

int find_centroid(int v, int p, int n){
    for(auto to : graph[v]){
        if(!vis[to.first] && to.first != p && saizu[to.first] > n/2){
            return find_centroid(to.first, v, n);
        }
    }
    return v;
}

void init_centroid(int v = 0, int p = -1, int l = 0){
    find_size(v, -1, l);
    
    int c = find_centroid(v, p, saizu[v]);
    vis[c] = 1;
    par[c] = p;
    lvl[c] = l;
    
    for(auto to : graph[c]){
        if(!vis[to.first]){
            dist[to.first][l] = to.second;
            init_centroid(to.first, c, l+1);
        }
    }
    vis[c] = 0;
}

void Init(int n, int x[], int y[], int w[]){
    for(int i = 0; i < n-1; i++){
        graph[x[i]].pb(mp(y[i], w[i]));
        graph[y[i]].pb(mp(x[i], w[i]));
    }
    init_centroid();
}

void update(int v){
    int x = v, l = lvl[v];
    while(x != -1){
        ans[x] = min(ans[x], dist[v][l]);
        st.push(x);
        x = par[x];
        l--;
    }
}

ll get(int v){
    ll x = v, mn = 2e18, l = lvl[v];
    while(x != -1){
        mn = min(ans[x]+dist[v][l], mn);
        x = par[x];
        l--;
    }
    return mn;
}

long long Query(int s, int x[], int t, int y[]){
    ll mn = 2e18;
    for(int i = 0; i < s; i++)
        update(x[i]);
    for(int i = 0; i < t; i++)
        mn = min(mn, get(y[i]));
    
    while(st.size()){
        ans[st.top()] = 2e18;
        st.pop();
    }
    
    return mn;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 121976 KB Output is correct
2 Correct 499 ms 129912 KB Output is correct
3 Correct 542 ms 130040 KB Output is correct
4 Correct 546 ms 130296 KB Output is correct
5 Correct 568 ms 130428 KB Output is correct
6 Correct 389 ms 129912 KB Output is correct
7 Correct 533 ms 129912 KB Output is correct
8 Correct 540 ms 130072 KB Output is correct
9 Correct 573 ms 130424 KB Output is correct
10 Correct 385 ms 129912 KB Output is correct
11 Correct 538 ms 129912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 121848 KB Output is correct
2 Correct 2258 ms 153208 KB Output is correct
3 Correct 3172 ms 156792 KB Output is correct
4 Correct 1029 ms 154592 KB Output is correct
5 Correct 4188 ms 191404 KB Output is correct
6 Correct 3193 ms 158588 KB Output is correct
7 Correct 1377 ms 150776 KB Output is correct
8 Correct 698 ms 151148 KB Output is correct
9 Correct 1521 ms 155640 KB Output is correct
10 Correct 1390 ms 152184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 121976 KB Output is correct
2 Correct 499 ms 129912 KB Output is correct
3 Correct 542 ms 130040 KB Output is correct
4 Correct 546 ms 130296 KB Output is correct
5 Correct 568 ms 130428 KB Output is correct
6 Correct 389 ms 129912 KB Output is correct
7 Correct 533 ms 129912 KB Output is correct
8 Correct 540 ms 130072 KB Output is correct
9 Correct 573 ms 130424 KB Output is correct
10 Correct 385 ms 129912 KB Output is correct
11 Correct 538 ms 129912 KB Output is correct
12 Correct 96 ms 121848 KB Output is correct
13 Correct 2258 ms 153208 KB Output is correct
14 Correct 3172 ms 156792 KB Output is correct
15 Correct 1029 ms 154592 KB Output is correct
16 Correct 4188 ms 191404 KB Output is correct
17 Correct 3193 ms 158588 KB Output is correct
18 Correct 1377 ms 150776 KB Output is correct
19 Correct 698 ms 151148 KB Output is correct
20 Correct 1521 ms 155640 KB Output is correct
21 Correct 1390 ms 152184 KB Output is correct
22 Correct 2673 ms 163448 KB Output is correct
23 Correct 2855 ms 183772 KB Output is correct
24 Correct 3835 ms 166564 KB Output is correct
25 Correct 3945 ms 170768 KB Output is correct
26 Correct 3909 ms 165388 KB Output is correct
27 Correct 4848 ms 194892 KB Output is correct
28 Correct 1313 ms 164940 KB Output is correct
29 Correct 3875 ms 164856 KB Output is correct
30 Correct 3882 ms 164088 KB Output is correct
31 Correct 3843 ms 164756 KB Output is correct
32 Correct 1551 ms 158712 KB Output is correct
33 Correct 711 ms 151864 KB Output is correct
34 Correct 1092 ms 148344 KB Output is correct
35 Correct 1099 ms 148344 KB Output is correct
36 Correct 1342 ms 148900 KB Output is correct
37 Correct 1342 ms 149112 KB Output is correct