답안 #881713

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
881713 2023-12-01T18:51:29 Z Tob 공장들 (JOI14_factories) C++14
0 / 100
31 ms 51804 KB
#include "factories.h"
#include <bits/stdc++.h>

#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair <ll, ll> pii;

const int N = 5e5 + 7;
const ll inf = 1e18;

int n, tim;
int par[N][21], dub[N], st[N], en[N];
ll ps[N], dow[N], up[N];
bool good[N];
vector <pii> adj[N], gadj[N];

int lca(int x, int y) {
  if (dub[x] < dub[y]) swap(x, y);
  for (int i = 20; i >= 0; i--) {
    int z = par[x][i];
    if (dub[z] >= dub[y]) x = z;
  }
  if (x == y) return x;
  for (int i = 20; i >= 0; i--) {
    int z = par[x][i], w = par[y][i];
    if (z != w) x = z, y = w;
  }
  return par[x][0];
}

void dfs(int x = 1, int p = 0) {
  dub[x] = dub[p]+1;
  par[x][0] = p;
  st[x] = ++tim;
  for (auto it : adj[x]) {
    if (it.F == p) continue;
    ps[it.F] = ps[x] + it.S;
    dfs(it.F, x);
  }
  en[x] = tim;
}

void DP(int x, bool rev) {
  if (!rev) {
    dow[x] = inf;
    if (good[x]) dow[x] = 0;
    for (auto y : gadj[x]) {
      DP(y.F, 0);
      dow[x] = min(dow[x], dow[y.F] + y.S);
    }
  }
  else {
    if (good[x]) up[x] = 0;
    int siz = gadj[x].size();
    vector <ll> pr(siz+2, inf), su(siz+2, inf);
    for (int i = 0; i < siz; i++) {
      pii y = gadj[x][i];
      pr[i+1] = min(pr[i], dow[y.F] + y.S);
    }
    for (int i = siz-1; i >= 0; i--) {
      pii y = gadj[x][i];
      su[i+1] = min(su[i+2], dow[y.F] + y.S);
    }
    for (int i = 0; i < siz; i++) {
      pii y = gadj[x][i];
      up[y.F] = (min(up[x], min(pr[i], su[i+2])) + y.S);
      DP(y.F, 1);
    }
  }
}

void Init(int nn, int A[], int B[], int D[]) {
  n = nn;
  up[1] = inf;
  for (int i = 0; i < n-1; i++) {
    A[i]++; B[i]++;
    adj[A[i]].pb({B[i], D[i]});
    adj[B[i]].pb({A[i], D[i]});
  }
  dfs();
  for (int k = 1; k <= 20; k++) {
    for (int i = 1; i <= n; i++) {
      par[i][k] = par[par[i][k-1]][k-1];
    }
  }
}

bool cmp(int x, int y) {
  return st[x] < st[y];
}

ll Query(int S, int X[], int T, int Y[]) {
  vector <int> v;
  for (int i = 0; i < S; i++) v.pb(++X[i]);
  for (int i = 0; i < T; i++) v.pb(++Y[i]);
  sort(all(v), cmp);
  for (int i = 1; i < S+T; i++) v.pb(lca(v[i-1], v[i]));
  sort(all(v), cmp);
  vector <int> v2;
  v2.pb(v[0]);
  for (int i = 1; i < v.size(); i++) if (v[i-1] != v[i]) {
    int x = v[i];
    while (en[v2.back()] < st[x]) v2.pop_back();
    int y = v2.back();
    gadj[y].pb({x, ps[x]-ps[y]});
    v2.pb(x);
  }
  for (int i = 0; i < S; i++) good[X[i]] = 1;
  DP(v[0], 0);
  DP(v[0], 1);
  ll mn = inf;
  for (int i = 0; i < T; i++) mn = min(mn, min(dow[Y[i]], up[Y[i]]));
  for (auto x : v) {
    gadj[x].clear();
    up[x] = dow[x] = 0;
    good[x] = 0;
  }
  return mn;
}

Compilation message

factories.cpp: In function 'll Query(int, int*, int, int*)':
factories.cpp:107:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |   for (int i = 1; i < v.size(); i++) if (v[i-1] != v[i]) {
      |                   ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 31 ms 51804 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 51804 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 31 ms 51804 KB Output isn't correct
2 Halted 0 ms 0 KB -