답안 #1065251

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1065251 2024-08-19T04:41:14 Z Muhammet 자매 도시 (APIO20_swap) C++17
100 / 100
267 ms 102844 KB
#include <bits/stdc++.h>
#include "swap.h"
using namespace std;

#define ll long long
#define sz(s) (int)s.size()
#define ff first
#define ss second

const int N = 400005;

vector <pair<int,pair<int,int>>> v;

int p[N], sp[N][40], p1[N], depth[N], cnt = 0, com[N];

pair <int,int> c[N], d[N];

vector <int> v1[N], v2[N];

int f(int z){
    for(int i = 20; i >= 0; i--){
        if(sp[z][i] != -1 and c[sp[z][i]].ff != 1){
            z = sp[z][i];
        }
    }
    if(c[z].ff == 1) return c[z].ss;
    if(sp[z][0] == -1) return -1;
    z = sp[z][0];
    return c[z].ss;
}

void dfs(int x, int y){
    com[x] = cnt;
    for(auto i : v2[x]){
        if(i != y){
            depth[i] = depth[x]+1;
            dfs(i,x);
        }
    }
}

int fnd(int x){
    if(x == p[x]) return x;
    return p[x] = fnd(p[x]);
}

int lca(int x, int y){
    if(depth[x] < depth[y]) swap(x,y);
    for(int i = 20; i >= 0; i--){
        if(depth[x] - (1<<i) >= depth[y] and sp[x][i] != -1){
            x = sp[x][i];
        }
    }
    for(int i = 20; i >= 0; i--){
        if(sp[x][i] != sp[y][i]){
            x = sp[x][i];
            y = sp[y][i];
        }
    }
    if(sp[x][0] == -1 or sp[x][0] != sp[y][0]) return -1;
    return sp[x][0];
}

void init(int n, int m, vector<int> u1, vector<int> v1, vector<int> w1) {
    for(int i = 0; i < m; i++){
        v.push_back({w1[i],{u1[i],v1[i]}});
    }
    sort(v.begin(), v.end());

    for(int i = 0; i <= n+m; i++){
        p[i] = i;
        c[i] = {0,0};
        d[i] = {i,i};
        p1[i] = i;
    }

    int cn = n-1;
    for(int i = 0; i < sz(v); i++){
        auto x = v[i].ss;
        int a = fnd(p[x.ff]), b = fnd(p[x.ss]);
        cn++;
        // cout << a << ' ' << b << ' ' << cn << ' ';
        if(c[a].ff + c[b].ff == 2 and a == b){
            v2[cn].push_back(a);
            p[a] = cn;
            p1[a] = cn;
            c[cn] = c[a];
        }
        else if(c[a].ff + c[b].ff != 0){
            p[a] = cn;
            p1[a] = cn;
            p[b] = cn;
            p1[b] = cn;
            c[cn] = {1,v[i].ff};
            v2[cn].push_back(a);
            if(a != b) v2[cn].push_back(b);
        }
        else {
            if(a == b){
                c[cn] = {1,v[i].ff};
                p[a] = cn;
                p1[a] = cn;
                v2[cn].push_back(a);
            }
            else {
                v2[cn].push_back(a);
                v2[cn].push_back(b);
                p[a] = cn;
                p1[a] = cn;
                p[b] = cn;
                p1[b] = cn;
                bool tr1 = (d[a].ff == x.ff or d[a].ff == x.ss), tr11 = (d[a].ss == x.ff or d[a].ss == x.ss);
                bool tr2 = (d[b].ff == x.ff or d[b].ff == x.ss), tr22 = (d[b].ss == x.ff or d[b].ss == x.ss);
                if((tr1+tr11) >= 1 and (tr2+tr22) >= 1){
                    c[cn] = {0,v[i].ff};
                    if(tr1 == 1) d[cn] .ff = d[a].ss;
                    else d[cn].ff = d[a].ff;
                    if(tr2 == 1) d[cn].ss = d[b].ss;
                    else d[cn].ss = d[b].ff;
                }
                else {                    
                    c[cn] = {1,v[i].ff};
                }
            }
        }
        // cout << c[cn].ff << ' ' << c[cn].ss << '\n';
    }
    for(int i = cn; i >= 0; i--){
        if(depth[i] == 0){  
            cnt++;
            dfs(i,-1);
        }
    }
    for(int i = 0; i <= cn; i++){
        sp[i][0] = p1[i];
        if(p1[i] == i) sp[i][0] = -1;
    }
    for(int j = 1; j <= 20; j++){
        for(int i = 0; i <= cn; i++){
            if((1<<j) > depth[i]){
                sp[i][j] = -1;
                continue;
            }
            if(sp[i][j-1] != -1){
                sp[i][j] = (sp[sp[i][j-1]][j-1]);
            }
        }
    }
}

int getMinimumFuelCapacity(int x, int y) {
    if(com[x] != com[y]) return -1;
    int z = lca(x,y);
    if(z == -1) return z;
    // cout << z << '\n';
    return f(z);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19032 KB Output is correct
2 Correct 9 ms 19036 KB Output is correct
3 Correct 8 ms 19100 KB Output is correct
4 Correct 9 ms 19292 KB Output is correct
5 Correct 8 ms 19548 KB Output is correct
6 Correct 9 ms 19548 KB Output is correct
7 Correct 10 ms 19500 KB Output is correct
8 Correct 10 ms 19548 KB Output is correct
9 Correct 70 ms 55492 KB Output is correct
10 Correct 75 ms 63772 KB Output is correct
11 Correct 70 ms 62916 KB Output is correct
12 Correct 84 ms 65492 KB Output is correct
13 Correct 72 ms 68548 KB Output is correct
14 Correct 68 ms 55708 KB Output is correct
15 Correct 143 ms 68100 KB Output is correct
16 Correct 160 ms 66832 KB Output is correct
17 Correct 155 ms 69676 KB Output is correct
18 Correct 223 ms 72728 KB Output is correct
19 Correct 76 ms 31512 KB Output is correct
20 Correct 141 ms 69276 KB Output is correct
21 Correct 145 ms 67976 KB Output is correct
22 Correct 163 ms 70956 KB Output is correct
23 Correct 223 ms 74032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19032 KB Output is correct
2 Correct 9 ms 19036 KB Output is correct
3 Correct 207 ms 73576 KB Output is correct
4 Correct 209 ms 76336 KB Output is correct
5 Correct 254 ms 75000 KB Output is correct
6 Correct 208 ms 76080 KB Output is correct
7 Correct 215 ms 75620 KB Output is correct
8 Correct 204 ms 73440 KB Output is correct
9 Correct 207 ms 75252 KB Output is correct
10 Correct 214 ms 72872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19032 KB Output is correct
2 Correct 9 ms 19036 KB Output is correct
3 Correct 8 ms 19100 KB Output is correct
4 Correct 9 ms 19292 KB Output is correct
5 Correct 8 ms 19548 KB Output is correct
6 Correct 9 ms 19548 KB Output is correct
7 Correct 10 ms 19500 KB Output is correct
8 Correct 10 ms 19548 KB Output is correct
9 Correct 8 ms 19032 KB Output is correct
10 Correct 8 ms 19544 KB Output is correct
11 Correct 10 ms 19548 KB Output is correct
12 Correct 10 ms 19548 KB Output is correct
13 Correct 8 ms 19548 KB Output is correct
14 Correct 10 ms 19548 KB Output is correct
15 Correct 8 ms 19548 KB Output is correct
16 Correct 8 ms 19548 KB Output is correct
17 Correct 7 ms 19548 KB Output is correct
18 Correct 10 ms 19548 KB Output is correct
19 Correct 9 ms 19544 KB Output is correct
20 Correct 8 ms 19548 KB Output is correct
21 Correct 9 ms 19548 KB Output is correct
22 Correct 10 ms 19824 KB Output is correct
23 Correct 8 ms 19548 KB Output is correct
24 Correct 8 ms 19752 KB Output is correct
25 Correct 8 ms 19804 KB Output is correct
26 Correct 9 ms 20056 KB Output is correct
27 Correct 8 ms 19548 KB Output is correct
28 Correct 10 ms 19888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 19032 KB Output is correct
2 Correct 9 ms 19032 KB Output is correct
3 Correct 9 ms 19036 KB Output is correct
4 Correct 8 ms 19100 KB Output is correct
5 Correct 9 ms 19292 KB Output is correct
6 Correct 8 ms 19548 KB Output is correct
7 Correct 9 ms 19548 KB Output is correct
8 Correct 10 ms 19500 KB Output is correct
9 Correct 10 ms 19548 KB Output is correct
10 Correct 70 ms 55492 KB Output is correct
11 Correct 75 ms 63772 KB Output is correct
12 Correct 70 ms 62916 KB Output is correct
13 Correct 84 ms 65492 KB Output is correct
14 Correct 72 ms 68548 KB Output is correct
15 Correct 8 ms 19544 KB Output is correct
16 Correct 10 ms 19548 KB Output is correct
17 Correct 10 ms 19548 KB Output is correct
18 Correct 8 ms 19548 KB Output is correct
19 Correct 10 ms 19548 KB Output is correct
20 Correct 8 ms 19548 KB Output is correct
21 Correct 8 ms 19548 KB Output is correct
22 Correct 7 ms 19548 KB Output is correct
23 Correct 10 ms 19548 KB Output is correct
24 Correct 9 ms 19544 KB Output is correct
25 Correct 8 ms 19548 KB Output is correct
26 Correct 9 ms 19548 KB Output is correct
27 Correct 10 ms 19824 KB Output is correct
28 Correct 8 ms 19548 KB Output is correct
29 Correct 8 ms 19752 KB Output is correct
30 Correct 8 ms 19804 KB Output is correct
31 Correct 9 ms 20056 KB Output is correct
32 Correct 8 ms 19548 KB Output is correct
33 Correct 10 ms 19888 KB Output is correct
34 Correct 16 ms 25480 KB Output is correct
35 Correct 76 ms 65220 KB Output is correct
36 Correct 79 ms 65180 KB Output is correct
37 Correct 83 ms 65148 KB Output is correct
38 Correct 93 ms 64452 KB Output is correct
39 Correct 74 ms 64200 KB Output is correct
40 Correct 67 ms 60616 KB Output is correct
41 Correct 73 ms 65476 KB Output is correct
42 Correct 95 ms 65224 KB Output is correct
43 Correct 70 ms 69320 KB Output is correct
44 Correct 78 ms 65484 KB Output is correct
45 Correct 120 ms 80068 KB Output is correct
46 Correct 84 ms 65312 KB Output is correct
47 Correct 81 ms 65220 KB Output is correct
48 Correct 90 ms 70084 KB Output is correct
49 Correct 86 ms 72132 KB Output is correct
50 Correct 70 ms 60816 KB Output is correct
51 Correct 93 ms 71876 KB Output is correct
52 Correct 125 ms 86212 KB Output is correct
53 Correct 128 ms 90820 KB Output is correct
54 Correct 145 ms 98940 KB Output is correct
55 Correct 73 ms 69012 KB Output is correct
56 Correct 134 ms 91584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 19032 KB Output is correct
2 Correct 9 ms 19036 KB Output is correct
3 Correct 8 ms 19100 KB Output is correct
4 Correct 9 ms 19292 KB Output is correct
5 Correct 8 ms 19548 KB Output is correct
6 Correct 9 ms 19548 KB Output is correct
7 Correct 10 ms 19500 KB Output is correct
8 Correct 10 ms 19548 KB Output is correct
9 Correct 70 ms 55492 KB Output is correct
10 Correct 75 ms 63772 KB Output is correct
11 Correct 70 ms 62916 KB Output is correct
12 Correct 84 ms 65492 KB Output is correct
13 Correct 72 ms 68548 KB Output is correct
14 Correct 68 ms 55708 KB Output is correct
15 Correct 143 ms 68100 KB Output is correct
16 Correct 160 ms 66832 KB Output is correct
17 Correct 155 ms 69676 KB Output is correct
18 Correct 223 ms 72728 KB Output is correct
19 Correct 207 ms 73576 KB Output is correct
20 Correct 209 ms 76336 KB Output is correct
21 Correct 254 ms 75000 KB Output is correct
22 Correct 208 ms 76080 KB Output is correct
23 Correct 215 ms 75620 KB Output is correct
24 Correct 204 ms 73440 KB Output is correct
25 Correct 207 ms 75252 KB Output is correct
26 Correct 214 ms 72872 KB Output is correct
27 Correct 8 ms 19544 KB Output is correct
28 Correct 10 ms 19548 KB Output is correct
29 Correct 10 ms 19548 KB Output is correct
30 Correct 8 ms 19548 KB Output is correct
31 Correct 10 ms 19548 KB Output is correct
32 Correct 8 ms 19548 KB Output is correct
33 Correct 8 ms 19548 KB Output is correct
34 Correct 7 ms 19548 KB Output is correct
35 Correct 10 ms 19548 KB Output is correct
36 Correct 16 ms 25480 KB Output is correct
37 Correct 76 ms 65220 KB Output is correct
38 Correct 79 ms 65180 KB Output is correct
39 Correct 83 ms 65148 KB Output is correct
40 Correct 93 ms 64452 KB Output is correct
41 Correct 74 ms 64200 KB Output is correct
42 Correct 67 ms 60616 KB Output is correct
43 Correct 73 ms 65476 KB Output is correct
44 Correct 95 ms 65224 KB Output is correct
45 Correct 70 ms 69320 KB Output is correct
46 Correct 78 ms 65484 KB Output is correct
47 Correct 21 ms 25364 KB Output is correct
48 Correct 154 ms 70012 KB Output is correct
49 Correct 146 ms 70016 KB Output is correct
50 Correct 155 ms 69936 KB Output is correct
51 Correct 157 ms 69784 KB Output is correct
52 Correct 175 ms 66992 KB Output is correct
53 Correct 148 ms 55800 KB Output is correct
54 Correct 150 ms 70956 KB Output is correct
55 Correct 139 ms 70168 KB Output is correct
56 Correct 210 ms 73208 KB Output is correct
57 Correct 205 ms 71124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 19032 KB Output is correct
2 Correct 9 ms 19032 KB Output is correct
3 Correct 9 ms 19036 KB Output is correct
4 Correct 8 ms 19100 KB Output is correct
5 Correct 9 ms 19292 KB Output is correct
6 Correct 8 ms 19548 KB Output is correct
7 Correct 9 ms 19548 KB Output is correct
8 Correct 10 ms 19500 KB Output is correct
9 Correct 10 ms 19548 KB Output is correct
10 Correct 70 ms 55492 KB Output is correct
11 Correct 75 ms 63772 KB Output is correct
12 Correct 70 ms 62916 KB Output is correct
13 Correct 84 ms 65492 KB Output is correct
14 Correct 72 ms 68548 KB Output is correct
15 Correct 68 ms 55708 KB Output is correct
16 Correct 143 ms 68100 KB Output is correct
17 Correct 160 ms 66832 KB Output is correct
18 Correct 155 ms 69676 KB Output is correct
19 Correct 223 ms 72728 KB Output is correct
20 Correct 207 ms 73576 KB Output is correct
21 Correct 209 ms 76336 KB Output is correct
22 Correct 254 ms 75000 KB Output is correct
23 Correct 208 ms 76080 KB Output is correct
24 Correct 215 ms 75620 KB Output is correct
25 Correct 204 ms 73440 KB Output is correct
26 Correct 207 ms 75252 KB Output is correct
27 Correct 214 ms 72872 KB Output is correct
28 Correct 8 ms 19544 KB Output is correct
29 Correct 10 ms 19548 KB Output is correct
30 Correct 10 ms 19548 KB Output is correct
31 Correct 8 ms 19548 KB Output is correct
32 Correct 10 ms 19548 KB Output is correct
33 Correct 8 ms 19548 KB Output is correct
34 Correct 8 ms 19548 KB Output is correct
35 Correct 7 ms 19548 KB Output is correct
36 Correct 10 ms 19548 KB Output is correct
37 Correct 16 ms 25480 KB Output is correct
38 Correct 76 ms 65220 KB Output is correct
39 Correct 79 ms 65180 KB Output is correct
40 Correct 83 ms 65148 KB Output is correct
41 Correct 93 ms 64452 KB Output is correct
42 Correct 74 ms 64200 KB Output is correct
43 Correct 67 ms 60616 KB Output is correct
44 Correct 73 ms 65476 KB Output is correct
45 Correct 95 ms 65224 KB Output is correct
46 Correct 70 ms 69320 KB Output is correct
47 Correct 78 ms 65484 KB Output is correct
48 Correct 21 ms 25364 KB Output is correct
49 Correct 154 ms 70012 KB Output is correct
50 Correct 146 ms 70016 KB Output is correct
51 Correct 155 ms 69936 KB Output is correct
52 Correct 157 ms 69784 KB Output is correct
53 Correct 175 ms 66992 KB Output is correct
54 Correct 148 ms 55800 KB Output is correct
55 Correct 150 ms 70956 KB Output is correct
56 Correct 139 ms 70168 KB Output is correct
57 Correct 210 ms 73208 KB Output is correct
58 Correct 205 ms 71124 KB Output is correct
59 Correct 76 ms 31512 KB Output is correct
60 Correct 141 ms 69276 KB Output is correct
61 Correct 145 ms 67976 KB Output is correct
62 Correct 163 ms 70956 KB Output is correct
63 Correct 223 ms 74032 KB Output is correct
64 Correct 9 ms 19544 KB Output is correct
65 Correct 8 ms 19548 KB Output is correct
66 Correct 9 ms 19548 KB Output is correct
67 Correct 10 ms 19824 KB Output is correct
68 Correct 8 ms 19548 KB Output is correct
69 Correct 8 ms 19752 KB Output is correct
70 Correct 8 ms 19804 KB Output is correct
71 Correct 9 ms 20056 KB Output is correct
72 Correct 8 ms 19548 KB Output is correct
73 Correct 10 ms 19888 KB Output is correct
74 Correct 120 ms 80068 KB Output is correct
75 Correct 84 ms 65312 KB Output is correct
76 Correct 81 ms 65220 KB Output is correct
77 Correct 90 ms 70084 KB Output is correct
78 Correct 86 ms 72132 KB Output is correct
79 Correct 70 ms 60816 KB Output is correct
80 Correct 93 ms 71876 KB Output is correct
81 Correct 125 ms 86212 KB Output is correct
82 Correct 128 ms 90820 KB Output is correct
83 Correct 145 ms 98940 KB Output is correct
84 Correct 73 ms 69012 KB Output is correct
85 Correct 134 ms 91584 KB Output is correct
86 Correct 80 ms 41600 KB Output is correct
87 Correct 179 ms 70188 KB Output is correct
88 Correct 149 ms 70028 KB Output is correct
89 Correct 210 ms 72420 KB Output is correct
90 Correct 173 ms 79128 KB Output is correct
91 Correct 205 ms 75380 KB Output is correct
92 Correct 230 ms 74780 KB Output is correct
93 Correct 262 ms 89972 KB Output is correct
94 Correct 267 ms 95136 KB Output is correct
95 Correct 230 ms 102844 KB Output is correct
96 Correct 238 ms 73624 KB Output is correct
97 Correct 249 ms 84212 KB Output is correct