Submission #523775

# Submission time Handle Problem Language Result Execution time Memory
523775 2022-02-08T06:52:35 Z maomao90 Swapping Cities (APIO20_swap) C++17
100 / 100
640 ms 65040 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

#define INF 1000000005
#define LINF 1000000000000000005ll
#define MAXL 20
#define MAXN 200005

int n, m;
vector<iii> edges;
vii oadj[MAXN], adj[MAXN];
int l[MAXN];
ii p[MAXL][MAXN];
int omn[MAXL][MAXN];

namespace dsu {
    int p[MAXN], rnk[MAXN];
    int findp(int u) {
        if (p[u] == u) return u;
        return p[u] = findp(p[u]);
    }
    bool join(int a, int b) {
        int pa = findp(a), pb = findp(b);
        if (pa == pb) return 0;
        if (rnk[pa] < rnk[pb]) swap(pa, pb);
        if (rnk[pa] == rnk[pb]) rnk[pa]++;
        p[pb] = pa;
        return 1;
    }
}

void dfs(int u) {
    REP (k, 1, MAXL) {
        if (p[k - 1][u].FI == -1) {
            p[k][u] = MP(-1, -1);
        } else {
            p[k][u].FI = p[k - 1][p[k - 1][u].FI].FI;
            p[k][u].SE = max(p[k - 1][u].SE, p[k - 1][p[k - 1][u].FI].SE);
        }
    }
    for (auto [v, w] : adj[u]) {
        if (v == p[0][u].FI) continue;
        l[v] = l[u] + 1;
        p[0][v] = MP(u, w);
        dfs(v);
    }
}

int dp[MAXN], rdp[MAXN], cost[MAXN];
void dfsdp(int u, int p) {
    dp[u] = cost[u] = INF;
    for (auto [v, w] : adj[u]) {
        if (v == p) continue;
        dfsdp(v, u);
        mnto(dp[u], max(dp[v], w));
    }
    if (oadj[u].size() >= 3) {
        mnto(dp[u], oadj[u][2].SE);
        mnto(cost[u], oadj[u][2].SE);
    }
    REP (i, 0, min((int) oadj[u].size(), 4)) {
        bool die = 0;
        for (auto [v, w] : adj[u]) {
            if (v == oadj[u][i].FI) {
                die = 1;
                break;
            }
        }
        if (!die) {
            mnto(dp[u], oadj[u][i].SE);
            mnto(cost[u], oadj[u][i].SE);
        }
    }
    cerr << u << ": " << dp[u] << "\n";
}

void reroot(int u, int p) {
    vi pre(adj[u].size());
    vi suf(adj[u].size());
    REP (i, 0, adj[u].size()) {
        auto [v, w] = adj[u][i];
        pre[i] = suf[i] = max(dp[v], w);
    }
    REP (i, 1, adj[u].size()) {
        mnto(pre[i], pre[i - 1]);
    }
    RREP (i, adj[u].size() - 2, 0) {
        mnto(suf[i], suf[i + 1]);
    }
    dp[u] = min(suf[0], cost[u]);
    rdp[u] = dp[u];
    cerr << u << ": " << rdp[u] << "\n";
    REP (i, 0, adj[u].size()) {
        auto [v, w] = adj[u][i];
        if (v == p) continue;
        dp[u] = min(cost[u], min(i == 0 ? INF : pre[i - 1],
                i + 1 == adj[u].size() ? INF : suf[i + 1]));
        cerr << " " << u << " " << v << ": " << dp[u] << "\n";
        reroot(v, u);
    }
}

void init(int n, int m, vi u, vi v, vi w) {
#ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
#endif
    REP (i, 0, n) {
        dsu::p[i] = i;
        dsu::rnk[i] = 0;
    }
    ::n = n; ::m = m;
    REP (i, 0, m) {
        oadj[u[i]].pb(MP(v[i], w[i]));
        oadj[v[i]].pb(MP(u[i], w[i]));
        edges.pb(MT(w[i], u[i], v[i]));
    }
    REP (i, 0, n) {
        sort(ALL(oadj[i]), [&] (ii l, ii r) {
                if (l.SE == r.SE) return l.FI < r.FI;
                return l.SE < r.SE;
                });
    }
    sort(ALL(edges));
    for (auto [w, u, v] : edges) {
        if (dsu::join(u, v)) {
            adj[u].pb(MP(v, w));
            adj[v].pb(MP(u, w));
        }
    }
    p[0][0] = MP(-1, -1);
    dfs(0);
    dfsdp(0, -1);
    reroot(0, -1);
}

// consider other path from x to y
// second mst
int getMinimumFuelCapacity(int x, int y) {
    cerr << x << " " << y << "\n";
    if (l[x] < l[y]) swap(x, y);
    int ox = x, oy = y;
    int cmx = -INF;
    RREP (k, MAXL - 1, 0) {
        if (p[k][x].FI == -1) continue;
        if (l[p[k][x].FI] > l[y]) {
            mxto(cmx, p[k][x].SE);
            x = p[k][x].FI;
            cerr << "jump " << k << " " << x << "\n";
        }
    }
    cerr << " " << x << " " << y << "\n";
    if (p[0][x].FI == y) {
        mxto(cmx, p[0][x].SE);
        x = p[0][x].FI;
    } else {
        if (l[x] != l[y]) {
            mxto(cmx, p[0][x].SE);
            x = p[0][x].FI;
        }
        assert(l[x] == l[y]);
        RREP (k, MAXL - 1, 0) {
            if (p[k][x].FI != p[k][y].FI) {
                mxto(cmx, p[k][x].SE);
                x = p[k][x].FI;

                mxto(cmx, p[k][y].SE);
                y = p[k][y].FI;
            }
        }
        mxto(cmx, p[0][x].SE);
        mxto(cmx, p[0][y].SE);
    }
    mxto(cmx, rdp[ox]);
    if (cmx >= INF) {
        return -1;
    }
    return cmx;
}

/*
10 9
0 1 9
0 2 8
0 3 4
0 4 5
0 5 3
0 6 1
0 7 8
0 8 4
0 9 6
10
0 8
0 5
3 9
2 4
1 7
6 8
1 2
5 6
3 4
5 6
*/

Compilation message

swap.cpp: In function 'void reroot(int, int)':
swap.cpp:9:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    9 | #define REP(i, s, e) for (int i = s; i < e; i++)
......
  106 |     REP (i, 0, adj[u].size()) {
      |          ~~~~~~~~~~~~~~~~~~~            
swap.cpp:106:5: note: in expansion of macro 'REP'
  106 |     REP (i, 0, adj[u].size()) {
      |     ^~~
swap.cpp:9:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    9 | #define REP(i, s, e) for (int i = s; i < e; i++)
......
  110 |     REP (i, 1, adj[u].size()) {
      |          ~~~~~~~~~~~~~~~~~~~            
swap.cpp:110:5: note: in expansion of macro 'REP'
  110 |     REP (i, 1, adj[u].size()) {
      |     ^~~
swap.cpp:9:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    9 | #define REP(i, s, e) for (int i = s; i < e; i++)
......
  119 |     REP (i, 0, adj[u].size()) {
      |          ~~~~~~~~~~~~~~~~~~~            
swap.cpp:119:5: note: in expansion of macro 'REP'
  119 |     REP (i, 0, adj[u].size()) {
      |     ^~~
swap.cpp:123:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |                 i + 1 == adj[u].size() ? INF : suf[i + 1]));
      |                 ~~~~~~^~~~~~~~~~~~~~~~
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:167:17: warning: unused variable 'oy' [-Wunused-variable]
  167 |     int ox = x, oy = y;
      |                 ^~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9804 KB Output is correct
2 Correct 6 ms 9804 KB Output is correct
3 Correct 5 ms 9808 KB Output is correct
4 Correct 5 ms 10060 KB Output is correct
5 Correct 6 ms 10188 KB Output is correct
6 Correct 8 ms 10188 KB Output is correct
7 Correct 7 ms 10220 KB Output is correct
8 Correct 7 ms 10316 KB Output is correct
9 Correct 137 ms 43792 KB Output is correct
10 Correct 186 ms 55108 KB Output is correct
11 Correct 180 ms 53000 KB Output is correct
12 Correct 205 ms 56332 KB Output is correct
13 Correct 170 ms 61196 KB Output is correct
14 Correct 196 ms 42268 KB Output is correct
15 Correct 475 ms 58380 KB Output is correct
16 Correct 504 ms 52728 KB Output is correct
17 Correct 456 ms 65040 KB Output is correct
18 Correct 495 ms 61200 KB Output is correct
19 Correct 110 ms 22436 KB Output is correct
20 Correct 542 ms 57116 KB Output is correct
21 Correct 462 ms 53428 KB Output is correct
22 Correct 508 ms 59520 KB Output is correct
23 Correct 514 ms 60764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9804 KB Output is correct
2 Correct 6 ms 9804 KB Output is correct
3 Correct 212 ms 45116 KB Output is correct
4 Correct 283 ms 45808 KB Output is correct
5 Correct 233 ms 45924 KB Output is correct
6 Correct 217 ms 45696 KB Output is correct
7 Correct 228 ms 45544 KB Output is correct
8 Correct 215 ms 44980 KB Output is correct
9 Correct 235 ms 45336 KB Output is correct
10 Correct 216 ms 44804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9804 KB Output is correct
2 Correct 6 ms 9804 KB Output is correct
3 Correct 5 ms 9808 KB Output is correct
4 Correct 5 ms 10060 KB Output is correct
5 Correct 6 ms 10188 KB Output is correct
6 Correct 8 ms 10188 KB Output is correct
7 Correct 7 ms 10220 KB Output is correct
8 Correct 7 ms 10316 KB Output is correct
9 Correct 5 ms 9796 KB Output is correct
10 Correct 6 ms 10092 KB Output is correct
11 Correct 7 ms 10344 KB Output is correct
12 Correct 6 ms 10088 KB Output is correct
13 Correct 8 ms 10084 KB Output is correct
14 Correct 7 ms 10108 KB Output is correct
15 Correct 6 ms 10212 KB Output is correct
16 Correct 6 ms 10220 KB Output is correct
17 Correct 6 ms 10316 KB Output is correct
18 Correct 6 ms 10188 KB Output is correct
19 Correct 6 ms 10052 KB Output is correct
20 Correct 7 ms 10212 KB Output is correct
21 Correct 6 ms 10060 KB Output is correct
22 Correct 6 ms 10092 KB Output is correct
23 Correct 6 ms 10108 KB Output is correct
24 Correct 6 ms 10216 KB Output is correct
25 Correct 6 ms 10236 KB Output is correct
26 Correct 7 ms 10316 KB Output is correct
27 Correct 8 ms 10316 KB Output is correct
28 Correct 6 ms 10188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9796 KB Output is correct
2 Correct 5 ms 9804 KB Output is correct
3 Correct 6 ms 9804 KB Output is correct
4 Correct 5 ms 9808 KB Output is correct
5 Correct 5 ms 10060 KB Output is correct
6 Correct 6 ms 10188 KB Output is correct
7 Correct 8 ms 10188 KB Output is correct
8 Correct 7 ms 10220 KB Output is correct
9 Correct 7 ms 10316 KB Output is correct
10 Correct 137 ms 43792 KB Output is correct
11 Correct 186 ms 55108 KB Output is correct
12 Correct 180 ms 53000 KB Output is correct
13 Correct 205 ms 56332 KB Output is correct
14 Correct 170 ms 61196 KB Output is correct
15 Correct 6 ms 10092 KB Output is correct
16 Correct 7 ms 10344 KB Output is correct
17 Correct 6 ms 10088 KB Output is correct
18 Correct 8 ms 10084 KB Output is correct
19 Correct 7 ms 10108 KB Output is correct
20 Correct 6 ms 10212 KB Output is correct
21 Correct 6 ms 10220 KB Output is correct
22 Correct 6 ms 10316 KB Output is correct
23 Correct 6 ms 10188 KB Output is correct
24 Correct 6 ms 10052 KB Output is correct
25 Correct 7 ms 10212 KB Output is correct
26 Correct 6 ms 10060 KB Output is correct
27 Correct 6 ms 10092 KB Output is correct
28 Correct 6 ms 10108 KB Output is correct
29 Correct 6 ms 10216 KB Output is correct
30 Correct 6 ms 10236 KB Output is correct
31 Correct 7 ms 10316 KB Output is correct
32 Correct 8 ms 10316 KB Output is correct
33 Correct 6 ms 10188 KB Output is correct
34 Correct 24 ms 14080 KB Output is correct
35 Correct 179 ms 54536 KB Output is correct
36 Correct 204 ms 47372 KB Output is correct
37 Correct 166 ms 42128 KB Output is correct
38 Correct 176 ms 40440 KB Output is correct
39 Correct 163 ms 39336 KB Output is correct
40 Correct 146 ms 37288 KB Output is correct
41 Correct 185 ms 49704 KB Output is correct
42 Correct 181 ms 52720 KB Output is correct
43 Correct 160 ms 56184 KB Output is correct
44 Correct 169 ms 40836 KB Output is correct
45 Correct 159 ms 39440 KB Output is correct
46 Correct 176 ms 44656 KB Output is correct
47 Correct 164 ms 39704 KB Output is correct
48 Correct 152 ms 40084 KB Output is correct
49 Correct 77 ms 23224 KB Output is correct
50 Correct 64 ms 21156 KB Output is correct
51 Correct 126 ms 34552 KB Output is correct
52 Correct 238 ms 49876 KB Output is correct
53 Correct 208 ms 47388 KB Output is correct
54 Correct 299 ms 57648 KB Output is correct
55 Correct 169 ms 59956 KB Output is correct
56 Correct 209 ms 46220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9804 KB Output is correct
2 Correct 6 ms 9804 KB Output is correct
3 Correct 5 ms 9808 KB Output is correct
4 Correct 5 ms 10060 KB Output is correct
5 Correct 6 ms 10188 KB Output is correct
6 Correct 8 ms 10188 KB Output is correct
7 Correct 7 ms 10220 KB Output is correct
8 Correct 7 ms 10316 KB Output is correct
9 Correct 137 ms 43792 KB Output is correct
10 Correct 186 ms 55108 KB Output is correct
11 Correct 180 ms 53000 KB Output is correct
12 Correct 205 ms 56332 KB Output is correct
13 Correct 170 ms 61196 KB Output is correct
14 Correct 196 ms 42268 KB Output is correct
15 Correct 475 ms 58380 KB Output is correct
16 Correct 504 ms 52728 KB Output is correct
17 Correct 456 ms 65040 KB Output is correct
18 Correct 495 ms 61200 KB Output is correct
19 Correct 212 ms 45116 KB Output is correct
20 Correct 283 ms 45808 KB Output is correct
21 Correct 233 ms 45924 KB Output is correct
22 Correct 217 ms 45696 KB Output is correct
23 Correct 228 ms 45544 KB Output is correct
24 Correct 215 ms 44980 KB Output is correct
25 Correct 235 ms 45336 KB Output is correct
26 Correct 216 ms 44804 KB Output is correct
27 Correct 6 ms 10092 KB Output is correct
28 Correct 7 ms 10344 KB Output is correct
29 Correct 6 ms 10088 KB Output is correct
30 Correct 8 ms 10084 KB Output is correct
31 Correct 7 ms 10108 KB Output is correct
32 Correct 6 ms 10212 KB Output is correct
33 Correct 6 ms 10220 KB Output is correct
34 Correct 6 ms 10316 KB Output is correct
35 Correct 6 ms 10188 KB Output is correct
36 Correct 24 ms 14080 KB Output is correct
37 Correct 179 ms 54536 KB Output is correct
38 Correct 204 ms 47372 KB Output is correct
39 Correct 166 ms 42128 KB Output is correct
40 Correct 176 ms 40440 KB Output is correct
41 Correct 163 ms 39336 KB Output is correct
42 Correct 146 ms 37288 KB Output is correct
43 Correct 185 ms 49704 KB Output is correct
44 Correct 181 ms 52720 KB Output is correct
45 Correct 160 ms 56184 KB Output is correct
46 Correct 169 ms 40836 KB Output is correct
47 Correct 28 ms 14304 KB Output is correct
48 Correct 485 ms 53496 KB Output is correct
49 Correct 490 ms 49140 KB Output is correct
50 Correct 491 ms 46284 KB Output is correct
51 Correct 452 ms 45628 KB Output is correct
52 Correct 391 ms 42920 KB Output is correct
53 Correct 270 ms 36228 KB Output is correct
54 Correct 516 ms 52364 KB Output is correct
55 Correct 491 ms 55008 KB Output is correct
56 Correct 440 ms 62080 KB Output is correct
57 Correct 393 ms 46392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9796 KB Output is correct
2 Correct 5 ms 9804 KB Output is correct
3 Correct 6 ms 9804 KB Output is correct
4 Correct 5 ms 9808 KB Output is correct
5 Correct 5 ms 10060 KB Output is correct
6 Correct 6 ms 10188 KB Output is correct
7 Correct 8 ms 10188 KB Output is correct
8 Correct 7 ms 10220 KB Output is correct
9 Correct 7 ms 10316 KB Output is correct
10 Correct 137 ms 43792 KB Output is correct
11 Correct 186 ms 55108 KB Output is correct
12 Correct 180 ms 53000 KB Output is correct
13 Correct 205 ms 56332 KB Output is correct
14 Correct 170 ms 61196 KB Output is correct
15 Correct 196 ms 42268 KB Output is correct
16 Correct 475 ms 58380 KB Output is correct
17 Correct 504 ms 52728 KB Output is correct
18 Correct 456 ms 65040 KB Output is correct
19 Correct 495 ms 61200 KB Output is correct
20 Correct 212 ms 45116 KB Output is correct
21 Correct 283 ms 45808 KB Output is correct
22 Correct 233 ms 45924 KB Output is correct
23 Correct 217 ms 45696 KB Output is correct
24 Correct 228 ms 45544 KB Output is correct
25 Correct 215 ms 44980 KB Output is correct
26 Correct 235 ms 45336 KB Output is correct
27 Correct 216 ms 44804 KB Output is correct
28 Correct 6 ms 10092 KB Output is correct
29 Correct 7 ms 10344 KB Output is correct
30 Correct 6 ms 10088 KB Output is correct
31 Correct 8 ms 10084 KB Output is correct
32 Correct 7 ms 10108 KB Output is correct
33 Correct 6 ms 10212 KB Output is correct
34 Correct 6 ms 10220 KB Output is correct
35 Correct 6 ms 10316 KB Output is correct
36 Correct 6 ms 10188 KB Output is correct
37 Correct 24 ms 14080 KB Output is correct
38 Correct 179 ms 54536 KB Output is correct
39 Correct 204 ms 47372 KB Output is correct
40 Correct 166 ms 42128 KB Output is correct
41 Correct 176 ms 40440 KB Output is correct
42 Correct 163 ms 39336 KB Output is correct
43 Correct 146 ms 37288 KB Output is correct
44 Correct 185 ms 49704 KB Output is correct
45 Correct 181 ms 52720 KB Output is correct
46 Correct 160 ms 56184 KB Output is correct
47 Correct 169 ms 40836 KB Output is correct
48 Correct 28 ms 14304 KB Output is correct
49 Correct 485 ms 53496 KB Output is correct
50 Correct 490 ms 49140 KB Output is correct
51 Correct 491 ms 46284 KB Output is correct
52 Correct 452 ms 45628 KB Output is correct
53 Correct 391 ms 42920 KB Output is correct
54 Correct 270 ms 36228 KB Output is correct
55 Correct 516 ms 52364 KB Output is correct
56 Correct 491 ms 55008 KB Output is correct
57 Correct 440 ms 62080 KB Output is correct
58 Correct 393 ms 46392 KB Output is correct
59 Correct 110 ms 22436 KB Output is correct
60 Correct 542 ms 57116 KB Output is correct
61 Correct 462 ms 53428 KB Output is correct
62 Correct 508 ms 59520 KB Output is correct
63 Correct 514 ms 60764 KB Output is correct
64 Correct 6 ms 10052 KB Output is correct
65 Correct 7 ms 10212 KB Output is correct
66 Correct 6 ms 10060 KB Output is correct
67 Correct 6 ms 10092 KB Output is correct
68 Correct 6 ms 10108 KB Output is correct
69 Correct 6 ms 10216 KB Output is correct
70 Correct 6 ms 10236 KB Output is correct
71 Correct 7 ms 10316 KB Output is correct
72 Correct 8 ms 10316 KB Output is correct
73 Correct 6 ms 10188 KB Output is correct
74 Correct 159 ms 39440 KB Output is correct
75 Correct 176 ms 44656 KB Output is correct
76 Correct 164 ms 39704 KB Output is correct
77 Correct 152 ms 40084 KB Output is correct
78 Correct 77 ms 23224 KB Output is correct
79 Correct 64 ms 21156 KB Output is correct
80 Correct 126 ms 34552 KB Output is correct
81 Correct 238 ms 49876 KB Output is correct
82 Correct 208 ms 47388 KB Output is correct
83 Correct 299 ms 57648 KB Output is correct
84 Correct 169 ms 59956 KB Output is correct
85 Correct 209 ms 46220 KB Output is correct
86 Correct 82 ms 21960 KB Output is correct
87 Correct 493 ms 48688 KB Output is correct
88 Correct 488 ms 48632 KB Output is correct
89 Correct 333 ms 43032 KB Output is correct
90 Correct 154 ms 26948 KB Output is correct
91 Correct 176 ms 28180 KB Output is correct
92 Correct 278 ms 39308 KB Output is correct
93 Correct 552 ms 54988 KB Output is correct
94 Correct 410 ms 51692 KB Output is correct
95 Correct 640 ms 60932 KB Output is correct
96 Correct 486 ms 56444 KB Output is correct
97 Correct 380 ms 48560 KB Output is correct