답안 #1081840

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1081840 2024-08-30T11:52:50 Z Malix 공장들 (JOI14_factories) C++14
100 / 100
2635 ms 296544 KB
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,ll> pi;
typedef vector<pi> pii;
typedef tuple<int,int,int> tii;
typedef vector<ll> li;
typedef vector<li> lii;
 
#define REP(i,a,b) for(int i=a;i<b;i++)
#define F first
#define S second
#define PB push_back
#define LSOne(s) ((s)&(-s))
 
ll INF=1000000000000000010;
int inf=1e9+10;
ll M=1e9+7;

int n,stv=0,env=0,k;
vector<pii> a;
vi st,en,sz,vis,par;
vector<ll> dist,d;
vii p,b;
vector<vector<ll>> dst;

void dfs(int x){
  st[x]=stv++;
  for(auto u:a[x])if(p[x][0]!=u.F){
    p[u.F][0]=x;
    dist[u.F]=dist[x]+u.S;
    dfs(u.F);
    sz[x]+=sz[u.F];
  }
  en[x]=env++;
}

bool ances(int x,int y){
  if(y==-1)return 1;
  if(st[x]>=st[y]&&en[x]<=en[y])return 1;
  return 0;
}

ll distance(int x,int y){
  if(ances(x,y)||ances(y,x))return abs(dist[x]-dist[y]);
  int z=y;
  for(int i=k-1;i>=0;i--)if(!ances(x,p[y][i]))y=p[y][i];
  y=p[y][0];
  return dist[x]+dist[z]-2*dist[y];
}

int centroid(int x,int y){
  for(auto u:a[x])if(vis[u.F]==0&&sz[u.F]>y/2){
    sz[x]-=sz[u.F];
    sz[u.F]=y;
    return centroid(u.F,y);
  }
  return x;
}

void decompose(int x){
  for(auto u:a[x])if(!vis[u.F]){
    int y=centroid(u.F,sz[u.F]);
    b[x].PB(y);
    vis[y]=1;
    par[y]=x;
    decompose(y);
  }
}

void Init(int N, int A[], int B[], int D[]) {
  n=N;
  k=log2(n)+1;
  a.resize(n);p.resize(n,vi(k,-1));sz.resize(n,1);dist.resize(n,0);
  REP(i,0,n-1){
    a[A[i]].PB({B[i],(ll)D[i]});
    a[B[i]].PB({A[i],(ll)D[i]});
  }
  st.resize(n);en.resize(n);
  dfs(0);
  REP(j,1,k)REP(i,0,n)if(p[i][j-1]!=-1)p[i][j]=p[p[i][j-1]][j-1];
  vis.resize(n,0);b.resize(n);par.resize(n,-1);
  int x=centroid(0,n);
  vis[x]=1;
  decompose(x);
  dst.resize(n,vector<ll>(k));d.resize(n,INF);
  REP(i,0,n){
    int r=i;
    int ps=0;
    while(r!=-1){
      dst[i][ps]=distance(i,r);
      r=par[r];
      ps++;
    }
  }
}

long long Query(int s, int X[], int t, int Y[]) {
  ll ans=INF;
  REP(i,0,s){
    int r=X[i];
    while(r!=-1){
      d[r]=INF;
      r=par[r];
    }
  }
  REP(i,0,t){
    int r=Y[i];
    while(r!=-1){
      d[r]=INF;
      r=par[r];
    }
  }
  REP(i,0,s){
    int r=X[i];
    int ps=0;
    while(r!=-1){
      d[r]=min(d[r],dst[X[i]][ps]);
      r=par[r];ps++;
    }
  }
  REP(i,0,t){
    int r=Y[i];
    int ps=0;
    while(r!=-1){
      if(d[r]!=INF)ans=min(ans,d[r]+dst[Y[i]][ps]);
      r=par[r];ps++;
    }
  }
  return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 856 KB Output is correct
2 Correct 208 ms 19544 KB Output is correct
3 Correct 239 ms 19688 KB Output is correct
4 Correct 240 ms 19580 KB Output is correct
5 Correct 276 ms 20052 KB Output is correct
6 Correct 146 ms 19540 KB Output is correct
7 Correct 231 ms 19796 KB Output is correct
8 Correct 244 ms 19796 KB Output is correct
9 Correct 256 ms 20148 KB Output is correct
10 Correct 131 ms 19536 KB Output is correct
11 Correct 238 ms 19732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1063 ms 254600 KB Output is correct
3 Correct 2129 ms 257556 KB Output is correct
4 Correct 639 ms 246636 KB Output is correct
5 Correct 1572 ms 295248 KB Output is correct
6 Correct 2122 ms 259832 KB Output is correct
7 Correct 632 ms 67292 KB Output is correct
8 Correct 235 ms 65780 KB Output is correct
9 Correct 581 ms 73000 KB Output is correct
10 Correct 640 ms 68440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 856 KB Output is correct
2 Correct 208 ms 19544 KB Output is correct
3 Correct 239 ms 19688 KB Output is correct
4 Correct 240 ms 19580 KB Output is correct
5 Correct 276 ms 20052 KB Output is correct
6 Correct 146 ms 19540 KB Output is correct
7 Correct 231 ms 19796 KB Output is correct
8 Correct 244 ms 19796 KB Output is correct
9 Correct 256 ms 20148 KB Output is correct
10 Correct 131 ms 19536 KB Output is correct
11 Correct 238 ms 19732 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1063 ms 254600 KB Output is correct
14 Correct 2129 ms 257556 KB Output is correct
15 Correct 639 ms 246636 KB Output is correct
16 Correct 1572 ms 295248 KB Output is correct
17 Correct 2122 ms 259832 KB Output is correct
18 Correct 632 ms 67292 KB Output is correct
19 Correct 235 ms 65780 KB Output is correct
20 Correct 581 ms 73000 KB Output is correct
21 Correct 640 ms 68440 KB Output is correct
22 Correct 1309 ms 261480 KB Output is correct
23 Correct 1484 ms 264092 KB Output is correct
24 Correct 2484 ms 265344 KB Output is correct
25 Correct 2457 ms 269572 KB Output is correct
26 Correct 2527 ms 265644 KB Output is correct
27 Correct 2228 ms 296544 KB Output is correct
28 Correct 663 ms 256884 KB Output is correct
29 Correct 2635 ms 265188 KB Output is correct
30 Correct 2516 ms 264528 KB Output is correct
31 Correct 2576 ms 265388 KB Output is correct
32 Correct 689 ms 73908 KB Output is correct
33 Correct 242 ms 66164 KB Output is correct
34 Correct 447 ms 64336 KB Output is correct
35 Correct 454 ms 64336 KB Output is correct
36 Correct 665 ms 65364 KB Output is correct
37 Correct 636 ms 65180 KB Output is correct