Submission #1064686

# Submission time Handle Problem Language Result Execution time Memory
1064686 2024-08-18T16:52:49 Z sammyuri Swapping Cities (APIO20_swap) C++17
100 / 100
304 ms 39364 KB
#include "swap.h"

#include <bits/stdc++.h>
using namespace std;
/*
a component becomes valid as soon as
there is either a loop or a node with degree 3+
if it is just a chain, clearly impossible
use persistent DSU and find for each node, the minimum time
until it is part of a valid component
*/
int n, m;
int curtime = 0;
const int MAXN = 1e5 + 5;
map<int, int> dsu[MAXN];
int sz[MAXN];
int mintime[MAXN];
bool good[MAXN];
int deg[MAXN];
vector<int> active_nodes[MAXN];
const int INF = 2e9;

int find(int node, int time) {
    auto it = dsu[node].upper_bound(time);
    it --;
    if ((*it).second == node) return node;
    return find((*it).second, time);
}


void init(int N, int M,
    std::vector<int> U, std::vector<int> V, std::vector<int> W) {
    n = N, m = M;
    curtime = 0;
    for (int i = 0; i < n; i ++) {
        mintime[i] = INF;
        dsu[i][0] = i;
        active_nodes[i].push_back(i);
        sz[i] = 1;
        deg[i] = 0; good[i] = false;
    }
    vector<pair<int, pair<int, int>>> edges;
    for (int i = 0; i < M; i ++) {
        edges.push_back({W[i], {U[i], V[i]}});
    }
    sort(edges.begin(), edges.end());
    for (auto ee : edges) {
        int a = ee.second.first, b = ee.second.second;
        curtime = ee.first;
        bool madegood = false;
        // check if made good
        deg[a] ++; deg[b] ++;
        if (deg[a] >= 3 || deg[b] >= 3)
            madegood = true;
        a = find(a, curtime); b = find(b, curtime);
        if (a == b)
            madegood = true;
        // unite components
        if (a != b) {
            if (sz[a] > sz[b])
                swap(a, b);
            sz[b] += sz[a];
            for (auto aa : active_nodes[a])
                active_nodes[b].push_back(aa);
            active_nodes[a].clear();
            dsu[a][curtime] = b;
        }
        good[b] = good[b] | good[a] | madegood;
        if (good[b]) {
            for (auto aa : active_nodes[b])
                mintime[aa] = curtime;
            active_nodes[b].clear();
        }
    }
    // for (int i = 0; i < n; i ++)
    //     cout << mintime[i] << " "; cout << endl;
}

int getMinimumFuelCapacity(int X, int Y) {
    // lower bound
    int req = min(mintime[X], mintime[Y]);
    // find smallest time until they are in same component
    curtime = 1;
    while (X != Y) {
        auto itx = dsu[X].lower_bound(curtime),
        ity = dsu[Y].lower_bound(curtime);
        int nextmin = 2e9;
        if (itx != dsu[X].end() && X != (*itx).second)
            nextmin = min(nextmin, (*itx).first);
        if (ity != dsu[Y].end() && Y != (*ity).second)
            nextmin = min(nextmin, (*ity).first);
        // cout << X << " " << Y << " " << curtime << endl;
        // cout << nextmin << endl;
        curtime = nextmin;
        if (itx != dsu[X].end() && (*itx).first == curtime)
            X = (*itx).second;
        if (ity != dsu[Y].end() && (*ity).first == curtime)
            Y = (*ity).second;
    }
    int ans = max(req, curtime);
    if (ans == INF)
        return -1;
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 3 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 3 ms 8796 KB Output is correct
9 Correct 51 ms 26048 KB Output is correct
10 Correct 59 ms 29632 KB Output is correct
11 Correct 57 ms 29384 KB Output is correct
12 Correct 61 ms 30668 KB Output is correct
13 Correct 55 ms 27748 KB Output is correct
14 Correct 59 ms 26300 KB Output is correct
15 Correct 157 ms 33996 KB Output is correct
16 Correct 158 ms 33168 KB Output is correct
17 Correct 156 ms 34508 KB Output is correct
18 Correct 100 ms 31436 KB Output is correct
19 Correct 81 ms 16984 KB Output is correct
20 Correct 156 ms 33992 KB Output is correct
21 Correct 153 ms 33588 KB Output is correct
22 Correct 172 ms 35192 KB Output is correct
23 Correct 110 ms 31892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 93 ms 29460 KB Output is correct
4 Correct 98 ms 30080 KB Output is correct
5 Correct 90 ms 30200 KB Output is correct
6 Correct 94 ms 30112 KB Output is correct
7 Correct 101 ms 30228 KB Output is correct
8 Correct 90 ms 29512 KB Output is correct
9 Correct 91 ms 29904 KB Output is correct
10 Correct 89 ms 29512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 3 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 3 ms 8796 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 2 ms 8792 KB Output is correct
12 Correct 2 ms 8792 KB Output is correct
13 Correct 2 ms 8796 KB Output is correct
14 Correct 2 ms 8792 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
17 Correct 2 ms 8796 KB Output is correct
18 Correct 2 ms 8780 KB Output is correct
19 Correct 2 ms 8796 KB Output is correct
20 Correct 2 ms 8796 KB Output is correct
21 Correct 2 ms 8796 KB Output is correct
22 Correct 2 ms 8796 KB Output is correct
23 Correct 3 ms 8796 KB Output is correct
24 Correct 2 ms 8796 KB Output is correct
25 Correct 2 ms 8796 KB Output is correct
26 Correct 2 ms 8796 KB Output is correct
27 Correct 2 ms 8796 KB Output is correct
28 Correct 2 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 3 ms 8540 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 3 ms 8796 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 3 ms 8796 KB Output is correct
10 Correct 51 ms 26048 KB Output is correct
11 Correct 59 ms 29632 KB Output is correct
12 Correct 57 ms 29384 KB Output is correct
13 Correct 61 ms 30668 KB Output is correct
14 Correct 55 ms 27748 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 2 ms 8792 KB Output is correct
17 Correct 2 ms 8792 KB Output is correct
18 Correct 2 ms 8796 KB Output is correct
19 Correct 2 ms 8792 KB Output is correct
20 Correct 2 ms 8796 KB Output is correct
21 Correct 2 ms 8796 KB Output is correct
22 Correct 2 ms 8796 KB Output is correct
23 Correct 2 ms 8780 KB Output is correct
24 Correct 2 ms 8796 KB Output is correct
25 Correct 2 ms 8796 KB Output is correct
26 Correct 2 ms 8796 KB Output is correct
27 Correct 2 ms 8796 KB Output is correct
28 Correct 3 ms 8796 KB Output is correct
29 Correct 2 ms 8796 KB Output is correct
30 Correct 2 ms 8796 KB Output is correct
31 Correct 2 ms 8796 KB Output is correct
32 Correct 2 ms 8796 KB Output is correct
33 Correct 2 ms 8796 KB Output is correct
34 Correct 10 ms 11040 KB Output is correct
35 Correct 71 ms 29196 KB Output is correct
36 Correct 61 ms 28880 KB Output is correct
37 Correct 66 ms 28108 KB Output is correct
38 Correct 64 ms 27440 KB Output is correct
39 Correct 60 ms 26956 KB Output is correct
40 Correct 51 ms 25088 KB Output is correct
41 Correct 61 ms 30092 KB Output is correct
42 Correct 62 ms 30408 KB Output is correct
43 Correct 55 ms 26576 KB Output is correct
44 Correct 56 ms 27184 KB Output is correct
45 Correct 69 ms 26964 KB Output is correct
46 Correct 59 ms 28872 KB Output is correct
47 Correct 59 ms 27592 KB Output is correct
48 Correct 63 ms 26812 KB Output is correct
49 Correct 54 ms 19936 KB Output is correct
50 Correct 45 ms 16088 KB Output is correct
51 Correct 61 ms 24012 KB Output is correct
52 Correct 97 ms 33104 KB Output is correct
53 Correct 89 ms 31936 KB Output is correct
54 Correct 130 ms 35524 KB Output is correct
55 Correct 50 ms 26480 KB Output is correct
56 Correct 166 ms 31092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 2 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 3 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 3 ms 8796 KB Output is correct
9 Correct 51 ms 26048 KB Output is correct
10 Correct 59 ms 29632 KB Output is correct
11 Correct 57 ms 29384 KB Output is correct
12 Correct 61 ms 30668 KB Output is correct
13 Correct 55 ms 27748 KB Output is correct
14 Correct 59 ms 26300 KB Output is correct
15 Correct 157 ms 33996 KB Output is correct
16 Correct 158 ms 33168 KB Output is correct
17 Correct 156 ms 34508 KB Output is correct
18 Correct 100 ms 31436 KB Output is correct
19 Correct 93 ms 29460 KB Output is correct
20 Correct 98 ms 30080 KB Output is correct
21 Correct 90 ms 30200 KB Output is correct
22 Correct 94 ms 30112 KB Output is correct
23 Correct 101 ms 30228 KB Output is correct
24 Correct 90 ms 29512 KB Output is correct
25 Correct 91 ms 29904 KB Output is correct
26 Correct 89 ms 29512 KB Output is correct
27 Correct 2 ms 8796 KB Output is correct
28 Correct 2 ms 8792 KB Output is correct
29 Correct 2 ms 8792 KB Output is correct
30 Correct 2 ms 8796 KB Output is correct
31 Correct 2 ms 8792 KB Output is correct
32 Correct 2 ms 8796 KB Output is correct
33 Correct 2 ms 8796 KB Output is correct
34 Correct 2 ms 8796 KB Output is correct
35 Correct 2 ms 8780 KB Output is correct
36 Correct 10 ms 11040 KB Output is correct
37 Correct 71 ms 29196 KB Output is correct
38 Correct 61 ms 28880 KB Output is correct
39 Correct 66 ms 28108 KB Output is correct
40 Correct 64 ms 27440 KB Output is correct
41 Correct 60 ms 26956 KB Output is correct
42 Correct 51 ms 25088 KB Output is correct
43 Correct 61 ms 30092 KB Output is correct
44 Correct 62 ms 30408 KB Output is correct
45 Correct 55 ms 26576 KB Output is correct
46 Correct 56 ms 27184 KB Output is correct
47 Correct 16 ms 11400 KB Output is correct
48 Correct 265 ms 33672 KB Output is correct
49 Correct 231 ms 32460 KB Output is correct
50 Correct 178 ms 32200 KB Output is correct
51 Correct 177 ms 31360 KB Output is correct
52 Correct 172 ms 29912 KB Output is correct
53 Correct 133 ms 26028 KB Output is correct
54 Correct 232 ms 34456 KB Output is correct
55 Correct 177 ms 34512 KB Output is correct
56 Correct 120 ms 30416 KB Output is correct
57 Correct 160 ms 31384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 3 ms 8540 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 3 ms 8796 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 3 ms 8796 KB Output is correct
10 Correct 51 ms 26048 KB Output is correct
11 Correct 59 ms 29632 KB Output is correct
12 Correct 57 ms 29384 KB Output is correct
13 Correct 61 ms 30668 KB Output is correct
14 Correct 55 ms 27748 KB Output is correct
15 Correct 59 ms 26300 KB Output is correct
16 Correct 157 ms 33996 KB Output is correct
17 Correct 158 ms 33168 KB Output is correct
18 Correct 156 ms 34508 KB Output is correct
19 Correct 100 ms 31436 KB Output is correct
20 Correct 93 ms 29460 KB Output is correct
21 Correct 98 ms 30080 KB Output is correct
22 Correct 90 ms 30200 KB Output is correct
23 Correct 94 ms 30112 KB Output is correct
24 Correct 101 ms 30228 KB Output is correct
25 Correct 90 ms 29512 KB Output is correct
26 Correct 91 ms 29904 KB Output is correct
27 Correct 89 ms 29512 KB Output is correct
28 Correct 2 ms 8796 KB Output is correct
29 Correct 2 ms 8792 KB Output is correct
30 Correct 2 ms 8792 KB Output is correct
31 Correct 2 ms 8796 KB Output is correct
32 Correct 2 ms 8792 KB Output is correct
33 Correct 2 ms 8796 KB Output is correct
34 Correct 2 ms 8796 KB Output is correct
35 Correct 2 ms 8796 KB Output is correct
36 Correct 2 ms 8780 KB Output is correct
37 Correct 10 ms 11040 KB Output is correct
38 Correct 71 ms 29196 KB Output is correct
39 Correct 61 ms 28880 KB Output is correct
40 Correct 66 ms 28108 KB Output is correct
41 Correct 64 ms 27440 KB Output is correct
42 Correct 60 ms 26956 KB Output is correct
43 Correct 51 ms 25088 KB Output is correct
44 Correct 61 ms 30092 KB Output is correct
45 Correct 62 ms 30408 KB Output is correct
46 Correct 55 ms 26576 KB Output is correct
47 Correct 56 ms 27184 KB Output is correct
48 Correct 16 ms 11400 KB Output is correct
49 Correct 265 ms 33672 KB Output is correct
50 Correct 231 ms 32460 KB Output is correct
51 Correct 178 ms 32200 KB Output is correct
52 Correct 177 ms 31360 KB Output is correct
53 Correct 172 ms 29912 KB Output is correct
54 Correct 133 ms 26028 KB Output is correct
55 Correct 232 ms 34456 KB Output is correct
56 Correct 177 ms 34512 KB Output is correct
57 Correct 120 ms 30416 KB Output is correct
58 Correct 160 ms 31384 KB Output is correct
59 Correct 81 ms 16984 KB Output is correct
60 Correct 156 ms 33992 KB Output is correct
61 Correct 153 ms 33588 KB Output is correct
62 Correct 172 ms 35192 KB Output is correct
63 Correct 110 ms 31892 KB Output is correct
64 Correct 2 ms 8796 KB Output is correct
65 Correct 2 ms 8796 KB Output is correct
66 Correct 2 ms 8796 KB Output is correct
67 Correct 2 ms 8796 KB Output is correct
68 Correct 3 ms 8796 KB Output is correct
69 Correct 2 ms 8796 KB Output is correct
70 Correct 2 ms 8796 KB Output is correct
71 Correct 2 ms 8796 KB Output is correct
72 Correct 2 ms 8796 KB Output is correct
73 Correct 2 ms 8796 KB Output is correct
74 Correct 69 ms 26964 KB Output is correct
75 Correct 59 ms 28872 KB Output is correct
76 Correct 59 ms 27592 KB Output is correct
77 Correct 63 ms 26812 KB Output is correct
78 Correct 54 ms 19936 KB Output is correct
79 Correct 45 ms 16088 KB Output is correct
80 Correct 61 ms 24012 KB Output is correct
81 Correct 97 ms 33104 KB Output is correct
82 Correct 89 ms 31936 KB Output is correct
83 Correct 130 ms 35524 KB Output is correct
84 Correct 50 ms 26480 KB Output is correct
85 Correct 166 ms 31092 KB Output is correct
86 Correct 53 ms 16332 KB Output is correct
87 Correct 171 ms 32560 KB Output is correct
88 Correct 177 ms 32376 KB Output is correct
89 Correct 124 ms 29640 KB Output is correct
90 Correct 97 ms 22204 KB Output is correct
91 Correct 111 ms 23920 KB Output is correct
92 Correct 121 ms 27624 KB Output is correct
93 Correct 194 ms 37060 KB Output is correct
94 Correct 239 ms 35528 KB Output is correct
95 Correct 304 ms 39364 KB Output is correct
96 Correct 107 ms 30412 KB Output is correct
97 Correct 212 ms 32860 KB Output is correct