Submission #915691

# Submission time Handle Problem Language Result Execution time Memory
915691 2024-01-24T14:45:06 Z duckindog Swapping Cities (APIO20_swap) C++14
50 / 100
2000 ms 27696 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];

vector<int> co;
bool sub1 = 1, sub2 = 1;
int maxw = 0;

int par[N];
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
  n = N; m = M;
  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});
    maxw = max(maxw, w);
    if (ad[u].size() > 2 || ad[v].size() > 2) sub1 = 0;
    if (u) {
      sub2 = 0;
    } else par[v] = w;
  }
  if (m != n - 1) sub2 = 0;

  if (sub2) {
    using pii = pair<int, int>;
    sort(ad[0].begin(), ad[0].end(), [&] (pii a, pii b) {
      return a.second < b.second;
    });
  }

  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 -1;
  if (sub1 && m == n) return maxw;

  if (sub2) {
    int it = 0;
    int answer = max(par[X], par[Y]);
    while (ad[0][it].first == X || ad[0][it].first == Y) it += 1;
    answer = max(answer, ad[0][it].second);

    if (!X || !Y) {
      it += 1;
      while (ad[0][it].first == X || ad[0][it].first == Y) it += 1;
      answer = max(answer, ad[0][it].second);
    }

    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:73:20: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   73 |       dd[v] = d[v] = 1;
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:105:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  105 |     int mid = l + r >> 1;
      |               ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2904 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Correct 2 ms 2908 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
6 Correct 1 ms 3164 KB Output is correct
7 Correct 1 ms 3164 KB Output is correct
8 Correct 1 ms 3164 KB Output is correct
9 Correct 30 ms 7772 KB Output is correct
10 Correct 39 ms 8708 KB Output is correct
11 Correct 37 ms 8660 KB Output is correct
12 Correct 46 ms 8756 KB Output is correct
13 Correct 39 ms 8916 KB Output is correct
14 Correct 42 ms 7764 KB Output is correct
15 Correct 82 ms 10408 KB Output is correct
16 Correct 77 ms 10364 KB Output is correct
17 Correct 90 ms 10824 KB Output is correct
18 Correct 92 ms 10548 KB Output is correct
19 Correct 42 ms 7508 KB Output is correct
20 Correct 83 ms 11596 KB Output is correct
21 Correct 87 ms 11656 KB Output is correct
22 Correct 88 ms 12000 KB Output is correct
23 Correct 83 ms 12080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2904 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 102 ms 12484 KB Output is correct
4 Correct 85 ms 16296 KB Output is correct
5 Correct 90 ms 16596 KB Output is correct
6 Correct 96 ms 16192 KB Output is correct
7 Correct 89 ms 16576 KB Output is correct
8 Correct 83 ms 16112 KB Output is correct
9 Correct 85 ms 16320 KB Output is correct
10 Correct 86 ms 16124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2904 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Correct 2 ms 2908 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
6 Correct 1 ms 3164 KB Output is correct
7 Correct 1 ms 3164 KB Output is correct
8 Correct 1 ms 3164 KB Output is correct
9 Correct 2 ms 3164 KB Output is correct
10 Correct 2 ms 3164 KB Output is correct
11 Correct 2 ms 3160 KB Output is correct
12 Correct 2 ms 3164 KB Output is correct
13 Correct 3 ms 3164 KB Output is correct
14 Correct 3 ms 3160 KB Output is correct
15 Correct 2 ms 3164 KB Output is correct
16 Correct 2 ms 3164 KB Output is correct
17 Correct 2 ms 3420 KB Output is correct
18 Correct 3 ms 3160 KB Output is correct
19 Correct 3 ms 3160 KB Output is correct
20 Correct 2 ms 3164 KB Output is correct
21 Correct 3 ms 3164 KB Output is correct
22 Correct 4 ms 3164 KB Output is correct
23 Correct 3 ms 3416 KB Output is correct
24 Correct 4 ms 3164 KB Output is correct
25 Correct 4 ms 3164 KB Output is correct
26 Correct 4 ms 3416 KB Output is correct
27 Correct 3 ms 3164 KB Output is correct
28 Correct 5 ms 3160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3164 KB Output is correct
2 Correct 1 ms 2904 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Correct 1 ms 2908 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 1 ms 3164 KB Output is correct
7 Correct 1 ms 3164 KB Output is correct
8 Correct 1 ms 3164 KB Output is correct
9 Correct 1 ms 3164 KB Output is correct
10 Correct 30 ms 7772 KB Output is correct
11 Correct 39 ms 8708 KB Output is correct
12 Correct 37 ms 8660 KB Output is correct
13 Correct 46 ms 8756 KB Output is correct
14 Correct 39 ms 8916 KB Output is correct
15 Correct 2 ms 3164 KB Output is correct
16 Correct 2 ms 3160 KB Output is correct
17 Correct 2 ms 3164 KB Output is correct
18 Correct 3 ms 3164 KB Output is correct
19 Correct 3 ms 3160 KB Output is correct
20 Correct 2 ms 3164 KB Output is correct
21 Correct 2 ms 3164 KB Output is correct
22 Correct 2 ms 3420 KB Output is correct
23 Correct 3 ms 3160 KB Output is correct
24 Correct 3 ms 3160 KB Output is correct
25 Correct 2 ms 3164 KB Output is correct
26 Correct 3 ms 3164 KB Output is correct
27 Correct 4 ms 3164 KB Output is correct
28 Correct 3 ms 3416 KB Output is correct
29 Correct 4 ms 3164 KB Output is correct
30 Correct 4 ms 3164 KB Output is correct
31 Correct 4 ms 3416 KB Output is correct
32 Correct 3 ms 3164 KB Output is correct
33 Correct 5 ms 3160 KB Output is correct
34 Correct 11 ms 3932 KB Output is correct
35 Correct 228 ms 24376 KB Output is correct
36 Correct 151 ms 17200 KB Output is correct
37 Correct 141 ms 10800 KB Output is correct
38 Correct 282 ms 9124 KB Output is correct
39 Correct 175 ms 8904 KB Output is correct
40 Correct 262 ms 8404 KB Output is correct
41 Correct 202 ms 18992 KB Output is correct
42 Correct 219 ms 21656 KB Output is correct
43 Correct 470 ms 22072 KB Output is correct
44 Correct 283 ms 9268 KB Output is correct
45 Correct 667 ms 16472 KB Output is correct
46 Correct 258 ms 19248 KB Output is correct
47 Correct 318 ms 8912 KB Output is correct
48 Correct 389 ms 9572 KB Output is correct
49 Correct 93 ms 12256 KB Output is correct
50 Correct 125 ms 9612 KB Output is correct
51 Correct 415 ms 14428 KB Output is correct
52 Correct 576 ms 21448 KB Output is correct
53 Correct 217 ms 13508 KB Output is correct
54 Correct 1060 ms 27696 KB Output is correct
55 Correct 384 ms 16964 KB Output is correct
56 Correct 1080 ms 19076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2904 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Correct 2 ms 2908 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
6 Correct 1 ms 3164 KB Output is correct
7 Correct 1 ms 3164 KB Output is correct
8 Correct 1 ms 3164 KB Output is correct
9 Correct 30 ms 7772 KB Output is correct
10 Correct 39 ms 8708 KB Output is correct
11 Correct 37 ms 8660 KB Output is correct
12 Correct 46 ms 8756 KB Output is correct
13 Correct 39 ms 8916 KB Output is correct
14 Correct 42 ms 7764 KB Output is correct
15 Correct 82 ms 10408 KB Output is correct
16 Correct 77 ms 10364 KB Output is correct
17 Correct 90 ms 10824 KB Output is correct
18 Correct 92 ms 10548 KB Output is correct
19 Correct 102 ms 12484 KB Output is correct
20 Correct 85 ms 16296 KB Output is correct
21 Correct 90 ms 16596 KB Output is correct
22 Correct 96 ms 16192 KB Output is correct
23 Correct 89 ms 16576 KB Output is correct
24 Correct 83 ms 16112 KB Output is correct
25 Correct 85 ms 16320 KB Output is correct
26 Correct 86 ms 16124 KB Output is correct
27 Correct 2 ms 3164 KB Output is correct
28 Correct 2 ms 3160 KB Output is correct
29 Correct 2 ms 3164 KB Output is correct
30 Correct 3 ms 3164 KB Output is correct
31 Correct 3 ms 3160 KB Output is correct
32 Correct 2 ms 3164 KB Output is correct
33 Correct 2 ms 3164 KB Output is correct
34 Correct 2 ms 3420 KB Output is correct
35 Correct 3 ms 3160 KB Output is correct
36 Correct 11 ms 3932 KB Output is correct
37 Correct 228 ms 24376 KB Output is correct
38 Correct 151 ms 17200 KB Output is correct
39 Correct 141 ms 10800 KB Output is correct
40 Correct 282 ms 9124 KB Output is correct
41 Correct 175 ms 8904 KB Output is correct
42 Correct 262 ms 8404 KB Output is correct
43 Correct 202 ms 18992 KB Output is correct
44 Correct 219 ms 21656 KB Output is correct
45 Correct 470 ms 22072 KB Output is correct
46 Correct 283 ms 9268 KB Output is correct
47 Execution timed out 2036 ms 4444 KB Time limit exceeded
48 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3164 KB Output is correct
2 Correct 1 ms 2904 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Correct 1 ms 2908 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 1 ms 3164 KB Output is correct
7 Correct 1 ms 3164 KB Output is correct
8 Correct 1 ms 3164 KB Output is correct
9 Correct 1 ms 3164 KB Output is correct
10 Correct 30 ms 7772 KB Output is correct
11 Correct 39 ms 8708 KB Output is correct
12 Correct 37 ms 8660 KB Output is correct
13 Correct 46 ms 8756 KB Output is correct
14 Correct 39 ms 8916 KB Output is correct
15 Correct 42 ms 7764 KB Output is correct
16 Correct 82 ms 10408 KB Output is correct
17 Correct 77 ms 10364 KB Output is correct
18 Correct 90 ms 10824 KB Output is correct
19 Correct 92 ms 10548 KB Output is correct
20 Correct 102 ms 12484 KB Output is correct
21 Correct 85 ms 16296 KB Output is correct
22 Correct 90 ms 16596 KB Output is correct
23 Correct 96 ms 16192 KB Output is correct
24 Correct 89 ms 16576 KB Output is correct
25 Correct 83 ms 16112 KB Output is correct
26 Correct 85 ms 16320 KB Output is correct
27 Correct 86 ms 16124 KB Output is correct
28 Correct 2 ms 3164 KB Output is correct
29 Correct 2 ms 3160 KB Output is correct
30 Correct 2 ms 3164 KB Output is correct
31 Correct 3 ms 3164 KB Output is correct
32 Correct 3 ms 3160 KB Output is correct
33 Correct 2 ms 3164 KB Output is correct
34 Correct 2 ms 3164 KB Output is correct
35 Correct 2 ms 3420 KB Output is correct
36 Correct 3 ms 3160 KB Output is correct
37 Correct 11 ms 3932 KB Output is correct
38 Correct 228 ms 24376 KB Output is correct
39 Correct 151 ms 17200 KB Output is correct
40 Correct 141 ms 10800 KB Output is correct
41 Correct 282 ms 9124 KB Output is correct
42 Correct 175 ms 8904 KB Output is correct
43 Correct 262 ms 8404 KB Output is correct
44 Correct 202 ms 18992 KB Output is correct
45 Correct 219 ms 21656 KB Output is correct
46 Correct 470 ms 22072 KB Output is correct
47 Correct 283 ms 9268 KB Output is correct
48 Execution timed out 2036 ms 4444 KB Time limit exceeded
49 Halted 0 ms 0 KB -