Submission #39965

# Submission time Handle Problem Language Result Execution time Memory
39965 2018-01-25T04:33:41 Z funcsr Factories (JOI14_factories) C++14
15 / 100
6000 ms 112712 KB
#pragma GCC optimize ("-O3")
#include "factories.h"
#include <iostream>
#include <queue>
#include <cassert>
#include <set>
#include <vector>
#include <algorithm>
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];
void dfs(int x, int p, int r, long long w) {
  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);
}

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];
}
long long dist(int x, int y) {
  int g = lca(x, y);
  return W[x]+W[y]-2*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];
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";
  long long m = INF;
  if (S <= 10 && T <= 10) {
    rep(i, S) rep(j, T) {
      int x = X[i], y = Y[j];
      m = min(m, dist(x, y));
    }
  }
  else {
    rep(i, N) D[i] = INF;
    priority_queue<P, vector<P>, greater<P> > q;
    rep(i, S) q.push(P(0, X[i])), D[X[i]] = 0;
    while (!q.empty()) {
      long long r = q.top()._1;
      int x = q.top()._2;
      q.pop();
      if (D[x] < r) continue;
      for (P p : G[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) m = min(m, D[Y[i]]);
  }
  return m;
}
# Verdict Execution time Memory Grader output
1 Correct 41 ms 82888 KB Output is correct
2 Correct 1377 ms 83284 KB Output is correct
3 Correct 3928 ms 83152 KB Output is correct
4 Correct 2478 ms 83492 KB Output is correct
5 Correct 1269 ms 83356 KB Output is correct
6 Correct 1260 ms 83512 KB Output is correct
7 Correct 3939 ms 83152 KB Output is correct
8 Correct 3099 ms 83152 KB Output is correct
9 Correct 1228 ms 83348 KB Output is correct
10 Correct 1273 ms 83512 KB Output is correct
11 Correct 3779 ms 83152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 82888 KB Output is correct
2 Execution timed out 6000 ms 108496 KB Execution timed out
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6000 ms 112712 KB Execution timed out
2 Halted 0 ms 0 KB -