Submission #408710

# Submission time Handle Problem Language Result Execution time Memory
408710 2021-05-19T14:18:36 Z MOUF_MAHMALAT Swapping Cities (APIO20_swap) C++14
100 / 100
392 ms 60488 KB
#include "swap.h"
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
struct node
{
    ll u1,u2,w;
} a[200009];
ll n,p[300009],c[100009],ans[300009];
ll dp[300009][20],h[300009],logn;
vector<vector<ll> >v;
bool cmp(const node&xo,const node&yo)
{
    return xo.w<yo.w;
}
ll gp(ll z)
{
    if(p[z]==z)
        return z;
    return p[z]=gp(p[z]);
}
void mrg(ll x,ll y,ll z)
{
    c[x]++,c[y]++;
    bool is = (c[x]>2||c[y]>2);
    x=gp(x),y=gp(y);
    n++;
    p[x]=p[y]=p[n]=n;
    if(x==y)
    {
        is=1;
        v[n].push_back(x);
    }
    else
    {
        v[n].push_back(x);
        v[n].push_back(y);
    }
    if(ans[x]<2e9||ans[y]<2e9)
        is=1;
    if(is)
        ans[n]=z;
    else
        ans[n]=2e9;
}
void dfs(ll d,ll pp)
{
    dp[d][0]=pp;
    h[d]=h[pp]+1;
    ans[d]=min(ans[d],ans[pp]);
    for(auto z:v[d])
        dfs(z,d);
    if(ans[d]==2e9)
        ans[d]=-1;
}
void init(int N,int M,vector<int> U,vector<int> V,vector<int> W)
{
    v.resize(N+M+9);
    for(ll i=0; i<M; i++)
    {
        a[i].u1=U[i];
        a[i].u2=V[i];
        a[i].w =W[i];
    }
    sort(a,a+M,cmp);
    n=N-1;
    for(ll i=0; i<N; i++)
        p[i]=i,ans[i]=2e9;
    for(ll i=0; i<M; i++)
        mrg(a[i].u1,a[i].u2,a[i].w);
    logn=log2(n)+1;
    ll root=gp(0);
    dfs(root,root);
    for(ll j=1; j<=logn; j++)
        for(ll i=0; i<=n; i++)
            dp[i][j]=dp[ dp[i][j-1] ][j-1];
}
int getMinimumFuelCapacity(int x, int y)
{
    ll lca;
    if(h[x]<h[y])
        swap(x,y);
    ll dif=h[x]-h[y];
    for(ll i=logn; i>=0; i--)
        if(dif&(1<<i))
            x=dp[x][i];
    for(ll i=logn; i>=0; i--)
        if(dp[x][i]!=dp[y][i])
            x=dp[x][i],y=dp[y][i];
    if(x==y)
        lca=x;
    else
        lca=dp[x][0];
    return ans[lca];
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 105 ms 23776 KB Output is correct
10 Correct 127 ms 28908 KB Output is correct
11 Correct 125 ms 28416 KB Output is correct
12 Correct 128 ms 30004 KB Output is correct
13 Correct 120 ms 32340 KB Output is correct
14 Correct 111 ms 23788 KB Output is correct
15 Correct 258 ms 30824 KB Output is correct
16 Correct 267 ms 30048 KB Output is correct
17 Correct 265 ms 31812 KB Output is correct
18 Correct 392 ms 34088 KB Output is correct
19 Correct 92 ms 8384 KB Output is correct
20 Correct 251 ms 32184 KB Output is correct
21 Correct 269 ms 31268 KB Output is correct
22 Correct 263 ms 33188 KB Output is correct
23 Correct 357 ms 35496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 319 ms 35860 KB Output is correct
4 Correct 318 ms 37496 KB Output is correct
5 Correct 298 ms 36644 KB Output is correct
6 Correct 295 ms 37416 KB Output is correct
7 Correct 310 ms 37024 KB Output is correct
8 Correct 311 ms 35692 KB Output is correct
9 Correct 310 ms 36716 KB Output is correct
10 Correct 324 ms 35376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 2 ms 588 KB Output is correct
12 Correct 2 ms 588 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
14 Correct 2 ms 588 KB Output is correct
15 Correct 2 ms 588 KB Output is correct
16 Correct 2 ms 588 KB Output is correct
17 Correct 2 ms 588 KB Output is correct
18 Correct 2 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 2 ms 588 KB Output is correct
22 Correct 2 ms 716 KB Output is correct
23 Correct 1 ms 588 KB Output is correct
24 Correct 3 ms 716 KB Output is correct
25 Correct 2 ms 824 KB Output is correct
26 Correct 2 ms 844 KB Output is correct
27 Correct 2 ms 588 KB Output is correct
28 Correct 2 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 588 KB Output is correct
10 Correct 105 ms 23776 KB Output is correct
11 Correct 127 ms 28908 KB Output is correct
12 Correct 125 ms 28416 KB Output is correct
13 Correct 128 ms 30004 KB Output is correct
14 Correct 120 ms 32340 KB Output is correct
15 Correct 2 ms 588 KB Output is correct
16 Correct 2 ms 588 KB Output is correct
17 Correct 2 ms 588 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 2 ms 588 KB Output is correct
20 Correct 2 ms 588 KB Output is correct
21 Correct 2 ms 588 KB Output is correct
22 Correct 2 ms 588 KB Output is correct
23 Correct 2 ms 588 KB Output is correct
24 Correct 1 ms 588 KB Output is correct
25 Correct 1 ms 588 KB Output is correct
26 Correct 2 ms 588 KB Output is correct
27 Correct 2 ms 716 KB Output is correct
28 Correct 1 ms 588 KB Output is correct
29 Correct 3 ms 716 KB Output is correct
30 Correct 2 ms 824 KB Output is correct
31 Correct 2 ms 844 KB Output is correct
32 Correct 2 ms 588 KB Output is correct
33 Correct 2 ms 844 KB Output is correct
34 Correct 12 ms 4556 KB Output is correct
35 Correct 133 ms 31788 KB Output is correct
36 Correct 144 ms 31884 KB Output is correct
37 Correct 151 ms 31920 KB Output is correct
38 Correct 130 ms 31428 KB Output is correct
39 Correct 130 ms 31260 KB Output is correct
40 Correct 127 ms 28828 KB Output is correct
41 Correct 140 ms 32188 KB Output is correct
42 Correct 128 ms 31892 KB Output is correct
43 Correct 126 ms 34876 KB Output is correct
44 Correct 150 ms 32224 KB Output is correct
45 Correct 177 ms 43448 KB Output is correct
46 Correct 131 ms 31840 KB Output is correct
47 Correct 156 ms 31976 KB Output is correct
48 Correct 173 ms 35576 KB Output is correct
49 Correct 142 ms 39004 KB Output is correct
50 Correct 102 ms 30636 KB Output is correct
51 Correct 147 ms 37512 KB Output is correct
52 Correct 201 ms 47456 KB Output is correct
53 Correct 216 ms 50628 KB Output is correct
54 Correct 229 ms 56676 KB Output is correct
55 Correct 118 ms 34744 KB Output is correct
56 Correct 207 ms 51396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 105 ms 23776 KB Output is correct
10 Correct 127 ms 28908 KB Output is correct
11 Correct 125 ms 28416 KB Output is correct
12 Correct 128 ms 30004 KB Output is correct
13 Correct 120 ms 32340 KB Output is correct
14 Correct 111 ms 23788 KB Output is correct
15 Correct 258 ms 30824 KB Output is correct
16 Correct 267 ms 30048 KB Output is correct
17 Correct 265 ms 31812 KB Output is correct
18 Correct 392 ms 34088 KB Output is correct
19 Correct 319 ms 35860 KB Output is correct
20 Correct 318 ms 37496 KB Output is correct
21 Correct 298 ms 36644 KB Output is correct
22 Correct 295 ms 37416 KB Output is correct
23 Correct 310 ms 37024 KB Output is correct
24 Correct 311 ms 35692 KB Output is correct
25 Correct 310 ms 36716 KB Output is correct
26 Correct 324 ms 35376 KB Output is correct
27 Correct 2 ms 588 KB Output is correct
28 Correct 2 ms 588 KB Output is correct
29 Correct 2 ms 588 KB Output is correct
30 Correct 1 ms 588 KB Output is correct
31 Correct 2 ms 588 KB Output is correct
32 Correct 2 ms 588 KB Output is correct
33 Correct 2 ms 588 KB Output is correct
34 Correct 2 ms 588 KB Output is correct
35 Correct 2 ms 588 KB Output is correct
36 Correct 12 ms 4556 KB Output is correct
37 Correct 133 ms 31788 KB Output is correct
38 Correct 144 ms 31884 KB Output is correct
39 Correct 151 ms 31920 KB Output is correct
40 Correct 130 ms 31428 KB Output is correct
41 Correct 130 ms 31260 KB Output is correct
42 Correct 127 ms 28828 KB Output is correct
43 Correct 140 ms 32188 KB Output is correct
44 Correct 128 ms 31892 KB Output is correct
45 Correct 126 ms 34876 KB Output is correct
46 Correct 150 ms 32224 KB Output is correct
47 Correct 21 ms 4744 KB Output is correct
48 Correct 261 ms 36780 KB Output is correct
49 Correct 259 ms 36704 KB Output is correct
50 Correct 257 ms 36700 KB Output is correct
51 Correct 256 ms 36632 KB Output is correct
52 Correct 248 ms 34712 KB Output is correct
53 Correct 202 ms 27204 KB Output is correct
54 Correct 258 ms 37660 KB Output is correct
55 Correct 255 ms 36672 KB Output is correct
56 Correct 377 ms 39236 KB Output is correct
57 Correct 274 ms 37676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 588 KB Output is correct
10 Correct 105 ms 23776 KB Output is correct
11 Correct 127 ms 28908 KB Output is correct
12 Correct 125 ms 28416 KB Output is correct
13 Correct 128 ms 30004 KB Output is correct
14 Correct 120 ms 32340 KB Output is correct
15 Correct 111 ms 23788 KB Output is correct
16 Correct 258 ms 30824 KB Output is correct
17 Correct 267 ms 30048 KB Output is correct
18 Correct 265 ms 31812 KB Output is correct
19 Correct 392 ms 34088 KB Output is correct
20 Correct 319 ms 35860 KB Output is correct
21 Correct 318 ms 37496 KB Output is correct
22 Correct 298 ms 36644 KB Output is correct
23 Correct 295 ms 37416 KB Output is correct
24 Correct 310 ms 37024 KB Output is correct
25 Correct 311 ms 35692 KB Output is correct
26 Correct 310 ms 36716 KB Output is correct
27 Correct 324 ms 35376 KB Output is correct
28 Correct 2 ms 588 KB Output is correct
29 Correct 2 ms 588 KB Output is correct
30 Correct 2 ms 588 KB Output is correct
31 Correct 1 ms 588 KB Output is correct
32 Correct 2 ms 588 KB Output is correct
33 Correct 2 ms 588 KB Output is correct
34 Correct 2 ms 588 KB Output is correct
35 Correct 2 ms 588 KB Output is correct
36 Correct 2 ms 588 KB Output is correct
37 Correct 12 ms 4556 KB Output is correct
38 Correct 133 ms 31788 KB Output is correct
39 Correct 144 ms 31884 KB Output is correct
40 Correct 151 ms 31920 KB Output is correct
41 Correct 130 ms 31428 KB Output is correct
42 Correct 130 ms 31260 KB Output is correct
43 Correct 127 ms 28828 KB Output is correct
44 Correct 140 ms 32188 KB Output is correct
45 Correct 128 ms 31892 KB Output is correct
46 Correct 126 ms 34876 KB Output is correct
47 Correct 150 ms 32224 KB Output is correct
48 Correct 21 ms 4744 KB Output is correct
49 Correct 261 ms 36780 KB Output is correct
50 Correct 259 ms 36704 KB Output is correct
51 Correct 257 ms 36700 KB Output is correct
52 Correct 256 ms 36632 KB Output is correct
53 Correct 248 ms 34712 KB Output is correct
54 Correct 202 ms 27204 KB Output is correct
55 Correct 258 ms 37660 KB Output is correct
56 Correct 255 ms 36672 KB Output is correct
57 Correct 377 ms 39236 KB Output is correct
58 Correct 274 ms 37676 KB Output is correct
59 Correct 92 ms 8384 KB Output is correct
60 Correct 251 ms 32184 KB Output is correct
61 Correct 269 ms 31268 KB Output is correct
62 Correct 263 ms 33188 KB Output is correct
63 Correct 357 ms 35496 KB Output is correct
64 Correct 1 ms 588 KB Output is correct
65 Correct 1 ms 588 KB Output is correct
66 Correct 2 ms 588 KB Output is correct
67 Correct 2 ms 716 KB Output is correct
68 Correct 1 ms 588 KB Output is correct
69 Correct 3 ms 716 KB Output is correct
70 Correct 2 ms 824 KB Output is correct
71 Correct 2 ms 844 KB Output is correct
72 Correct 2 ms 588 KB Output is correct
73 Correct 2 ms 844 KB Output is correct
74 Correct 177 ms 43448 KB Output is correct
75 Correct 131 ms 31840 KB Output is correct
76 Correct 156 ms 31976 KB Output is correct
77 Correct 173 ms 35576 KB Output is correct
78 Correct 142 ms 39004 KB Output is correct
79 Correct 102 ms 30636 KB Output is correct
80 Correct 147 ms 37512 KB Output is correct
81 Correct 201 ms 47456 KB Output is correct
82 Correct 216 ms 50628 KB Output is correct
83 Correct 229 ms 56676 KB Output is correct
84 Correct 118 ms 34744 KB Output is correct
85 Correct 207 ms 51396 KB Output is correct
86 Correct 80 ms 16360 KB Output is correct
87 Correct 252 ms 36904 KB Output is correct
88 Correct 262 ms 36660 KB Output is correct
89 Correct 296 ms 38680 KB Output is correct
90 Correct 245 ms 45268 KB Output is correct
91 Correct 254 ms 42372 KB Output is correct
92 Correct 317 ms 41184 KB Output is correct
93 Correct 327 ms 51412 KB Output is correct
94 Correct 390 ms 55052 KB Output is correct
95 Correct 372 ms 60488 KB Output is correct
96 Correct 347 ms 39424 KB Output is correct
97 Correct 356 ms 47240 KB Output is correct