답안 #708746

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
708746 2023-03-12T08:58:13 Z ToroTN 공장들 (JOI14_factories) C++14
100 / 100
6194 ms 238932 KB
#include "factories.h"
#include<bits/stdc++.h>
using namespace std;
//#include "grader.cpp"
#define ll long long
#define pb push_back
#define X first
#define Y second
const int maxmum=500005,maxlift=25;
vector<pair<int,int> > v[maxmum];
int n,lv[maxmum],par[maxmum][maxlift],sz_opt[maxmum];
ll d[maxmum];
void dfs(int u,int p)
{
    for(int i=0;i<sz_opt[u];i++)
    {
        int node=v[u][i].X,cost=v[u][i].Y;
        if(node==p)continue;
        lv[node]=lv[u]+1;
        d[node]=d[u]+cost;
        par[node][0]=u;
        dfs(node,u);
    }
}
int lca(int a,int b)
{
    if(lv[a]<lv[b])swap(a,b);
    int lv_diff=lv[a]-lv[b];
    for(int i=19;i>=0;i--)
    {
        if(lv_diff&(1<<i))
        {
            a=par[a][i];
        }
    }
    if(a==b)return a;
    for(int i=19;i>=0;i--)
    {
        if(par[a][i]!=par[b][i])
        {
            a=par[a][i],b=par[b][i];
        }
    }
    return par[a][0];
}
ll lca_dis(int a,int b)
{
    return d[a]+d[b]-2*d[lca(a,b)];
}
int sz[maxmum],vis[maxmum],par_cen[maxmum];
ll opt[maxmum][maxlift];
int dfs_sz(int u,int p)
{
    sz[u]=1;
    for(int i=0;i<sz_opt[u];i++)
    {
        int node=v[u][i].X;
        if(node==p||vis[node])continue;
        sz[u]+=dfs_sz(node,u);
    }
    return sz[u];
}
int find_cen(int u,int p,int size)
{
    for(int i=0;i<sz_opt[u];i++)
    {
        int node=v[u][i].X;
        if(node==p||vis[node])continue;
        if(sz[node]>size/2)return find_cen(node,u,size);
    }
    return u;
}
void centroid(int u,int p)
{
    dfs_sz(u,u);
    int cen=find_cen(u,u,sz[u]);
    vis[cen]=1;
    par_cen[cen]=p;
    for(int i=0;i<sz_opt[cen];i++)
    {
        ll node=v[cen][i].X;
        if(node==p||vis[node])continue;
        centroid(node,cen);
    }
}
ll mn[maxmum];
void Init(int N, int A[], int B[], int D[]) 
{
    n=N;
    for(int i=0;i<n-1;i++)
    {
        v[A[i]+1].pb({B[i]+1,D[i]});
        v[B[i]+1].pb({A[i]+1,D[i]});
    }
    for(int i=1;i<=n;i++)sz_opt[i]=v[i].size();
    par[1][0]=1;
    dfs(1,1);
    for(int i=1;i<=19;i++)
    {
        for(int j=1;j<=n;j++)
        {
            par[j][i]=par[par[j][i-1]][i-1];
        }
    }
    dfs_sz(1,1);
    centroid(1,0);
    for(int i=1;i<=n;i++)
    {
        ll x=i,kk=0;
        while(1)
        {
            opt[i][kk]=lca_dis(x,i);
            ++kk;
            x=par_cen[x];
            if(x==0)break;
        }
    }
    for(int i=1;i<=n;i++)mn[i]=1e18;
}
void update(int num,int type)
{
    int u=num,kk=0;
    while(1)
    {
        if(type==0)
        {
            mn[u]=min(mn[u],opt[num][kk]);
        }else
        {
            mn[u]=1e18;
        }
        u=par_cen[u];
        ++kk;
        if(u==0)break;
    }
}
long long Query(int S, int X[], int T, int Y[]) 
{
    for(int i=0;i<S;i++)
    {
        int u=X[i]+1,kk=0;
        while(1)
        {
            mn[u]=min(mn[u],opt[X[i]+1][kk]);
            u=par_cen[u];
            ++kk;
            if(u==0)break;
        }
    }
    ll ans=1e18;
    for(int i=0;i<T;i++)
    {
        int u=Y[i]+1,kk=0;
        while(1)
        {
            ans=min(ans,opt[Y[i]+1][kk]+mn[u]);
            u=par_cen[u];
            ++kk;
            if(u==0)break;
        }
    }
    for(int i=0;i<S;i++)
    {
        int u=X[i]+1,kk=0;
        while(1)
        {
            mn[u]=1e18;
            u=par_cen[u];
            ++kk;
            if(u==0)break;
        }
    }
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 12628 KB Output is correct
2 Correct 255 ms 22948 KB Output is correct
3 Correct 277 ms 23092 KB Output is correct
4 Correct 268 ms 22824 KB Output is correct
5 Correct 284 ms 23272 KB Output is correct
6 Correct 188 ms 22756 KB Output is correct
7 Correct 275 ms 22888 KB Output is correct
8 Correct 279 ms 22708 KB Output is correct
9 Correct 288 ms 22884 KB Output is correct
10 Correct 190 ms 22596 KB Output is correct
11 Correct 269 ms 22760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12372 KB Output is correct
2 Correct 2118 ms 208180 KB Output is correct
3 Correct 3835 ms 210436 KB Output is correct
4 Correct 711 ms 208956 KB Output is correct
5 Correct 5378 ms 238932 KB Output is correct
6 Correct 4002 ms 211736 KB Output is correct
7 Correct 887 ms 60164 KB Output is correct
8 Correct 342 ms 60640 KB Output is correct
9 Correct 1108 ms 64416 KB Output is correct
10 Correct 916 ms 61328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 12628 KB Output is correct
2 Correct 255 ms 22948 KB Output is correct
3 Correct 277 ms 23092 KB Output is correct
4 Correct 268 ms 22824 KB Output is correct
5 Correct 284 ms 23272 KB Output is correct
6 Correct 188 ms 22756 KB Output is correct
7 Correct 275 ms 22888 KB Output is correct
8 Correct 279 ms 22708 KB Output is correct
9 Correct 288 ms 22884 KB Output is correct
10 Correct 190 ms 22596 KB Output is correct
11 Correct 269 ms 22760 KB Output is correct
12 Correct 8 ms 12372 KB Output is correct
13 Correct 2118 ms 208180 KB Output is correct
14 Correct 3835 ms 210436 KB Output is correct
15 Correct 711 ms 208956 KB Output is correct
16 Correct 5378 ms 238932 KB Output is correct
17 Correct 4002 ms 211736 KB Output is correct
18 Correct 887 ms 60164 KB Output is correct
19 Correct 342 ms 60640 KB Output is correct
20 Correct 1108 ms 64416 KB Output is correct
21 Correct 916 ms 61328 KB Output is correct
22 Correct 2668 ms 209560 KB Output is correct
23 Correct 2799 ms 211948 KB Output is correct
24 Correct 5012 ms 212172 KB Output is correct
25 Correct 4864 ms 215688 KB Output is correct
26 Correct 4758 ms 212352 KB Output is correct
27 Correct 6194 ms 235612 KB Output is correct
28 Correct 817 ms 212692 KB Output is correct
29 Correct 4800 ms 211976 KB Output is correct
30 Correct 4680 ms 211564 KB Output is correct
31 Correct 4459 ms 212040 KB Output is correct
32 Correct 1085 ms 65280 KB Output is correct
33 Correct 325 ms 60836 KB Output is correct
34 Correct 600 ms 57940 KB Output is correct
35 Correct 589 ms 57800 KB Output is correct
36 Correct 883 ms 58580 KB Output is correct
37 Correct 873 ms 58560 KB Output is correct