Submission #115043

# Submission time Handle Problem Language Result Execution time Memory
115043 2019-06-04T17:02:01 Z liwi Factories (JOI14_factories) C++11
100 / 100
5668 ms 194304 KB
#include <vector>
#include <climits>
#include <iostream>

using namespace std;

#define pb push_back
#define mp make_pair

#define MAXN 500001
#define LOGN 25

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

int num_nodes,num_q,sz[MAXN],lvl[MAXN],par[MAXN],upd[MAXN],qcnt,A[MAXN],B[MAXN],D[MAXN],cur_lvl;
ll small[MAXN],dis[MAXN][LOGN];
pii ret;
bool done[MAXN];
vector<pii> tconnections[MAXN];

int getSz(int node, int prev, ll dd){
    sz[node] = 1;
    dis[node][cur_lvl] = dd;
    for(pii check:tconnections[node]){
        if(check.first == prev || done[check.first]) continue;
        sz[node]+=getSz(check.first,node,dd+check.second);
    }
    return sz[node];
}

int centroid(int node, int prev, int psz){
    sz[node] = 1;
    int mxsz = 0;
    for(pii check:tconnections[node]){
        if(check.first == prev || done[check.first]) continue;
        centroid(check.first,node,psz);
        mxsz = max(mxsz,sz[check.first]);
        sz[node]+=sz[check.first];
    }
    mxsz = max(mxsz, psz-sz[node]);
    if(mxsz < ret.first) ret = mp(mxsz, node);
    return ret.second;
}

void getTree(int node, int prev, int mx, int lv){
    cur_lvl = lv;
    ret = mp(INT_MAX,-1);
    node = centroid(node,-1,mx);
    par[node] = prev, lvl[node] = lv, done[node] = true;
    getSz(node,-1,0);
    for(pii check:tconnections[node]){
        if(check.first == prev || done[check.first]) continue;
        getTree(check.first,node,sz[check.first],lv+1);
    }
}

void Init(int N, int A[MAXN], int B[MAXN], int D[MAXN]){
    for(int i=0; i<N-1; i++){
        int a = A[i], b = B[i], dis = D[i];
        tconnections[a].pb(mp(b,dis));
        tconnections[b].pb(mp(a,dis));
    }
    getTree(0,-1,num_nodes,0);
}

ll Query(int s1, int X[MAXN], int s2, int Y[MAXN]){
    ++qcnt;
    for(int i=0; i<s1; i++){
        int current = X[i];
        while(current != -1){
            if(upd[current] == qcnt) small[current] = small[current]<dis[X[i]][lvl[current]]?small[current]:dis[X[i]][lvl[current]];
            else small[current] = dis[X[i]][lvl[current]], upd[current] = qcnt;
            current = par[current];
        }
    }
    ll best = LONG_MAX;
    for(int i=0; i<s2; i++){
        int current = Y[i];
        while(current != -1){
            if(upd[current] == qcnt) best = best<dis[Y[i]][lvl[current]]+small[current]?best:dis[Y[i]][lvl[current]]+small[current];
            current = par[current];
        }
    }
    return best;
}
# Verdict Execution time Memory Grader output
1 Correct 18 ms 12544 KB Output is correct
2 Correct 322 ms 24440 KB Output is correct
3 Correct 337 ms 24312 KB Output is correct
4 Correct 325 ms 24244 KB Output is correct
5 Correct 348 ms 24696 KB Output is correct
6 Correct 254 ms 24292 KB Output is correct
7 Correct 337 ms 24184 KB Output is correct
8 Correct 362 ms 24188 KB Output is correct
9 Correct 341 ms 24624 KB Output is correct
10 Correct 240 ms 24300 KB Output is correct
11 Correct 328 ms 24304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12288 KB Output is correct
2 Correct 2634 ms 153848 KB Output is correct
3 Correct 4035 ms 156200 KB Output is correct
4 Correct 1180 ms 154468 KB Output is correct
5 Correct 4968 ms 190264 KB Output is correct
6 Correct 4022 ms 157608 KB Output is correct
7 Correct 1127 ms 51836 KB Output is correct
8 Correct 480 ms 52584 KB Output is correct
9 Correct 1249 ms 59640 KB Output is correct
10 Correct 1133 ms 53024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 12544 KB Output is correct
2 Correct 322 ms 24440 KB Output is correct
3 Correct 337 ms 24312 KB Output is correct
4 Correct 325 ms 24244 KB Output is correct
5 Correct 348 ms 24696 KB Output is correct
6 Correct 254 ms 24292 KB Output is correct
7 Correct 337 ms 24184 KB Output is correct
8 Correct 362 ms 24188 KB Output is correct
9 Correct 341 ms 24624 KB Output is correct
10 Correct 240 ms 24300 KB Output is correct
11 Correct 328 ms 24304 KB Output is correct
12 Correct 13 ms 12288 KB Output is correct
13 Correct 2634 ms 153848 KB Output is correct
14 Correct 4035 ms 156200 KB Output is correct
15 Correct 1180 ms 154468 KB Output is correct
16 Correct 4968 ms 190264 KB Output is correct
17 Correct 4022 ms 157608 KB Output is correct
18 Correct 1127 ms 51836 KB Output is correct
19 Correct 480 ms 52584 KB Output is correct
20 Correct 1249 ms 59640 KB Output is correct
21 Correct 1133 ms 53024 KB Output is correct
22 Correct 3108 ms 155388 KB Output is correct
23 Correct 3234 ms 157732 KB Output is correct
24 Correct 4733 ms 158004 KB Output is correct
25 Correct 4481 ms 161528 KB Output is correct
26 Correct 4640 ms 158328 KB Output is correct
27 Correct 5668 ms 194304 KB Output is correct
28 Correct 1365 ms 158660 KB Output is correct
29 Correct 4571 ms 158024 KB Output is correct
30 Correct 4530 ms 157144 KB Output is correct
31 Correct 4763 ms 157908 KB Output is correct
32 Correct 1302 ms 60024 KB Output is correct
33 Correct 508 ms 52456 KB Output is correct
34 Correct 847 ms 49476 KB Output is correct
35 Correct 848 ms 49584 KB Output is correct
36 Correct 1126 ms 50320 KB Output is correct
37 Correct 1140 ms 50156 KB Output is correct