답안 #1096043

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1096043 2024-10-03T15:49:39 Z onlk97 자매 도시 (APIO20_swap) C++14
100 / 100
215 ms 67308 KB
#include "swap.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;

bool cmp(pair <pair <int,int>,int> a,pair <pair <int,int>,int> b){
    return a.second<b.second;
}
int dsu0[100010],dsu[300010],Deg[100010],deg[300010][4],hvcyc[300010];
int prt0(int n){
    if (dsu0[n]==n) return n;
    return dsu0[n]=prt0(dsu0[n]);
}
void unn0(int u,int v){
    dsu0[prt0(u)]=dsu0[prt0(v)];
}
int prt(int n){
    if (dsu[n]==n) return n;
    return dsu[n]=prt(dsu[n]);
}
void unn(int u,int v){
    u=prt(u); v=prt(v);
    if (u==v) return;
    dsu[u]=v;
    hvcyc[v]|=hvcyc[u];
    for (int i=0; i<4; i++) deg[v][i]+=deg[u][i];
}
vector <int> krt[300010];
int cost[300010],ans[300010];
int dep[300010],fa[20][300010];
void dfs(int cur,int prv,int heeh){
    if (prv) dep[cur]=dep[prv]+1;
    fa[0][cur]=prv;
    if (hvcyc[cur]||deg[cur][3]) heeh=min(heeh,cost[cur]);
    ans[cur]=heeh;
    for (int i:krt[cur]) dfs(i,cur,heeh);
}
int lca(int u,int v){
    if (dep[u]<dep[v]) swap(u,v);
    int d=dep[u]-dep[v];
    for (int i=0; i<20; i++){
        if (d&(1<<i)) u=fa[i][u];
    }
    if (u==v) return u;
    for (int i=19; i>=0; i--){
        if (fa[i][u]!=fa[i][v]){
            u=fa[i][u];
            v=fa[i][v];
        }
    }
    return fa[0][u];
}
void init(int N,int M,vector <int> U,vector <int> V,vector <int> W){
    pair <pair <int,int>,int> edg[M+1];
    for (int i=1; i<=M; i++) edg[i]={{U[i-1]+1,V[i-1]+1},W[i-1]};
    sort(edg+1,edg+M+1,cmp);
    for (int i=1; i<=N; i++) dsu0[i]=i;
    for (int i=1; i<=N; i++) deg[i][0]=1;
    for (int i=1; i<=N+M; i++) dsu[i]=i;
    for (int i=1; i<=M; i++){
        if (prt0(edg[i].first.first)==prt0(edg[i].first.second)){
            int u=prt(edg[i].first.first);
            krt[N+i].push_back(u);
            unn(u,N+i);
            hvcyc[N+i]=1;
        } else {
            int u=prt(edg[i].first.first),v=prt(edg[i].first.second);
            krt[N+i].push_back(u); krt[N+i].push_back(v);
            unn(u,N+i); unn(v,N+i);
            unn0(edg[i].first.first,edg[i].first.second);
            if (Deg[edg[i].first.first]<3) deg[N+i][Deg[edg[i].first.first]]--;
            if (Deg[edg[i].first.second]<3) deg[N+i][Deg[edg[i].first.second]]--;
            Deg[edg[i].first.first]++; Deg[edg[i].first.second]++;
            deg[N+i][min(3,Deg[edg[i].first.first])]++;
            deg[N+i][min(3,Deg[edg[i].first.second])]++;
        }
        cost[N+i]=edg[i].second;
    }
    dfs(N+M,0,2e9);
    for (int i=1; i<20; i++){
        for (int j=1; j<=N+M; j++) fa[i][j]=fa[i-1][fa[i-1][j]];
    }
}

int getMinimumFuelCapacity(int X,int Y){
    X++; Y++;
    int l=lca(X,Y);
    int op=ans[l];
    if (op>1e9) op=-1;
    return op;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 4 ms 7772 KB Output is correct
5 Correct 4 ms 7892 KB Output is correct
6 Correct 4 ms 7772 KB Output is correct
7 Correct 4 ms 7888 KB Output is correct
8 Correct 4 ms 7772 KB Output is correct
9 Correct 54 ms 32216 KB Output is correct
10 Correct 67 ms 37804 KB Output is correct
11 Correct 60 ms 37292 KB Output is correct
12 Correct 73 ms 38988 KB Output is correct
13 Correct 66 ms 41300 KB Output is correct
14 Correct 63 ms 32592 KB Output is correct
15 Correct 153 ms 42024 KB Output is correct
16 Correct 128 ms 41140 KB Output is correct
17 Correct 142 ms 43068 KB Output is correct
18 Correct 190 ms 45492 KB Output is correct
19 Correct 60 ms 17492 KB Output is correct
20 Correct 134 ms 43476 KB Output is correct
21 Correct 141 ms 42448 KB Output is correct
22 Correct 140 ms 44464 KB Output is correct
23 Correct 180 ms 46760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 152 ms 46512 KB Output is correct
4 Correct 160 ms 48308 KB Output is correct
5 Correct 176 ms 47320 KB Output is correct
6 Correct 153 ms 47924 KB Output is correct
7 Correct 157 ms 47916 KB Output is correct
8 Correct 158 ms 46332 KB Output is correct
9 Correct 152 ms 47568 KB Output is correct
10 Correct 152 ms 46088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 4 ms 7772 KB Output is correct
5 Correct 4 ms 7892 KB Output is correct
6 Correct 4 ms 7772 KB Output is correct
7 Correct 4 ms 7888 KB Output is correct
8 Correct 4 ms 7772 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 3 ms 7768 KB Output is correct
11 Correct 4 ms 7772 KB Output is correct
12 Correct 4 ms 7772 KB Output is correct
13 Correct 3 ms 7772 KB Output is correct
14 Correct 4 ms 7772 KB Output is correct
15 Correct 4 ms 7772 KB Output is correct
16 Correct 5 ms 7772 KB Output is correct
17 Correct 4 ms 7772 KB Output is correct
18 Correct 4 ms 7768 KB Output is correct
19 Correct 3 ms 7788 KB Output is correct
20 Correct 4 ms 7772 KB Output is correct
21 Correct 4 ms 7772 KB Output is correct
22 Correct 4 ms 8028 KB Output is correct
23 Correct 4 ms 7772 KB Output is correct
24 Correct 4 ms 8028 KB Output is correct
25 Correct 4 ms 8072 KB Output is correct
26 Correct 4 ms 8028 KB Output is correct
27 Correct 3 ms 7976 KB Output is correct
28 Correct 4 ms 8028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 4 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 4 ms 7772 KB Output is correct
6 Correct 4 ms 7892 KB Output is correct
7 Correct 4 ms 7772 KB Output is correct
8 Correct 4 ms 7888 KB Output is correct
9 Correct 4 ms 7772 KB Output is correct
10 Correct 54 ms 32216 KB Output is correct
11 Correct 67 ms 37804 KB Output is correct
12 Correct 60 ms 37292 KB Output is correct
13 Correct 73 ms 38988 KB Output is correct
14 Correct 66 ms 41300 KB Output is correct
15 Correct 3 ms 7768 KB Output is correct
16 Correct 4 ms 7772 KB Output is correct
17 Correct 4 ms 7772 KB Output is correct
18 Correct 3 ms 7772 KB Output is correct
19 Correct 4 ms 7772 KB Output is correct
20 Correct 4 ms 7772 KB Output is correct
21 Correct 5 ms 7772 KB Output is correct
22 Correct 4 ms 7772 KB Output is correct
23 Correct 4 ms 7768 KB Output is correct
24 Correct 3 ms 7788 KB Output is correct
25 Correct 4 ms 7772 KB Output is correct
26 Correct 4 ms 7772 KB Output is correct
27 Correct 4 ms 8028 KB Output is correct
28 Correct 4 ms 7772 KB Output is correct
29 Correct 4 ms 8028 KB Output is correct
30 Correct 4 ms 8072 KB Output is correct
31 Correct 4 ms 8028 KB Output is correct
32 Correct 3 ms 7976 KB Output is correct
33 Correct 4 ms 8028 KB Output is correct
34 Correct 10 ms 11868 KB Output is correct
35 Correct 73 ms 38600 KB Output is correct
36 Correct 68 ms 38736 KB Output is correct
37 Correct 75 ms 38736 KB Output is correct
38 Correct 67 ms 38232 KB Output is correct
39 Correct 67 ms 37972 KB Output is correct
40 Correct 62 ms 35408 KB Output is correct
41 Correct 70 ms 38988 KB Output is correct
42 Correct 68 ms 38740 KB Output is correct
43 Correct 63 ms 41812 KB Output is correct
44 Correct 73 ms 39000 KB Output is correct
45 Correct 97 ms 50256 KB Output is correct
46 Correct 68 ms 38740 KB Output is correct
47 Correct 72 ms 38864 KB Output is correct
48 Correct 78 ms 42316 KB Output is correct
49 Correct 78 ms 46164 KB Output is correct
50 Correct 60 ms 37704 KB Output is correct
51 Correct 88 ms 44756 KB Output is correct
52 Correct 105 ms 54104 KB Output is correct
53 Correct 128 ms 57428 KB Output is correct
54 Correct 128 ms 63312 KB Output is correct
55 Correct 65 ms 41480 KB Output is correct
56 Correct 119 ms 58196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 4 ms 7772 KB Output is correct
5 Correct 4 ms 7892 KB Output is correct
6 Correct 4 ms 7772 KB Output is correct
7 Correct 4 ms 7888 KB Output is correct
8 Correct 4 ms 7772 KB Output is correct
9 Correct 54 ms 32216 KB Output is correct
10 Correct 67 ms 37804 KB Output is correct
11 Correct 60 ms 37292 KB Output is correct
12 Correct 73 ms 38988 KB Output is correct
13 Correct 66 ms 41300 KB Output is correct
14 Correct 63 ms 32592 KB Output is correct
15 Correct 153 ms 42024 KB Output is correct
16 Correct 128 ms 41140 KB Output is correct
17 Correct 142 ms 43068 KB Output is correct
18 Correct 190 ms 45492 KB Output is correct
19 Correct 152 ms 46512 KB Output is correct
20 Correct 160 ms 48308 KB Output is correct
21 Correct 176 ms 47320 KB Output is correct
22 Correct 153 ms 47924 KB Output is correct
23 Correct 157 ms 47916 KB Output is correct
24 Correct 158 ms 46332 KB Output is correct
25 Correct 152 ms 47568 KB Output is correct
26 Correct 152 ms 46088 KB Output is correct
27 Correct 3 ms 7768 KB Output is correct
28 Correct 4 ms 7772 KB Output is correct
29 Correct 4 ms 7772 KB Output is correct
30 Correct 3 ms 7772 KB Output is correct
31 Correct 4 ms 7772 KB Output is correct
32 Correct 4 ms 7772 KB Output is correct
33 Correct 5 ms 7772 KB Output is correct
34 Correct 4 ms 7772 KB Output is correct
35 Correct 4 ms 7768 KB Output is correct
36 Correct 10 ms 11868 KB Output is correct
37 Correct 73 ms 38600 KB Output is correct
38 Correct 68 ms 38736 KB Output is correct
39 Correct 75 ms 38736 KB Output is correct
40 Correct 67 ms 38232 KB Output is correct
41 Correct 67 ms 37972 KB Output is correct
42 Correct 62 ms 35408 KB Output is correct
43 Correct 70 ms 38988 KB Output is correct
44 Correct 68 ms 38740 KB Output is correct
45 Correct 63 ms 41812 KB Output is correct
46 Correct 73 ms 39000 KB Output is correct
47 Correct 19 ms 11848 KB Output is correct
48 Correct 154 ms 43616 KB Output is correct
49 Correct 143 ms 43444 KB Output is correct
50 Correct 138 ms 43444 KB Output is correct
51 Correct 141 ms 43204 KB Output is correct
52 Correct 155 ms 41468 KB Output is correct
53 Correct 126 ms 34112 KB Output is correct
54 Correct 147 ms 44476 KB Output is correct
55 Correct 141 ms 43444 KB Output is correct
56 Correct 177 ms 46008 KB Output is correct
57 Correct 161 ms 44588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 4 ms 7516 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 4 ms 7772 KB Output is correct
6 Correct 4 ms 7892 KB Output is correct
7 Correct 4 ms 7772 KB Output is correct
8 Correct 4 ms 7888 KB Output is correct
9 Correct 4 ms 7772 KB Output is correct
10 Correct 54 ms 32216 KB Output is correct
11 Correct 67 ms 37804 KB Output is correct
12 Correct 60 ms 37292 KB Output is correct
13 Correct 73 ms 38988 KB Output is correct
14 Correct 66 ms 41300 KB Output is correct
15 Correct 63 ms 32592 KB Output is correct
16 Correct 153 ms 42024 KB Output is correct
17 Correct 128 ms 41140 KB Output is correct
18 Correct 142 ms 43068 KB Output is correct
19 Correct 190 ms 45492 KB Output is correct
20 Correct 152 ms 46512 KB Output is correct
21 Correct 160 ms 48308 KB Output is correct
22 Correct 176 ms 47320 KB Output is correct
23 Correct 153 ms 47924 KB Output is correct
24 Correct 157 ms 47916 KB Output is correct
25 Correct 158 ms 46332 KB Output is correct
26 Correct 152 ms 47568 KB Output is correct
27 Correct 152 ms 46088 KB Output is correct
28 Correct 3 ms 7768 KB Output is correct
29 Correct 4 ms 7772 KB Output is correct
30 Correct 4 ms 7772 KB Output is correct
31 Correct 3 ms 7772 KB Output is correct
32 Correct 4 ms 7772 KB Output is correct
33 Correct 4 ms 7772 KB Output is correct
34 Correct 5 ms 7772 KB Output is correct
35 Correct 4 ms 7772 KB Output is correct
36 Correct 4 ms 7768 KB Output is correct
37 Correct 10 ms 11868 KB Output is correct
38 Correct 73 ms 38600 KB Output is correct
39 Correct 68 ms 38736 KB Output is correct
40 Correct 75 ms 38736 KB Output is correct
41 Correct 67 ms 38232 KB Output is correct
42 Correct 67 ms 37972 KB Output is correct
43 Correct 62 ms 35408 KB Output is correct
44 Correct 70 ms 38988 KB Output is correct
45 Correct 68 ms 38740 KB Output is correct
46 Correct 63 ms 41812 KB Output is correct
47 Correct 73 ms 39000 KB Output is correct
48 Correct 19 ms 11848 KB Output is correct
49 Correct 154 ms 43616 KB Output is correct
50 Correct 143 ms 43444 KB Output is correct
51 Correct 138 ms 43444 KB Output is correct
52 Correct 141 ms 43204 KB Output is correct
53 Correct 155 ms 41468 KB Output is correct
54 Correct 126 ms 34112 KB Output is correct
55 Correct 147 ms 44476 KB Output is correct
56 Correct 141 ms 43444 KB Output is correct
57 Correct 177 ms 46008 KB Output is correct
58 Correct 161 ms 44588 KB Output is correct
59 Correct 60 ms 17492 KB Output is correct
60 Correct 134 ms 43476 KB Output is correct
61 Correct 141 ms 42448 KB Output is correct
62 Correct 140 ms 44464 KB Output is correct
63 Correct 180 ms 46760 KB Output is correct
64 Correct 3 ms 7788 KB Output is correct
65 Correct 4 ms 7772 KB Output is correct
66 Correct 4 ms 7772 KB Output is correct
67 Correct 4 ms 8028 KB Output is correct
68 Correct 4 ms 7772 KB Output is correct
69 Correct 4 ms 8028 KB Output is correct
70 Correct 4 ms 8072 KB Output is correct
71 Correct 4 ms 8028 KB Output is correct
72 Correct 3 ms 7976 KB Output is correct
73 Correct 4 ms 8028 KB Output is correct
74 Correct 97 ms 50256 KB Output is correct
75 Correct 68 ms 38740 KB Output is correct
76 Correct 72 ms 38864 KB Output is correct
77 Correct 78 ms 42316 KB Output is correct
78 Correct 78 ms 46164 KB Output is correct
79 Correct 60 ms 37704 KB Output is correct
80 Correct 88 ms 44756 KB Output is correct
81 Correct 105 ms 54104 KB Output is correct
82 Correct 128 ms 57428 KB Output is correct
83 Correct 128 ms 63312 KB Output is correct
84 Correct 65 ms 41480 KB Output is correct
85 Correct 119 ms 58196 KB Output is correct
86 Correct 56 ms 23560 KB Output is correct
87 Correct 143 ms 43444 KB Output is correct
88 Correct 160 ms 43444 KB Output is correct
89 Correct 172 ms 45452 KB Output is correct
90 Correct 146 ms 52392 KB Output is correct
91 Correct 159 ms 49524 KB Output is correct
92 Correct 174 ms 48164 KB Output is correct
93 Correct 183 ms 58120 KB Output is correct
94 Correct 215 ms 62000 KB Output is correct
95 Correct 198 ms 67308 KB Output is correct
96 Correct 169 ms 46332 KB Output is correct
97 Correct 189 ms 54044 KB Output is correct