Submission #568300

# Submission time Handle Problem Language Result Execution time Memory
568300 2022-05-25T07:12:18 Z hibiki Swapping Cities (APIO20_swap) C++11
100 / 100
325 ms 55504 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define f first
#define s second

// global
int n,m;
int deg[100005],l[300005],mn[300005],deep[300005];
int spa[300005][20];
vector<int> v[300005];
priority_queue<pair<int,pair<int,int> > > pq;

int fi(int idx)
{
    if(l[idx] == idx) return idx;
    return l[idx] = fi(l[idx]);
}

void uni(int x,int y,int w)
{
    bool chk = deg[x] > 1 | deg[y] > 1;
    deg[x]++;
    deg[y]++;
    x = fi(x), y = fi(y);
    if(x == y)
    {
        if(!mn[x])
            mn[x] = w;
        return ;
    }
    l[x] = l[y] = n;
    if(chk || mn[x] || mn[y])
        mn[n] = w;
    v[n].pb(x);
    v[n].pb(y);
    n++;
}

void dfs(int nw,int fa)
{
    for(auto go: v[nw])
    {
        if(go == fa) continue;
        if(!mn[go]) mn[go] = mn[nw];
        deep[go] = deep[nw] + 1;
        spa[go][0] = nw;
        dfs(go, nw);
    }
}

int query(int a,int b)
{
    if(deep[a] > deep[b]) swap(a,b);
    int dif = deep[b] - deep[a];
    for(int  i = 0; i < 20; i++)
        if((1<<i)&dif)
            b = spa[b][i];
    if(a == b) return mn[a];
    for(int i = 19; i >= 0; i--)
    {
        if(spa[a][i] != spa[b][i])
        {
            a = spa[a][i];
            b = spa[b][i];
        }
    }
    return mn[spa[a][0]];
}

void init(int N, int M,
          vector<int> U, vector<int> V, vector<int> W)
{
    n = N, m = M;
    memset(spa, -1, sizeof(spa));
    for(int i = 0; i < 300005; i++)
        l[i] = i;
    for(int i = 0; i < m; i++)
        pq.push({-W[i],{U[i],V[i]}});
    while(!pq.empty())
    {
        int x = pq.top().s.f, y = pq.top().s.s, w = -pq.top().f;
        pq.pop();
        uni(x,y,w);
    }
    dfs(n - 1, -1);
    for(int i = 0; i < 300005; i++)
        if(!mn[i]) mn[i] = -1;
    for(int j = 1; j < 20; j++)
    {
        for(int i = 0; i < n; i++)
        {
            if(spa[i][j - 1] == -1) continue;
            spa[i][j] = spa[spa[i][j - 1]][j - 1];
        }
    }
    return ;
}

int getMinimumFuelCapacity(int X, int Y)
{
    return query(X,Y);
}

Compilation message

swap.cpp: In function 'void uni(int, int, int)':
swap.cpp:24:23: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
   24 |     bool chk = deg[x] > 1 | deg[y] > 1;
      |                ~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 33080 KB Output is correct
2 Correct 18 ms 33084 KB Output is correct
3 Correct 15 ms 33100 KB Output is correct
4 Correct 16 ms 33236 KB Output is correct
5 Correct 16 ms 33216 KB Output is correct
6 Correct 16 ms 33272 KB Output is correct
7 Correct 17 ms 33228 KB Output is correct
8 Correct 15 ms 33224 KB Output is correct
9 Correct 92 ms 40988 KB Output is correct
10 Correct 110 ms 42624 KB Output is correct
11 Correct 116 ms 42560 KB Output is correct
12 Correct 113 ms 43064 KB Output is correct
13 Correct 111 ms 46940 KB Output is correct
14 Correct 98 ms 41264 KB Output is correct
15 Correct 244 ms 46904 KB Output is correct
16 Correct 209 ms 46704 KB Output is correct
17 Correct 245 ms 47204 KB Output is correct
18 Correct 301 ms 51184 KB Output is correct
19 Correct 83 ms 39772 KB Output is correct
20 Correct 217 ms 47840 KB Output is correct
21 Correct 227 ms 47976 KB Output is correct
22 Correct 245 ms 48524 KB Output is correct
23 Correct 316 ms 52516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 33080 KB Output is correct
2 Correct 18 ms 33084 KB Output is correct
3 Correct 295 ms 54752 KB Output is correct
4 Correct 271 ms 55504 KB Output is correct
5 Correct 305 ms 55384 KB Output is correct
6 Correct 294 ms 55336 KB Output is correct
7 Correct 280 ms 55444 KB Output is correct
8 Correct 290 ms 54808 KB Output is correct
9 Correct 257 ms 55264 KB Output is correct
10 Correct 302 ms 54684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 33080 KB Output is correct
2 Correct 18 ms 33084 KB Output is correct
3 Correct 15 ms 33100 KB Output is correct
4 Correct 16 ms 33236 KB Output is correct
5 Correct 16 ms 33216 KB Output is correct
6 Correct 16 ms 33272 KB Output is correct
7 Correct 17 ms 33228 KB Output is correct
8 Correct 15 ms 33224 KB Output is correct
9 Correct 14 ms 33108 KB Output is correct
10 Correct 16 ms 33220 KB Output is correct
11 Correct 15 ms 33244 KB Output is correct
12 Correct 14 ms 33236 KB Output is correct
13 Correct 15 ms 33172 KB Output is correct
14 Correct 14 ms 33216 KB Output is correct
15 Correct 16 ms 33236 KB Output is correct
16 Correct 18 ms 33224 KB Output is correct
17 Correct 16 ms 33264 KB Output is correct
18 Correct 15 ms 33208 KB Output is correct
19 Correct 17 ms 33216 KB Output is correct
20 Correct 15 ms 33220 KB Output is correct
21 Correct 15 ms 33260 KB Output is correct
22 Correct 16 ms 33236 KB Output is correct
23 Correct 15 ms 33204 KB Output is correct
24 Correct 15 ms 33236 KB Output is correct
25 Correct 15 ms 33224 KB Output is correct
26 Correct 15 ms 33288 KB Output is correct
27 Correct 15 ms 33224 KB Output is correct
28 Correct 17 ms 33332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 33108 KB Output is correct
2 Correct 15 ms 33080 KB Output is correct
3 Correct 18 ms 33084 KB Output is correct
4 Correct 15 ms 33100 KB Output is correct
5 Correct 16 ms 33236 KB Output is correct
6 Correct 16 ms 33216 KB Output is correct
7 Correct 16 ms 33272 KB Output is correct
8 Correct 17 ms 33228 KB Output is correct
9 Correct 15 ms 33224 KB Output is correct
10 Correct 92 ms 40988 KB Output is correct
11 Correct 110 ms 42624 KB Output is correct
12 Correct 116 ms 42560 KB Output is correct
13 Correct 113 ms 43064 KB Output is correct
14 Correct 111 ms 46940 KB Output is correct
15 Correct 16 ms 33220 KB Output is correct
16 Correct 15 ms 33244 KB Output is correct
17 Correct 14 ms 33236 KB Output is correct
18 Correct 15 ms 33172 KB Output is correct
19 Correct 14 ms 33216 KB Output is correct
20 Correct 16 ms 33236 KB Output is correct
21 Correct 18 ms 33224 KB Output is correct
22 Correct 16 ms 33264 KB Output is correct
23 Correct 15 ms 33208 KB Output is correct
24 Correct 17 ms 33216 KB Output is correct
25 Correct 15 ms 33220 KB Output is correct
26 Correct 15 ms 33260 KB Output is correct
27 Correct 16 ms 33236 KB Output is correct
28 Correct 15 ms 33204 KB Output is correct
29 Correct 15 ms 33236 KB Output is correct
30 Correct 15 ms 33224 KB Output is correct
31 Correct 15 ms 33288 KB Output is correct
32 Correct 15 ms 33224 KB Output is correct
33 Correct 17 ms 33332 KB Output is correct
34 Correct 24 ms 34428 KB Output is correct
35 Correct 109 ms 42936 KB Output is correct
36 Correct 112 ms 42720 KB Output is correct
37 Correct 110 ms 42816 KB Output is correct
38 Correct 123 ms 42664 KB Output is correct
39 Correct 115 ms 42652 KB Output is correct
40 Correct 115 ms 41776 KB Output is correct
41 Correct 119 ms 43052 KB Output is correct
42 Correct 124 ms 42808 KB Output is correct
43 Correct 106 ms 47968 KB Output is correct
44 Correct 118 ms 43180 KB Output is correct
45 Correct 143 ms 46700 KB Output is correct
46 Correct 113 ms 42836 KB Output is correct
47 Correct 114 ms 42944 KB Output is correct
48 Correct 132 ms 45240 KB Output is correct
49 Correct 84 ms 41692 KB Output is correct
50 Correct 74 ms 40184 KB Output is correct
51 Correct 121 ms 44680 KB Output is correct
52 Correct 159 ms 46520 KB Output is correct
53 Correct 181 ms 48476 KB Output is correct
54 Correct 182 ms 48504 KB Output is correct
55 Correct 112 ms 47504 KB Output is correct
56 Correct 175 ms 49540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 33080 KB Output is correct
2 Correct 18 ms 33084 KB Output is correct
3 Correct 15 ms 33100 KB Output is correct
4 Correct 16 ms 33236 KB Output is correct
5 Correct 16 ms 33216 KB Output is correct
6 Correct 16 ms 33272 KB Output is correct
7 Correct 17 ms 33228 KB Output is correct
8 Correct 15 ms 33224 KB Output is correct
9 Correct 92 ms 40988 KB Output is correct
10 Correct 110 ms 42624 KB Output is correct
11 Correct 116 ms 42560 KB Output is correct
12 Correct 113 ms 43064 KB Output is correct
13 Correct 111 ms 46940 KB Output is correct
14 Correct 98 ms 41264 KB Output is correct
15 Correct 244 ms 46904 KB Output is correct
16 Correct 209 ms 46704 KB Output is correct
17 Correct 245 ms 47204 KB Output is correct
18 Correct 301 ms 51184 KB Output is correct
19 Correct 295 ms 54752 KB Output is correct
20 Correct 271 ms 55504 KB Output is correct
21 Correct 305 ms 55384 KB Output is correct
22 Correct 294 ms 55336 KB Output is correct
23 Correct 280 ms 55444 KB Output is correct
24 Correct 290 ms 54808 KB Output is correct
25 Correct 257 ms 55264 KB Output is correct
26 Correct 302 ms 54684 KB Output is correct
27 Correct 16 ms 33220 KB Output is correct
28 Correct 15 ms 33244 KB Output is correct
29 Correct 14 ms 33236 KB Output is correct
30 Correct 15 ms 33172 KB Output is correct
31 Correct 14 ms 33216 KB Output is correct
32 Correct 16 ms 33236 KB Output is correct
33 Correct 18 ms 33224 KB Output is correct
34 Correct 16 ms 33264 KB Output is correct
35 Correct 15 ms 33208 KB Output is correct
36 Correct 24 ms 34428 KB Output is correct
37 Correct 109 ms 42936 KB Output is correct
38 Correct 112 ms 42720 KB Output is correct
39 Correct 110 ms 42816 KB Output is correct
40 Correct 123 ms 42664 KB Output is correct
41 Correct 115 ms 42652 KB Output is correct
42 Correct 115 ms 41776 KB Output is correct
43 Correct 119 ms 43052 KB Output is correct
44 Correct 124 ms 42808 KB Output is correct
45 Correct 106 ms 47968 KB Output is correct
46 Correct 118 ms 43180 KB Output is correct
47 Correct 31 ms 34956 KB Output is correct
48 Correct 223 ms 47652 KB Output is correct
49 Correct 225 ms 47704 KB Output is correct
50 Correct 220 ms 47648 KB Output is correct
51 Correct 224 ms 47696 KB Output is correct
52 Correct 257 ms 47276 KB Output is correct
53 Correct 183 ms 45628 KB Output is correct
54 Correct 237 ms 48516 KB Output is correct
55 Correct 224 ms 47608 KB Output is correct
56 Correct 325 ms 51864 KB Output is correct
57 Correct 257 ms 48628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 33108 KB Output is correct
2 Correct 15 ms 33080 KB Output is correct
3 Correct 18 ms 33084 KB Output is correct
4 Correct 15 ms 33100 KB Output is correct
5 Correct 16 ms 33236 KB Output is correct
6 Correct 16 ms 33216 KB Output is correct
7 Correct 16 ms 33272 KB Output is correct
8 Correct 17 ms 33228 KB Output is correct
9 Correct 15 ms 33224 KB Output is correct
10 Correct 92 ms 40988 KB Output is correct
11 Correct 110 ms 42624 KB Output is correct
12 Correct 116 ms 42560 KB Output is correct
13 Correct 113 ms 43064 KB Output is correct
14 Correct 111 ms 46940 KB Output is correct
15 Correct 98 ms 41264 KB Output is correct
16 Correct 244 ms 46904 KB Output is correct
17 Correct 209 ms 46704 KB Output is correct
18 Correct 245 ms 47204 KB Output is correct
19 Correct 301 ms 51184 KB Output is correct
20 Correct 295 ms 54752 KB Output is correct
21 Correct 271 ms 55504 KB Output is correct
22 Correct 305 ms 55384 KB Output is correct
23 Correct 294 ms 55336 KB Output is correct
24 Correct 280 ms 55444 KB Output is correct
25 Correct 290 ms 54808 KB Output is correct
26 Correct 257 ms 55264 KB Output is correct
27 Correct 302 ms 54684 KB Output is correct
28 Correct 16 ms 33220 KB Output is correct
29 Correct 15 ms 33244 KB Output is correct
30 Correct 14 ms 33236 KB Output is correct
31 Correct 15 ms 33172 KB Output is correct
32 Correct 14 ms 33216 KB Output is correct
33 Correct 16 ms 33236 KB Output is correct
34 Correct 18 ms 33224 KB Output is correct
35 Correct 16 ms 33264 KB Output is correct
36 Correct 15 ms 33208 KB Output is correct
37 Correct 24 ms 34428 KB Output is correct
38 Correct 109 ms 42936 KB Output is correct
39 Correct 112 ms 42720 KB Output is correct
40 Correct 110 ms 42816 KB Output is correct
41 Correct 123 ms 42664 KB Output is correct
42 Correct 115 ms 42652 KB Output is correct
43 Correct 115 ms 41776 KB Output is correct
44 Correct 119 ms 43052 KB Output is correct
45 Correct 124 ms 42808 KB Output is correct
46 Correct 106 ms 47968 KB Output is correct
47 Correct 118 ms 43180 KB Output is correct
48 Correct 31 ms 34956 KB Output is correct
49 Correct 223 ms 47652 KB Output is correct
50 Correct 225 ms 47704 KB Output is correct
51 Correct 220 ms 47648 KB Output is correct
52 Correct 224 ms 47696 KB Output is correct
53 Correct 257 ms 47276 KB Output is correct
54 Correct 183 ms 45628 KB Output is correct
55 Correct 237 ms 48516 KB Output is correct
56 Correct 224 ms 47608 KB Output is correct
57 Correct 325 ms 51864 KB Output is correct
58 Correct 257 ms 48628 KB Output is correct
59 Correct 83 ms 39772 KB Output is correct
60 Correct 217 ms 47840 KB Output is correct
61 Correct 227 ms 47976 KB Output is correct
62 Correct 245 ms 48524 KB Output is correct
63 Correct 316 ms 52516 KB Output is correct
64 Correct 17 ms 33216 KB Output is correct
65 Correct 15 ms 33220 KB Output is correct
66 Correct 15 ms 33260 KB Output is correct
67 Correct 16 ms 33236 KB Output is correct
68 Correct 15 ms 33204 KB Output is correct
69 Correct 15 ms 33236 KB Output is correct
70 Correct 15 ms 33224 KB Output is correct
71 Correct 15 ms 33288 KB Output is correct
72 Correct 15 ms 33224 KB Output is correct
73 Correct 17 ms 33332 KB Output is correct
74 Correct 143 ms 46700 KB Output is correct
75 Correct 113 ms 42836 KB Output is correct
76 Correct 114 ms 42944 KB Output is correct
77 Correct 132 ms 45240 KB Output is correct
78 Correct 84 ms 41692 KB Output is correct
79 Correct 74 ms 40184 KB Output is correct
80 Correct 121 ms 44680 KB Output is correct
81 Correct 159 ms 46520 KB Output is correct
82 Correct 181 ms 48476 KB Output is correct
83 Correct 182 ms 48504 KB Output is correct
84 Correct 112 ms 47504 KB Output is correct
85 Correct 175 ms 49540 KB Output is correct
86 Correct 72 ms 39032 KB Output is correct
87 Correct 263 ms 47776 KB Output is correct
88 Correct 236 ms 47636 KB Output is correct
89 Correct 254 ms 49552 KB Output is correct
90 Correct 169 ms 45964 KB Output is correct
91 Correct 163 ms 46480 KB Output is correct
92 Correct 250 ms 49592 KB Output is correct
93 Correct 256 ms 51252 KB Output is correct
94 Correct 314 ms 52664 KB Output is correct
95 Correct 290 ms 52292 KB Output is correct
96 Correct 297 ms 52048 KB Output is correct
97 Correct 271 ms 52276 KB Output is correct