Submission #209753

# Submission time Handle Problem Language Result Execution time Memory
209753 2020-03-15T13:17:55 Z DodgeBallMan Factories (JOI14_factories) C++14
100 / 100
5882 ms 205924 KB
#include <bits/stdc++.h>
#include "factories.h"

#define pii pair<int, int>
#define x first
#define y second 

using namespace std;

const int N = 5e5 + 10;
int book[N], sz[N], dep[N], par[N], n, now;
bool chk[N];
long long d[N][25], mn[N];
vector<pii> g[N];

int getsz( int u, int p ) { sz[u] = 1; for( pii v : g[u] ) if( v.x != p && !chk[v.x] ) sz[u] += getsz( v.x, u ); return sz[u]; }

int find_cen( int u, int p, int all, pii &ret ) {
    int mx = all - sz[u];
    for( pii v : g[u] ) if( v.x != p && !chk[v.x] ) {
        mx = max( mx, find_cen( v.x, u, all, ret ) );
    }
    ret = min( ret, pii( mx, u ) );
    return sz[u];
}

void dfs( int u, int p, int dep ) {
    for( pii v : g[u] ) if( v.x != p && !chk[v.x] ) {
        d[v.x][dep] = d[u][dep] + ( long long )v.y;
        dfs( v.x, u, dep );
    }
}

void build_centroid( int u, int p ) {
    pii ret( 1e9, -1 );
    getsz( u, u ), find_cen( u, u, sz[u], ret );
    //printf("%d %d %d\n",sz[u],ret.x,ret.y);
    dep[u=ret.y] = dep[p] + 1, par[u] = p;
    //printf("centroid : %d\n",u);
    dfs( u, u, dep[u] );
    chk[u] = true;
    for( pii v : g[u] ) if( !chk[v.x] ) build_centroid( v.x, u );
}

long long query( int u ) {
    //printf("QUERY%d\n",u);
    long long ret = 1e18;
    if( book[u] == now ) ret = min( ret, mn[u] );
    for( int i = par[u] ; i ; i = par[i] ) {
        //printf("%d %d\n",i,par[i]);
        if( book[i] != now ) continue ;
        ret = min( mn[i] + d[u][dep[i]], ret );
    }
    return ret;
}

void update( int u ) {
    //printf("UPDATE%d\n",u);
    book[u] = now, mn[u] = 0;
    for( int i = par[u] ; i ; i = par[i] ) {
        if( book[i] != now ) {
            book[i] = now, mn[i] = d[u][dep[i]];
        }
        else mn[i] = min( mn[i], d[u][dep[i]] );
        //printf("%d %d %d %d\n",i,par[i],book[i],mn[i]);
    }
}

void Init( int N, int A[], int B[], int D[] ) {
    n = N;
    for( int i = 0 ; i < n-1 ; i++ ) g[++A[i]].emplace_back(++B[i],D[i]), g[B[i]].emplace_back( A[i], D[i] );
    dep[1] = 1;
    build_centroid( 1, 0 );
}

long long Query( int s, int x[], int t, int y[] ) {
    ++now;
    long long ret = 1e18;
    for( int i = 0 ; i < s ; i++ ) update( ++x[i] );
    for( int i = 0 ; i < t ; i++ ) {
        ret = min( ret, query( ++y[i] ) );
    }
    return ret;
}
/*int main()
{
    int n, q, a[100], b[100], d[100];
    scanf("%d %d",&n,&q);
    for( int i = 0 ; i < n - 1 ; i++ ) scanf("%d %d %d",&a[i],&b[i],&d[i]);
    Init( n, a, b, d );
    for( int i = 0 ; i < q ; i++ ) {
        int s, t, x[100], y[100];
        scanf("%d %d",&s,&t);
        for( int j = 0 ; j < s ; j++ ) scanf("%d",&x[j]);
        for( int j = 0 ; j < t ; j++ ) scanf("%d",&y[j]);
        printf("%lld\n",Query( s, x, t, y ) );
    }
    return 0;
}*/
# Verdict Execution time Memory Grader output
1 Correct 21 ms 12664 KB Output is correct
2 Correct 392 ms 22392 KB Output is correct
3 Correct 419 ms 22392 KB Output is correct
4 Correct 416 ms 22392 KB Output is correct
5 Correct 445 ms 22624 KB Output is correct
6 Correct 317 ms 22396 KB Output is correct
7 Correct 417 ms 22392 KB Output is correct
8 Correct 425 ms 22392 KB Output is correct
9 Correct 434 ms 22648 KB Output is correct
10 Correct 318 ms 22392 KB Output is correct
11 Correct 421 ms 22424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12408 KB Output is correct
2 Correct 2500 ms 153724 KB Output is correct
3 Correct 3863 ms 174068 KB Output is correct
4 Correct 846 ms 172760 KB Output is correct
5 Correct 5061 ms 203244 KB Output is correct
6 Correct 3959 ms 176088 KB Output is correct
7 Correct 1012 ms 62840 KB Output is correct
8 Correct 512 ms 63496 KB Output is correct
9 Correct 1157 ms 67192 KB Output is correct
10 Correct 1058 ms 64248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 12664 KB Output is correct
2 Correct 392 ms 22392 KB Output is correct
3 Correct 419 ms 22392 KB Output is correct
4 Correct 416 ms 22392 KB Output is correct
5 Correct 445 ms 22624 KB Output is correct
6 Correct 317 ms 22396 KB Output is correct
7 Correct 417 ms 22392 KB Output is correct
8 Correct 425 ms 22392 KB Output is correct
9 Correct 434 ms 22648 KB Output is correct
10 Correct 318 ms 22392 KB Output is correct
11 Correct 421 ms 22424 KB Output is correct
12 Correct 13 ms 12408 KB Output is correct
13 Correct 2500 ms 153724 KB Output is correct
14 Correct 3863 ms 174068 KB Output is correct
15 Correct 846 ms 172760 KB Output is correct
16 Correct 5061 ms 203244 KB Output is correct
17 Correct 3959 ms 176088 KB Output is correct
18 Correct 1012 ms 62840 KB Output is correct
19 Correct 512 ms 63496 KB Output is correct
20 Correct 1157 ms 67192 KB Output is correct
21 Correct 1058 ms 64248 KB Output is correct
22 Correct 2805 ms 179552 KB Output is correct
23 Correct 2845 ms 182224 KB Output is correct
24 Correct 4742 ms 182128 KB Output is correct
25 Correct 4759 ms 186056 KB Output is correct
26 Correct 4647 ms 182292 KB Output is correct
27 Correct 5882 ms 205924 KB Output is correct
28 Correct 1035 ms 183004 KB Output is correct
29 Correct 5033 ms 181760 KB Output is correct
30 Correct 5082 ms 181220 KB Output is correct
31 Correct 5166 ms 181880 KB Output is correct
32 Correct 1365 ms 68172 KB Output is correct
33 Correct 539 ms 63944 KB Output is correct
34 Correct 856 ms 60488 KB Output is correct
35 Correct 914 ms 60564 KB Output is correct
36 Correct 1174 ms 61184 KB Output is correct
37 Correct 1142 ms 61116 KB Output is correct