Submission #394291

# Submission time Handle Problem Language Result Execution time Memory
394291 2021-04-26T10:58:01 Z thuanqvbn03 Swapping Cities (APIO20_swap) C++17
100 / 100
239 ms 17056 KB
//Flower_On_Stone
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 200005;

struct Comp
{
    int endPoint[2], parent, size, val;
} comp[MAX_N];

int wParent[MAX_N], depth[MAX_N];
vector<int> adj[MAX_N];

int findParent(int node)
{
    while (comp[node].parent != -1)
    {
        node = comp[node].parent;
    }
    return node;
}

void join(int u, int v, int w)
{
    int x = findParent(u), y = findParent(v);
    if (x == y)
    {
        if (comp[x].val < 0)
        {
            comp[x].val = w;
        }
        return;
    }
    if (comp[x].size < comp[y].size)
    {
        swap(x, y);
        swap(u, v);
    }
    comp[x].size += comp[y].size;
    comp[y].parent = x;
    wParent[y] = w;
    adj[x].push_back(y);
    if (comp[x].val < 0 && comp[y].val < 0 && (u == comp[x].endPoint[0] || u == comp[x].endPoint[1]) && (v == comp[y].endPoint[0] || v == comp[y].endPoint[1]))
    {
        if (u == comp[x].endPoint[0])
        {
            comp[x].endPoint[0] = comp[x].endPoint[1];
        }
        comp[x].endPoint[1] = (v == comp[y].endPoint[0] ? comp[y].endPoint[1] : comp[y].endPoint[0]);
    }
    else if (comp[x].val < 0)
    {
        comp[x].val = w;
    }
}

void dfs(int node)
{
    for (auto &u : adj[node])
    {
        depth[u] = depth[node] + 1;
        dfs(u);
    }
}

void init(int numNode, int numEdge, vector<int> u, vector<int> v, vector<int> w)
{
    vector<int> index(numEdge);
    iota(index.begin(), index.end(), 0);
    sort(index.begin(), index.end(), [&](int x, int y) { return w[x] < w[y]; });
    for (int i = 0; i < numNode; i++)
    {
        comp[i].endPoint[0] = comp[i].endPoint[1] = i;
        comp[i].parent = comp[i].val = -1;
        comp[i].size = 1;
    }
    for (auto &id : index)
    {
        join(u[id], v[id], w[id]);
    }
    for (int node = 0; node < numNode; node++)
    {
        if (comp[node].parent < 0)
        {
            dfs(node);
        }
    }
}

int getMinimumFuelCapacity(int u, int v)
{
    if (findParent(u) != findParent(v))
    {
        return -1;
    }
    int resuft = -1;
    while (u != v)
    {
        if (depth[u] < depth[v])
        {
            swap(u, v);
        }
        resuft = max(resuft, wParent[u]);
        u = comp[u].parent;
    }
    while (u >= 0 && comp[u].val < 0)
    {
        resuft = max(resuft, wParent[u]);
        u = comp[u].parent;
    }
    return (u < 0 ? -1 : max(resuft, comp[u].val));
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 3 ms 5068 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 4 ms 5012 KB Output is correct
9 Correct 71 ms 10692 KB Output is correct
10 Correct 73 ms 11760 KB Output is correct
11 Correct 64 ms 11768 KB Output is correct
12 Correct 68 ms 12072 KB Output is correct
13 Correct 57 ms 11300 KB Output is correct
14 Correct 64 ms 10816 KB Output is correct
15 Correct 203 ms 13464 KB Output is correct
16 Correct 179 ms 13252 KB Output is correct
17 Correct 171 ms 13696 KB Output is correct
18 Correct 156 ms 12988 KB Output is correct
19 Correct 81 ms 9796 KB Output is correct
20 Correct 166 ms 14416 KB Output is correct
21 Correct 166 ms 14504 KB Output is correct
22 Correct 185 ms 14948 KB Output is correct
23 Correct 132 ms 14108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 131 ms 13292 KB Output is correct
4 Correct 120 ms 13420 KB Output is correct
5 Correct 131 ms 13588 KB Output is correct
6 Correct 130 ms 13372 KB Output is correct
7 Correct 143 ms 13572 KB Output is correct
8 Correct 118 ms 13308 KB Output is correct
9 Correct 120 ms 13376 KB Output is correct
10 Correct 132 ms 13336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 3 ms 5068 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 4 ms 5012 KB Output is correct
9 Correct 4 ms 4956 KB Output is correct
10 Correct 4 ms 5068 KB Output is correct
11 Correct 4 ms 5068 KB Output is correct
12 Correct 4 ms 5068 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 4 ms 5068 KB Output is correct
15 Correct 3 ms 5068 KB Output is correct
16 Correct 4 ms 5068 KB Output is correct
17 Correct 4 ms 5020 KB Output is correct
18 Correct 4 ms 5072 KB Output is correct
19 Correct 4 ms 5068 KB Output is correct
20 Correct 4 ms 5068 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 4 ms 5012 KB Output is correct
23 Correct 3 ms 5068 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 4 ms 5068 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4956 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 3 ms 5068 KB Output is correct
8 Correct 4 ms 5068 KB Output is correct
9 Correct 4 ms 5012 KB Output is correct
10 Correct 71 ms 10692 KB Output is correct
11 Correct 73 ms 11760 KB Output is correct
12 Correct 64 ms 11768 KB Output is correct
13 Correct 68 ms 12072 KB Output is correct
14 Correct 57 ms 11300 KB Output is correct
15 Correct 4 ms 5068 KB Output is correct
16 Correct 4 ms 5068 KB Output is correct
17 Correct 4 ms 5068 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 4 ms 5068 KB Output is correct
20 Correct 3 ms 5068 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 4 ms 5020 KB Output is correct
23 Correct 4 ms 5072 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 4 ms 5012 KB Output is correct
28 Correct 3 ms 5068 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 4 ms 5068 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 4 ms 5068 KB Output is correct
33 Correct 4 ms 5068 KB Output is correct
34 Correct 11 ms 6092 KB Output is correct
35 Correct 69 ms 12812 KB Output is correct
36 Correct 69 ms 12732 KB Output is correct
37 Correct 66 ms 12740 KB Output is correct
38 Correct 69 ms 12688 KB Output is correct
39 Correct 66 ms 12584 KB Output is correct
40 Correct 58 ms 11924 KB Output is correct
41 Correct 70 ms 12836 KB Output is correct
42 Correct 71 ms 12716 KB Output is correct
43 Correct 54 ms 11900 KB Output is correct
44 Correct 76 ms 12672 KB Output is correct
45 Correct 103 ms 13052 KB Output is correct
46 Correct 73 ms 12812 KB Output is correct
47 Correct 65 ms 12744 KB Output is correct
48 Correct 69 ms 12836 KB Output is correct
49 Correct 93 ms 10284 KB Output is correct
50 Correct 62 ms 9540 KB Output is correct
51 Correct 77 ms 11988 KB Output is correct
52 Correct 108 ms 14424 KB Output is correct
53 Correct 119 ms 14916 KB Output is correct
54 Correct 139 ms 15584 KB Output is correct
55 Correct 54 ms 12084 KB Output is correct
56 Correct 112 ms 14548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 3 ms 5068 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 4 ms 5012 KB Output is correct
9 Correct 71 ms 10692 KB Output is correct
10 Correct 73 ms 11760 KB Output is correct
11 Correct 64 ms 11768 KB Output is correct
12 Correct 68 ms 12072 KB Output is correct
13 Correct 57 ms 11300 KB Output is correct
14 Correct 64 ms 10816 KB Output is correct
15 Correct 203 ms 13464 KB Output is correct
16 Correct 179 ms 13252 KB Output is correct
17 Correct 171 ms 13696 KB Output is correct
18 Correct 156 ms 12988 KB Output is correct
19 Correct 131 ms 13292 KB Output is correct
20 Correct 120 ms 13420 KB Output is correct
21 Correct 131 ms 13588 KB Output is correct
22 Correct 130 ms 13372 KB Output is correct
23 Correct 143 ms 13572 KB Output is correct
24 Correct 118 ms 13308 KB Output is correct
25 Correct 120 ms 13376 KB Output is correct
26 Correct 132 ms 13336 KB Output is correct
27 Correct 4 ms 5068 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 3 ms 4940 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 3 ms 5068 KB Output is correct
33 Correct 4 ms 5068 KB Output is correct
34 Correct 4 ms 5020 KB Output is correct
35 Correct 4 ms 5072 KB Output is correct
36 Correct 11 ms 6092 KB Output is correct
37 Correct 69 ms 12812 KB Output is correct
38 Correct 69 ms 12732 KB Output is correct
39 Correct 66 ms 12740 KB Output is correct
40 Correct 69 ms 12688 KB Output is correct
41 Correct 66 ms 12584 KB Output is correct
42 Correct 58 ms 11924 KB Output is correct
43 Correct 70 ms 12836 KB Output is correct
44 Correct 71 ms 12716 KB Output is correct
45 Correct 54 ms 11900 KB Output is correct
46 Correct 76 ms 12672 KB Output is correct
47 Correct 24 ms 6548 KB Output is correct
48 Correct 183 ms 14964 KB Output is correct
49 Correct 165 ms 15016 KB Output is correct
50 Correct 214 ms 14892 KB Output is correct
51 Correct 162 ms 15076 KB Output is correct
52 Correct 155 ms 14588 KB Output is correct
53 Correct 152 ms 13792 KB Output is correct
54 Correct 173 ms 15540 KB Output is correct
55 Correct 184 ms 14960 KB Output is correct
56 Correct 142 ms 14208 KB Output is correct
57 Correct 174 ms 15368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4956 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 3 ms 5068 KB Output is correct
8 Correct 4 ms 5068 KB Output is correct
9 Correct 4 ms 5012 KB Output is correct
10 Correct 71 ms 10692 KB Output is correct
11 Correct 73 ms 11760 KB Output is correct
12 Correct 64 ms 11768 KB Output is correct
13 Correct 68 ms 12072 KB Output is correct
14 Correct 57 ms 11300 KB Output is correct
15 Correct 64 ms 10816 KB Output is correct
16 Correct 203 ms 13464 KB Output is correct
17 Correct 179 ms 13252 KB Output is correct
18 Correct 171 ms 13696 KB Output is correct
19 Correct 156 ms 12988 KB Output is correct
20 Correct 131 ms 13292 KB Output is correct
21 Correct 120 ms 13420 KB Output is correct
22 Correct 131 ms 13588 KB Output is correct
23 Correct 130 ms 13372 KB Output is correct
24 Correct 143 ms 13572 KB Output is correct
25 Correct 118 ms 13308 KB Output is correct
26 Correct 120 ms 13376 KB Output is correct
27 Correct 132 ms 13336 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 4 ms 5068 KB Output is correct
31 Correct 3 ms 4940 KB Output is correct
32 Correct 4 ms 5068 KB Output is correct
33 Correct 3 ms 5068 KB Output is correct
34 Correct 4 ms 5068 KB Output is correct
35 Correct 4 ms 5020 KB Output is correct
36 Correct 4 ms 5072 KB Output is correct
37 Correct 11 ms 6092 KB Output is correct
38 Correct 69 ms 12812 KB Output is correct
39 Correct 69 ms 12732 KB Output is correct
40 Correct 66 ms 12740 KB Output is correct
41 Correct 69 ms 12688 KB Output is correct
42 Correct 66 ms 12584 KB Output is correct
43 Correct 58 ms 11924 KB Output is correct
44 Correct 70 ms 12836 KB Output is correct
45 Correct 71 ms 12716 KB Output is correct
46 Correct 54 ms 11900 KB Output is correct
47 Correct 76 ms 12672 KB Output is correct
48 Correct 24 ms 6548 KB Output is correct
49 Correct 183 ms 14964 KB Output is correct
50 Correct 165 ms 15016 KB Output is correct
51 Correct 214 ms 14892 KB Output is correct
52 Correct 162 ms 15076 KB Output is correct
53 Correct 155 ms 14588 KB Output is correct
54 Correct 152 ms 13792 KB Output is correct
55 Correct 173 ms 15540 KB Output is correct
56 Correct 184 ms 14960 KB Output is correct
57 Correct 142 ms 14208 KB Output is correct
58 Correct 174 ms 15368 KB Output is correct
59 Correct 81 ms 9796 KB Output is correct
60 Correct 166 ms 14416 KB Output is correct
61 Correct 166 ms 14504 KB Output is correct
62 Correct 185 ms 14948 KB Output is correct
63 Correct 132 ms 14108 KB Output is correct
64 Correct 4 ms 5068 KB Output is correct
65 Correct 4 ms 5068 KB Output is correct
66 Correct 4 ms 5068 KB Output is correct
67 Correct 4 ms 5012 KB Output is correct
68 Correct 3 ms 5068 KB Output is correct
69 Correct 4 ms 5068 KB Output is correct
70 Correct 4 ms 5068 KB Output is correct
71 Correct 4 ms 5068 KB Output is correct
72 Correct 4 ms 5068 KB Output is correct
73 Correct 4 ms 5068 KB Output is correct
74 Correct 103 ms 13052 KB Output is correct
75 Correct 73 ms 12812 KB Output is correct
76 Correct 65 ms 12744 KB Output is correct
77 Correct 69 ms 12836 KB Output is correct
78 Correct 93 ms 10284 KB Output is correct
79 Correct 62 ms 9540 KB Output is correct
80 Correct 77 ms 11988 KB Output is correct
81 Correct 108 ms 14424 KB Output is correct
82 Correct 119 ms 14916 KB Output is correct
83 Correct 139 ms 15584 KB Output is correct
84 Correct 54 ms 12084 KB Output is correct
85 Correct 112 ms 14548 KB Output is correct
86 Correct 66 ms 8908 KB Output is correct
87 Correct 179 ms 15008 KB Output is correct
88 Correct 172 ms 14968 KB Output is correct
89 Correct 150 ms 14532 KB Output is correct
90 Correct 166 ms 12172 KB Output is correct
91 Correct 154 ms 12260 KB Output is correct
92 Correct 158 ms 14232 KB Output is correct
93 Correct 207 ms 16268 KB Output is correct
94 Correct 237 ms 16528 KB Output is correct
95 Correct 239 ms 17056 KB Output is correct
96 Correct 139 ms 14308 KB Output is correct
97 Correct 178 ms 15804 KB Output is correct