Submission #1055247

# Submission time Handle Problem Language Result Execution time Memory
1055247 2024-08-12T15:54:49 Z alexander707070 Swapping Cities (APIO20_swap) C++17
47 / 100
362 ms 45300 KB
#include<bits/stdc++.h>
#include "swap.h"

#define MAXN 100007
using namespace std;

int n,m;

struct edge{
    int from,to,cost;
    
    inline friend bool operator < (edge fr,edge sc){
        return fr.cost<sc.cost;
    }
}e[MAXN];

int closest[MAXN],special[MAXN],parent[MAXN];
int lt[MAXN],rt[MAXN],in[MAXN],out[MAXN],num;
vector<int> query[MAXN];
vector<int> tree[MAXN],edges;
bool treeedge[MAXN];

struct union_find{
    vector< pair<int,int> > dsu[MAXN];

    int sz[MAXN],trip[MAXN],deg[MAXN],tim,topv[MAXN];

    void reset(){
        for(int i=1;i<=n;i++){
            dsu[i]={{i,0}}; sz[i]=1; trip[i]=deg[i]=0;
            topv[i]=i;
        }
        tim=0;
    }

    int root(int x){
        if(dsu[x].back().first==x)return dsu[x].back().first;
        return root(dsu[x].back().first);
    }

    void mergev(int x,int y){
        tim++;

        int rootx=root(x);
        int rooty=root(y);

        deg[x]++; deg[y]++;

        if(deg[x]>=3)trip[rootx]=x;
        if(deg[y]>=3)trip[rootx]=y;

        if(rootx==rooty)return;
        if(sz[rootx]<sz[rooty])swap(rootx,rooty);

        dsu[rooty].push_back({rootx,tim});
        sz[rootx]+=sz[rooty];
        if(trip[rooty]!=0)trip[rootx]=trip[rooty];

        if(in[topv[rooty]]<in[topv[rootx]])topv[rootx]=topv[rooty];
    }

    int comp(int x,int when){
        int l=0,r=dsu[x].size(),tt;

        while(l+1<r){
            tt=(l+r)/2;
            if(dsu[x][tt].second<=when){
                l=tt;
            }else{
                r=tt;
            }
        }

        return dsu[x][l].first;
    }

    int pastroot(int x,int when){
        int curr=comp(x,when);

        if(curr==x)return x;
        return pastroot(curr,when);
    }
}graph,bcc;

void build_mst(){
    graph.reset();

    for(int i=1;i<=m;i++){
        graph.mergev(e[i].from,e[i].to);

        for(int f:query[i]){
            closest[f]=graph.trip[graph.root(f)];
        }
    }
}

void dfs(int x,int p){
    parent[x]=p;
    num++; in[x]=num;

    for(int i:tree[x]){
        if(i==p)continue;
        dfs(i,x);
    }

    out[x]=num;
}

bool subtree(int x,int y){
    return in[y]>=in[x] and out[y]<=out[x];
}

void init(int N, int M,vector<int> U, vector<int> V, vector<int> W) {
    
    n=N; m=M;
    for(int i=1;i<=m;i++){
        e[i]={U[i-1]+1,V[i-1]+1,W[i-1]};
    }

    sort(e+1,e+m+1);

    for(int i=1;i<=n;i++){
        lt[i]=0; rt[i]=m+1;
    }

    for(int i=1;i<=18;i++){
        for(int f=1;f<=m;f++)query[f].clear();

        for(int f=1;f<=n;f++){
            query[(lt[f]+rt[f])/2].push_back(f);
        }

        build_mst();

        for(int f=1;f<=n;f++){
            if(closest[f]!=0){
                rt[f]=(lt[f]+rt[f])/2;
                special[f]=closest[f];
            }else{
                lt[f]=(lt[f]+rt[f])/2;
            }
        }
    }

    bcc.reset();
    for(int i=1;i<=m;i++){
        if(bcc.root(e[i].from)==bcc.root(e[i].to))continue;

        tree[e[i].from].push_back(e[i].to);
        tree[e[i].to].push_back(e[i].from);

        bcc.mergev(e[i].from,e[i].to);
        treeedge[i]=true;
    }

    dfs(1,0);

    bcc.reset();
    for(int i=1;i<=m;i++){
        if(treeedge[i])continue;

        int x=bcc.root(e[i].from),y=bcc.root(e[i].to);
        while(!subtree(bcc.topv[x],bcc.topv[y])){
            bcc.mergev(x,bcc.root(parent[bcc.topv[x]]));
            x=bcc.root(x);

            bcc.tim--;
        }

        while(bcc.root(x)!=bcc.root(y)){
            bcc.mergev(y,bcc.root(parent[bcc.topv[y]]));
            y=bcc.root(y);

            bcc.tim--;
        }

        bcc.tim++;

        edges.push_back(e[i].cost);
    }
}

int getMinimumFuelCapacity(int X, int Y){
    X++; Y++;
    if(rt[X]==m+1)return -1;

    int ans=e[max(rt[X],rt[Y])].cost;

    int l=0,r=edges.size()+1,tt;
    while(l+1<r){
        tt=(l+r)/2;
        if(bcc.pastroot(X,tt)==bcc.pastroot(Y,tt)){
            r=tt;
        }else{
            l=tt;
        }
    }

    if(r!=edges.size()+1)ans=min(ans,edges[r-1]);

    l=0; r=m+1;
    while(l+1<r){
        tt=(l+r)/2;
        if(graph.pastroot(X,tt)==graph.pastroot(Y,tt)){
            r=tt;
        }else{
            l=tt;
        }
    }

    ans=max(ans,e[r].cost);

    return ans;
}

/*
int main(){
    init(5,5,{0,1,2,3,2},{1,2,3,0,4},{5,6,4,1,10});

    cout<<getMinimumFuelCapacity(0,2)<<"\n";
}
*/

Compilation message

swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:199:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  199 |     if(r!=edges.size()+1)ans=min(ans,edges[r-1]);
      |        ~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 2 ms 14936 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 2 ms 15040 KB Output is correct
6 Correct 2 ms 14940 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 3 ms 15196 KB Output is correct
9 Correct 138 ms 32804 KB Output is correct
10 Correct 176 ms 37524 KB Output is correct
11 Correct 182 ms 37068 KB Output is correct
12 Correct 178 ms 38408 KB Output is correct
13 Correct 122 ms 39432 KB Output is correct
14 Correct 139 ms 32464 KB Output is correct
15 Correct 200 ms 39508 KB Output is correct
16 Correct 204 ms 37948 KB Output is correct
17 Correct 212 ms 41160 KB Output is correct
18 Correct 150 ms 40476 KB Output is correct
19 Incorrect 47 ms 20820 KB Output isn't correct
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 2 ms 14936 KB Output is correct
3 Correct 196 ms 41164 KB Output is correct
4 Correct 207 ms 41780 KB Output is correct
5 Correct 192 ms 41524 KB Output is correct
6 Correct 204 ms 41872 KB Output is correct
7 Correct 206 ms 41800 KB Output is correct
8 Correct 200 ms 40932 KB Output is correct
9 Correct 207 ms 41472 KB Output is correct
10 Correct 202 ms 40840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 2 ms 14936 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 2 ms 15040 KB Output is correct
6 Correct 2 ms 14940 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 3 ms 15196 KB Output is correct
9 Correct 2 ms 14940 KB Output is correct
10 Correct 2 ms 14940 KB Output is correct
11 Correct 2 ms 14940 KB Output is correct
12 Correct 2 ms 14940 KB Output is correct
13 Correct 3 ms 14940 KB Output is correct
14 Correct 2 ms 15028 KB Output is correct
15 Correct 3 ms 14940 KB Output is correct
16 Correct 2 ms 14940 KB Output is correct
17 Correct 3 ms 15036 KB Output is correct
18 Correct 3 ms 14940 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 3 ms 14984 KB Output is correct
21 Correct 3 ms 15196 KB Output is correct
22 Correct 3 ms 14940 KB Output is correct
23 Correct 3 ms 15028 KB Output is correct
24 Correct 3 ms 15196 KB Output is correct
25 Correct 3 ms 15196 KB Output is correct
26 Correct 3 ms 15196 KB Output is correct
27 Correct 3 ms 15228 KB Output is correct
28 Correct 3 ms 15196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14940 KB Output is correct
2 Correct 2 ms 14936 KB Output is correct
3 Correct 2 ms 14936 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 2 ms 15040 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 2 ms 14940 KB Output is correct
9 Correct 3 ms 15196 KB Output is correct
10 Correct 138 ms 32804 KB Output is correct
11 Correct 176 ms 37524 KB Output is correct
12 Correct 182 ms 37068 KB Output is correct
13 Correct 178 ms 38408 KB Output is correct
14 Correct 122 ms 39432 KB Output is correct
15 Correct 2 ms 14940 KB Output is correct
16 Correct 2 ms 14940 KB Output is correct
17 Correct 2 ms 14940 KB Output is correct
18 Correct 3 ms 14940 KB Output is correct
19 Correct 2 ms 15028 KB Output is correct
20 Correct 3 ms 14940 KB Output is correct
21 Correct 2 ms 14940 KB Output is correct
22 Correct 3 ms 15036 KB Output is correct
23 Correct 3 ms 14940 KB Output is correct
24 Correct 3 ms 14940 KB Output is correct
25 Correct 3 ms 14984 KB Output is correct
26 Correct 3 ms 15196 KB Output is correct
27 Correct 3 ms 14940 KB Output is correct
28 Correct 3 ms 15028 KB Output is correct
29 Correct 3 ms 15196 KB Output is correct
30 Correct 3 ms 15196 KB Output is correct
31 Correct 3 ms 15196 KB Output is correct
32 Correct 3 ms 15228 KB Output is correct
33 Correct 3 ms 15196 KB Output is correct
34 Correct 23 ms 18008 KB Output is correct
35 Correct 185 ms 38888 KB Output is correct
36 Correct 197 ms 37652 KB Output is correct
37 Correct 189 ms 36936 KB Output is correct
38 Correct 184 ms 36836 KB Output is correct
39 Correct 172 ms 37212 KB Output is correct
40 Correct 168 ms 35788 KB Output is correct
41 Correct 188 ms 37392 KB Output is correct
42 Correct 180 ms 38156 KB Output is correct
43 Correct 161 ms 42444 KB Output is correct
44 Correct 188 ms 38844 KB Output is correct
45 Runtime error 35 ms 42320 KB Execution killed with signal 11
46 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 2 ms 14936 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 2 ms 15040 KB Output is correct
6 Correct 2 ms 14940 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 3 ms 15196 KB Output is correct
9 Correct 138 ms 32804 KB Output is correct
10 Correct 176 ms 37524 KB Output is correct
11 Correct 182 ms 37068 KB Output is correct
12 Correct 178 ms 38408 KB Output is correct
13 Correct 122 ms 39432 KB Output is correct
14 Correct 139 ms 32464 KB Output is correct
15 Correct 200 ms 39508 KB Output is correct
16 Correct 204 ms 37948 KB Output is correct
17 Correct 212 ms 41160 KB Output is correct
18 Correct 150 ms 40476 KB Output is correct
19 Correct 196 ms 41164 KB Output is correct
20 Correct 207 ms 41780 KB Output is correct
21 Correct 192 ms 41524 KB Output is correct
22 Correct 204 ms 41872 KB Output is correct
23 Correct 206 ms 41800 KB Output is correct
24 Correct 200 ms 40932 KB Output is correct
25 Correct 207 ms 41472 KB Output is correct
26 Correct 202 ms 40840 KB Output is correct
27 Correct 2 ms 14940 KB Output is correct
28 Correct 2 ms 14940 KB Output is correct
29 Correct 2 ms 14940 KB Output is correct
30 Correct 3 ms 14940 KB Output is correct
31 Correct 2 ms 15028 KB Output is correct
32 Correct 3 ms 14940 KB Output is correct
33 Correct 2 ms 14940 KB Output is correct
34 Correct 3 ms 15036 KB Output is correct
35 Correct 3 ms 14940 KB Output is correct
36 Correct 23 ms 18008 KB Output is correct
37 Correct 185 ms 38888 KB Output is correct
38 Correct 197 ms 37652 KB Output is correct
39 Correct 189 ms 36936 KB Output is correct
40 Correct 184 ms 36836 KB Output is correct
41 Correct 172 ms 37212 KB Output is correct
42 Correct 168 ms 35788 KB Output is correct
43 Correct 188 ms 37392 KB Output is correct
44 Correct 180 ms 38156 KB Output is correct
45 Correct 161 ms 42444 KB Output is correct
46 Correct 188 ms 38844 KB Output is correct
47 Correct 30 ms 18004 KB Output is correct
48 Correct 362 ms 40304 KB Output is correct
49 Correct 328 ms 39612 KB Output is correct
50 Correct 343 ms 39624 KB Output is correct
51 Correct 312 ms 39676 KB Output is correct
52 Correct 308 ms 38804 KB Output is correct
53 Correct 246 ms 33824 KB Output is correct
54 Correct 354 ms 40176 KB Output is correct
55 Correct 342 ms 40308 KB Output is correct
56 Correct 222 ms 45300 KB Output is correct
57 Correct 325 ms 42036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14940 KB Output is correct
2 Correct 2 ms 14936 KB Output is correct
3 Correct 2 ms 14936 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 2 ms 15040 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 2 ms 14940 KB Output is correct
9 Correct 3 ms 15196 KB Output is correct
10 Correct 138 ms 32804 KB Output is correct
11 Correct 176 ms 37524 KB Output is correct
12 Correct 182 ms 37068 KB Output is correct
13 Correct 178 ms 38408 KB Output is correct
14 Correct 122 ms 39432 KB Output is correct
15 Correct 139 ms 32464 KB Output is correct
16 Correct 200 ms 39508 KB Output is correct
17 Correct 204 ms 37948 KB Output is correct
18 Correct 212 ms 41160 KB Output is correct
19 Correct 150 ms 40476 KB Output is correct
20 Correct 196 ms 41164 KB Output is correct
21 Correct 207 ms 41780 KB Output is correct
22 Correct 192 ms 41524 KB Output is correct
23 Correct 204 ms 41872 KB Output is correct
24 Correct 206 ms 41800 KB Output is correct
25 Correct 200 ms 40932 KB Output is correct
26 Correct 207 ms 41472 KB Output is correct
27 Correct 202 ms 40840 KB Output is correct
28 Correct 2 ms 14940 KB Output is correct
29 Correct 2 ms 14940 KB Output is correct
30 Correct 2 ms 14940 KB Output is correct
31 Correct 3 ms 14940 KB Output is correct
32 Correct 2 ms 15028 KB Output is correct
33 Correct 3 ms 14940 KB Output is correct
34 Correct 2 ms 14940 KB Output is correct
35 Correct 3 ms 15036 KB Output is correct
36 Correct 3 ms 14940 KB Output is correct
37 Correct 23 ms 18008 KB Output is correct
38 Correct 185 ms 38888 KB Output is correct
39 Correct 197 ms 37652 KB Output is correct
40 Correct 189 ms 36936 KB Output is correct
41 Correct 184 ms 36836 KB Output is correct
42 Correct 172 ms 37212 KB Output is correct
43 Correct 168 ms 35788 KB Output is correct
44 Correct 188 ms 37392 KB Output is correct
45 Correct 180 ms 38156 KB Output is correct
46 Correct 161 ms 42444 KB Output is correct
47 Correct 188 ms 38844 KB Output is correct
48 Correct 30 ms 18004 KB Output is correct
49 Correct 362 ms 40304 KB Output is correct
50 Correct 328 ms 39612 KB Output is correct
51 Correct 343 ms 39624 KB Output is correct
52 Correct 312 ms 39676 KB Output is correct
53 Correct 308 ms 38804 KB Output is correct
54 Correct 246 ms 33824 KB Output is correct
55 Correct 354 ms 40176 KB Output is correct
56 Correct 342 ms 40308 KB Output is correct
57 Correct 222 ms 45300 KB Output is correct
58 Correct 325 ms 42036 KB Output is correct
59 Incorrect 47 ms 20820 KB Output isn't correct
60 Halted 0 ms 0 KB -