Submission #881765

# Submission time Handle Problem Language Result Execution time Memory
881765 2023-12-01T21:00:53 Z Tob Factories (JOI14_factories) C++14
100 / 100
4207 ms 195876 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;
  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[]) {
  up[1] = inf;
  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]));
  v.pb(1);
  sort(all(v), cmp);
  vector <int> v2;
  v2.pb(1);
  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(1, 0);
  DP(1, 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:108:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |   for (int i = 1; i < v.size(); i++) if (v[i-1] != v[i]) {
      |                   ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 32 ms 52068 KB Output is correct
2 Correct 1083 ms 65976 KB Output is correct
3 Correct 1197 ms 66080 KB Output is correct
4 Correct 1115 ms 66088 KB Output is correct
5 Correct 1022 ms 66448 KB Output is correct
6 Correct 809 ms 65952 KB Output is correct
7 Correct 1177 ms 66220 KB Output is correct
8 Correct 1116 ms 66132 KB Output is correct
9 Correct 1024 ms 66640 KB Output is correct
10 Correct 810 ms 65944 KB Output is correct
11 Correct 1155 ms 65976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 51804 KB Output is correct
2 Correct 1493 ms 150896 KB Output is correct
3 Correct 2156 ms 155656 KB Output is correct
4 Correct 1016 ms 147928 KB Output is correct
5 Correct 2199 ms 189884 KB Output is correct
6 Correct 2239 ms 156468 KB Output is correct
7 Correct 1564 ms 86036 KB Output is correct
8 Correct 937 ms 84868 KB Output is correct
9 Correct 1658 ms 91680 KB Output is correct
10 Correct 1706 ms 86964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 52068 KB Output is correct
2 Correct 1083 ms 65976 KB Output is correct
3 Correct 1197 ms 66080 KB Output is correct
4 Correct 1115 ms 66088 KB Output is correct
5 Correct 1022 ms 66448 KB Output is correct
6 Correct 809 ms 65952 KB Output is correct
7 Correct 1177 ms 66220 KB Output is correct
8 Correct 1116 ms 66132 KB Output is correct
9 Correct 1024 ms 66640 KB Output is correct
10 Correct 810 ms 65944 KB Output is correct
11 Correct 1155 ms 65976 KB Output is correct
12 Correct 12 ms 51804 KB Output is correct
13 Correct 1493 ms 150896 KB Output is correct
14 Correct 2156 ms 155656 KB Output is correct
15 Correct 1016 ms 147928 KB Output is correct
16 Correct 2199 ms 189884 KB Output is correct
17 Correct 2239 ms 156468 KB Output is correct
18 Correct 1564 ms 86036 KB Output is correct
19 Correct 937 ms 84868 KB Output is correct
20 Correct 1658 ms 91680 KB Output is correct
21 Correct 1706 ms 86964 KB Output is correct
22 Correct 3097 ms 164768 KB Output is correct
23 Correct 2762 ms 164916 KB Output is correct
24 Correct 3614 ms 170920 KB Output is correct
25 Correct 3668 ms 173052 KB Output is correct
26 Correct 4014 ms 162900 KB Output is correct
27 Correct 3475 ms 195876 KB Output is correct
28 Correct 1817 ms 159152 KB Output is correct
29 Correct 4082 ms 161728 KB Output is correct
30 Correct 4021 ms 160972 KB Output is correct
31 Correct 4207 ms 161468 KB Output is correct
32 Correct 1707 ms 98120 KB Output is correct
33 Correct 1181 ms 88832 KB Output is correct
34 Correct 1614 ms 85840 KB Output is correct
35 Correct 1577 ms 85388 KB Output is correct
36 Correct 1824 ms 86984 KB Output is correct
37 Correct 1877 ms 86400 KB Output is correct