답안 #474278

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
474278 2021-09-17T18:41:27 Z JovanB 자매 도시 (APIO20_swap) C++17
100 / 100
495 ms 47920 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;

const int N = 100000;
const int LOG = 17;
const int INF = 1000000007;

int val[N+5][LOG+1];
int par[N+5][LOG+1];

vector <pair <int, int>> graf[N+5];

int tjm;
int in[N+5], out[N+5];

void dfs(int v, int p, int d){
    in[v] = ++tjm;
    par[v][0] = p;
    val[v][0] = d;
    for(int j=1; j<=LOG; j++){
        par[v][j] = par[par[v][j-1]][j-1];
        val[v][j] = max(val[v][j-1], val[par[v][j-1]][j-1]);
    }
    for(auto pr : graf[v]) if(pr.first != p) dfs(pr.first, v, pr.second);
    out[v] = tjm;
}

int moze[N+5];
int deg[N+5];
vector <int> vec[N+5];
int comp[N+5];
bool ima[N+5];

void init(int n, int m, vector <int> U, vector <int> V, vector <int> W) {
    for(int i=1; i<=n; i++) moze[i] = INF, comp[i] = i, vec[i].push_back(i);
    vector <tuple <int, int, int>> edges;
    for(int i=0; i<m; i++){
        int a = U[i] + 1;
        int b = V[i] + 1;
        int c = W[i];
        edges.push_back({c, a, b});
    }
    sort(edges.begin(), edges.end());
    for(auto e : edges){
        int c, a, b;
        tie(c, a, b) = e;
        if(comp[a] == comp[b]){
            if(ima[comp[a]]) continue;
            ima[comp[a]] = 1;
            for(auto x : vec[comp[a]]) moze[x] = c;
            continue;
        }
        graf[a].push_back({b, c});
        graf[b].push_back({a, c});
        bool novi = 0;
        deg[a]++;
        deg[b]++;
        if(deg[a] >= 3 || deg[b] >= 3) novi = 1;
        a = comp[a];
        b = comp[b];
        if(vec[a].size() < vec[b].size()) swap(a, b);
        if(!ima[a] && (novi || ima[b])){
            ima[a] = 1;
            for(auto x : vec[a]) moze[x] = c;
        }
        if(!ima[b] && (novi || ima[a])){
            ima[b] = 1;
            for(auto x : vec[b]) moze[x] = c;
        }
        for(auto x : vec[b]) comp[x] = a, vec[a].push_back(x);
        vec[b].clear();
    }
    dfs(1, 0, 0);
}

bool is_parent(int a, int b){ return (a == 0) || (in[a] <= in[b] && out[b] <= out[a]); }

int query(int x, int y){
    int res = 0;
    for(int j=LOG; j>=0; j--) if(!is_parent(par[x][j], y)) res = max(res, val[x][j]), x = par[x][j];
    if(!is_parent(x, y)) res = max(res, val[x][0]);
    for(int j=LOG; j>=0; j--) if(!is_parent(par[y][j], x)) res = max(res, val[y][j]), y = par[y][j];
    if(!is_parent(y, x)) res = max(res, val[y][0]);
    return res;
}

int getMinimumFuelCapacity(int x, int y) {
    x++;
    y++;
    int g = max(query(x, y), moze[x]);
    return (g == INF ? -1 : g);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 4 ms 5012 KB Output is correct
4 Correct 4 ms 5152 KB Output is correct
5 Correct 4 ms 5324 KB Output is correct
6 Correct 4 ms 5324 KB Output is correct
7 Correct 4 ms 5324 KB Output is correct
8 Correct 4 ms 5324 KB Output is correct
9 Correct 167 ms 32256 KB Output is correct
10 Correct 206 ms 38632 KB Output is correct
11 Correct 189 ms 37864 KB Output is correct
12 Correct 205 ms 39956 KB Output is correct
13 Correct 165 ms 38396 KB Output is correct
14 Correct 157 ms 32020 KB Output is correct
15 Correct 470 ms 42980 KB Output is correct
16 Correct 422 ms 40860 KB Output is correct
17 Correct 478 ms 45084 KB Output is correct
18 Correct 434 ms 41336 KB Output is correct
19 Correct 132 ms 15464 KB Output is correct
20 Correct 434 ms 42564 KB Output is correct
21 Correct 463 ms 41140 KB Output is correct
22 Correct 495 ms 44268 KB Output is correct
23 Correct 433 ms 41632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 242 ms 36412 KB Output is correct
4 Correct 239 ms 37480 KB Output is correct
5 Correct 258 ms 37136 KB Output is correct
6 Correct 237 ms 37396 KB Output is correct
7 Correct 242 ms 37396 KB Output is correct
8 Correct 228 ms 36284 KB Output is correct
9 Correct 234 ms 37108 KB Output is correct
10 Correct 230 ms 36028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 4 ms 5012 KB Output is correct
4 Correct 4 ms 5152 KB Output is correct
5 Correct 4 ms 5324 KB Output is correct
6 Correct 4 ms 5324 KB Output is correct
7 Correct 4 ms 5324 KB Output is correct
8 Correct 4 ms 5324 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 5 ms 5324 KB Output is correct
11 Correct 5 ms 5284 KB Output is correct
12 Correct 4 ms 5324 KB Output is correct
13 Correct 4 ms 5196 KB Output is correct
14 Correct 5 ms 5196 KB Output is correct
15 Correct 5 ms 5324 KB Output is correct
16 Correct 4 ms 5324 KB Output is correct
17 Correct 4 ms 5324 KB Output is correct
18 Correct 4 ms 5324 KB Output is correct
19 Correct 4 ms 5196 KB Output is correct
20 Correct 4 ms 5284 KB Output is correct
21 Correct 4 ms 5196 KB Output is correct
22 Correct 5 ms 5196 KB Output is correct
23 Correct 4 ms 5156 KB Output is correct
24 Correct 5 ms 5324 KB Output is correct
25 Correct 5 ms 5324 KB Output is correct
26 Correct 5 ms 5324 KB Output is correct
27 Correct 5 ms 5324 KB Output is correct
28 Correct 6 ms 5364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 5012 KB Output is correct
5 Correct 4 ms 5152 KB Output is correct
6 Correct 4 ms 5324 KB Output is correct
7 Correct 4 ms 5324 KB Output is correct
8 Correct 4 ms 5324 KB Output is correct
9 Correct 4 ms 5324 KB Output is correct
10 Correct 167 ms 32256 KB Output is correct
11 Correct 206 ms 38632 KB Output is correct
12 Correct 189 ms 37864 KB Output is correct
13 Correct 205 ms 39956 KB Output is correct
14 Correct 165 ms 38396 KB Output is correct
15 Correct 5 ms 5324 KB Output is correct
16 Correct 5 ms 5284 KB Output is correct
17 Correct 4 ms 5324 KB Output is correct
18 Correct 4 ms 5196 KB Output is correct
19 Correct 5 ms 5196 KB Output is correct
20 Correct 5 ms 5324 KB Output is correct
21 Correct 4 ms 5324 KB Output is correct
22 Correct 4 ms 5324 KB Output is correct
23 Correct 4 ms 5324 KB Output is correct
24 Correct 4 ms 5196 KB Output is correct
25 Correct 4 ms 5284 KB Output is correct
26 Correct 4 ms 5196 KB Output is correct
27 Correct 5 ms 5196 KB Output is correct
28 Correct 4 ms 5156 KB Output is correct
29 Correct 5 ms 5324 KB Output is correct
30 Correct 5 ms 5324 KB Output is correct
31 Correct 5 ms 5324 KB Output is correct
32 Correct 5 ms 5324 KB Output is correct
33 Correct 6 ms 5364 KB Output is correct
34 Correct 17 ms 9128 KB Output is correct
35 Correct 188 ms 39444 KB Output is correct
36 Correct 184 ms 37812 KB Output is correct
37 Correct 190 ms 36160 KB Output is correct
38 Correct 204 ms 35432 KB Output is correct
39 Correct 174 ms 34848 KB Output is correct
40 Correct 154 ms 32440 KB Output is correct
41 Correct 202 ms 38744 KB Output is correct
42 Correct 186 ms 39196 KB Output is correct
43 Correct 168 ms 37252 KB Output is correct
44 Correct 186 ms 35904 KB Output is correct
45 Correct 162 ms 31800 KB Output is correct
46 Correct 192 ms 37120 KB Output is correct
47 Correct 182 ms 34612 KB Output is correct
48 Correct 174 ms 33764 KB Output is correct
49 Correct 71 ms 14644 KB Output is correct
50 Correct 59 ms 13304 KB Output is correct
51 Correct 131 ms 27320 KB Output is correct
52 Correct 215 ms 40552 KB Output is correct
53 Correct 201 ms 39640 KB Output is correct
54 Correct 253 ms 43996 KB Output is correct
55 Correct 163 ms 37952 KB Output is correct
56 Correct 189 ms 38328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 4 ms 5012 KB Output is correct
4 Correct 4 ms 5152 KB Output is correct
5 Correct 4 ms 5324 KB Output is correct
6 Correct 4 ms 5324 KB Output is correct
7 Correct 4 ms 5324 KB Output is correct
8 Correct 4 ms 5324 KB Output is correct
9 Correct 167 ms 32256 KB Output is correct
10 Correct 206 ms 38632 KB Output is correct
11 Correct 189 ms 37864 KB Output is correct
12 Correct 205 ms 39956 KB Output is correct
13 Correct 165 ms 38396 KB Output is correct
14 Correct 157 ms 32020 KB Output is correct
15 Correct 470 ms 42980 KB Output is correct
16 Correct 422 ms 40860 KB Output is correct
17 Correct 478 ms 45084 KB Output is correct
18 Correct 434 ms 41336 KB Output is correct
19 Correct 242 ms 36412 KB Output is correct
20 Correct 239 ms 37480 KB Output is correct
21 Correct 258 ms 37136 KB Output is correct
22 Correct 237 ms 37396 KB Output is correct
23 Correct 242 ms 37396 KB Output is correct
24 Correct 228 ms 36284 KB Output is correct
25 Correct 234 ms 37108 KB Output is correct
26 Correct 230 ms 36028 KB Output is correct
27 Correct 5 ms 5324 KB Output is correct
28 Correct 5 ms 5284 KB Output is correct
29 Correct 4 ms 5324 KB Output is correct
30 Correct 4 ms 5196 KB Output is correct
31 Correct 5 ms 5196 KB Output is correct
32 Correct 5 ms 5324 KB Output is correct
33 Correct 4 ms 5324 KB Output is correct
34 Correct 4 ms 5324 KB Output is correct
35 Correct 4 ms 5324 KB Output is correct
36 Correct 17 ms 9128 KB Output is correct
37 Correct 188 ms 39444 KB Output is correct
38 Correct 184 ms 37812 KB Output is correct
39 Correct 190 ms 36160 KB Output is correct
40 Correct 204 ms 35432 KB Output is correct
41 Correct 174 ms 34848 KB Output is correct
42 Correct 154 ms 32440 KB Output is correct
43 Correct 202 ms 38744 KB Output is correct
44 Correct 186 ms 39196 KB Output is correct
45 Correct 168 ms 37252 KB Output is correct
46 Correct 186 ms 35904 KB Output is correct
47 Correct 27 ms 9164 KB Output is correct
48 Correct 454 ms 42248 KB Output is correct
49 Correct 456 ms 40888 KB Output is correct
50 Correct 407 ms 40248 KB Output is correct
51 Correct 463 ms 39636 KB Output is correct
52 Correct 339 ms 37304 KB Output is correct
53 Correct 250 ms 30508 KB Output is correct
54 Correct 422 ms 42476 KB Output is correct
55 Correct 425 ms 42872 KB Output is correct
56 Correct 414 ms 41404 KB Output is correct
57 Correct 329 ms 39908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 5012 KB Output is correct
5 Correct 4 ms 5152 KB Output is correct
6 Correct 4 ms 5324 KB Output is correct
7 Correct 4 ms 5324 KB Output is correct
8 Correct 4 ms 5324 KB Output is correct
9 Correct 4 ms 5324 KB Output is correct
10 Correct 167 ms 32256 KB Output is correct
11 Correct 206 ms 38632 KB Output is correct
12 Correct 189 ms 37864 KB Output is correct
13 Correct 205 ms 39956 KB Output is correct
14 Correct 165 ms 38396 KB Output is correct
15 Correct 157 ms 32020 KB Output is correct
16 Correct 470 ms 42980 KB Output is correct
17 Correct 422 ms 40860 KB Output is correct
18 Correct 478 ms 45084 KB Output is correct
19 Correct 434 ms 41336 KB Output is correct
20 Correct 242 ms 36412 KB Output is correct
21 Correct 239 ms 37480 KB Output is correct
22 Correct 258 ms 37136 KB Output is correct
23 Correct 237 ms 37396 KB Output is correct
24 Correct 242 ms 37396 KB Output is correct
25 Correct 228 ms 36284 KB Output is correct
26 Correct 234 ms 37108 KB Output is correct
27 Correct 230 ms 36028 KB Output is correct
28 Correct 5 ms 5324 KB Output is correct
29 Correct 5 ms 5284 KB Output is correct
30 Correct 4 ms 5324 KB Output is correct
31 Correct 4 ms 5196 KB Output is correct
32 Correct 5 ms 5196 KB Output is correct
33 Correct 5 ms 5324 KB Output is correct
34 Correct 4 ms 5324 KB Output is correct
35 Correct 4 ms 5324 KB Output is correct
36 Correct 4 ms 5324 KB Output is correct
37 Correct 17 ms 9128 KB Output is correct
38 Correct 188 ms 39444 KB Output is correct
39 Correct 184 ms 37812 KB Output is correct
40 Correct 190 ms 36160 KB Output is correct
41 Correct 204 ms 35432 KB Output is correct
42 Correct 174 ms 34848 KB Output is correct
43 Correct 154 ms 32440 KB Output is correct
44 Correct 202 ms 38744 KB Output is correct
45 Correct 186 ms 39196 KB Output is correct
46 Correct 168 ms 37252 KB Output is correct
47 Correct 186 ms 35904 KB Output is correct
48 Correct 27 ms 9164 KB Output is correct
49 Correct 454 ms 42248 KB Output is correct
50 Correct 456 ms 40888 KB Output is correct
51 Correct 407 ms 40248 KB Output is correct
52 Correct 463 ms 39636 KB Output is correct
53 Correct 339 ms 37304 KB Output is correct
54 Correct 250 ms 30508 KB Output is correct
55 Correct 422 ms 42476 KB Output is correct
56 Correct 425 ms 42872 KB Output is correct
57 Correct 414 ms 41404 KB Output is correct
58 Correct 329 ms 39908 KB Output is correct
59 Correct 132 ms 15464 KB Output is correct
60 Correct 434 ms 42564 KB Output is correct
61 Correct 463 ms 41140 KB Output is correct
62 Correct 495 ms 44268 KB Output is correct
63 Correct 433 ms 41632 KB Output is correct
64 Correct 4 ms 5196 KB Output is correct
65 Correct 4 ms 5284 KB Output is correct
66 Correct 4 ms 5196 KB Output is correct
67 Correct 5 ms 5196 KB Output is correct
68 Correct 4 ms 5156 KB Output is correct
69 Correct 5 ms 5324 KB Output is correct
70 Correct 5 ms 5324 KB Output is correct
71 Correct 5 ms 5324 KB Output is correct
72 Correct 5 ms 5324 KB Output is correct
73 Correct 6 ms 5364 KB Output is correct
74 Correct 162 ms 31800 KB Output is correct
75 Correct 192 ms 37120 KB Output is correct
76 Correct 182 ms 34612 KB Output is correct
77 Correct 174 ms 33764 KB Output is correct
78 Correct 71 ms 14644 KB Output is correct
79 Correct 59 ms 13304 KB Output is correct
80 Correct 131 ms 27320 KB Output is correct
81 Correct 215 ms 40552 KB Output is correct
82 Correct 201 ms 39640 KB Output is correct
83 Correct 253 ms 43996 KB Output is correct
84 Correct 163 ms 37952 KB Output is correct
85 Correct 189 ms 38328 KB Output is correct
86 Correct 77 ms 16088 KB Output is correct
87 Correct 409 ms 40592 KB Output is correct
88 Correct 418 ms 40676 KB Output is correct
89 Correct 288 ms 36024 KB Output is correct
90 Correct 176 ms 18600 KB Output is correct
91 Correct 186 ms 19384 KB Output is correct
92 Correct 285 ms 30872 KB Output is correct
93 Correct 434 ms 44084 KB Output is correct
94 Correct 374 ms 43444 KB Output is correct
95 Correct 494 ms 47920 KB Output is correct
96 Correct 427 ms 40088 KB Output is correct
97 Correct 356 ms 40256 KB Output is correct