Submission #916088

# Submission time Handle Problem Language Result Execution time Memory
916088 2024-01-25T09:23:10 Z duckindog Swapping Cities (APIO20_swap) C++14
67 / 100
2000 ms 60616 KB
#include<bits/stdc++.h>

using namespace std;

//#define LOCAL

#ifndef LOCAL
#include "swap.h"
#endif // LOCAL

const int N = 1e5 + 10;

int n, m;
vector<pair<int, int>> ad[N];

//tree
int f[N][17], g[N][17], dp[N][17], st[N], ed[N], it;
int dpin[N], dpout[N];
void dfs(int u, int pre = -1) {
  st[u] = ++it;

  for (int i = 1; i <= 16; ++i) {
    f[u][i] = f[f[u][i - 1]][i - 1];
    g[u][i] = max(g[u][i - 1], g[f[u][i - 1]][i - 1]);
    dp[u][i] = min(dp[u][i - 1], dp[f[u][i - 1]][i - 1]);
  }

  vector<pair<int, int>> best;
  sort(ad[u].begin(), ad[u].end(), [&] (auto& a, auto& b) {
    return a.second < b.second;
  });

  for (auto duck : ad[u]) {
    int v, w; tie(v, w) = duck;
    if (v == pre) continue;
    best.push_back({w, v});
  }

  sort(best.begin(), best.end());
  dpin[u] = 1e9 + 1;
  for (auto duck : ad[u]) {
    int v, w; tie(v, w) = duck;
    if (v == pre) continue;

    f[v][0] = u;
    g[v][0] = w;

    if (best.size() >= 2) {
      dp[v][0] = (best[0].second != v ? best[0].first : best[1].first);
    } else dp[v][0] = 1e9 + 1;

    dfs(v, u);
    dpin[u] = min(dpin[u], max(w, dpin[v]));
  }

  if (best.size() >= 2) {
    dpin[u] = min(dpin[u], best[1].first);
  }

  ed[u] = it;
}
void dfs_out(int u, int pre = 0) {

  vector<pair<int, int>> best, bestmax;
  for (auto duck : ad[u]) {
    int v, w; tie(v, w) = duck;
    best.push_back({w, v});
    if (v == pre) continue;
    bestmax.push_back({max(w, dpin[v]), v});
  }
  sort(best.begin(), best.end());
  sort(bestmax.begin(), bestmax.end());

  for (auto duck : ad[u]) {
    int v, w; tie(v, w) = duck;
    if (v == pre) continue;
    dpout[v] = dpout[u];

    if (bestmax.size() >= 2) {
      dpout[v] = min(dpout[v], bestmax[0].second != v ? bestmax[0].first : bestmax[1].first);
    }

    if (best.size() >= 3) {
      if (best[0].second != v && best[1].second != v) dpout[v] = min(dpout[v], best[1].first);
      else dpout[v] = min(dpout[v], best[2].first);

    }
    dpout[v] = max(w, dpout[v]);
    dfs_out(v, u);
  }

}

bool anc(int u, int v) {
  return st[u] <= st[v] && ed[u] >= ed[v];
}
int lca(int u, int v) {
  if (anc(u, v)) return u;
  if (anc(v, u)) return v;

  for (int i = 16; i >= 0; --i)
    if (!anc(f[u][i], v)) u = f[u][i];
  return f[u][0];
}
int wmax(int u, int v) {
  int ret = 0, l = lca(u, v);

  for (int i = 16; i >= 0; --i) {
    if (!anc(f[u][i], v) || f[u][i] == l) {
      ret = max(ret, g[u][i]);
      u = f[u][i];
    }
  }
  for (int i = 16; i >= 0; --i) {
    if (!anc(f[v][i], u) || f[v][i] == l) {
      ret = max(ret, g[v][i]);
      v = f[v][i];
    }
  }

  return ret;
}
int binary_jump(int u, int v) {
  int ret = 1e9 + 1;

  for (int i = 16; i >= 0; --i) if (!anc(f[u][i], v)) {
    ret = min(ret, dp[u][i]);
    u = f[u][i];
  }
  for (int i = 16; i >= 0; --i) if (!anc(f[v][i], u)) {
    ret = min(ret, dp[v][i]);
    v = f[v][i];
  }

  return ret;
}

vector<int> co;
bool sub1;

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
  n = N; m = M;
  sub1 = 1;
  for (int i = 0; i < m; ++i) {
    int u = U[i], v = V[i], w = W[i];
    co.push_back(w);
    ad[u].push_back({v, w});
    ad[v].push_back({u, w});
    if (ad[u].size() > 2 || ad[v].size() > 2) sub1 = 0;
  }
  if (m == n - 1) {
    dfs(0);
    dpout[0] = 1e9 + 1;
    dfs_out(0);
  }


  sort(co.begin(), co.end());
  co.resize(unique(co.begin(), co.end()) - co.begin());

}

bool dd[N];
int special;
bool dfs1(int u, int pre, int cw) {
  if (u == special) return 1;
  bool ret = 0;
  for (auto duck : ad[u]) {
    int v, w; tie(v, w) = duck;
    if (v == pre || w > cw) continue;
    if (!dd[v]) {
      dd[v] = 1;
      ret |= dfs1(v, u, cw);
    }
  }
  return ret;
}

bool dfs2(int u, int pre, int cw) {
  int cnt = pre != -1;
  bool ret = 0;
  map<int, int> d;
  for (auto duck : ad[u]) {
    int v, w; tie(v, w) = duck;
    if (v == pre || w > cw) continue;
    if (dd[v] && !d[v]) return 1;
    if (!dd[v]) {
      dd[v] = d[v] = 1;
      cnt += 1;
      ret |= dfs2(v, u, cw);
    }
  }

  return (cnt > 2 || ret);
}

int getMinimumFuelCapacity(int X, int Y) {

  if (m == n - 1) {
    if (sub1) return -1;
    int answer = 1e9 + 1;
    int l = lca(X, Y);
    if (anc(Y, X)) swap(X, Y);
    if (!anc(X, Y)) answer = min(answer, dpin[X]);
    answer = min({answer, dpin[Y], binary_jump(X, Y)});


    int maxw = wmax(X, Y);
    if (X != l && Y != l) {

      if (ad[l].size() >= 3) {
        for (int i = 16; i >= 0; --i) if (!anc(f[X][i], Y)) X = f[X][i];
        for (int i = 16; i >= 0; --i) if (!anc(f[Y][i], X)) Y = f[Y][i];
        int it = 0;
        while (ad[l][it].first == X || ad[l][it].first == Y) it += 1;
        answer = min(answer, ad[l][it].second);
      }

    } else {

      answer = min({answer, dpout[Y]});
    }

    answer = max(answer, maxw);

    return answer;
  }


  int l = 0, r = co.size() - 1;
  int answer = -1;
  special = Y;
  while (l <= r) {
    int mid = l + r >> 1;

    bool ret = 1;
    memset(dd, 0, sizeof dd); ret &= dfs1(X, -1, co[mid]);
    memset(dd, 0, sizeof dd); ret &= dfs2(X, -1, co[mid]);

    if (ret) r = mid - 1, answer = co[mid];
    else l = mid + 1;
  }

  return answer;
}

#ifdef LOCAL
int32_t main() {
  cin.tie(0)->sync_with_stdio(0);

  if (fopen("duck.inp", "r")) {
    freopen("duck.inp", "r", stdin);
    freopen("duck.out", "w", stdout);
  }

  int n, m; cin >> n >> m;
  vector<int> U(m), V(m), W(m);
  for (int i = 0; i < m; ++i) cin >> U[i] >> V[i] >> W[i];
  init(n, m, U, V, W);

  int q; cin >> q;
  for (int i = 1; i <= q; ++i) {
    int X, Y; cin >> X >> Y;
    cout << getMinimumFuelCapacity(X, Y) << '\n';
  }


}
#endif // LOCAL

Compilation message

swap.cpp: In function 'bool dfs2(int, int, int)':
swap.cpp:188:20: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  188 |       dd[v] = d[v] = 1;
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:234:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  234 |     int mid = l + r >> 1;
      |               ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3724 KB Output is correct
2 Correct 2 ms 3676 KB Output is correct
3 Correct 2 ms 3676 KB Output is correct
4 Correct 3 ms 3932 KB Output is correct
5 Correct 3 ms 5980 KB Output is correct
6 Correct 3 ms 6236 KB Output is correct
7 Correct 3 ms 5976 KB Output is correct
8 Correct 3 ms 6236 KB Output is correct
9 Correct 138 ms 40240 KB Output is correct
10 Correct 176 ms 50208 KB Output is correct
11 Correct 186 ms 47944 KB Output is correct
12 Correct 212 ms 51072 KB Output is correct
13 Correct 172 ms 56908 KB Output is correct
14 Correct 116 ms 38484 KB Output is correct
15 Correct 214 ms 53236 KB Output is correct
16 Correct 223 ms 47308 KB Output is correct
17 Correct 231 ms 60616 KB Output is correct
18 Correct 208 ms 56148 KB Output is correct
19 Execution timed out 2052 ms 11856 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3724 KB Output is correct
2 Correct 2 ms 3676 KB Output is correct
3 Correct 336 ms 37852 KB Output is correct
4 Correct 309 ms 39180 KB Output is correct
5 Correct 303 ms 38400 KB Output is correct
6 Correct 326 ms 38960 KB Output is correct
7 Correct 288 ms 38768 KB Output is correct
8 Correct 338 ms 38116 KB Output is correct
9 Correct 290 ms 38596 KB Output is correct
10 Correct 277 ms 37576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3724 KB Output is correct
2 Correct 2 ms 3676 KB Output is correct
3 Correct 2 ms 3676 KB Output is correct
4 Correct 3 ms 3932 KB Output is correct
5 Correct 3 ms 5980 KB Output is correct
6 Correct 3 ms 6236 KB Output is correct
7 Correct 3 ms 5976 KB Output is correct
8 Correct 3 ms 6236 KB Output is correct
9 Correct 1 ms 3684 KB Output is correct
10 Correct 2 ms 5980 KB Output is correct
11 Correct 2 ms 5976 KB Output is correct
12 Correct 2 ms 5980 KB Output is correct
13 Correct 2 ms 3932 KB Output is correct
14 Correct 2 ms 5980 KB Output is correct
15 Correct 2 ms 5980 KB Output is correct
16 Correct 3 ms 5980 KB Output is correct
17 Correct 3 ms 6232 KB Output is correct
18 Correct 3 ms 5980 KB Output is correct
19 Correct 3 ms 3932 KB Output is correct
20 Correct 3 ms 3932 KB Output is correct
21 Correct 3 ms 3676 KB Output is correct
22 Correct 3 ms 3932 KB Output is correct
23 Correct 3 ms 3672 KB Output is correct
24 Correct 4 ms 3932 KB Output is correct
25 Correct 3 ms 3928 KB Output is correct
26 Correct 4 ms 3928 KB Output is correct
27 Correct 3 ms 3748 KB Output is correct
28 Correct 5 ms 3784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3684 KB Output is correct
2 Correct 1 ms 3724 KB Output is correct
3 Correct 2 ms 3676 KB Output is correct
4 Correct 2 ms 3676 KB Output is correct
5 Correct 3 ms 3932 KB Output is correct
6 Correct 3 ms 5980 KB Output is correct
7 Correct 3 ms 6236 KB Output is correct
8 Correct 3 ms 5976 KB Output is correct
9 Correct 3 ms 6236 KB Output is correct
10 Correct 138 ms 40240 KB Output is correct
11 Correct 176 ms 50208 KB Output is correct
12 Correct 186 ms 47944 KB Output is correct
13 Correct 212 ms 51072 KB Output is correct
14 Correct 172 ms 56908 KB Output is correct
15 Correct 2 ms 5980 KB Output is correct
16 Correct 2 ms 5976 KB Output is correct
17 Correct 2 ms 5980 KB Output is correct
18 Correct 2 ms 3932 KB Output is correct
19 Correct 2 ms 5980 KB Output is correct
20 Correct 2 ms 5980 KB Output is correct
21 Correct 3 ms 5980 KB Output is correct
22 Correct 3 ms 6232 KB Output is correct
23 Correct 3 ms 5980 KB Output is correct
24 Correct 3 ms 3932 KB Output is correct
25 Correct 3 ms 3932 KB Output is correct
26 Correct 3 ms 3676 KB Output is correct
27 Correct 3 ms 3932 KB Output is correct
28 Correct 3 ms 3672 KB Output is correct
29 Correct 4 ms 3932 KB Output is correct
30 Correct 3 ms 3928 KB Output is correct
31 Correct 4 ms 3928 KB Output is correct
32 Correct 3 ms 3748 KB Output is correct
33 Correct 5 ms 3784 KB Output is correct
34 Correct 11 ms 10588 KB Output is correct
35 Correct 179 ms 49232 KB Output is correct
36 Correct 141 ms 41160 KB Output is correct
37 Correct 122 ms 34968 KB Output is correct
38 Correct 110 ms 33316 KB Output is correct
39 Correct 139 ms 31936 KB Output is correct
40 Correct 111 ms 30408 KB Output is correct
41 Correct 142 ms 43528 KB Output is correct
42 Correct 166 ms 47052 KB Output is correct
43 Correct 159 ms 51084 KB Output is correct
44 Correct 116 ms 32972 KB Output is correct
45 Correct 692 ms 20184 KB Output is correct
46 Correct 394 ms 21620 KB Output is correct
47 Correct 326 ms 11640 KB Output is correct
48 Correct 452 ms 12296 KB Output is correct
49 Correct 96 ms 15700 KB Output is correct
50 Correct 123 ms 12492 KB Output is correct
51 Correct 397 ms 17748 KB Output is correct
52 Correct 725 ms 25480 KB Output is correct
53 Correct 213 ms 17864 KB Output is correct
54 Correct 968 ms 32104 KB Output is correct
55 Correct 392 ms 19508 KB Output is correct
56 Correct 969 ms 23328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3724 KB Output is correct
2 Correct 2 ms 3676 KB Output is correct
3 Correct 2 ms 3676 KB Output is correct
4 Correct 3 ms 3932 KB Output is correct
5 Correct 3 ms 5980 KB Output is correct
6 Correct 3 ms 6236 KB Output is correct
7 Correct 3 ms 5976 KB Output is correct
8 Correct 3 ms 6236 KB Output is correct
9 Correct 138 ms 40240 KB Output is correct
10 Correct 176 ms 50208 KB Output is correct
11 Correct 186 ms 47944 KB Output is correct
12 Correct 212 ms 51072 KB Output is correct
13 Correct 172 ms 56908 KB Output is correct
14 Correct 116 ms 38484 KB Output is correct
15 Correct 214 ms 53236 KB Output is correct
16 Correct 223 ms 47308 KB Output is correct
17 Correct 231 ms 60616 KB Output is correct
18 Correct 208 ms 56148 KB Output is correct
19 Correct 336 ms 37852 KB Output is correct
20 Correct 309 ms 39180 KB Output is correct
21 Correct 303 ms 38400 KB Output is correct
22 Correct 326 ms 38960 KB Output is correct
23 Correct 288 ms 38768 KB Output is correct
24 Correct 338 ms 38116 KB Output is correct
25 Correct 290 ms 38596 KB Output is correct
26 Correct 277 ms 37576 KB Output is correct
27 Correct 2 ms 5980 KB Output is correct
28 Correct 2 ms 5976 KB Output is correct
29 Correct 2 ms 5980 KB Output is correct
30 Correct 2 ms 3932 KB Output is correct
31 Correct 2 ms 5980 KB Output is correct
32 Correct 2 ms 5980 KB Output is correct
33 Correct 3 ms 5980 KB Output is correct
34 Correct 3 ms 6232 KB Output is correct
35 Correct 3 ms 5980 KB Output is correct
36 Correct 11 ms 10588 KB Output is correct
37 Correct 179 ms 49232 KB Output is correct
38 Correct 141 ms 41160 KB Output is correct
39 Correct 122 ms 34968 KB Output is correct
40 Correct 110 ms 33316 KB Output is correct
41 Correct 139 ms 31936 KB Output is correct
42 Correct 111 ms 30408 KB Output is correct
43 Correct 142 ms 43528 KB Output is correct
44 Correct 166 ms 47052 KB Output is correct
45 Correct 159 ms 51084 KB Output is correct
46 Correct 116 ms 32972 KB Output is correct
47 Correct 29 ms 11088 KB Output is correct
48 Correct 490 ms 47436 KB Output is correct
49 Correct 464 ms 43092 KB Output is correct
50 Correct 446 ms 40100 KB Output is correct
51 Correct 467 ms 38736 KB Output is correct
52 Correct 423 ms 35960 KB Output is correct
53 Correct 395 ms 32764 KB Output is correct
54 Correct 582 ms 45800 KB Output is correct
55 Correct 552 ms 49144 KB Output is correct
56 Correct 510 ms 57392 KB Output is correct
57 Correct 465 ms 38296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3684 KB Output is correct
2 Correct 1 ms 3724 KB Output is correct
3 Correct 2 ms 3676 KB Output is correct
4 Correct 2 ms 3676 KB Output is correct
5 Correct 3 ms 3932 KB Output is correct
6 Correct 3 ms 5980 KB Output is correct
7 Correct 3 ms 6236 KB Output is correct
8 Correct 3 ms 5976 KB Output is correct
9 Correct 3 ms 6236 KB Output is correct
10 Correct 138 ms 40240 KB Output is correct
11 Correct 176 ms 50208 KB Output is correct
12 Correct 186 ms 47944 KB Output is correct
13 Correct 212 ms 51072 KB Output is correct
14 Correct 172 ms 56908 KB Output is correct
15 Correct 116 ms 38484 KB Output is correct
16 Correct 214 ms 53236 KB Output is correct
17 Correct 223 ms 47308 KB Output is correct
18 Correct 231 ms 60616 KB Output is correct
19 Correct 208 ms 56148 KB Output is correct
20 Correct 336 ms 37852 KB Output is correct
21 Correct 309 ms 39180 KB Output is correct
22 Correct 303 ms 38400 KB Output is correct
23 Correct 326 ms 38960 KB Output is correct
24 Correct 288 ms 38768 KB Output is correct
25 Correct 338 ms 38116 KB Output is correct
26 Correct 290 ms 38596 KB Output is correct
27 Correct 277 ms 37576 KB Output is correct
28 Correct 2 ms 5980 KB Output is correct
29 Correct 2 ms 5976 KB Output is correct
30 Correct 2 ms 5980 KB Output is correct
31 Correct 2 ms 3932 KB Output is correct
32 Correct 2 ms 5980 KB Output is correct
33 Correct 2 ms 5980 KB Output is correct
34 Correct 3 ms 5980 KB Output is correct
35 Correct 3 ms 6232 KB Output is correct
36 Correct 3 ms 5980 KB Output is correct
37 Correct 11 ms 10588 KB Output is correct
38 Correct 179 ms 49232 KB Output is correct
39 Correct 141 ms 41160 KB Output is correct
40 Correct 122 ms 34968 KB Output is correct
41 Correct 110 ms 33316 KB Output is correct
42 Correct 139 ms 31936 KB Output is correct
43 Correct 111 ms 30408 KB Output is correct
44 Correct 142 ms 43528 KB Output is correct
45 Correct 166 ms 47052 KB Output is correct
46 Correct 159 ms 51084 KB Output is correct
47 Correct 116 ms 32972 KB Output is correct
48 Correct 29 ms 11088 KB Output is correct
49 Correct 490 ms 47436 KB Output is correct
50 Correct 464 ms 43092 KB Output is correct
51 Correct 446 ms 40100 KB Output is correct
52 Correct 467 ms 38736 KB Output is correct
53 Correct 423 ms 35960 KB Output is correct
54 Correct 395 ms 32764 KB Output is correct
55 Correct 582 ms 45800 KB Output is correct
56 Correct 552 ms 49144 KB Output is correct
57 Correct 510 ms 57392 KB Output is correct
58 Correct 465 ms 38296 KB Output is correct
59 Execution timed out 2052 ms 11856 KB Time limit exceeded
60 Halted 0 ms 0 KB -