답안 #566710

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
566710 2022-05-22T17:43:17 Z SSRS 공장들 (JOI14_factories) C++14
100 / 100
3092 ms 115148 KB
#include <bits/stdc++.h>
#include "factories.h"
using namespace std;
const long long INF = 1000000000000000;
struct heavy_light_decomposition{
  vector<int> p, sz, in, next;
  vector<long long> d;
  heavy_light_decomposition(){
  }
  heavy_light_decomposition(vector<int> &p, vector<vector<int>> &c, vector<long long> &d): p(p), d(d){
    int N = p.size();
    sz = vector<int>(N, 1);
    dfs1(c);
    in = vector<int>(N);
    next = vector<int>(N, 0);
    int t = 0;
    dfs2(c, t);
  }
  void dfs1(vector<vector<int>> &c, int v = 0){
    for (int &w : c[v]){
      dfs1(c, w);
      sz[v] += sz[w];
      if (sz[w] > sz[c[v][0]]){
        swap(w, c[v][0]);
      }
    }
  }
  void dfs2(vector<vector<int>> &c, int &t, int v = 0){
    in[v] = t;
    t++;
    for (int w : c[v]){
      if (w == c[v][0]){
        next[w] = next[v];
      } else {
        next[w] = w;
      }
      dfs2(c, t, w);
    }
  }
  int lca(int u, int v){
    while (true){
      if (in[u] > in[v]){
        swap(u, v);
      }
      if (next[u] == next[v]){
        return u;
      }
      v = p[next[v]];
    }
  }
  long long dist(int u, int v){
    return d[u] + d[v] - 2 * d[lca(u, v)];
  }
};
heavy_light_decomposition HLD;
vector<vector<pair<long long, int>>> E2;
vector<long long> d2;
void Init(int N, int A[], int B[], int D[]){
  vector<vector<pair<int, int>>> E(N);
  for (int i = 0; i < N - 1; i++){
    E[A[i]].push_back(make_pair(D[i], B[i]));
    E[B[i]].push_back(make_pair(D[i], A[i]));
  }
  vector<int> p(N, -1);
  vector<vector<int>> c(N);
  vector<long long> d(N, 0);
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    for (auto P : E[v]){
      int w = P.second;
      if (w != p[v]){
        p[w] = v;
        c[v].push_back(w);
        d[w] = d[v] + P.first;
        Q.push(w);
      }
    }
  }
  HLD = heavy_light_decomposition(p, c, d);
  E2.resize(N);
  d2 = vector<long long>(N, -1);
}
long long Query(int S, int X[], int T, int Y[]){
  vector<pair<int, int>> V;
  for (int i = 0; i < S; i++){
    V.push_back(make_pair(HLD.in[X[i]], X[i]));
  }
  for (int i = 0; i < T; i++){
    V.push_back(make_pair(HLD.in[Y[i]], Y[i]));
  }
  sort(V.begin(), V.end());
  stack<int> st;
  st.push(V[0].second);
  for (int i = 1; i < S + T; i++){
    int v = HLD.lca(V[i - 1].second, V[i].second);
    while (!st.empty()){
      int w = st.top();
      if (HLD.d[v] < HLD.d[w]){
        st.pop();
        int pr = v;
        if (!st.empty()){
          if (HLD.d[pr] < HLD.d[st.top()]){
            pr = st.top();
          }
        }
        if (pr != w){
          long long D = HLD.d[w] - HLD.d[pr];
          E2[pr].push_back(make_pair(D, w));
          E2[w].push_back(make_pair(D, pr));
        }
      } else {
        break;
      }
    }
    if (st.empty()){
      st.push(v);
    } else if (st.top() != v){
      st.push(v);
    }
    if (st.top() != V[i].second){
      st.push(V[i].second);
    }
  }
  while (st.size() >= 2){
    int v = st.top();
    st.pop();
    int w = st.top();
    long long D = HLD.d[v] - HLD.d[w];
    E2[v].push_back(make_pair(D, w));
    E2[w].push_back(make_pair(D, v));
  }
  priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
  for (int i = 0; i < S; i++){
    pq.push(make_pair(0, X[i]));
  }
  vector<int> s;
  while (!pq.empty()){
    long long c = pq.top().first;
    int v = pq.top().second;
    pq.pop();
    if (d2[v] == -1){
      d2[v] = c;
      s.push_back(v);
      for (auto P : E2[v]){
        int w = P.second;
        if (d2[w] == -1){
          pq.push(make_pair(c + P.first, w));
        }
      }
    }
  }
  long long ans = INF;
  for (int i = 0; i < T; i++){
    ans = min(ans, d2[Y[i]]);
  }
  int cnt = s.size();
  for (int i = 0; i < cnt; i++){
    E2[s[i]].clear();
    d2[s[i]] = -1;
  }
  return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 596 KB Output is correct
2 Correct 783 ms 9144 KB Output is correct
3 Correct 816 ms 9320 KB Output is correct
4 Correct 761 ms 9308 KB Output is correct
5 Correct 686 ms 9324 KB Output is correct
6 Correct 681 ms 9180 KB Output is correct
7 Correct 810 ms 9164 KB Output is correct
8 Correct 768 ms 9172 KB Output is correct
9 Correct 713 ms 9328 KB Output is correct
10 Correct 701 ms 9164 KB Output is correct
11 Correct 840 ms 9192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 468 KB Output is correct
2 Correct 1630 ms 94848 KB Output is correct
3 Correct 1544 ms 97440 KB Output is correct
4 Correct 1108 ms 91672 KB Output is correct
5 Correct 1452 ms 115092 KB Output is correct
6 Correct 1608 ms 97976 KB Output is correct
7 Correct 1048 ms 27116 KB Output is correct
8 Correct 754 ms 26332 KB Output is correct
9 Correct 786 ms 29544 KB Output is correct
10 Correct 1045 ms 27328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 596 KB Output is correct
2 Correct 783 ms 9144 KB Output is correct
3 Correct 816 ms 9320 KB Output is correct
4 Correct 761 ms 9308 KB Output is correct
5 Correct 686 ms 9324 KB Output is correct
6 Correct 681 ms 9180 KB Output is correct
7 Correct 810 ms 9164 KB Output is correct
8 Correct 768 ms 9172 KB Output is correct
9 Correct 713 ms 9328 KB Output is correct
10 Correct 701 ms 9164 KB Output is correct
11 Correct 840 ms 9192 KB Output is correct
12 Correct 4 ms 468 KB Output is correct
13 Correct 1630 ms 94848 KB Output is correct
14 Correct 1544 ms 97440 KB Output is correct
15 Correct 1108 ms 91672 KB Output is correct
16 Correct 1452 ms 115092 KB Output is correct
17 Correct 1608 ms 97976 KB Output is correct
18 Correct 1048 ms 27116 KB Output is correct
19 Correct 754 ms 26332 KB Output is correct
20 Correct 786 ms 29544 KB Output is correct
21 Correct 1045 ms 27328 KB Output is correct
22 Correct 2959 ms 97612 KB Output is correct
23 Correct 2801 ms 98304 KB Output is correct
24 Correct 3092 ms 100340 KB Output is correct
25 Correct 2907 ms 101428 KB Output is correct
26 Correct 2821 ms 100692 KB Output is correct
27 Correct 2374 ms 115148 KB Output is correct
28 Correct 1935 ms 95140 KB Output is correct
29 Correct 2689 ms 100420 KB Output is correct
30 Correct 2752 ms 100204 KB Output is correct
31 Correct 2719 ms 100392 KB Output is correct
32 Correct 1135 ms 29860 KB Output is correct
33 Correct 1065 ms 27716 KB Output is correct
34 Correct 1311 ms 26060 KB Output is correct
35 Correct 1244 ms 26060 KB Output is correct
36 Correct 1285 ms 26684 KB Output is correct
37 Correct 1297 ms 26596 KB Output is correct