Submission #708478

# Submission time Handle Problem Language Result Execution time Memory
708478 2023-03-11T20:07:01 Z Dan4Life Factories (JOI14_factories) C++17
100 / 100
5357 ms 181544 KB
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
using ll = long long;
const int mxN = (int)5e5+5;
const int mxLg = (int)20;
const ll LINF = (ll)3e18;
int n, sub[mxN], par[mxN], vis[mxN], lev[mxN];
ll best[mxN], dis[mxN][mxLg];
vector<pair<int,ll>> adj[mxN];

int fcen(int s, int p, int n){
    for(auto x : adj[s]){
        int u = x.fi;
        if(u!=p and sub[u]>n/2 and !vis[u])
            return fcen(u,s,n);
    }
    return s;
}

int dfs(int s, int p, int S, bool ok){
    sub[s]=1;
    for(auto x : adj[s]){
        int u = x.fi; ll w = x.se;
        if(u==p or vis[u]) continue;
        if(ok) dis[u][lev[u]-lev[S]]=dis[s][lev[s]-lev[S]]+w;
        sub[s]+=dfs(u,s, S,ok);
    }
    return sub[s];
}

void cen_decomp1(int s, int p){
    int n = dfs(s,p,s,0);
    int cen = fcen(s,p,n);
    vis[cen] = 1;
    if(p!=-1) lev[cen]=lev[p]+1;
    par[cen]=p;
    for(auto x : adj[cen]){
        int u = x.fi;
        if(u!=p and !vis[u])
            cen_decomp1(u,cen);
    }
}

void cen_decomp(int s, int p){
    int n = dfs(s,p,s,0);
    int cen = fcen(s,p,n);
    vis[cen] = 1; dfs(cen,p,cen,1);
    for(auto x : adj[cen]){
        int u = x.fi, w = x.se;
        if(u!=p and !vis[u]) cen_decomp(u,cen);
    }
}

void Init(int N, int a[], int b[], int d[]) {
    n = N; memset(par,-1,sizeof(par));
    for(int i = 0; i <= n; i++) best[i]=LINF;
    for(int i = 0; i < n-1; i++){
        adj[a[i]].pb({b[i],d[i]});
        adj[b[i]].pb({a[i],d[i]});
    }
    cen_decomp1(0,-1); fill(vis,vis+n,0); cen_decomp(0,-1);
}

ll Query(int s, int x[], int t, int y[]) {
    for(int i = 0; i < s; i++){
        int a = x[i], j = 0;
        while(a!=-1){
            ll DD = dis[x[i]][j];
            best[a]=min(best[a],DD), a = par[a], j++;
        }
    }
    ll ans = LINF;
    for(int i = 0; i < t; i++){
        int x = y[i], j = 0;
        while(x!=-1){
            ans = min(ans, dis[y[i]][j]+best[x]);
            x=par[x], j++;
        }
    }
    for(int i = 0; i < s; i++){
        int a = x[i];
        while(a!=-1) best[a]=LINF, a = par[a];
    }
    return ans;
}

Compilation message

factories.cpp: In function 'void cen_decomp(int, int)':
factories.cpp:53:23: warning: unused variable 'w' [-Wunused-variable]
   53 |         int u = x.fi, w = x.se;
      |                       ^
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14420 KB Output is correct
2 Correct 251 ms 23220 KB Output is correct
3 Correct 268 ms 23228 KB Output is correct
4 Correct 254 ms 23196 KB Output is correct
5 Correct 281 ms 23656 KB Output is correct
6 Correct 182 ms 23240 KB Output is correct
7 Correct 257 ms 23192 KB Output is correct
8 Correct 264 ms 23244 KB Output is correct
9 Correct 290 ms 23680 KB Output is correct
10 Correct 173 ms 23188 KB Output is correct
11 Correct 261 ms 23232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14164 KB Output is correct
2 Correct 2013 ms 140648 KB Output is correct
3 Correct 3549 ms 145200 KB Output is correct
4 Correct 617 ms 138224 KB Output is correct
5 Correct 4732 ms 181544 KB Output is correct
6 Correct 3611 ms 146528 KB Output is correct
7 Correct 921 ms 48384 KB Output is correct
8 Correct 336 ms 47828 KB Output is correct
9 Correct 1024 ms 53524 KB Output is correct
10 Correct 844 ms 49520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14420 KB Output is correct
2 Correct 251 ms 23220 KB Output is correct
3 Correct 268 ms 23228 KB Output is correct
4 Correct 254 ms 23196 KB Output is correct
5 Correct 281 ms 23656 KB Output is correct
6 Correct 182 ms 23240 KB Output is correct
7 Correct 257 ms 23192 KB Output is correct
8 Correct 264 ms 23244 KB Output is correct
9 Correct 290 ms 23680 KB Output is correct
10 Correct 173 ms 23188 KB Output is correct
11 Correct 261 ms 23232 KB Output is correct
12 Correct 8 ms 14164 KB Output is correct
13 Correct 2013 ms 140648 KB Output is correct
14 Correct 3549 ms 145200 KB Output is correct
15 Correct 617 ms 138224 KB Output is correct
16 Correct 4732 ms 181544 KB Output is correct
17 Correct 3611 ms 146528 KB Output is correct
18 Correct 921 ms 48384 KB Output is correct
19 Correct 336 ms 47828 KB Output is correct
20 Correct 1024 ms 53524 KB Output is correct
21 Correct 844 ms 49520 KB Output is correct
22 Correct 2302 ms 142040 KB Output is correct
23 Correct 2414 ms 144436 KB Output is correct
24 Correct 3930 ms 146844 KB Output is correct
25 Correct 4170 ms 150496 KB Output is correct
26 Correct 4035 ms 147116 KB Output is correct
27 Correct 5357 ms 176868 KB Output is correct
28 Correct 791 ms 142160 KB Output is correct
29 Correct 4005 ms 146632 KB Output is correct
30 Correct 3977 ms 145964 KB Output is correct
31 Correct 3918 ms 146804 KB Output is correct
32 Correct 1033 ms 54920 KB Output is correct
33 Correct 333 ms 48288 KB Output is correct
34 Correct 580 ms 45944 KB Output is correct
35 Correct 591 ms 45844 KB Output is correct
36 Correct 781 ms 47032 KB Output is correct
37 Correct 816 ms 47000 KB Output is correct