Submission #729719

# Submission time Handle Problem Language Result Execution time Memory
729719 2023-04-24T11:52:15 Z onepunchac168 Swapping Cities (APIO20_swap) C++14
100 / 100
281 ms 57072 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mask(x) (1LL<<(x))
typedef long long ll;
typedef pair <ll,ll> ii;
typedef pair <ll,ii> iii;
ll n,m;
ll parent[100005][20];
ll mx[100005][20];
vector <ii> vt[100005];
ll sz[100005];
ll solve(ll aa,ll bb)
{
    if (aa==-1&&bb==-1)
    {
        return -1;
    }
    if (aa==-1)
    {
        return bb;
    }
    if (bb==-1)
    {
        return aa;
    }
    return min(aa,bb);
}
ll solvea(ll aa,ll bb)
{
    if (aa==-1&&bb==-1)
    {
        return -1;
    }
    if (aa==-1)
    {
        return bb;
    }
    if (bb==-1)
    {
        return aa;
    }
    return max(aa,bb);
}
struct DSU{
    ll lab[100005];
    bool check[100005];
    ll tmp[100005];
    ll countedge[100005];
    void makeset(int u)
    {
        lab[u]=-1;
        tmp[u]=-1;
        countedge[u]=0;
    }
    ll findset(int u)
    {
        if (lab[u]<0)
        {
            return u;
        }
        return lab[u]=findset(lab[u]);
    }
    void Union(int u,int v,ll w)
    {
        int r=findset(u);
        int s=findset(v);
        countedge[u]++;
        countedge[v]++;
        if (r==s)
        {
            if (tmp[r]==-1)
            {
                tmp[r]=w;
            }
            return;
        }
        if (lab[r]>lab[s])
        {
            swap(r,s);
        }
        lab[r]+=lab[s];
        lab[s]=r;
        if (tmp[s]!=-1&&tmp[r]==-1)
        {
            tmp[r]=w;
        }
        if (countedge[u]>=3||countedge[v]>=3)
        {
            if (tmp[r]==-1){
                tmp[r]=w;
            }
        }
        vt[r].pb({s,w});
    }
} dsu;
void dfs(int u)
{
    for (auto v:vt[u])
    {
        parent[v.fi][0]=u;
        mx[v.fi][0]=v.se;
        for (int i=1;i<=18;i++)
        {
            mx[v.fi][i]=max(mx[v.fi][i-1],mx[parent[v.fi][i-1]][i-1]);
            parent[v.fi][i]=parent[parent[v.fi][i-1]][i-1];
        }
        sz[v.fi]=sz[u]+1;
        if (dsu.tmp[v.fi]!=-1)
        {

        }
        else if (dsu.tmp[u]!=-1)
        {
            dsu.tmp[v.fi]=solvea(dsu.tmp[u],v.se);
        }
        dfs(v.fi);
    }
}
void init(int N, int M,std::vector<int> U, std::vector<int> V, std::vector<int> W) {
    n=N;
    m=M-1;
    vector <iii> vta;
    for (int i=1;i<=n;i++)
    {
        dsu.makeset(i);
    }
    for (int i=0;i<=m;i++)
    {
        ll u=U[i];
        ll v=V[i];
        u++;
        v++;
        ll w=W[i];
        vta.pb({w,{u,v}});
    }
    sort (vta.begin(),vta.end());
    for (auto v:vta)
    {
        dsu.Union(v.se.fi,v.se.se,v.fi);
    }
    ll ss=dsu.findset(1);
    sz[ss]=0;
    dfs(ss);
}
ii lca(int u,int v)
{
    if (sz[u]>sz[v]){swap(u,v);}
    ll pp=0;
    for (int i=18;i>=0;i--)
    {
        if (sz[v]-mask(i)>=sz[u])
        {
            pp=max(pp,mx[v][i]);
            v=parent[v][i];
        }
    }
    if (u==v){return {u,pp};}
    for (int i=18;i>=0;i--)
    {
        if (parent[u][i]!=parent[v][i])
        {
            pp=max({pp,mx[u][i],mx[v][i]});
            u=parent[u][i];
            v=parent[v][i];
        }
    }
    if (u!=v)
    {
        pp=max({pp,mx[u][0],mx[v][0]});
        u=parent[u][0];
        v=parent[v][0];
    }
    return {u,pp};
}
int getMinimumFuelCapacity(int X, int Y) {
    X++;
    Y++;
    ii aa=lca(X,Y);
    if (dsu.tmp[X]==-1&&dsu.tmp[Y]==-1)
    {
        return -1;
    }
    aa.se=max(aa.se,solve(dsu.tmp[X],dsu.tmp[Y]));
    return aa.se;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2672 KB Output is correct
4 Correct 3 ms 2796 KB Output is correct
5 Correct 2 ms 3028 KB Output is correct
6 Correct 2 ms 3028 KB Output is correct
7 Correct 3 ms 3028 KB Output is correct
8 Correct 2 ms 3060 KB Output is correct
9 Correct 75 ms 35980 KB Output is correct
10 Correct 93 ms 43312 KB Output is correct
11 Correct 93 ms 42756 KB Output is correct
12 Correct 103 ms 45072 KB Output is correct
13 Correct 90 ms 45588 KB Output is correct
14 Correct 84 ms 36120 KB Output is correct
15 Correct 202 ms 45216 KB Output is correct
16 Correct 209 ms 44088 KB Output is correct
17 Correct 218 ms 46692 KB Output is correct
18 Correct 192 ms 46268 KB Output is correct
19 Correct 79 ms 12812 KB Output is correct
20 Correct 206 ms 45408 KB Output is correct
21 Correct 225 ms 43804 KB Output is correct
22 Correct 220 ms 46544 KB Output is correct
23 Correct 185 ms 46476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 207 ms 45192 KB Output is correct
4 Correct 174 ms 47348 KB Output is correct
5 Correct 194 ms 46024 KB Output is correct
6 Correct 180 ms 47264 KB Output is correct
7 Correct 186 ms 46592 KB Output is correct
8 Correct 174 ms 44920 KB Output is correct
9 Correct 189 ms 46472 KB Output is correct
10 Correct 189 ms 44572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2672 KB Output is correct
4 Correct 3 ms 2796 KB Output is correct
5 Correct 2 ms 3028 KB Output is correct
6 Correct 2 ms 3028 KB Output is correct
7 Correct 3 ms 3028 KB Output is correct
8 Correct 2 ms 3060 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 3 ms 3028 KB Output is correct
11 Correct 2 ms 3068 KB Output is correct
12 Correct 3 ms 3028 KB Output is correct
13 Correct 3 ms 3028 KB Output is correct
14 Correct 3 ms 3028 KB Output is correct
15 Correct 3 ms 3028 KB Output is correct
16 Correct 3 ms 3028 KB Output is correct
17 Correct 2 ms 3028 KB Output is correct
18 Correct 2 ms 3028 KB Output is correct
19 Correct 2 ms 2900 KB Output is correct
20 Correct 2 ms 3068 KB Output is correct
21 Correct 2 ms 3028 KB Output is correct
22 Correct 2 ms 2840 KB Output is correct
23 Correct 3 ms 3028 KB Output is correct
24 Correct 3 ms 3156 KB Output is correct
25 Correct 3 ms 3156 KB Output is correct
26 Correct 3 ms 3156 KB Output is correct
27 Correct 2 ms 3028 KB Output is correct
28 Correct 3 ms 3104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2672 KB Output is correct
5 Correct 3 ms 2796 KB Output is correct
6 Correct 2 ms 3028 KB Output is correct
7 Correct 2 ms 3028 KB Output is correct
8 Correct 3 ms 3028 KB Output is correct
9 Correct 2 ms 3060 KB Output is correct
10 Correct 75 ms 35980 KB Output is correct
11 Correct 93 ms 43312 KB Output is correct
12 Correct 93 ms 42756 KB Output is correct
13 Correct 103 ms 45072 KB Output is correct
14 Correct 90 ms 45588 KB Output is correct
15 Correct 3 ms 3028 KB Output is correct
16 Correct 2 ms 3068 KB Output is correct
17 Correct 3 ms 3028 KB Output is correct
18 Correct 3 ms 3028 KB Output is correct
19 Correct 3 ms 3028 KB Output is correct
20 Correct 3 ms 3028 KB Output is correct
21 Correct 3 ms 3028 KB Output is correct
22 Correct 2 ms 3028 KB Output is correct
23 Correct 2 ms 3028 KB Output is correct
24 Correct 2 ms 2900 KB Output is correct
25 Correct 2 ms 3068 KB Output is correct
26 Correct 2 ms 3028 KB Output is correct
27 Correct 2 ms 2840 KB Output is correct
28 Correct 3 ms 3028 KB Output is correct
29 Correct 3 ms 3156 KB Output is correct
30 Correct 3 ms 3156 KB Output is correct
31 Correct 3 ms 3156 KB Output is correct
32 Correct 2 ms 3028 KB Output is correct
33 Correct 3 ms 3104 KB Output is correct
34 Correct 11 ms 8528 KB Output is correct
35 Correct 96 ms 46304 KB Output is correct
36 Correct 92 ms 46308 KB Output is correct
37 Correct 91 ms 46300 KB Output is correct
38 Correct 103 ms 45676 KB Output is correct
39 Correct 95 ms 45368 KB Output is correct
40 Correct 81 ms 41724 KB Output is correct
41 Correct 99 ms 46648 KB Output is correct
42 Correct 92 ms 46364 KB Output is correct
43 Correct 76 ms 46648 KB Output is correct
44 Correct 93 ms 46428 KB Output is correct
45 Correct 105 ms 41788 KB Output is correct
46 Correct 119 ms 46296 KB Output is correct
47 Correct 100 ms 46120 KB Output is correct
48 Correct 94 ms 45864 KB Output is correct
49 Correct 61 ms 15376 KB Output is correct
50 Correct 52 ms 13392 KB Output is correct
51 Correct 85 ms 35236 KB Output is correct
52 Correct 121 ms 50864 KB Output is correct
53 Correct 150 ms 52016 KB Output is correct
54 Correct 135 ms 53244 KB Output is correct
55 Correct 76 ms 46524 KB Output is correct
56 Correct 123 ms 51416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2672 KB Output is correct
4 Correct 3 ms 2796 KB Output is correct
5 Correct 2 ms 3028 KB Output is correct
6 Correct 2 ms 3028 KB Output is correct
7 Correct 3 ms 3028 KB Output is correct
8 Correct 2 ms 3060 KB Output is correct
9 Correct 75 ms 35980 KB Output is correct
10 Correct 93 ms 43312 KB Output is correct
11 Correct 93 ms 42756 KB Output is correct
12 Correct 103 ms 45072 KB Output is correct
13 Correct 90 ms 45588 KB Output is correct
14 Correct 84 ms 36120 KB Output is correct
15 Correct 202 ms 45216 KB Output is correct
16 Correct 209 ms 44088 KB Output is correct
17 Correct 218 ms 46692 KB Output is correct
18 Correct 192 ms 46268 KB Output is correct
19 Correct 207 ms 45192 KB Output is correct
20 Correct 174 ms 47348 KB Output is correct
21 Correct 194 ms 46024 KB Output is correct
22 Correct 180 ms 47264 KB Output is correct
23 Correct 186 ms 46592 KB Output is correct
24 Correct 174 ms 44920 KB Output is correct
25 Correct 189 ms 46472 KB Output is correct
26 Correct 189 ms 44572 KB Output is correct
27 Correct 3 ms 3028 KB Output is correct
28 Correct 2 ms 3068 KB Output is correct
29 Correct 3 ms 3028 KB Output is correct
30 Correct 3 ms 3028 KB Output is correct
31 Correct 3 ms 3028 KB Output is correct
32 Correct 3 ms 3028 KB Output is correct
33 Correct 3 ms 3028 KB Output is correct
34 Correct 2 ms 3028 KB Output is correct
35 Correct 2 ms 3028 KB Output is correct
36 Correct 11 ms 8528 KB Output is correct
37 Correct 96 ms 46304 KB Output is correct
38 Correct 92 ms 46308 KB Output is correct
39 Correct 91 ms 46300 KB Output is correct
40 Correct 103 ms 45676 KB Output is correct
41 Correct 95 ms 45368 KB Output is correct
42 Correct 81 ms 41724 KB Output is correct
43 Correct 99 ms 46648 KB Output is correct
44 Correct 92 ms 46364 KB Output is correct
45 Correct 76 ms 46648 KB Output is correct
46 Correct 93 ms 46428 KB Output is correct
47 Correct 20 ms 8420 KB Output is correct
48 Correct 229 ms 50288 KB Output is correct
49 Correct 215 ms 50304 KB Output is correct
50 Correct 222 ms 50176 KB Output is correct
51 Correct 205 ms 49884 KB Output is correct
52 Correct 222 ms 47164 KB Output is correct
53 Correct 171 ms 35824 KB Output is correct
54 Correct 214 ms 50676 KB Output is correct
55 Correct 252 ms 50188 KB Output is correct
56 Correct 195 ms 50544 KB Output is correct
57 Correct 263 ms 50216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2672 KB Output is correct
5 Correct 3 ms 2796 KB Output is correct
6 Correct 2 ms 3028 KB Output is correct
7 Correct 2 ms 3028 KB Output is correct
8 Correct 3 ms 3028 KB Output is correct
9 Correct 2 ms 3060 KB Output is correct
10 Correct 75 ms 35980 KB Output is correct
11 Correct 93 ms 43312 KB Output is correct
12 Correct 93 ms 42756 KB Output is correct
13 Correct 103 ms 45072 KB Output is correct
14 Correct 90 ms 45588 KB Output is correct
15 Correct 84 ms 36120 KB Output is correct
16 Correct 202 ms 45216 KB Output is correct
17 Correct 209 ms 44088 KB Output is correct
18 Correct 218 ms 46692 KB Output is correct
19 Correct 192 ms 46268 KB Output is correct
20 Correct 207 ms 45192 KB Output is correct
21 Correct 174 ms 47348 KB Output is correct
22 Correct 194 ms 46024 KB Output is correct
23 Correct 180 ms 47264 KB Output is correct
24 Correct 186 ms 46592 KB Output is correct
25 Correct 174 ms 44920 KB Output is correct
26 Correct 189 ms 46472 KB Output is correct
27 Correct 189 ms 44572 KB Output is correct
28 Correct 3 ms 3028 KB Output is correct
29 Correct 2 ms 3068 KB Output is correct
30 Correct 3 ms 3028 KB Output is correct
31 Correct 3 ms 3028 KB Output is correct
32 Correct 3 ms 3028 KB Output is correct
33 Correct 3 ms 3028 KB Output is correct
34 Correct 3 ms 3028 KB Output is correct
35 Correct 2 ms 3028 KB Output is correct
36 Correct 2 ms 3028 KB Output is correct
37 Correct 11 ms 8528 KB Output is correct
38 Correct 96 ms 46304 KB Output is correct
39 Correct 92 ms 46308 KB Output is correct
40 Correct 91 ms 46300 KB Output is correct
41 Correct 103 ms 45676 KB Output is correct
42 Correct 95 ms 45368 KB Output is correct
43 Correct 81 ms 41724 KB Output is correct
44 Correct 99 ms 46648 KB Output is correct
45 Correct 92 ms 46364 KB Output is correct
46 Correct 76 ms 46648 KB Output is correct
47 Correct 93 ms 46428 KB Output is correct
48 Correct 20 ms 8420 KB Output is correct
49 Correct 229 ms 50288 KB Output is correct
50 Correct 215 ms 50304 KB Output is correct
51 Correct 222 ms 50176 KB Output is correct
52 Correct 205 ms 49884 KB Output is correct
53 Correct 222 ms 47164 KB Output is correct
54 Correct 171 ms 35824 KB Output is correct
55 Correct 214 ms 50676 KB Output is correct
56 Correct 252 ms 50188 KB Output is correct
57 Correct 195 ms 50544 KB Output is correct
58 Correct 263 ms 50216 KB Output is correct
59 Correct 79 ms 12812 KB Output is correct
60 Correct 206 ms 45408 KB Output is correct
61 Correct 225 ms 43804 KB Output is correct
62 Correct 220 ms 46544 KB Output is correct
63 Correct 185 ms 46476 KB Output is correct
64 Correct 2 ms 2900 KB Output is correct
65 Correct 2 ms 3068 KB Output is correct
66 Correct 2 ms 3028 KB Output is correct
67 Correct 2 ms 2840 KB Output is correct
68 Correct 3 ms 3028 KB Output is correct
69 Correct 3 ms 3156 KB Output is correct
70 Correct 3 ms 3156 KB Output is correct
71 Correct 3 ms 3156 KB Output is correct
72 Correct 2 ms 3028 KB Output is correct
73 Correct 3 ms 3104 KB Output is correct
74 Correct 105 ms 41788 KB Output is correct
75 Correct 119 ms 46296 KB Output is correct
76 Correct 100 ms 46120 KB Output is correct
77 Correct 94 ms 45864 KB Output is correct
78 Correct 61 ms 15376 KB Output is correct
79 Correct 52 ms 13392 KB Output is correct
80 Correct 85 ms 35236 KB Output is correct
81 Correct 121 ms 50864 KB Output is correct
82 Correct 150 ms 52016 KB Output is correct
83 Correct 135 ms 53244 KB Output is correct
84 Correct 76 ms 46524 KB Output is correct
85 Correct 123 ms 51416 KB Output is correct
86 Correct 66 ms 18536 KB Output is correct
87 Correct 254 ms 50264 KB Output is correct
88 Correct 211 ms 50148 KB Output is correct
89 Correct 195 ms 47168 KB Output is correct
90 Correct 135 ms 19128 KB Output is correct
91 Correct 149 ms 20296 KB Output is correct
92 Correct 199 ms 38736 KB Output is correct
93 Correct 244 ms 54452 KB Output is correct
94 Correct 281 ms 55984 KB Output is correct
95 Correct 270 ms 57072 KB Output is correct
96 Correct 189 ms 50400 KB Output is correct
97 Correct 235 ms 52776 KB Output is correct