답안 #337725

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
337725 2020-12-21T12:43:09 Z Thistle 공장들 (JOI14_factories) C++14
0 / 100
37 ms 15084 KB
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using H = pair<ll, ll>;
using vi = vector<int>;
#define all(a) (a).begin(),(a).end()
#define fs first
#define sc second
#define rng(i,s,n) for(ll i = (s) ; i < (n) ; i++)
#define rep(i,n) rng(i, 0, (n))
#define mkp make_pair
#define vec vector
#define pb emplace_back
#define siz(a) (int)(a).size()
#define crdcomp(b) sort(all((b)));(b).erase(unique(all((b))),(b).end())
#define getidx(b,i) (lower_bound(all(b),(i))-(b).begin())
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

int n;
int spt[1200000][20];
vec<ll> dpto;//オイラーツアー・深さ
vi eul;//オイラーツアー・番号
int out[6000000];
int dpt[6000000];//深さ
vi e[600000];
int lgt[12000000];

int lca(int a,int b){
  int k=lgt[out[b]-out[a]+1];
  int x=spt[out[a]][k],y=spt[out[b]-(1<<k)+1][k];
  if(dpto[x]<dpto[y]) return eul[x];
  else return eul[y];
}
int dist(int a,int b){
  int k=lca(a,b);
  return dpt[a]+dpt[b]-2*dpt[k];
}
void Init(int N, int A[], int B[], int D[]) {
  map<H, ll>mp;
  n=N;
  rep(i,n-1){
    e[A[i]].pb(B[i]);
    e[B[i]].pb(A[i]);
    if(A[i]>B[i]) swap(A[i],B[i]);
    mp[H{A[i],B[i]}]=D[i];
  }
  auto dfs=[&](int x,int p,ll d, auto& dfs)->void{
    dpt[x]=d;
    out[x]=siz(eul);
    eul.pb(x);dpto.pb(d);
    for(int g:e[x]){
      if(g==p) continue;
      dfs(g,x,d+mp[H{min(x,g),max(x,g)}],dfs);
      eul.pb(x);dpto.pb(d);
    }
  };
  dfs(0,-1,0,dfs);

  rep(i,siz(eul)) spt[i][0]=i;
  rng(i,2,siz(eul)) lgt[i]=lgt[i<<1]+1;

  rep(i,19)rep(j,siz(eul)-(1<<(i+1))+1){
    int s=spt[j][i],t=spt[j+(1<<i)][i];
    if(dpto[s]<dpto[t]) spt[j][i+1]=s;
    else spt[j][i+1]=t;
  }
}

long long Query(int S, int X[], int T, int Y[]) {
  vi v(S+T);
  set<int>se,se2;
  rep(i,S) {
    v[i]=X[i];
    se2.insert(X[i]);
  }
  rep(i,T){
    v[i+S]=Y[i];
    se.insert(Y[i]);
  }
  crdcomp(v);
  sort(all(v),[&](int& x,int& y){
    return out[x]<out[y];
  });
  int sz=siz(v);
  rep(i,sz-1) v.pb(lca(v[i],v[i+1]));
  crdcomp(v);
  sort(all(v),[&](int& x,int& y){
    return out[x]<out[y];
  });
  //オイラーツアー順でソートされた、木DPするための頂点列
  //deptがこいつ以下の物がこいつの直接的な親
  //BITを定義して、前から見ていくときに、deptが自分以下のもののうちのindexの最大値を求める
  vec<vec<int>>f(siz(v),vec<int>());
  stack<int>st;
  /*rep(i,siz(v)){
    if(!st.empty()) {
      while(!st.empty()&&dist(v[st.top()],v[i])!=dpt[v[i]]-dpt[v[st.top()]]){
        st.pop();
      }
      if(!st.empty()) f[st.top()].pb(i);
    }
    st.push(i);
  }*/
  ll ans=1e16;
  auto dfs=[&](int x,ll near,auto& dfs)->ll{
    if(se2.find(v[x])!=se2.end()) near=0;
    for(auto g:f[x]){
      chmin(near, dfs(g,near+dist(v[g],v[x]),dfs)+dpt[v[g]]-dpt[v[x]]);
    }
    for(auto g:f[x]){
      dfs(g,near+dpt[v[g]]-dpt[v[x]],dfs);
    }
    if(se.find(v[x])!=se.end()){
      chmin(ans,near);
    }
    return near;
  };
  dfs(0,1e16,dfs);
  return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 37 ms 15084 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 14700 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 37 ms 15084 KB Output isn't correct
2 Halted 0 ms 0 KB -