답안 #1055236

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1055236 2024-08-12T15:47:10 Z alexander707070 자매 도시 (APIO20_swap) C++17
30 / 100
457 ms 45228 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=-1,r=edges.size(),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())ans=min(ans,edges[r]);

    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, 6, {0, 0, 1, 1, 1, 2}, {1, 2, 2, 3, 4, 3}, {4, 4, 1, 2, 10, 3});

    cout<<getMinimumFuelCapacity(1, 2)<<"\n";
    cout<<getMinimumFuelCapacity(2,4)<<"\n";
    cout<<getMinimumFuelCapacity(0,1)<<"\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())ans=min(ans,edges[r]);
      |        ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 2 ms 14940 KB Output is correct
6 Correct 2 ms 14936 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
9 Correct 135 ms 32672 KB Output is correct
10 Correct 182 ms 37652 KB Output is correct
11 Correct 177 ms 36876 KB Output is correct
12 Correct 180 ms 38408 KB Output is correct
13 Correct 120 ms 39436 KB Output is correct
14 Correct 146 ms 32528 KB Output is correct
15 Correct 203 ms 39512 KB Output is correct
16 Correct 204 ms 37892 KB Output is correct
17 Correct 207 ms 41264 KB Output is correct
18 Correct 149 ms 40308 KB Output is correct
19 Incorrect 49 ms 20820 KB Output isn't correct
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 189 ms 41172 KB Output is correct
4 Correct 216 ms 41932 KB Output is correct
5 Correct 201 ms 41568 KB Output is correct
6 Correct 196 ms 41776 KB Output is correct
7 Correct 201 ms 41800 KB Output is correct
8 Correct 187 ms 40828 KB Output is correct
9 Correct 199 ms 41548 KB Output is correct
10 Correct 192 ms 40732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 2 ms 14940 KB Output is correct
6 Correct 2 ms 14936 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
9 Incorrect 2 ms 14940 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 14940 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 2 ms 14940 KB Output is correct
6 Correct 2 ms 14936 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
9 Correct 135 ms 32672 KB Output is correct
10 Correct 182 ms 37652 KB Output is correct
11 Correct 177 ms 36876 KB Output is correct
12 Correct 180 ms 38408 KB Output is correct
13 Correct 120 ms 39436 KB Output is correct
14 Correct 146 ms 32528 KB Output is correct
15 Correct 203 ms 39512 KB Output is correct
16 Correct 204 ms 37892 KB Output is correct
17 Correct 207 ms 41264 KB Output is correct
18 Correct 149 ms 40308 KB Output is correct
19 Correct 189 ms 41172 KB Output is correct
20 Correct 216 ms 41932 KB Output is correct
21 Correct 201 ms 41568 KB Output is correct
22 Correct 196 ms 41776 KB Output is correct
23 Correct 201 ms 41800 KB Output is correct
24 Correct 187 ms 40828 KB Output is correct
25 Correct 199 ms 41548 KB Output is correct
26 Correct 192 ms 40732 KB Output is correct
27 Correct 3 ms 14940 KB Output is correct
28 Correct 2 ms 14940 KB Output is correct
29 Correct 2 ms 15180 KB Output is correct
30 Correct 3 ms 14940 KB Output is correct
31 Correct 2 ms 14940 KB Output is correct
32 Correct 2 ms 14940 KB Output is correct
33 Correct 3 ms 14940 KB Output is correct
34 Correct 4 ms 15196 KB Output is correct
35 Correct 3 ms 14968 KB Output is correct
36 Correct 23 ms 18004 KB Output is correct
37 Correct 185 ms 38700 KB Output is correct
38 Correct 202 ms 37668 KB Output is correct
39 Correct 181 ms 37040 KB Output is correct
40 Correct 182 ms 36988 KB Output is correct
41 Correct 217 ms 37252 KB Output is correct
42 Correct 192 ms 35864 KB Output is correct
43 Correct 273 ms 37440 KB Output is correct
44 Correct 198 ms 38000 KB Output is correct
45 Correct 170 ms 42264 KB Output is correct
46 Correct 198 ms 38848 KB Output is correct
47 Correct 31 ms 17976 KB Output is correct
48 Correct 391 ms 40312 KB Output is correct
49 Correct 386 ms 39576 KB Output is correct
50 Correct 388 ms 39568 KB Output is correct
51 Correct 333 ms 39908 KB Output is correct
52 Correct 324 ms 38840 KB Output is correct
53 Correct 305 ms 33824 KB Output is correct
54 Correct 457 ms 40136 KB Output is correct
55 Correct 402 ms 40308 KB Output is correct
56 Correct 287 ms 45228 KB Output is correct
57 Correct 394 ms 41904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 14940 KB Output isn't correct
2 Halted 0 ms 0 KB -