Submission #567848

# Submission time Handle Problem Language Result Execution time Memory
567848 2022-05-24T09:20:45 Z Tien_Noob Swapping Cities (APIO20_swap) C++17
100 / 100
349 ms 44540 KB
//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#include <swap.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 1e5, M = 2e5, K = N + M, cbit = 18, oo = 1e9 + 1;
int f[K + 1][cbit + 1], id[M + 1];
int n, m;
int lab[K + 1], weight[K + 1], deg[K + 1], depth[K + 1];
bool able[K + 1];
vector<int> adj[K + 1];
int FindSet(int u)
{
    return lab[u] < 0 ? u : lab[u] = FindSet(lab[u]);
}
void unite(int u, int v, int w)
{
    bool ok = deg[u] > 1 | deg[v] > 1;
    ++deg[u];
    ++deg[v];
    u = FindSet(u);
    v = FindSet(v);
    if (u == v)
    {
        if (!able[u])
        {
            able[u] = true;
            weight[u] = w;
        }
        return ;
    }
    ++n;
    able[n] = able[u] | able[v] | ok;
    if (able[n])
    {
        weight[n] = w;
    }
    lab[u] = lab[v] = n;
    adj[n].push_back(u);
    adj[n].push_back(v);
    //cerr << n << ' ' << u << '\n';
    //cerr << n << ' ' << v << '\n';
}
void DFS(int u, int par)
{
    if (par != 0 && !able[u])
    {
        weight[u] = weight[par];
    }
    //cerr << u << ' ' << weight[u] << '\n';
    for (int v : adj[u])
    {
        //cerr << u << ' ' << v << '\n';
        depth[v] = depth[u] + 1;
        f[v][0] = u;
        for (int i = 1; i <= cbit; ++ i)
        {
            f[v][i] = f[f[v][i - 1]][i - 1];
        }
        DFS(v, u);
    }
}
int get(int u, int v)
{
    if (depth[v] < depth[u])
    {
        swap(u, v);
    }
    int k = depth[v] - depth[u];
    for (int i = cbit; i >= 0; -- i)
    {
        if ((k >> i) & 1)
        {
            v = f[v][i];
        }
    }
    if (u == v)
    {
        //cerr << u << '\n';
        return weight[u];
    }
    for (int i = cbit; i >= 0; -- i)
    {
        if (f[u][i] != f[v][i])
        {
            u = f[u][i];
            v = f[v][i];
        }
    }
    //cerr << f[u][0] << '\n';
    return weight[f[u][0]];
}
void init(int _n, int _m, vector<int> u, vector<int> v, vector<int> w)
{
    n = _n;
    m = _m;
    iota(id, id + m, 0);
    memset(lab, -1, sizeof(lab));
    memset(weight, -1, sizeof(weight));
    sort(id, id + m, [&](const int &a, const int &b)
         {
             return w[a] < w[b];
         });
    for (int i = 0; i < m; ++ i)
    {
        unite(u[id[i]] + 1, v[id[i]] + 1, w[id[i]]);
    }
    DFS(n, 0);
}
int getMinimumFuelCapacity(int X, int Y)
{
    return get(X + 1, Y + 1);
}
/*void read()
{
    init(5, 6, {0, 0, 1, 1, 1, 2}, {1, 2, 2, 3, 4, 3}, {4, 4, 1, 2, 10, 3});
    cout << getMinimumFuelCapacity(1, 2) << '\n';
    cout << getMinimumFuelCapacity(2, 4) << '\n';
    cout << getMinimumFuelCapacity(0, 1);

    init(3, 2, {0, 0}, {1, 2}, {5, 5});
    cout << getMinimumFuelCapacity(1, 2);
}
void solve()
{

}
int  main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        //freopen(TASK".OUT", "w", stdout);
    }
    int t = 1;
    bool typetest = false;
    if (typetest)
    {
        cin >> t;
    }
    for (int __ = 1; __ <= t; ++ __)
    {
        //cout << "Case " << __ << ": ";
        read();
        solve();
    }
}*/

Compilation message

swap.cpp: In function 'void unite(int, int, int)':
swap.cpp:20:22: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
   20 |     bool ok = deg[u] > 1 | deg[v] > 1;
      |               ~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9940 KB Output is correct
8 Correct 6 ms 9940 KB Output is correct
9 Correct 92 ms 27124 KB Output is correct
10 Correct 115 ms 30740 KB Output is correct
11 Correct 120 ms 30452 KB Output is correct
12 Correct 120 ms 31648 KB Output is correct
13 Correct 102 ms 34864 KB Output is correct
14 Correct 100 ms 27092 KB Output is correct
15 Correct 237 ms 32812 KB Output is correct
16 Correct 208 ms 32128 KB Output is correct
17 Correct 219 ms 33552 KB Output is correct
18 Correct 304 ms 36524 KB Output is correct
19 Correct 76 ms 16736 KB Output is correct
20 Correct 209 ms 34028 KB Output is correct
21 Correct 227 ms 33488 KB Output is correct
22 Correct 225 ms 34840 KB Output is correct
23 Correct 307 ms 37964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 236 ms 39468 KB Output is correct
4 Correct 231 ms 44540 KB Output is correct
5 Correct 295 ms 44064 KB Output is correct
6 Correct 231 ms 44428 KB Output is correct
7 Correct 241 ms 44488 KB Output is correct
8 Correct 256 ms 43132 KB Output is correct
9 Correct 247 ms 43980 KB Output is correct
10 Correct 283 ms 42908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9940 KB Output is correct
8 Correct 6 ms 9940 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9940 KB Output is correct
11 Correct 6 ms 9928 KB Output is correct
12 Correct 6 ms 9928 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 7 ms 9904 KB Output is correct
15 Correct 6 ms 9928 KB Output is correct
16 Correct 7 ms 9940 KB Output is correct
17 Correct 6 ms 9940 KB Output is correct
18 Correct 6 ms 9940 KB Output is correct
19 Correct 5 ms 9812 KB Output is correct
20 Correct 6 ms 9940 KB Output is correct
21 Correct 6 ms 9940 KB Output is correct
22 Correct 6 ms 9812 KB Output is correct
23 Correct 6 ms 9880 KB Output is correct
24 Correct 6 ms 9940 KB Output is correct
25 Correct 7 ms 9924 KB Output is correct
26 Correct 9 ms 9924 KB Output is correct
27 Correct 6 ms 9940 KB Output is correct
28 Correct 6 ms 9920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 6 ms 9940 KB Output is correct
9 Correct 6 ms 9940 KB Output is correct
10 Correct 92 ms 27124 KB Output is correct
11 Correct 115 ms 30740 KB Output is correct
12 Correct 120 ms 30452 KB Output is correct
13 Correct 120 ms 31648 KB Output is correct
14 Correct 102 ms 34864 KB Output is correct
15 Correct 5 ms 9940 KB Output is correct
16 Correct 6 ms 9928 KB Output is correct
17 Correct 6 ms 9928 KB Output is correct
18 Correct 6 ms 9812 KB Output is correct
19 Correct 7 ms 9904 KB Output is correct
20 Correct 6 ms 9928 KB Output is correct
21 Correct 7 ms 9940 KB Output is correct
22 Correct 6 ms 9940 KB Output is correct
23 Correct 6 ms 9940 KB Output is correct
24 Correct 5 ms 9812 KB Output is correct
25 Correct 6 ms 9940 KB Output is correct
26 Correct 6 ms 9940 KB Output is correct
27 Correct 6 ms 9812 KB Output is correct
28 Correct 6 ms 9880 KB Output is correct
29 Correct 6 ms 9940 KB Output is correct
30 Correct 7 ms 9924 KB Output is correct
31 Correct 9 ms 9924 KB Output is correct
32 Correct 6 ms 9940 KB Output is correct
33 Correct 6 ms 9920 KB Output is correct
34 Correct 19 ms 12880 KB Output is correct
35 Correct 132 ms 33564 KB Output is correct
36 Correct 123 ms 33552 KB Output is correct
37 Correct 117 ms 33484 KB Output is correct
38 Correct 119 ms 33220 KB Output is correct
39 Correct 126 ms 33096 KB Output is correct
40 Correct 110 ms 31080 KB Output is correct
41 Correct 119 ms 33728 KB Output is correct
42 Correct 116 ms 33508 KB Output is correct
43 Correct 98 ms 37564 KB Output is correct
44 Correct 119 ms 33848 KB Output is correct
45 Correct 141 ms 33508 KB Output is correct
46 Correct 119 ms 33484 KB Output is correct
47 Correct 120 ms 33680 KB Output is correct
48 Correct 120 ms 35000 KB Output is correct
49 Correct 66 ms 17060 KB Output is correct
50 Correct 57 ms 16820 KB Output is correct
51 Correct 104 ms 29424 KB Output is correct
52 Correct 167 ms 36744 KB Output is correct
53 Correct 163 ms 38364 KB Output is correct
54 Correct 162 ms 38356 KB Output is correct
55 Correct 95 ms 37276 KB Output is correct
56 Correct 159 ms 39164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9940 KB Output is correct
8 Correct 6 ms 9940 KB Output is correct
9 Correct 92 ms 27124 KB Output is correct
10 Correct 115 ms 30740 KB Output is correct
11 Correct 120 ms 30452 KB Output is correct
12 Correct 120 ms 31648 KB Output is correct
13 Correct 102 ms 34864 KB Output is correct
14 Correct 100 ms 27092 KB Output is correct
15 Correct 237 ms 32812 KB Output is correct
16 Correct 208 ms 32128 KB Output is correct
17 Correct 219 ms 33552 KB Output is correct
18 Correct 304 ms 36524 KB Output is correct
19 Correct 236 ms 39468 KB Output is correct
20 Correct 231 ms 44540 KB Output is correct
21 Correct 295 ms 44064 KB Output is correct
22 Correct 231 ms 44428 KB Output is correct
23 Correct 241 ms 44488 KB Output is correct
24 Correct 256 ms 43132 KB Output is correct
25 Correct 247 ms 43980 KB Output is correct
26 Correct 283 ms 42908 KB Output is correct
27 Correct 5 ms 9940 KB Output is correct
28 Correct 6 ms 9928 KB Output is correct
29 Correct 6 ms 9928 KB Output is correct
30 Correct 6 ms 9812 KB Output is correct
31 Correct 7 ms 9904 KB Output is correct
32 Correct 6 ms 9928 KB Output is correct
33 Correct 7 ms 9940 KB Output is correct
34 Correct 6 ms 9940 KB Output is correct
35 Correct 6 ms 9940 KB Output is correct
36 Correct 19 ms 12880 KB Output is correct
37 Correct 132 ms 33564 KB Output is correct
38 Correct 123 ms 33552 KB Output is correct
39 Correct 117 ms 33484 KB Output is correct
40 Correct 119 ms 33220 KB Output is correct
41 Correct 126 ms 33096 KB Output is correct
42 Correct 110 ms 31080 KB Output is correct
43 Correct 119 ms 33728 KB Output is correct
44 Correct 116 ms 33508 KB Output is correct
45 Correct 98 ms 37564 KB Output is correct
46 Correct 119 ms 33848 KB Output is correct
47 Correct 26 ms 13132 KB Output is correct
48 Correct 264 ms 38360 KB Output is correct
49 Correct 216 ms 38432 KB Output is correct
50 Correct 237 ms 38372 KB Output is correct
51 Correct 216 ms 38248 KB Output is correct
52 Correct 206 ms 36916 KB Output is correct
53 Correct 199 ms 31456 KB Output is correct
54 Correct 250 ms 39464 KB Output is correct
55 Correct 245 ms 38432 KB Output is correct
56 Correct 264 ms 41624 KB Output is correct
57 Correct 227 ms 39312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 6 ms 9940 KB Output is correct
9 Correct 6 ms 9940 KB Output is correct
10 Correct 92 ms 27124 KB Output is correct
11 Correct 115 ms 30740 KB Output is correct
12 Correct 120 ms 30452 KB Output is correct
13 Correct 120 ms 31648 KB Output is correct
14 Correct 102 ms 34864 KB Output is correct
15 Correct 100 ms 27092 KB Output is correct
16 Correct 237 ms 32812 KB Output is correct
17 Correct 208 ms 32128 KB Output is correct
18 Correct 219 ms 33552 KB Output is correct
19 Correct 304 ms 36524 KB Output is correct
20 Correct 236 ms 39468 KB Output is correct
21 Correct 231 ms 44540 KB Output is correct
22 Correct 295 ms 44064 KB Output is correct
23 Correct 231 ms 44428 KB Output is correct
24 Correct 241 ms 44488 KB Output is correct
25 Correct 256 ms 43132 KB Output is correct
26 Correct 247 ms 43980 KB Output is correct
27 Correct 283 ms 42908 KB Output is correct
28 Correct 5 ms 9940 KB Output is correct
29 Correct 6 ms 9928 KB Output is correct
30 Correct 6 ms 9928 KB Output is correct
31 Correct 6 ms 9812 KB Output is correct
32 Correct 7 ms 9904 KB Output is correct
33 Correct 6 ms 9928 KB Output is correct
34 Correct 7 ms 9940 KB Output is correct
35 Correct 6 ms 9940 KB Output is correct
36 Correct 6 ms 9940 KB Output is correct
37 Correct 19 ms 12880 KB Output is correct
38 Correct 132 ms 33564 KB Output is correct
39 Correct 123 ms 33552 KB Output is correct
40 Correct 117 ms 33484 KB Output is correct
41 Correct 119 ms 33220 KB Output is correct
42 Correct 126 ms 33096 KB Output is correct
43 Correct 110 ms 31080 KB Output is correct
44 Correct 119 ms 33728 KB Output is correct
45 Correct 116 ms 33508 KB Output is correct
46 Correct 98 ms 37564 KB Output is correct
47 Correct 119 ms 33848 KB Output is correct
48 Correct 26 ms 13132 KB Output is correct
49 Correct 264 ms 38360 KB Output is correct
50 Correct 216 ms 38432 KB Output is correct
51 Correct 237 ms 38372 KB Output is correct
52 Correct 216 ms 38248 KB Output is correct
53 Correct 206 ms 36916 KB Output is correct
54 Correct 199 ms 31456 KB Output is correct
55 Correct 250 ms 39464 KB Output is correct
56 Correct 245 ms 38432 KB Output is correct
57 Correct 264 ms 41624 KB Output is correct
58 Correct 227 ms 39312 KB Output is correct
59 Correct 76 ms 16736 KB Output is correct
60 Correct 209 ms 34028 KB Output is correct
61 Correct 227 ms 33488 KB Output is correct
62 Correct 225 ms 34840 KB Output is correct
63 Correct 307 ms 37964 KB Output is correct
64 Correct 5 ms 9812 KB Output is correct
65 Correct 6 ms 9940 KB Output is correct
66 Correct 6 ms 9940 KB Output is correct
67 Correct 6 ms 9812 KB Output is correct
68 Correct 6 ms 9880 KB Output is correct
69 Correct 6 ms 9940 KB Output is correct
70 Correct 7 ms 9924 KB Output is correct
71 Correct 9 ms 9924 KB Output is correct
72 Correct 6 ms 9940 KB Output is correct
73 Correct 6 ms 9920 KB Output is correct
74 Correct 141 ms 33508 KB Output is correct
75 Correct 119 ms 33484 KB Output is correct
76 Correct 120 ms 33680 KB Output is correct
77 Correct 120 ms 35000 KB Output is correct
78 Correct 66 ms 17060 KB Output is correct
79 Correct 57 ms 16820 KB Output is correct
80 Correct 104 ms 29424 KB Output is correct
81 Correct 167 ms 36744 KB Output is correct
82 Correct 163 ms 38364 KB Output is correct
83 Correct 162 ms 38356 KB Output is correct
84 Correct 95 ms 37276 KB Output is correct
85 Correct 159 ms 39164 KB Output is correct
86 Correct 68 ms 19752 KB Output is correct
87 Correct 229 ms 38440 KB Output is correct
88 Correct 229 ms 38384 KB Output is correct
89 Correct 244 ms 38568 KB Output is correct
90 Correct 141 ms 21540 KB Output is correct
91 Correct 184 ms 23484 KB Output is correct
92 Correct 219 ms 34504 KB Output is correct
93 Correct 263 ms 41512 KB Output is correct
94 Correct 268 ms 42656 KB Output is correct
95 Correct 270 ms 42172 KB Output is correct
96 Correct 349 ms 41904 KB Output is correct
97 Correct 264 ms 42308 KB Output is correct