답안 #116546

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
116546 2019-06-12T22:17:23 Z YazanZk 공장들 (JOI14_factories) C++14
100 / 100
7508 ms 233416 KB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

const int N = 5 * 1e5 + 1 ;
const ll inf = 1e18 ;

vector < pair < int, int > > G[N];
int sub[N], p[N] , par[N];
bitset < N > blocked;

void get_size(int u, int pa) {
    p[u] = pa ;
    sub[u] = 1 ;
    for(auto e : G[u]) {
        int v = e.first ;
        if(blocked[v] || v == pa) continue ;
        get_size(v, u);
        sub[u] += sub[v];
    }
}

vector < ll > dis[N];

void solve(int u , int p , ll d){
     for(auto e : G[u]){
          int v = e.first , w = e.second ;
          if(blocked[v] || v == p) continue ;
          dis[v].emplace_back(d + w);
          solve(v , u , d + w);
     }
}

int get_centroid(int src) {

    get_size(src, src);

    int centroid = src, best = sub[src] ;
    queue < int > Q ;
    Q.emplace(src);

    while(!Q.empty()) {
        int u = Q.front();
        Q.pop();

        int size = sub[src] - sub[u];
        for(auto e : G[u]) {
            int v = e.first;
            if(blocked[v] || v == p[u])
                continue ;
            Q.emplace(v);
            size = max(size, sub[v]);
        }

        if(best > size) {
            best = size;
            centroid = u ;
        }
    }

    blocked[centroid] = true;

    solve(centroid , centroid , 0);

    for(auto e : G[centroid]) {
        int v = e.first ;
        if(blocked[v]) continue ;
        int child = get_centroid(v);
        par[child] = centroid ;
    }

    return centroid ;
}

ll ans[N];
int vis[N];
int t = 1 ;

inline void update(int &u) {
    int v = u ;
    int i = dis[u].size() - 1;
    while(true) {
        if(vis[v] != t)
            vis[v] = t , ans[v] = inf ;
        ans[v] = min(ans[v], dis[u][i]);
        if(v == par[v]) break ;
        v = par[v];
        i--;
    }
}

inline ll query(int &u) {
    int v = u;
    ll ret = inf ;
    int i = dis[u].size() - 1 ;
    while(true) {
        if(vis[v] != t)
            vis[v] = t , ans[v] = inf ;
        ret = min(ret, ans[v] + dis[u][i]);
        if(v == par[v]) break ;
        v = par[v];
        i-- ;
    }
    return ret;
}


ll Query(int S, int X[], int T, int Y[]) {

    t++ ;

    for(int i = 0 ; i < T ; i++) {
        update(Y[i]);
    }

    ll ret = inf ;
    for(int i = 0 ; i < S ; i++) {
        ret = min(ret,  query(X[i]));
    }

    return ret;
}

void Init(int n , int A[], int B[], int D[]) {


    for(int i = 0 ; i < n - 1 ; i++) {
        int u = A[i], v = B[i], w = D[i];
        G[u].push_back({v, w});
        G[v].push_back({u, w});
    }

    int root = get_centroid(0);
    par[root] = root;

    for(int i = 0 ; i < n ; i++) dis[i].push_back(0);
}

# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 24320 KB Output is correct
2 Correct 343 ms 32844 KB Output is correct
3 Correct 360 ms 33016 KB Output is correct
4 Correct 357 ms 33108 KB Output is correct
5 Correct 372 ms 33272 KB Output is correct
6 Correct 255 ms 32388 KB Output is correct
7 Correct 359 ms 32988 KB Output is correct
8 Correct 356 ms 33148 KB Output is correct
9 Correct 371 ms 33272 KB Output is correct
10 Correct 257 ms 32396 KB Output is correct
11 Correct 358 ms 32964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 24064 KB Output is correct
2 Correct 3613 ms 137124 KB Output is correct
3 Correct 5272 ms 181532 KB Output is correct
4 Correct 1248 ms 83676 KB Output is correct
5 Correct 6395 ms 233416 KB Output is correct
6 Correct 5508 ms 182416 KB Output is correct
7 Correct 1359 ms 55532 KB Output is correct
8 Correct 484 ms 44904 KB Output is correct
9 Correct 1507 ms 69256 KB Output is correct
10 Correct 1368 ms 56776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 24320 KB Output is correct
2 Correct 343 ms 32844 KB Output is correct
3 Correct 360 ms 33016 KB Output is correct
4 Correct 357 ms 33108 KB Output is correct
5 Correct 372 ms 33272 KB Output is correct
6 Correct 255 ms 32388 KB Output is correct
7 Correct 359 ms 32988 KB Output is correct
8 Correct 356 ms 33148 KB Output is correct
9 Correct 371 ms 33272 KB Output is correct
10 Correct 257 ms 32396 KB Output is correct
11 Correct 358 ms 32964 KB Output is correct
12 Correct 22 ms 24064 KB Output is correct
13 Correct 3613 ms 137124 KB Output is correct
14 Correct 5272 ms 181532 KB Output is correct
15 Correct 1248 ms 83676 KB Output is correct
16 Correct 6395 ms 233416 KB Output is correct
17 Correct 5508 ms 182416 KB Output is correct
18 Correct 1359 ms 55532 KB Output is correct
19 Correct 484 ms 44904 KB Output is correct
20 Correct 1507 ms 69256 KB Output is correct
21 Correct 1368 ms 56776 KB Output is correct
22 Correct 3935 ms 138200 KB Output is correct
23 Correct 4145 ms 141072 KB Output is correct
24 Correct 5918 ms 183096 KB Output is correct
25 Correct 5993 ms 186812 KB Output is correct
26 Correct 5908 ms 183692 KB Output is correct
27 Correct 7508 ms 230296 KB Output is correct
28 Correct 1594 ms 87772 KB Output is correct
29 Correct 5896 ms 182880 KB Output is correct
30 Correct 5918 ms 182688 KB Output is correct
31 Correct 6146 ms 183060 KB Output is correct
32 Correct 1486 ms 69688 KB Output is correct
33 Correct 440 ms 45288 KB Output is correct
34 Correct 914 ms 52136 KB Output is correct
35 Correct 982 ms 52608 KB Output is correct
36 Correct 1344 ms 54276 KB Output is correct
37 Correct 1308 ms 54136 KB Output is correct