답안 #292423

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
292423 2020-09-06T23:00:02 Z VROOM_VARUN 공장들 (JOI14_factories) C++14
100 / 100
5956 ms 345592 KB
/*
ID: varunra2
LANG: C++
TASK: factories
*/

#include <bits/stdc++.h>

#include "factories.h"
using namespace std;

#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
  cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif

#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second

const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions
#define MAXN (int)5e5 + 5
#define MAXSEG 20
int n;
int par[MAXN];
bool use[MAXN];
int sub[MAXN];
VII adj[MAXN];
int lca[MAXN][MAXSEG];
ll d[MAXN];
int tin[MAXN];
int tout[MAXN];
int timer = 0;
VI centpar[MAXN];
vector<ll> distpar[MAXN];

int dfs(int u, int v) {
  sub[u] = 1;
  for (auto& x : adj[u]) {
    if (x.x == v or use[x.x]) continue;
    sub[u] += dfs(x.x, u);
  }
  return sub[u];
}

int getCent(int u, int v, int siz) {
  for (auto& x : adj[u]) {
    if (x.x == v or use[x.x]) continue;
    if (sub[x.x] * 2 > siz) {
      return getCent(x.x, u, siz);
    }
  }
  return u;
}

void decompose(int u, int v, int cnt) {
  int cur = getCent(u, -1, dfs(u, -1));
  use[cur] = true;
  par[cur] = v;

  for (auto& x : adj[cur]) {
    if (use[x.x]) continue;
    decompose(x.x, cur, cnt);
  }
}

void getAdj(int a[], int b[], int d[]) {
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    u = a[i];
    v = b[i];
    w = d[i];
    adj[u].PB(MP(v, w));
    adj[v].PB(MP(u, w));
  }
}

void dfsLCA(int u, int v) {
  tin[u] = ++timer;
  lca[u][0] = v;
  for (int i = 1; i < MAXSEG; i++) {
    int to = lca[u][i - 1];
    if (to == -1)
      lca[u][i] = -1;
    else
      lca[u][i] = lca[to][i - 1];
  }
  for (auto& x : adj[u]) {
    if (x.x == v) continue;
    d[x.x] = d[u] + x.y;
    dfsLCA(x.x, u);
  }
  tout[u] = ++timer;
}

bool isAnc(int u, int v) {
  if (u == -1) return true;
  if (v == -1) return false;
  return tin[u] <= tin[v] and tout[u] >= tout[v];
}

int getLCA(int u, int v) {
  if (isAnc(u, v)) return u;
  if (isAnc(v, u)) return v;
  for (int i = MAXSEG - 1; i >= 0; i--) {
    if (!isAnc(lca[u][i], v)) {
      u = lca[u][i];
    }
  }
  return lca[u][0];
}

inline ll getDist(int u, int v) { return d[u] + d[v] - 2 * d[getLCA(u, v)]; }

ll mindist[MAXN];
bool vis[MAXN];

void Init(int N, int a[], int b[], int d[]) {
  // init_siz(N);
  n = N;
  getAdj(a, b, d);
  decompose(0, -1, 0);
  dfsLCA(0, -1);
  for (int i = 0; i < MAXN; i++) {
    mindist[i] = 0;
    vis[i] = false;
  }
  for (int i = 0; i < n; i++) {
    int u = i;
    while (u != -1) {
      centpar[i].PB(u);
      distpar[i].PB(getDist(i, u));
      u = par[u];
    }
  }
}

ll Query(int s, int x[], int t, int y[]) {
  for (int i = 0; i < s; i++) {
    // int u = x[i];
    // while (u != -1) {
    //   if (vis[u]) {
    //     mindist[u] = min(mindist[u], getDist(u, x[i]));
    //   } else {
    //     vis[u] = true;
    //     mindist[u] = getDist(u, x[i]);
    //   }
    //   u = par[u];
    // }
    for (int j = 0; j < centpar[x[i]].size(); j++) {
      int to = centpar[x[i]][j];
      ll todist = distpar[x[i]][j];
      if (vis[to]) {
        mindist[to] = min(mindist[to], todist);
      } else {
        vis[to] = true;
        mindist[to] = todist;
      }
      // mindist[centpar[i][j]] = min(mindist[centpar[i][j]])
    }
  }

  ll ret = (ll)1e17;

  for (int i = 0; i < t; i++) {
    // int u = y[i];
    // while (u != -1) {
    //   if (vis[u]) {
    //     ret = min(ret, mindist[u] + getDist(u, y[i]));
    //   }
    //   u = par[u];
    // }
    for(int j = 0; j < centpar[y[i]].size(); j++) {
      int to = centpar[y[i]][j];
      ll todist = distpar[y[i]][j];
      if(vis[to]) {
        ret = min(ret, mindist[to] + todist);
      }
    }
  }

  for (int i = 0; i < s; i++) {
    int u = x[i];
    while (u != -1) {
      vis[u] = false;
      u = par[u];
    }
  }

  for (int i = 0; i < t; i++) {
    int u = y[i];
    while (u != -1) {
      vis[u] = false;
      u = par[u];
    }
  }

  return ret;
}

// int main() {
// #ifndef ONLINE_JUDGE
//   freopen("factories.in", "r", stdin);
//   freopen("factories.out", "w", stdout);
// #endif
//   cin.sync_with_stdio(0);
//   cin.tie(0);

//   int N, q;
//   cin >> N >> q;

//   int a[N - 1];
//   int b[N - 1];
//   int d[N - 1];

//   for (int i = 0; i < N - 1; i++) {
//     cin >> a[i] >> b[i] >> d[i];
//   }

//   Init(N, a, b, d);

//   for (int i = 0; i < q; i++) {
//     int s, t;
//     cin >> s >> t;
//     int x[s];
//     int y[t];
//     for (int j = 0; j < s; j++) {
//       cin >> x[j];
//     }
//     for (int j = 0; j < t; j++) {
//       cin >> y[j];
//     }

//     cout << Query(s, x, t, y) << '\n';
//   }

//   return 0;
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 40440 KB Output is correct
2 Correct 455 ms 49556 KB Output is correct
3 Correct 496 ms 50040 KB Output is correct
4 Correct 492 ms 49784 KB Output is correct
5 Correct 525 ms 50424 KB Output is correct
6 Correct 345 ms 49144 KB Output is correct
7 Correct 496 ms 49912 KB Output is correct
8 Correct 498 ms 49912 KB Output is correct
9 Correct 519 ms 50168 KB Output is correct
10 Correct 342 ms 49144 KB Output is correct
11 Correct 494 ms 49804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 40192 KB Output is correct
2 Correct 3219 ms 232904 KB Output is correct
3 Correct 5045 ms 284304 KB Output is correct
4 Correct 1188 ms 159060 KB Output is correct
5 Correct 5502 ms 342096 KB Output is correct
6 Correct 5205 ms 285136 KB Output is correct
7 Correct 1632 ms 88824 KB Output is correct
8 Correct 765 ms 72804 KB Output is correct
9 Correct 1786 ms 98296 KB Output is correct
10 Correct 1676 ms 89848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 40440 KB Output is correct
2 Correct 455 ms 49556 KB Output is correct
3 Correct 496 ms 50040 KB Output is correct
4 Correct 492 ms 49784 KB Output is correct
5 Correct 525 ms 50424 KB Output is correct
6 Correct 345 ms 49144 KB Output is correct
7 Correct 496 ms 49912 KB Output is correct
8 Correct 498 ms 49912 KB Output is correct
9 Correct 519 ms 50168 KB Output is correct
10 Correct 342 ms 49144 KB Output is correct
11 Correct 494 ms 49804 KB Output is correct
12 Correct 29 ms 40192 KB Output is correct
13 Correct 3219 ms 232904 KB Output is correct
14 Correct 5045 ms 284304 KB Output is correct
15 Correct 1188 ms 159060 KB Output is correct
16 Correct 5502 ms 342096 KB Output is correct
17 Correct 5205 ms 285136 KB Output is correct
18 Correct 1632 ms 88824 KB Output is correct
19 Correct 765 ms 72804 KB Output is correct
20 Correct 1786 ms 98296 KB Output is correct
21 Correct 1676 ms 89848 KB Output is correct
22 Correct 3701 ms 232980 KB Output is correct
23 Correct 3989 ms 236980 KB Output is correct
24 Correct 5749 ms 285708 KB Output is correct
25 Correct 5731 ms 314076 KB Output is correct
26 Correct 5867 ms 310780 KB Output is correct
27 Correct 5956 ms 345592 KB Output is correct
28 Correct 1471 ms 166848 KB Output is correct
29 Correct 5896 ms 289784 KB Output is correct
30 Correct 5927 ms 289532 KB Output is correct
31 Correct 5903 ms 289996 KB Output is correct
32 Correct 1682 ms 112888 KB Output is correct
33 Correct 760 ms 87080 KB Output is correct
34 Correct 1220 ms 96632 KB Output is correct
35 Correct 1228 ms 97144 KB Output is correct
36 Correct 1571 ms 100604 KB Output is correct
37 Correct 1572 ms 100600 KB Output is correct