답안 #952407

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
952407 2024-03-23T19:05:52 Z hyforces 자매 도시 (APIO20_swap) C++14
100 / 100
356 ms 48520 KB
#include<bits/stdc++.h>
#include"swap.h"
using namespace std;
#define N 100100
#define J 18
#define inf 0x3f3f3f3f
vector<pair<int,int>>adj[N];
int parent[N],fae[N];
pair<int,int>jump[N][J];
int tin[N],tout[N],t;
void dfs(int u,int fa){
    tin[u]=++t;
    jump[u][0]={parent[u],fae[u]};
    for(int a=1;a<J;++a){
        jump[u][a]={jump[jump[u][a-1].first][a-1].first,max({jump[u][a-1].second,jump[jump[u][a-1].first][a-1].second})};
    }
    for(auto a:adj[u]){
        if(a.first==fa)continue;
        fae[a.first]=a.second;
        parent[a.first]=u;
        dfs(a.first,u);
    }
    tout[u]=t;
}
bool isanc(int u,int v){
    return tin[u]<=tin[v]&&tout[v]<=tout[u];
}
int get(int u,int v){
    int ret=0;
    for(int a=J-1;a>=0;--a){
        if(!isanc(jump[u][a].first,v)){
            ret=max(ret,jump[u][a].second);
            u=jump[u][a].first;
        }
    }
    for(int a=J-1;a>=0;--a){
        if(!isanc(jump[v][a].first,u)){
            ret=max(ret,jump[v][a].second);
            v=jump[v][a].first;
        }
    }
    if(u==v)return ret;
    if(parent[u]==v)return max(ret,fae[u]);
    if(parent[v]==u)return max(ret,fae[v]);
    return max({ret,fae[u],fae[v]});
}

int deg[N],kt[N];
int ktv=0;

int getMinimumFuelCapacity(int u,int v){
    int ret=max(get(u,v),min(kt[u],kt[v]));
    return ret==inf?-1:max(ret,0);
}

struct DSU{
    bool mark[N];
    int fa[N];
    vector<int>buk[N];
    int root(int u){return fa[u]==u?u:fa[u]=root(fa[u]);}
    void activate(int u){
        u=root(u);
        if(mark[u])return;
        mark[u]=true;
        for(auto a:buk[u])kt[a]=ktv;
    }
    bool merge(int u,int v){
        u=root(u),v=root(v);
        if(u==v){
            activate(u);
            return false;
        }
        if(mark[u])activate(v);
        if(mark[v])activate(u);
        mark[u]|=mark[v];
        if(buk[u].size()<buk[v].size())buk[u].swap(buk[v]);
        for(auto a:buk[v])buk[u].push_back(a);
        buk[v].clear();
        fa[v]=u;
        return true;
    }
    void init(){
        for(int a=0;a<N;++a){
            fa[a]=a;
            buk[a].push_back(a);
        }
    }
}calc;

void init(int n,int m,vector<int>U,vector<int>V,vector<int>W){
    memset(kt,0x3f,sizeof(kt));
    calc.init();
    vector<int>ord;
    for(int a=0;a<m;++a)ord.push_back(a);
    sort(ord.begin(),ord.end(),[&](const int&x,const int&y)->bool{return W[x]<W[y];});
    for(int i=0;i<m;++i){int a=ord[i];
        ktv=W[a];
        if((++deg[U[a]])>=3)calc.activate(U[a]);
        if((++deg[V[a]])>=3)calc.activate(V[a]);
        if(calc.merge(U[a],V[a])){
            adj[U[a]].push_back({V[a],W[a]});
            adj[V[a]].push_back({U[a],W[a]});
        }
    }
    dfs(0,0);
}


# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 12636 KB Output is correct
2 Correct 5 ms 12636 KB Output is correct
3 Correct 5 ms 12636 KB Output is correct
4 Correct 7 ms 12636 KB Output is correct
5 Correct 6 ms 12888 KB Output is correct
6 Correct 7 ms 12892 KB Output is correct
7 Correct 7 ms 12952 KB Output is correct
8 Correct 7 ms 12892 KB Output is correct
9 Correct 88 ms 34748 KB Output is correct
10 Correct 116 ms 39380 KB Output is correct
11 Correct 126 ms 38524 KB Output is correct
12 Correct 121 ms 40396 KB Output is correct
13 Correct 119 ms 39376 KB Output is correct
14 Correct 116 ms 33984 KB Output is correct
15 Correct 277 ms 41184 KB Output is correct
16 Correct 355 ms 38612 KB Output is correct
17 Correct 289 ms 43852 KB Output is correct
18 Correct 262 ms 39632 KB Output is correct
19 Correct 90 ms 20756 KB Output is correct
20 Correct 333 ms 41348 KB Output is correct
21 Correct 268 ms 39760 KB Output is correct
22 Correct 288 ms 43128 KB Output is correct
23 Correct 295 ms 40344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 12636 KB Output is correct
2 Correct 5 ms 12636 KB Output is correct
3 Correct 147 ms 33624 KB Output is correct
4 Correct 170 ms 34664 KB Output is correct
5 Correct 156 ms 34028 KB Output is correct
6 Correct 141 ms 34484 KB Output is correct
7 Correct 142 ms 34340 KB Output is correct
8 Correct 159 ms 33520 KB Output is correct
9 Correct 156 ms 34120 KB Output is correct
10 Correct 137 ms 33424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 12636 KB Output is correct
2 Correct 5 ms 12636 KB Output is correct
3 Correct 5 ms 12636 KB Output is correct
4 Correct 7 ms 12636 KB Output is correct
5 Correct 6 ms 12888 KB Output is correct
6 Correct 7 ms 12892 KB Output is correct
7 Correct 7 ms 12952 KB Output is correct
8 Correct 7 ms 12892 KB Output is correct
9 Correct 6 ms 12632 KB Output is correct
10 Correct 6 ms 12888 KB Output is correct
11 Correct 7 ms 12892 KB Output is correct
12 Correct 6 ms 12720 KB Output is correct
13 Correct 6 ms 12892 KB Output is correct
14 Correct 7 ms 12888 KB Output is correct
15 Correct 6 ms 12888 KB Output is correct
16 Correct 6 ms 12892 KB Output is correct
17 Correct 6 ms 12892 KB Output is correct
18 Correct 6 ms 12892 KB Output is correct
19 Correct 6 ms 12892 KB Output is correct
20 Correct 6 ms 12892 KB Output is correct
21 Correct 8 ms 12912 KB Output is correct
22 Correct 6 ms 12888 KB Output is correct
23 Correct 6 ms 12892 KB Output is correct
24 Correct 8 ms 12980 KB Output is correct
25 Correct 7 ms 12840 KB Output is correct
26 Correct 8 ms 12892 KB Output is correct
27 Correct 7 ms 12940 KB Output is correct
28 Correct 8 ms 12820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12632 KB Output is correct
2 Correct 5 ms 12636 KB Output is correct
3 Correct 5 ms 12636 KB Output is correct
4 Correct 5 ms 12636 KB Output is correct
5 Correct 7 ms 12636 KB Output is correct
6 Correct 6 ms 12888 KB Output is correct
7 Correct 7 ms 12892 KB Output is correct
8 Correct 7 ms 12952 KB Output is correct
9 Correct 7 ms 12892 KB Output is correct
10 Correct 88 ms 34748 KB Output is correct
11 Correct 116 ms 39380 KB Output is correct
12 Correct 126 ms 38524 KB Output is correct
13 Correct 121 ms 40396 KB Output is correct
14 Correct 119 ms 39376 KB Output is correct
15 Correct 6 ms 12888 KB Output is correct
16 Correct 7 ms 12892 KB Output is correct
17 Correct 6 ms 12720 KB Output is correct
18 Correct 6 ms 12892 KB Output is correct
19 Correct 7 ms 12888 KB Output is correct
20 Correct 6 ms 12888 KB Output is correct
21 Correct 6 ms 12892 KB Output is correct
22 Correct 6 ms 12892 KB Output is correct
23 Correct 6 ms 12892 KB Output is correct
24 Correct 6 ms 12892 KB Output is correct
25 Correct 6 ms 12892 KB Output is correct
26 Correct 8 ms 12912 KB Output is correct
27 Correct 6 ms 12888 KB Output is correct
28 Correct 6 ms 12892 KB Output is correct
29 Correct 8 ms 12980 KB Output is correct
30 Correct 7 ms 12840 KB Output is correct
31 Correct 8 ms 12892 KB Output is correct
32 Correct 7 ms 12940 KB Output is correct
33 Correct 8 ms 12820 KB Output is correct
34 Correct 15 ms 14168 KB Output is correct
35 Correct 137 ms 41684 KB Output is correct
36 Correct 128 ms 39124 KB Output is correct
37 Correct 123 ms 37072 KB Output is correct
38 Correct 108 ms 35796 KB Output is correct
39 Correct 106 ms 35052 KB Output is correct
40 Correct 105 ms 33236 KB Output is correct
41 Correct 127 ms 40184 KB Output is correct
42 Correct 131 ms 41424 KB Output is correct
43 Correct 107 ms 39492 KB Output is correct
44 Correct 105 ms 35992 KB Output is correct
45 Correct 129 ms 33744 KB Output is correct
46 Correct 137 ms 37984 KB Output is correct
47 Correct 105 ms 34772 KB Output is correct
48 Correct 105 ms 33964 KB Output is correct
49 Correct 56 ms 20036 KB Output is correct
50 Correct 51 ms 18900 KB Output is correct
51 Correct 110 ms 30068 KB Output is correct
52 Correct 167 ms 40800 KB Output is correct
53 Correct 137 ms 39116 KB Output is correct
54 Correct 178 ms 44804 KB Output is correct
55 Correct 104 ms 41020 KB Output is correct
56 Correct 166 ms 37916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 12636 KB Output is correct
2 Correct 5 ms 12636 KB Output is correct
3 Correct 5 ms 12636 KB Output is correct
4 Correct 7 ms 12636 KB Output is correct
5 Correct 6 ms 12888 KB Output is correct
6 Correct 7 ms 12892 KB Output is correct
7 Correct 7 ms 12952 KB Output is correct
8 Correct 7 ms 12892 KB Output is correct
9 Correct 88 ms 34748 KB Output is correct
10 Correct 116 ms 39380 KB Output is correct
11 Correct 126 ms 38524 KB Output is correct
12 Correct 121 ms 40396 KB Output is correct
13 Correct 119 ms 39376 KB Output is correct
14 Correct 116 ms 33984 KB Output is correct
15 Correct 277 ms 41184 KB Output is correct
16 Correct 355 ms 38612 KB Output is correct
17 Correct 289 ms 43852 KB Output is correct
18 Correct 262 ms 39632 KB Output is correct
19 Correct 147 ms 33624 KB Output is correct
20 Correct 170 ms 34664 KB Output is correct
21 Correct 156 ms 34028 KB Output is correct
22 Correct 141 ms 34484 KB Output is correct
23 Correct 142 ms 34340 KB Output is correct
24 Correct 159 ms 33520 KB Output is correct
25 Correct 156 ms 34120 KB Output is correct
26 Correct 137 ms 33424 KB Output is correct
27 Correct 6 ms 12888 KB Output is correct
28 Correct 7 ms 12892 KB Output is correct
29 Correct 6 ms 12720 KB Output is correct
30 Correct 6 ms 12892 KB Output is correct
31 Correct 7 ms 12888 KB Output is correct
32 Correct 6 ms 12888 KB Output is correct
33 Correct 6 ms 12892 KB Output is correct
34 Correct 6 ms 12892 KB Output is correct
35 Correct 6 ms 12892 KB Output is correct
36 Correct 15 ms 14168 KB Output is correct
37 Correct 137 ms 41684 KB Output is correct
38 Correct 128 ms 39124 KB Output is correct
39 Correct 123 ms 37072 KB Output is correct
40 Correct 108 ms 35796 KB Output is correct
41 Correct 106 ms 35052 KB Output is correct
42 Correct 105 ms 33236 KB Output is correct
43 Correct 127 ms 40184 KB Output is correct
44 Correct 131 ms 41424 KB Output is correct
45 Correct 107 ms 39492 KB Output is correct
46 Correct 105 ms 35992 KB Output is correct
47 Correct 22 ms 14560 KB Output is correct
48 Correct 325 ms 44340 KB Output is correct
49 Correct 286 ms 42420 KB Output is correct
50 Correct 260 ms 41728 KB Output is correct
51 Correct 283 ms 40380 KB Output is correct
52 Correct 232 ms 38560 KB Output is correct
53 Correct 161 ms 33812 KB Output is correct
54 Correct 272 ms 44460 KB Output is correct
55 Correct 292 ms 45232 KB Output is correct
56 Correct 349 ms 44940 KB Output is correct
57 Correct 226 ms 40764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12632 KB Output is correct
2 Correct 5 ms 12636 KB Output is correct
3 Correct 5 ms 12636 KB Output is correct
4 Correct 5 ms 12636 KB Output is correct
5 Correct 7 ms 12636 KB Output is correct
6 Correct 6 ms 12888 KB Output is correct
7 Correct 7 ms 12892 KB Output is correct
8 Correct 7 ms 12952 KB Output is correct
9 Correct 7 ms 12892 KB Output is correct
10 Correct 88 ms 34748 KB Output is correct
11 Correct 116 ms 39380 KB Output is correct
12 Correct 126 ms 38524 KB Output is correct
13 Correct 121 ms 40396 KB Output is correct
14 Correct 119 ms 39376 KB Output is correct
15 Correct 116 ms 33984 KB Output is correct
16 Correct 277 ms 41184 KB Output is correct
17 Correct 355 ms 38612 KB Output is correct
18 Correct 289 ms 43852 KB Output is correct
19 Correct 262 ms 39632 KB Output is correct
20 Correct 147 ms 33624 KB Output is correct
21 Correct 170 ms 34664 KB Output is correct
22 Correct 156 ms 34028 KB Output is correct
23 Correct 141 ms 34484 KB Output is correct
24 Correct 142 ms 34340 KB Output is correct
25 Correct 159 ms 33520 KB Output is correct
26 Correct 156 ms 34120 KB Output is correct
27 Correct 137 ms 33424 KB Output is correct
28 Correct 6 ms 12888 KB Output is correct
29 Correct 7 ms 12892 KB Output is correct
30 Correct 6 ms 12720 KB Output is correct
31 Correct 6 ms 12892 KB Output is correct
32 Correct 7 ms 12888 KB Output is correct
33 Correct 6 ms 12888 KB Output is correct
34 Correct 6 ms 12892 KB Output is correct
35 Correct 6 ms 12892 KB Output is correct
36 Correct 6 ms 12892 KB Output is correct
37 Correct 15 ms 14168 KB Output is correct
38 Correct 137 ms 41684 KB Output is correct
39 Correct 128 ms 39124 KB Output is correct
40 Correct 123 ms 37072 KB Output is correct
41 Correct 108 ms 35796 KB Output is correct
42 Correct 106 ms 35052 KB Output is correct
43 Correct 105 ms 33236 KB Output is correct
44 Correct 127 ms 40184 KB Output is correct
45 Correct 131 ms 41424 KB Output is correct
46 Correct 107 ms 39492 KB Output is correct
47 Correct 105 ms 35992 KB Output is correct
48 Correct 22 ms 14560 KB Output is correct
49 Correct 325 ms 44340 KB Output is correct
50 Correct 286 ms 42420 KB Output is correct
51 Correct 260 ms 41728 KB Output is correct
52 Correct 283 ms 40380 KB Output is correct
53 Correct 232 ms 38560 KB Output is correct
54 Correct 161 ms 33812 KB Output is correct
55 Correct 272 ms 44460 KB Output is correct
56 Correct 292 ms 45232 KB Output is correct
57 Correct 349 ms 44940 KB Output is correct
58 Correct 226 ms 40764 KB Output is correct
59 Correct 90 ms 20756 KB Output is correct
60 Correct 333 ms 41348 KB Output is correct
61 Correct 268 ms 39760 KB Output is correct
62 Correct 288 ms 43128 KB Output is correct
63 Correct 295 ms 40344 KB Output is correct
64 Correct 6 ms 12892 KB Output is correct
65 Correct 6 ms 12892 KB Output is correct
66 Correct 8 ms 12912 KB Output is correct
67 Correct 6 ms 12888 KB Output is correct
68 Correct 6 ms 12892 KB Output is correct
69 Correct 8 ms 12980 KB Output is correct
70 Correct 7 ms 12840 KB Output is correct
71 Correct 8 ms 12892 KB Output is correct
72 Correct 7 ms 12940 KB Output is correct
73 Correct 8 ms 12820 KB Output is correct
74 Correct 129 ms 33744 KB Output is correct
75 Correct 137 ms 37984 KB Output is correct
76 Correct 105 ms 34772 KB Output is correct
77 Correct 105 ms 33964 KB Output is correct
78 Correct 56 ms 20036 KB Output is correct
79 Correct 51 ms 18900 KB Output is correct
80 Correct 110 ms 30068 KB Output is correct
81 Correct 167 ms 40800 KB Output is correct
82 Correct 137 ms 39116 KB Output is correct
83 Correct 178 ms 44804 KB Output is correct
84 Correct 104 ms 41020 KB Output is correct
85 Correct 166 ms 37916 KB Output is correct
86 Correct 58 ms 21720 KB Output is correct
87 Correct 301 ms 41584 KB Output is correct
88 Correct 273 ms 42088 KB Output is correct
89 Correct 249 ms 36944 KB Output is correct
90 Correct 129 ms 23760 KB Output is correct
91 Correct 142 ms 23976 KB Output is correct
92 Correct 206 ms 34484 KB Output is correct
93 Correct 307 ms 44956 KB Output is correct
94 Correct 264 ms 42812 KB Output is correct
95 Correct 356 ms 48520 KB Output is correct
96 Correct 298 ms 42696 KB Output is correct
97 Correct 256 ms 40828 KB Output is correct