Submission #52310

# Submission time Handle Problem Language Result Execution time Memory
52310 2018-06-25T09:24:47 Z Diuven Factories (JOI14_factories) C++11
100 / 100
7532 ms 351568 KB
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;

const int MX=500010, LG=20;
const ll inf=2e17;

struct edge {
    int to; ll co;
};

int n, dep[MX];
vector<edge> G[MX];

vector<edge> C, D[MX];
bool done[MX];


int sub[MX];
void dfs(int v, int p=-1){
    sub[v]=1;
    for(edge &e:G[v]){
        int x=e.to;
        if(done[x] || x==p) continue;
        dfs(x,v);
        sub[v]+=sub[x];
    }
}

int findcen(int v, int sz){
    for(edge &e:G[v]){
        int x=e.to;
        if(done[x] || sub[x]>sub[v]) continue;
        if(sub[x]*2>sz) return findcen(x,sz);
    }
    return v;
}
int now;
void dfs2(int v, int p, ll d){
    D[v].push_back({now, d});
    for(edge &e:G[v]){
        int x=e.to; ll c=e.co;
        if(done[x] || x==p) continue;
        dfs2(x,v,d+c);
    }
}

ll mn1[MX], mn2[MX];

void process(int v=1){
    dfs(v,0);
    v=findcen(v,sub[v]); done[v]=true;
    now=v;
    for(edge &e:G[v]){
        int x=e.to; ll c=e.co;
        if(done[x]) continue;
        dfs2(x,v,c);
    }
    D[v].push_back({v,0});
    for(edge &e:G[v]){
        int x=e.to;
        if(done[x]) continue;
        process(x);
    }
}

void Init(int N, int A[], int B[], int D[]){
    n=N;
    fill(mn1, mn1+n+1, inf);
    fill(mn2, mn2+n+1, inf);
    for(int i=0; i<=n-2; i++){
        int u=A[i]+1, v=B[i]+1;
        G[u].push_back({v,D[i]});
        G[v].push_back({u,D[i]});
    }
    process();
}

ll Query(int S, int X[], int T, int Y[]) {
    for(int i=0; i<S; i++) X[i]++;
    for(int i=0; i<T; i++) Y[i]++;

    vector<int> V, W;

    for(int i=0; i<S; i++){
        for(edge &e:D[X[i]])
            V.push_back(e.to), mn1[e.to]=min(mn1[e.to], e.co);
    }
    for(int i=0; i<T; i++){
        for(edge &e:D[Y[i]])
            W.push_back(e.to), mn2[e.to]=min(mn2[e.to], e.co);
    }
    ll ans=inf;
    for(int v:V) ans=min(ans, mn1[v]+mn2[v]);
    for(int w:W) ans=min(ans, mn1[w]+mn2[w]);
    for(int v:V) mn1[v]=inf;
    for(int w:W) mn2[w]=inf;
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 38 ms 24184 KB Output is correct
2 Correct 444 ms 33320 KB Output is correct
3 Correct 496 ms 33828 KB Output is correct
4 Correct 540 ms 34100 KB Output is correct
5 Correct 562 ms 34344 KB Output is correct
6 Correct 489 ms 34344 KB Output is correct
7 Correct 495 ms 34344 KB Output is correct
8 Correct 503 ms 34344 KB Output is correct
9 Correct 587 ms 34372 KB Output is correct
10 Correct 365 ms 34372 KB Output is correct
11 Correct 506 ms 34372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 34372 KB Output is correct
2 Correct 3809 ms 199932 KB Output is correct
3 Correct 6087 ms 270924 KB Output is correct
4 Correct 1567 ms 270924 KB Output is correct
5 Correct 7169 ms 345828 KB Output is correct
6 Correct 5665 ms 345828 KB Output is correct
7 Correct 1877 ms 345828 KB Output is correct
8 Correct 803 ms 345828 KB Output is correct
9 Correct 2028 ms 345828 KB Output is correct
10 Correct 1854 ms 345828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 24184 KB Output is correct
2 Correct 444 ms 33320 KB Output is correct
3 Correct 496 ms 33828 KB Output is correct
4 Correct 540 ms 34100 KB Output is correct
5 Correct 562 ms 34344 KB Output is correct
6 Correct 489 ms 34344 KB Output is correct
7 Correct 495 ms 34344 KB Output is correct
8 Correct 503 ms 34344 KB Output is correct
9 Correct 587 ms 34372 KB Output is correct
10 Correct 365 ms 34372 KB Output is correct
11 Correct 506 ms 34372 KB Output is correct
12 Correct 26 ms 34372 KB Output is correct
13 Correct 3809 ms 199932 KB Output is correct
14 Correct 6087 ms 270924 KB Output is correct
15 Correct 1567 ms 270924 KB Output is correct
16 Correct 7169 ms 345828 KB Output is correct
17 Correct 5665 ms 345828 KB Output is correct
18 Correct 1877 ms 345828 KB Output is correct
19 Correct 803 ms 345828 KB Output is correct
20 Correct 2028 ms 345828 KB Output is correct
21 Correct 1854 ms 345828 KB Output is correct
22 Correct 4903 ms 345828 KB Output is correct
23 Correct 4825 ms 345828 KB Output is correct
24 Correct 6491 ms 345828 KB Output is correct
25 Correct 6320 ms 345828 KB Output is correct
26 Correct 6110 ms 345828 KB Output is correct
27 Correct 7532 ms 351568 KB Output is correct
28 Correct 2213 ms 351568 KB Output is correct
29 Correct 6606 ms 351568 KB Output is correct
30 Correct 6432 ms 351568 KB Output is correct
31 Correct 6422 ms 351568 KB Output is correct
32 Correct 2176 ms 351568 KB Output is correct
33 Correct 754 ms 351568 KB Output is correct
34 Correct 1559 ms 351568 KB Output is correct
35 Correct 1575 ms 351568 KB Output is correct
36 Correct 1880 ms 351568 KB Output is correct
37 Correct 1838 ms 351568 KB Output is correct