Submission #1105678

# Submission time Handle Problem Language Result Execution time Memory
1105678 2024-10-27T10:02:58 Z ASN49K Swapping Cities (APIO20_swap) C++14
100 / 100
173 ms 31048 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(),x.end()


const int N=100'000,LG=16;
int cost[2*N],t[2*N],tt[2*N][LG+1],cnt_edges[N],size,tin[2*N],tout[2*N],l_child[2*N],r_child[2*N];
int root(int x)
{
    if(t[x]!=x)
    {
        t[x]=root(t[x]);
    }
    return t[x];
}
void unite(int x,int y,int c)
{
    bool ok=(++cnt_edges[x]>=3 || ++cnt_edges[y]>=3);
    x=root(x);
    y=root(y);
    if(x!=y)
    {
        tt[x][0]=t[x]=size;
        l_child[size]=x;
        tt[y][0]=t[y]=size;
        r_child[size]=y;
        if(ok || cost[x]>0 || cost[y]>0)
        {
            cost[size]=c;
        }
        tt[size][0]=t[size]=size;
        size++;
    }
    else if(!cost[x])
    {
        cost[x]=c;
    }
}

void dfs(int nod,int n)
{
    static int timp=0;
    tin[nod]=++timp;
    if(nod>=n)
    {
        dfs(l_child[nod],n);
        dfs(r_child[nod],n);
    }
    tout[nod]=timp;
}

void init(int n, int m,std::vector<int> x, std::vector<int> y, std::vector<int> w)
{
    vector<int>ord(m);
    iota(all(ord) , 0);
    sort(all(ord) , [&](int x,int y){
        return w[x]<w[y];
    });
    for(int i=0;i<n;i++)
    {
        tt[i][0]=t[i]=i;
    }
    size=n;
    for(int i=0;i<m;i++)
    {
        unite(x[ord[i]] , y[ord[i]] , w[ord[i]]);
    }

    for(int i=1;i<=LG;i++)
    {
        for(int j=0;j<size;j++)
        {
            tt[j][i]=tt[tt[j][i-1]][i-1];
        }
    }
    for(int i=size-1;i>=0;i--)
    {
        if(!tin[i])
        {
            dfs(i,n);
        }
    }
}

bool is_parent(int x,int y)
{
    return tin[x]<=tin[y] && tout[y]<=tout[x];
}
int lca(int x,int y)
{
    if(is_parent(x,y))
    {
        return x;
    }
    if(is_parent(y,x))
    {
        return y;
    }
    for(int i=LG;i>=0;i--)
    {
        if(!is_parent(tt[x][i],y))
        {
            x=tt[x][i];
        }
    }
    return tt[x][0];
}
int getMinimumFuelCapacity(int x, int y)
{
    if(root(x)!=root(y) || cost[root(x)]==0)
    {
        return -1;
    }
    int l=lca(x,y);
    if(cost[l]>0)
    {
        return cost[l];
    }

    for(int i=LG;i>=0;i--)
    {
        if(cost[tt[l][i]]==0)
        {
            l=tt[l][i];
        }
    }
    return cost[tt[l][0]];
}

/*
5 6
0 1 4
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 4432 KB Output is correct
4 Correct 1 ms 4432 KB Output is correct
5 Correct 1 ms 4432 KB Output is correct
6 Correct 2 ms 4432 KB Output is correct
7 Correct 1 ms 4432 KB Output is correct
8 Correct 2 ms 4432 KB Output is correct
9 Correct 35 ms 19140 KB Output is correct
10 Correct 52 ms 22088 KB Output is correct
11 Correct 45 ms 22024 KB Output is correct
12 Correct 46 ms 22600 KB Output is correct
13 Correct 47 ms 24136 KB Output is correct
14 Correct 41 ms 19528 KB Output is correct
15 Correct 86 ms 25944 KB Output is correct
16 Correct 82 ms 25928 KB Output is correct
17 Correct 91 ms 26440 KB Output is correct
18 Correct 91 ms 27976 KB Output is correct
19 Correct 70 ms 12496 KB Output is correct
20 Correct 136 ms 26784 KB Output is correct
21 Correct 124 ms 27108 KB Output is correct
22 Correct 127 ms 27684 KB Output is correct
23 Correct 173 ms 29220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 156 ms 29816 KB Output is correct
4 Correct 142 ms 30256 KB Output is correct
5 Correct 145 ms 30408 KB Output is correct
6 Correct 132 ms 30108 KB Output is correct
7 Correct 132 ms 30284 KB Output is correct
8 Correct 122 ms 29836 KB Output is correct
9 Correct 122 ms 30020 KB Output is correct
10 Correct 144 ms 29844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 4432 KB Output is correct
4 Correct 1 ms 4432 KB Output is correct
5 Correct 1 ms 4432 KB Output is correct
6 Correct 2 ms 4432 KB Output is correct
7 Correct 1 ms 4432 KB Output is correct
8 Correct 2 ms 4432 KB Output is correct
9 Correct 1 ms 4432 KB Output is correct
10 Correct 1 ms 4432 KB Output is correct
11 Correct 1 ms 4432 KB Output is correct
12 Correct 1 ms 4432 KB Output is correct
13 Correct 1 ms 4432 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4432 KB Output is correct
16 Correct 1 ms 4432 KB Output is correct
17 Correct 2 ms 4432 KB Output is correct
18 Correct 1 ms 4432 KB Output is correct
19 Correct 1 ms 4432 KB Output is correct
20 Correct 1 ms 4432 KB Output is correct
21 Correct 1 ms 4552 KB Output is correct
22 Correct 1 ms 4432 KB Output is correct
23 Correct 1 ms 4600 KB Output is correct
24 Correct 2 ms 4432 KB Output is correct
25 Correct 2 ms 4432 KB Output is correct
26 Correct 2 ms 4432 KB Output is correct
27 Correct 2 ms 4432 KB Output is correct
28 Correct 2 ms 4432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 4432 KB Output is correct
4 Correct 1 ms 4432 KB Output is correct
5 Correct 1 ms 4432 KB Output is correct
6 Correct 1 ms 4432 KB Output is correct
7 Correct 2 ms 4432 KB Output is correct
8 Correct 1 ms 4432 KB Output is correct
9 Correct 2 ms 4432 KB Output is correct
10 Correct 35 ms 19140 KB Output is correct
11 Correct 52 ms 22088 KB Output is correct
12 Correct 45 ms 22024 KB Output is correct
13 Correct 46 ms 22600 KB Output is correct
14 Correct 47 ms 24136 KB Output is correct
15 Correct 1 ms 4432 KB Output is correct
16 Correct 1 ms 4432 KB Output is correct
17 Correct 1 ms 4432 KB Output is correct
18 Correct 1 ms 4432 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4432 KB Output is correct
21 Correct 1 ms 4432 KB Output is correct
22 Correct 2 ms 4432 KB Output is correct
23 Correct 1 ms 4432 KB Output is correct
24 Correct 1 ms 4432 KB Output is correct
25 Correct 1 ms 4432 KB Output is correct
26 Correct 1 ms 4552 KB Output is correct
27 Correct 1 ms 4432 KB Output is correct
28 Correct 1 ms 4600 KB Output is correct
29 Correct 2 ms 4432 KB Output is correct
30 Correct 2 ms 4432 KB Output is correct
31 Correct 2 ms 4432 KB Output is correct
32 Correct 2 ms 4432 KB Output is correct
33 Correct 2 ms 4432 KB Output is correct
34 Correct 7 ms 7420 KB Output is correct
35 Correct 46 ms 22344 KB Output is correct
36 Correct 46 ms 22416 KB Output is correct
37 Correct 50 ms 22596 KB Output is correct
38 Correct 51 ms 22344 KB Output is correct
39 Correct 44 ms 22344 KB Output is correct
40 Correct 40 ms 21840 KB Output is correct
41 Correct 42 ms 22600 KB Output is correct
42 Correct 43 ms 22364 KB Output is correct
43 Correct 43 ms 24628 KB Output is correct
44 Correct 45 ms 23112 KB Output is correct
45 Correct 71 ms 23624 KB Output is correct
46 Correct 41 ms 22344 KB Output is correct
47 Correct 43 ms 22620 KB Output is correct
48 Correct 51 ms 23788 KB Output is correct
49 Correct 49 ms 11500 KB Output is correct
50 Correct 38 ms 12372 KB Output is correct
51 Correct 56 ms 20296 KB Output is correct
52 Correct 62 ms 25672 KB Output is correct
53 Correct 80 ms 27464 KB Output is correct
54 Correct 76 ms 27224 KB Output is correct
55 Correct 42 ms 24648 KB Output is correct
56 Correct 76 ms 27416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 4432 KB Output is correct
4 Correct 1 ms 4432 KB Output is correct
5 Correct 1 ms 4432 KB Output is correct
6 Correct 2 ms 4432 KB Output is correct
7 Correct 1 ms 4432 KB Output is correct
8 Correct 2 ms 4432 KB Output is correct
9 Correct 35 ms 19140 KB Output is correct
10 Correct 52 ms 22088 KB Output is correct
11 Correct 45 ms 22024 KB Output is correct
12 Correct 46 ms 22600 KB Output is correct
13 Correct 47 ms 24136 KB Output is correct
14 Correct 41 ms 19528 KB Output is correct
15 Correct 86 ms 25944 KB Output is correct
16 Correct 82 ms 25928 KB Output is correct
17 Correct 91 ms 26440 KB Output is correct
18 Correct 91 ms 27976 KB Output is correct
19 Correct 156 ms 29816 KB Output is correct
20 Correct 142 ms 30256 KB Output is correct
21 Correct 145 ms 30408 KB Output is correct
22 Correct 132 ms 30108 KB Output is correct
23 Correct 132 ms 30284 KB Output is correct
24 Correct 122 ms 29836 KB Output is correct
25 Correct 122 ms 30020 KB Output is correct
26 Correct 144 ms 29844 KB Output is correct
27 Correct 1 ms 4432 KB Output is correct
28 Correct 1 ms 4432 KB Output is correct
29 Correct 1 ms 4432 KB Output is correct
30 Correct 1 ms 4432 KB Output is correct
31 Correct 1 ms 4444 KB Output is correct
32 Correct 1 ms 4432 KB Output is correct
33 Correct 1 ms 4432 KB Output is correct
34 Correct 2 ms 4432 KB Output is correct
35 Correct 1 ms 4432 KB Output is correct
36 Correct 7 ms 7420 KB Output is correct
37 Correct 46 ms 22344 KB Output is correct
38 Correct 46 ms 22416 KB Output is correct
39 Correct 50 ms 22596 KB Output is correct
40 Correct 51 ms 22344 KB Output is correct
41 Correct 44 ms 22344 KB Output is correct
42 Correct 40 ms 21840 KB Output is correct
43 Correct 42 ms 22600 KB Output is correct
44 Correct 43 ms 22364 KB Output is correct
45 Correct 43 ms 24628 KB Output is correct
46 Correct 45 ms 23112 KB Output is correct
47 Correct 14 ms 7788 KB Output is correct
48 Correct 111 ms 26916 KB Output is correct
49 Correct 121 ms 27240 KB Output is correct
50 Correct 116 ms 27172 KB Output is correct
51 Correct 118 ms 26936 KB Output is correct
52 Correct 109 ms 26756 KB Output is correct
53 Correct 100 ms 22340 KB Output is correct
54 Correct 119 ms 27684 KB Output is correct
55 Correct 117 ms 26780 KB Output is correct
56 Correct 139 ms 28708 KB Output is correct
57 Correct 123 ms 28196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 4432 KB Output is correct
4 Correct 1 ms 4432 KB Output is correct
5 Correct 1 ms 4432 KB Output is correct
6 Correct 1 ms 4432 KB Output is correct
7 Correct 2 ms 4432 KB Output is correct
8 Correct 1 ms 4432 KB Output is correct
9 Correct 2 ms 4432 KB Output is correct
10 Correct 35 ms 19140 KB Output is correct
11 Correct 52 ms 22088 KB Output is correct
12 Correct 45 ms 22024 KB Output is correct
13 Correct 46 ms 22600 KB Output is correct
14 Correct 47 ms 24136 KB Output is correct
15 Correct 41 ms 19528 KB Output is correct
16 Correct 86 ms 25944 KB Output is correct
17 Correct 82 ms 25928 KB Output is correct
18 Correct 91 ms 26440 KB Output is correct
19 Correct 91 ms 27976 KB Output is correct
20 Correct 156 ms 29816 KB Output is correct
21 Correct 142 ms 30256 KB Output is correct
22 Correct 145 ms 30408 KB Output is correct
23 Correct 132 ms 30108 KB Output is correct
24 Correct 132 ms 30284 KB Output is correct
25 Correct 122 ms 29836 KB Output is correct
26 Correct 122 ms 30020 KB Output is correct
27 Correct 144 ms 29844 KB Output is correct
28 Correct 1 ms 4432 KB Output is correct
29 Correct 1 ms 4432 KB Output is correct
30 Correct 1 ms 4432 KB Output is correct
31 Correct 1 ms 4432 KB Output is correct
32 Correct 1 ms 4444 KB Output is correct
33 Correct 1 ms 4432 KB Output is correct
34 Correct 1 ms 4432 KB Output is correct
35 Correct 2 ms 4432 KB Output is correct
36 Correct 1 ms 4432 KB Output is correct
37 Correct 7 ms 7420 KB Output is correct
38 Correct 46 ms 22344 KB Output is correct
39 Correct 46 ms 22416 KB Output is correct
40 Correct 50 ms 22596 KB Output is correct
41 Correct 51 ms 22344 KB Output is correct
42 Correct 44 ms 22344 KB Output is correct
43 Correct 40 ms 21840 KB Output is correct
44 Correct 42 ms 22600 KB Output is correct
45 Correct 43 ms 22364 KB Output is correct
46 Correct 43 ms 24628 KB Output is correct
47 Correct 45 ms 23112 KB Output is correct
48 Correct 14 ms 7788 KB Output is correct
49 Correct 111 ms 26916 KB Output is correct
50 Correct 121 ms 27240 KB Output is correct
51 Correct 116 ms 27172 KB Output is correct
52 Correct 118 ms 26936 KB Output is correct
53 Correct 109 ms 26756 KB Output is correct
54 Correct 100 ms 22340 KB Output is correct
55 Correct 119 ms 27684 KB Output is correct
56 Correct 117 ms 26780 KB Output is correct
57 Correct 139 ms 28708 KB Output is correct
58 Correct 123 ms 28196 KB Output is correct
59 Correct 70 ms 12496 KB Output is correct
60 Correct 136 ms 26784 KB Output is correct
61 Correct 124 ms 27108 KB Output is correct
62 Correct 127 ms 27684 KB Output is correct
63 Correct 173 ms 29220 KB Output is correct
64 Correct 1 ms 4432 KB Output is correct
65 Correct 1 ms 4432 KB Output is correct
66 Correct 1 ms 4552 KB Output is correct
67 Correct 1 ms 4432 KB Output is correct
68 Correct 1 ms 4600 KB Output is correct
69 Correct 2 ms 4432 KB Output is correct
70 Correct 2 ms 4432 KB Output is correct
71 Correct 2 ms 4432 KB Output is correct
72 Correct 2 ms 4432 KB Output is correct
73 Correct 2 ms 4432 KB Output is correct
74 Correct 71 ms 23624 KB Output is correct
75 Correct 41 ms 22344 KB Output is correct
76 Correct 43 ms 22620 KB Output is correct
77 Correct 51 ms 23788 KB Output is correct
78 Correct 49 ms 11500 KB Output is correct
79 Correct 38 ms 12372 KB Output is correct
80 Correct 56 ms 20296 KB Output is correct
81 Correct 62 ms 25672 KB Output is correct
82 Correct 80 ms 27464 KB Output is correct
83 Correct 76 ms 27224 KB Output is correct
84 Correct 42 ms 24648 KB Output is correct
85 Correct 76 ms 27416 KB Output is correct
86 Correct 41 ms 12492 KB Output is correct
87 Correct 110 ms 27048 KB Output is correct
88 Correct 113 ms 27120 KB Output is correct
89 Correct 136 ms 27848 KB Output is correct
90 Correct 109 ms 15308 KB Output is correct
91 Correct 110 ms 17444 KB Output is correct
92 Correct 139 ms 24672 KB Output is correct
93 Correct 126 ms 29592 KB Output is correct
94 Correct 149 ms 30836 KB Output is correct
95 Correct 141 ms 31048 KB Output is correct
96 Correct 137 ms 29216 KB Output is correct
97 Correct 144 ms 30196 KB Output is correct