Submission #916091

# Submission time Handle Problem Language Result Execution time Memory
916091 2024-01-25T09:24:40 Z duckindog Swapping Cities (APIO20_swap) C++14
73 / 100
2000 ms 56264 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;
int answers1;

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;
    answers1 = max(answers1, w);
  }
  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 (sub1 && m != n - 1) return answers1;
  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:190:20: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  190 |       dd[v] = d[v] = 1;
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:236:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  236 |     int mid = l + r >> 1;
      |               ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3676 KB Output is correct
2 Correct 1 ms 3676 KB Output is correct
3 Correct 2 ms 3672 KB Output is correct
4 Correct 2 ms 3932 KB Output is correct
5 Correct 2 ms 5980 KB Output is correct
6 Correct 3 ms 5980 KB Output is correct
7 Correct 3 ms 5980 KB Output is correct
8 Correct 3 ms 6236 KB Output is correct
9 Correct 130 ms 38736 KB Output is correct
10 Correct 176 ms 48300 KB Output is correct
11 Correct 170 ms 46080 KB Output is correct
12 Correct 220 ms 49100 KB Output is correct
13 Correct 188 ms 54792 KB Output is correct
14 Correct 133 ms 36612 KB Output is correct
15 Correct 202 ms 49028 KB Output is correct
16 Correct 195 ms 42956 KB Output is correct
17 Correct 250 ms 56264 KB Output is correct
18 Correct 222 ms 51656 KB Output is correct
19 Correct 42 ms 8024 KB Output is correct
20 Correct 82 ms 16400 KB Output is correct
21 Correct 80 ms 16520 KB Output is correct
22 Correct 84 ms 17204 KB Output is correct
23 Correct 84 ms 17140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3676 KB Output is correct
2 Correct 1 ms 3676 KB Output is correct
3 Correct 312 ms 34176 KB Output is correct
4 Correct 272 ms 35244 KB Output is correct
5 Correct 269 ms 34516 KB Output is correct
6 Correct 277 ms 35312 KB Output is correct
7 Correct 292 ms 34948 KB Output is correct
8 Correct 309 ms 33960 KB Output is correct
9 Correct 263 ms 34752 KB Output is correct
10 Correct 317 ms 33572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3676 KB Output is correct
2 Correct 1 ms 3676 KB Output is correct
3 Correct 2 ms 3672 KB Output is correct
4 Correct 2 ms 3932 KB Output is correct
5 Correct 2 ms 5980 KB Output is correct
6 Correct 3 ms 5980 KB Output is correct
7 Correct 3 ms 5980 KB Output is correct
8 Correct 3 ms 6236 KB Output is correct
9 Correct 2 ms 3676 KB Output is correct
10 Correct 4 ms 5980 KB Output is correct
11 Correct 2 ms 5980 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 6068 KB Output is correct
15 Correct 3 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 3 ms 6232 KB Output is correct
18 Correct 3 ms 5976 KB Output is correct
19 Correct 3 ms 3676 KB Output is correct
20 Correct 2 ms 3924 KB Output is correct
21 Correct 3 ms 3908 KB Output is correct
22 Correct 3 ms 3932 KB Output is correct
23 Correct 3 ms 3676 KB Output is correct
24 Correct 4 ms 3932 KB Output is correct
25 Correct 4 ms 3932 KB Output is correct
26 Correct 4 ms 3932 KB Output is correct
27 Correct 3 ms 3928 KB Output is correct
28 Correct 5 ms 3928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3676 KB Output is correct
2 Correct 2 ms 3676 KB Output is correct
3 Correct 1 ms 3676 KB Output is correct
4 Correct 2 ms 3672 KB Output is correct
5 Correct 2 ms 3932 KB Output is correct
6 Correct 2 ms 5980 KB Output is correct
7 Correct 3 ms 5980 KB Output is correct
8 Correct 3 ms 5980 KB Output is correct
9 Correct 3 ms 6236 KB Output is correct
10 Correct 130 ms 38736 KB Output is correct
11 Correct 176 ms 48300 KB Output is correct
12 Correct 170 ms 46080 KB Output is correct
13 Correct 220 ms 49100 KB Output is correct
14 Correct 188 ms 54792 KB Output is correct
15 Correct 4 ms 5980 KB Output is correct
16 Correct 2 ms 5980 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 6068 KB Output is correct
20 Correct 3 ms 5980 KB Output is correct
21 Correct 2 ms 5980 KB Output is correct
22 Correct 3 ms 6232 KB Output is correct
23 Correct 3 ms 5976 KB Output is correct
24 Correct 3 ms 3676 KB Output is correct
25 Correct 2 ms 3924 KB Output is correct
26 Correct 3 ms 3908 KB Output is correct
27 Correct 3 ms 3932 KB Output is correct
28 Correct 3 ms 3676 KB Output is correct
29 Correct 4 ms 3932 KB Output is correct
30 Correct 4 ms 3932 KB Output is correct
31 Correct 4 ms 3932 KB Output is correct
32 Correct 3 ms 3928 KB Output is correct
33 Correct 5 ms 3928 KB Output is correct
34 Correct 11 ms 10328 KB Output is correct
35 Correct 170 ms 47476 KB Output is correct
36 Correct 139 ms 39220 KB Output is correct
37 Correct 122 ms 33108 KB Output is correct
38 Correct 130 ms 31528 KB Output is correct
39 Correct 107 ms 30028 KB Output is correct
40 Correct 92 ms 28696 KB Output is correct
41 Correct 147 ms 41436 KB Output is correct
42 Correct 159 ms 45152 KB Output is correct
43 Correct 158 ms 49352 KB Output is correct
44 Correct 115 ms 30896 KB Output is correct
45 Correct 728 ms 16916 KB Output is correct
46 Correct 325 ms 20016 KB Output is correct
47 Correct 337 ms 9524 KB Output is correct
48 Correct 476 ms 10196 KB Output is correct
49 Correct 93 ms 12868 KB Output is correct
50 Correct 126 ms 10184 KB Output is correct
51 Correct 438 ms 15040 KB Output is correct
52 Correct 757 ms 22328 KB Output is correct
53 Correct 216 ms 14132 KB Output is correct
54 Correct 1110 ms 28632 KB Output is correct
55 Correct 438 ms 17716 KB Output is correct
56 Correct 1165 ms 19692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3676 KB Output is correct
2 Correct 1 ms 3676 KB Output is correct
3 Correct 2 ms 3672 KB Output is correct
4 Correct 2 ms 3932 KB Output is correct
5 Correct 2 ms 5980 KB Output is correct
6 Correct 3 ms 5980 KB Output is correct
7 Correct 3 ms 5980 KB Output is correct
8 Correct 3 ms 6236 KB Output is correct
9 Correct 130 ms 38736 KB Output is correct
10 Correct 176 ms 48300 KB Output is correct
11 Correct 170 ms 46080 KB Output is correct
12 Correct 220 ms 49100 KB Output is correct
13 Correct 188 ms 54792 KB Output is correct
14 Correct 133 ms 36612 KB Output is correct
15 Correct 202 ms 49028 KB Output is correct
16 Correct 195 ms 42956 KB Output is correct
17 Correct 250 ms 56264 KB Output is correct
18 Correct 222 ms 51656 KB Output is correct
19 Correct 312 ms 34176 KB Output is correct
20 Correct 272 ms 35244 KB Output is correct
21 Correct 269 ms 34516 KB Output is correct
22 Correct 277 ms 35312 KB Output is correct
23 Correct 292 ms 34948 KB Output is correct
24 Correct 309 ms 33960 KB Output is correct
25 Correct 263 ms 34752 KB Output is correct
26 Correct 317 ms 33572 KB Output is correct
27 Correct 4 ms 5980 KB Output is correct
28 Correct 2 ms 5980 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 6068 KB Output is correct
32 Correct 3 ms 5980 KB Output is correct
33 Correct 2 ms 5980 KB Output is correct
34 Correct 3 ms 6232 KB Output is correct
35 Correct 3 ms 5976 KB Output is correct
36 Correct 11 ms 10328 KB Output is correct
37 Correct 170 ms 47476 KB Output is correct
38 Correct 139 ms 39220 KB Output is correct
39 Correct 122 ms 33108 KB Output is correct
40 Correct 130 ms 31528 KB Output is correct
41 Correct 107 ms 30028 KB Output is correct
42 Correct 92 ms 28696 KB Output is correct
43 Correct 147 ms 41436 KB Output is correct
44 Correct 159 ms 45152 KB Output is correct
45 Correct 158 ms 49352 KB Output is correct
46 Correct 115 ms 30896 KB Output is correct
47 Correct 31 ms 10516 KB Output is correct
48 Correct 416 ms 43312 KB Output is correct
49 Correct 477 ms 39220 KB Output is correct
50 Correct 437 ms 36040 KB Output is correct
51 Correct 452 ms 34292 KB Output is correct
52 Correct 414 ms 32124 KB Output is correct
53 Correct 362 ms 29224 KB Output is correct
54 Correct 473 ms 41812 KB Output is correct
55 Correct 528 ms 45108 KB Output is correct
56 Correct 449 ms 53044 KB Output is correct
57 Correct 406 ms 33848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3676 KB Output is correct
2 Correct 2 ms 3676 KB Output is correct
3 Correct 1 ms 3676 KB Output is correct
4 Correct 2 ms 3672 KB Output is correct
5 Correct 2 ms 3932 KB Output is correct
6 Correct 2 ms 5980 KB Output is correct
7 Correct 3 ms 5980 KB Output is correct
8 Correct 3 ms 5980 KB Output is correct
9 Correct 3 ms 6236 KB Output is correct
10 Correct 130 ms 38736 KB Output is correct
11 Correct 176 ms 48300 KB Output is correct
12 Correct 170 ms 46080 KB Output is correct
13 Correct 220 ms 49100 KB Output is correct
14 Correct 188 ms 54792 KB Output is correct
15 Correct 133 ms 36612 KB Output is correct
16 Correct 202 ms 49028 KB Output is correct
17 Correct 195 ms 42956 KB Output is correct
18 Correct 250 ms 56264 KB Output is correct
19 Correct 222 ms 51656 KB Output is correct
20 Correct 312 ms 34176 KB Output is correct
21 Correct 272 ms 35244 KB Output is correct
22 Correct 269 ms 34516 KB Output is correct
23 Correct 277 ms 35312 KB Output is correct
24 Correct 292 ms 34948 KB Output is correct
25 Correct 309 ms 33960 KB Output is correct
26 Correct 263 ms 34752 KB Output is correct
27 Correct 317 ms 33572 KB Output is correct
28 Correct 4 ms 5980 KB Output is correct
29 Correct 2 ms 5980 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 6068 KB Output is correct
33 Correct 3 ms 5980 KB Output is correct
34 Correct 2 ms 5980 KB Output is correct
35 Correct 3 ms 6232 KB Output is correct
36 Correct 3 ms 5976 KB Output is correct
37 Correct 11 ms 10328 KB Output is correct
38 Correct 170 ms 47476 KB Output is correct
39 Correct 139 ms 39220 KB Output is correct
40 Correct 122 ms 33108 KB Output is correct
41 Correct 130 ms 31528 KB Output is correct
42 Correct 107 ms 30028 KB Output is correct
43 Correct 92 ms 28696 KB Output is correct
44 Correct 147 ms 41436 KB Output is correct
45 Correct 159 ms 45152 KB Output is correct
46 Correct 158 ms 49352 KB Output is correct
47 Correct 115 ms 30896 KB Output is correct
48 Correct 31 ms 10516 KB Output is correct
49 Correct 416 ms 43312 KB Output is correct
50 Correct 477 ms 39220 KB Output is correct
51 Correct 437 ms 36040 KB Output is correct
52 Correct 452 ms 34292 KB Output is correct
53 Correct 414 ms 32124 KB Output is correct
54 Correct 362 ms 29224 KB Output is correct
55 Correct 473 ms 41812 KB Output is correct
56 Correct 528 ms 45108 KB Output is correct
57 Correct 449 ms 53044 KB Output is correct
58 Correct 406 ms 33848 KB Output is correct
59 Correct 42 ms 8024 KB Output is correct
60 Correct 82 ms 16400 KB Output is correct
61 Correct 80 ms 16520 KB Output is correct
62 Correct 84 ms 17204 KB Output is correct
63 Correct 84 ms 17140 KB Output is correct
64 Correct 3 ms 3676 KB Output is correct
65 Correct 2 ms 3924 KB Output is correct
66 Correct 3 ms 3908 KB Output is correct
67 Correct 3 ms 3932 KB Output is correct
68 Correct 3 ms 3676 KB Output is correct
69 Correct 4 ms 3932 KB Output is correct
70 Correct 4 ms 3932 KB Output is correct
71 Correct 4 ms 3932 KB Output is correct
72 Correct 3 ms 3928 KB Output is correct
73 Correct 5 ms 3928 KB Output is correct
74 Correct 728 ms 16916 KB Output is correct
75 Correct 325 ms 20016 KB Output is correct
76 Correct 337 ms 9524 KB Output is correct
77 Correct 476 ms 10196 KB Output is correct
78 Correct 93 ms 12868 KB Output is correct
79 Correct 126 ms 10184 KB Output is correct
80 Correct 438 ms 15040 KB Output is correct
81 Correct 757 ms 22328 KB Output is correct
82 Correct 216 ms 14132 KB Output is correct
83 Correct 1110 ms 28632 KB Output is correct
84 Correct 438 ms 17716 KB Output is correct
85 Correct 1165 ms 19692 KB Output is correct
86 Execution timed out 2098 ms 11020 KB Time limit exceeded
87 Halted 0 ms 0 KB -