답안 #39970

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
39970 2018-01-25T05:15:39 Z funcsr 공장들 (JOI14_factories) C++14
0 / 100
5653 ms 524288 KB
#include "factories.h"
#include <iostream>
#include <queue>
#include <cassert>
#include <set>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
typedef pair<long long, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define index(x, y) (int)(lower_bound(all(x), y) - x.begin())
#define pb push_back
#define _1 first
#define _2 second
#define INF (1LL<<60)

int N;
vector<P> G[500000];
int U[19][500000];
int R[500000];
long long W[500000];
int IN[500000], OUT[500000];
vector<int> T;
void dfs(int x, int p, int r, long long w) {
  IN[x] = T.size();
  T.pb(x);
  R[x] = r, W[x] = w;
  U[0][x] = p;
  for (P pp : G[x]) if (pp._1 != p) dfs(pp._1, x, r+1, w+pp._2);
  OUT[x] = (int)T.size()-1;
}

int lca(int x, int y) {
  if (R[x] < R[y]) swap(x, y);
  for (int k=18; k>=0; k--) if ((R[x]-R[y])>>k) x = U[k][x];
  if (x == y) return x;
  for (int k=18; k>=0; k--) if (U[k][x] != U[k][y]) x = U[k][x], y = U[k][y];
  return U[0][x];
}
inline long long dist(int x, int y) {
  int g = lca(x, y);
  return W[x]+W[y]-2LL*W[g];
}

void Init(int NN, int A[], int B[], int D[]) {
  N = NN;
  rep(i, N-1) {
    G[A[i]].pb(P(B[i], D[i]));
    G[B[i]].pb(P(A[i], D[i]));
  }
  dfs(0, -1, 0, 0);
  rep(k, 18) rep(x, N) {
    if (U[k][x] == -1) U[k+1][x] = -1;
    U[k+1][x] = U[k][U[k][x]];
  }
}

long long D[500000];
bool isT[500000];
vector<P> G2[500000];

long long Query(int S, int X[], int T, int Y[]) {
  //cout<<"query {"; rep(i, S) cout<<X[i]<<",";cout<<"}, {";rep(i, T) cout<<Y[i]<<",";cout<<"}\n";
  vector<P> ps;
  rep(i, S) ps.pb(P(IN[X[i]], X[i]));
  rep(i, T) ps.pb(P(IN[Y[i]], Y[i]));
  sort(all(ps)); uniq(ps);
  vector<P> new_ps(ps);
  rep(i, (int)ps.size()-1) {
    int v = lca(ps[i]._2, ps[i+1]._2);
    new_ps.pb(P(IN[v], v));
  }
  swap(new_ps, ps);
  sort(all(ps)); uniq(ps);
  //cout<<"{"; for (P p : ps) cout<<p._2<<",";cout<<"}\n";
  stack<int> st;
  for (P p : ps) {
    int v = p._2;
    D[v] = INF;
    while (!st.empty() && !(IN[st.top()] <= IN[v] && IN[v] <= OUT[st.top()])) st.pop();
    if (!st.empty()) {
      int p = st.top();
      //cout<<p<<"<->"<<v<<", w="<<W[v]-W[p]<<"\n";
      G2[p].pb(P(v, W[v]-W[p]));
      G2[v].pb(P(p, W[v]-W[p]));
    }
    st.push(v);
  }

  rep(i, T) isT[Y[i]] = true;
  priority_queue<P, vector<P>, greater<P> > q;
  rep(i, S) q.push(P(0, X[i])), D[X[i]] = 0;
  long long ans = INF;
  while (!q.empty()) {
    long long r = q.top()._1;
    int x = q.top()._2;
    q.pop();
    if (D[x] < r) continue;
    if (isT[x]) {
      ans = r;
      break;
    }
    for (P p : G2[x]) {
      int t = p._1, len = p._2;
      if (D[t] > r+len) {
        D[t] = r+len;
        q.push(P(D[t], t));
      }
    }
  }
  rep(i, T) isT[Y[i]] = false;
  for (P p : ps) G2[p._2].clear();
  while (!q.empty()) q.pop();
  return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 99136 KB Output is correct
2 Correct 1465 ms 99860 KB Output is correct
3 Correct 1517 ms 99648 KB Output is correct
4 Memory limit exceeded 3753 ms 524288 KB Memory limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 99136 KB Output is correct
2 Correct 3098 ms 141836 KB Output is correct
3 Memory limit exceeded 2461 ms 524288 KB Memory limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5418 ms 155272 KB Output is correct
2 Correct 5653 ms 152984 KB Output is correct
3 Memory limit exceeded 2983 ms 524288 KB Memory limit exceeded
4 Halted 0 ms 0 KB -