답안 #683467

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
683467 2023-01-18T13:10:46 Z nguyentunglam 자매 도시 (APIO20_swap) C++14
100 / 100
441 ms 51320 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 2e5 + 10;
int lab[N], order[N], deg[N], noline[N], mx[N], h[N];
int par[20][N], f[20][N];
int cur;
vector<int> lst[N];
vector<pair<int, int> > adj[N];
int root(int v) {
    return lab[v] < 0 ? v : lab[v] = root(lab[v]);
}
void update(int u) {
    if (mx[u] <= 2) return;
    for(int j : lst[u]) noline[j] = cur;
    lst[u].clear();
}
bool join(int u, int v) {
    u = root(u); v = root(v);
    if (u == v) return 0;
    if (lab[u] > lab[v]) swap(u, v);
    mx[u] = max(mx[u], mx[v]);
    lab[u] += lab[v];
    for(int j : lst[v]) lst[u].push_back(j);
    lab[v] = u;
    return 1;
}
void dfs(int u) {
    for(auto [v, w] : adj[u]) if (v != par[0][u]) {
        par[0][v] = u;
        f[0][v] = w;
        for(int j = 1; j <= 17; j++) {
            int p = par[j - 1][v];
            par[j][v] = par[j - 1][p];
            f[j][v] = max(f[j - 1][v], f[j - 1][p]);
        }
        h[v] = h[u] + 1;
        dfs(v);
    }
}
int lca(int u, int v) {
    int ret = 0;
    if (h[u] < h[v]) swap(u, v);
    int delta = h[u] - h[v];
    for(int j = 0; j <= 17; j++) if ((delta >> j) & 1) {
        ret = max(ret, f[j][u]);
        u = par[j][u];
    }
    if (u == v) return ret;
    for(int j = 17; j >= 0; j--) if (par[j][u] != par[j][v]) {
        ret = max(ret, f[j][u]);
        ret = max(ret, f[j][v]);
        u = par[j][u];
        v = par[j][v];
    }
    return max({ret, f[0][u], f[0][v]});
}
void init(int n, int m, vector<int> u, vector<int> v, vector<int> w) {
    for(int i = 0; i < n; i++) {
        lab[i] = -1;
        lst[i].push_back(i);
    }
    for(int i = 0; i < m; i++) order[i] = i;
    sort(order, order + m, [&] (int &x, int &y) {
         return w[x] < w[y];
         });
    for(int j = 0; j < m; j++) {
        int i = order[j];
        cur = w[i];
        if (join(u[i], v[i])) {
            adj[u[i]].push_back({v[i], w[i]});
            adj[v[i]].push_back({u[i], w[i]});
        }
        else mx[root(u[i])] = 3;
        int r = root(u[i]);
        mx[r] = max(mx[r], max(++deg[u[i]], ++deg[v[i]]));
        update(r);
    }
    dfs(0);
}
int getMinimumFuelCapacity(int x, int y) {
    if (!noline[x]) return -1;
    return max(noline[x], lca(x, y));
}
//int main() {
//    #define task ""
//    cin.tie(0) -> sync_with_stdio(0);
//    if (fopen ("task.inp", "r")) {
//        freopen ("task.inp", "r", stdin);
//        freopen ("task.out", "w", stdout);
//    }
//    if (fopen (task".inp", "r")) {
//        freopen (task".inp", "r", stdin);
//        freopen (task".out", "w", stdout);
//    }
////    int u[1000] = {0, 0, 1, 1, 1, 2};
////    int v[1000] = {1, 2, 2, 3, 4, 3};
////    int w[1000] = {4, 4, 1, 2, 10, 3};
//    vector<int> u = {0, 0};
//    vector<int> v = {1, 2};
//    vector<int> w = {5, 5};
//    init(3, 2, u, v, w);
//    cout << getMinimumFuelCapacity(1, 2);
//}

Compilation message

swap.cpp: In function 'void dfs(int)':
swap.cpp:32:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   32 |     for(auto [v, w] : adj[u]) if (v != par[0][u]) {
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 6 ms 9940 KB Output is correct
3 Correct 6 ms 9928 KB Output is correct
4 Correct 5 ms 9940 KB Output is correct
5 Correct 6 ms 10196 KB Output is correct
6 Correct 7 ms 10132 KB Output is correct
7 Correct 6 ms 10196 KB Output is correct
8 Correct 6 ms 10196 KB Output is correct
9 Correct 138 ms 34884 KB Output is correct
10 Correct 166 ms 41284 KB Output is correct
11 Correct 186 ms 40372 KB Output is correct
12 Correct 188 ms 42368 KB Output is correct
13 Correct 136 ms 40900 KB Output is correct
14 Correct 129 ms 34372 KB Output is correct
15 Correct 217 ms 43456 KB Output is correct
16 Correct 222 ms 41112 KB Output is correct
17 Correct 239 ms 45780 KB Output is correct
18 Correct 193 ms 41560 KB Output is correct
19 Correct 101 ms 18668 KB Output is correct
20 Correct 347 ms 44236 KB Output is correct
21 Correct 347 ms 42816 KB Output is correct
22 Correct 369 ms 45812 KB Output is correct
23 Correct 334 ms 42932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 6 ms 9940 KB Output is correct
3 Correct 186 ms 37028 KB Output is correct
4 Correct 223 ms 38148 KB Output is correct
5 Correct 188 ms 37468 KB Output is correct
6 Correct 208 ms 37948 KB Output is correct
7 Correct 193 ms 37856 KB Output is correct
8 Correct 189 ms 36824 KB Output is correct
9 Correct 209 ms 37544 KB Output is correct
10 Correct 200 ms 36548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 6 ms 9940 KB Output is correct
3 Correct 6 ms 9928 KB Output is correct
4 Correct 5 ms 9940 KB Output is correct
5 Correct 6 ms 10196 KB Output is correct
6 Correct 7 ms 10132 KB Output is correct
7 Correct 6 ms 10196 KB Output is correct
8 Correct 6 ms 10196 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 6 ms 10068 KB Output is correct
11 Correct 6 ms 10100 KB Output is correct
12 Correct 6 ms 10196 KB Output is correct
13 Correct 6 ms 10068 KB Output is correct
14 Correct 6 ms 10068 KB Output is correct
15 Correct 6 ms 10196 KB Output is correct
16 Correct 7 ms 10196 KB Output is correct
17 Correct 6 ms 10196 KB Output is correct
18 Correct 7 ms 10068 KB Output is correct
19 Correct 5 ms 10068 KB Output is correct
20 Correct 5 ms 10180 KB Output is correct
21 Correct 6 ms 10068 KB Output is correct
22 Correct 5 ms 9940 KB Output is correct
23 Correct 5 ms 10068 KB Output is correct
24 Correct 6 ms 10228 KB Output is correct
25 Correct 6 ms 10176 KB Output is correct
26 Correct 7 ms 10164 KB Output is correct
27 Correct 6 ms 10224 KB Output is correct
28 Correct 6 ms 10196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 5 ms 9812 KB Output is correct
3 Correct 6 ms 9940 KB Output is correct
4 Correct 6 ms 9928 KB Output is correct
5 Correct 5 ms 9940 KB Output is correct
6 Correct 6 ms 10196 KB Output is correct
7 Correct 7 ms 10132 KB Output is correct
8 Correct 6 ms 10196 KB Output is correct
9 Correct 6 ms 10196 KB Output is correct
10 Correct 138 ms 34884 KB Output is correct
11 Correct 166 ms 41284 KB Output is correct
12 Correct 186 ms 40372 KB Output is correct
13 Correct 188 ms 42368 KB Output is correct
14 Correct 136 ms 40900 KB Output is correct
15 Correct 6 ms 10068 KB Output is correct
16 Correct 6 ms 10100 KB Output is correct
17 Correct 6 ms 10196 KB Output is correct
18 Correct 6 ms 10068 KB Output is correct
19 Correct 6 ms 10068 KB Output is correct
20 Correct 6 ms 10196 KB Output is correct
21 Correct 7 ms 10196 KB Output is correct
22 Correct 6 ms 10196 KB Output is correct
23 Correct 7 ms 10068 KB Output is correct
24 Correct 5 ms 10068 KB Output is correct
25 Correct 5 ms 10180 KB Output is correct
26 Correct 6 ms 10068 KB Output is correct
27 Correct 5 ms 9940 KB Output is correct
28 Correct 5 ms 10068 KB Output is correct
29 Correct 6 ms 10228 KB Output is correct
30 Correct 6 ms 10176 KB Output is correct
31 Correct 7 ms 10164 KB Output is correct
32 Correct 6 ms 10224 KB Output is correct
33 Correct 6 ms 10196 KB Output is correct
34 Correct 16 ms 13396 KB Output is correct
35 Correct 187 ms 41792 KB Output is correct
36 Correct 180 ms 39512 KB Output is correct
37 Correct 194 ms 37068 KB Output is correct
38 Correct 175 ms 35948 KB Output is correct
39 Correct 159 ms 34924 KB Output is correct
40 Correct 139 ms 32776 KB Output is correct
41 Correct 172 ms 40776 KB Output is correct
42 Correct 175 ms 41984 KB Output is correct
43 Correct 143 ms 39664 KB Output is correct
44 Correct 159 ms 35532 KB Output is correct
45 Correct 138 ms 31920 KB Output is correct
46 Correct 178 ms 40520 KB Output is correct
47 Correct 206 ms 37528 KB Output is correct
48 Correct 148 ms 36980 KB Output is correct
49 Correct 66 ms 17168 KB Output is correct
50 Correct 60 ms 17240 KB Output is correct
51 Correct 130 ms 30668 KB Output is correct
52 Correct 204 ms 43796 KB Output is correct
53 Correct 214 ms 41876 KB Output is correct
54 Correct 228 ms 47484 KB Output is correct
55 Correct 136 ms 42604 KB Output is correct
56 Correct 193 ms 41264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 6 ms 9940 KB Output is correct
3 Correct 6 ms 9928 KB Output is correct
4 Correct 5 ms 9940 KB Output is correct
5 Correct 6 ms 10196 KB Output is correct
6 Correct 7 ms 10132 KB Output is correct
7 Correct 6 ms 10196 KB Output is correct
8 Correct 6 ms 10196 KB Output is correct
9 Correct 138 ms 34884 KB Output is correct
10 Correct 166 ms 41284 KB Output is correct
11 Correct 186 ms 40372 KB Output is correct
12 Correct 188 ms 42368 KB Output is correct
13 Correct 136 ms 40900 KB Output is correct
14 Correct 129 ms 34372 KB Output is correct
15 Correct 217 ms 43456 KB Output is correct
16 Correct 222 ms 41112 KB Output is correct
17 Correct 239 ms 45780 KB Output is correct
18 Correct 193 ms 41560 KB Output is correct
19 Correct 186 ms 37028 KB Output is correct
20 Correct 223 ms 38148 KB Output is correct
21 Correct 188 ms 37468 KB Output is correct
22 Correct 208 ms 37948 KB Output is correct
23 Correct 193 ms 37856 KB Output is correct
24 Correct 189 ms 36824 KB Output is correct
25 Correct 209 ms 37544 KB Output is correct
26 Correct 200 ms 36548 KB Output is correct
27 Correct 6 ms 10068 KB Output is correct
28 Correct 6 ms 10100 KB Output is correct
29 Correct 6 ms 10196 KB Output is correct
30 Correct 6 ms 10068 KB Output is correct
31 Correct 6 ms 10068 KB Output is correct
32 Correct 6 ms 10196 KB Output is correct
33 Correct 7 ms 10196 KB Output is correct
34 Correct 6 ms 10196 KB Output is correct
35 Correct 7 ms 10068 KB Output is correct
36 Correct 16 ms 13396 KB Output is correct
37 Correct 187 ms 41792 KB Output is correct
38 Correct 180 ms 39512 KB Output is correct
39 Correct 194 ms 37068 KB Output is correct
40 Correct 175 ms 35948 KB Output is correct
41 Correct 159 ms 34924 KB Output is correct
42 Correct 139 ms 32776 KB Output is correct
43 Correct 172 ms 40776 KB Output is correct
44 Correct 175 ms 41984 KB Output is correct
45 Correct 143 ms 39664 KB Output is correct
46 Correct 159 ms 35532 KB Output is correct
47 Correct 24 ms 13392 KB Output is correct
48 Correct 371 ms 43256 KB Output is correct
49 Correct 410 ms 41092 KB Output is correct
50 Correct 368 ms 39828 KB Output is correct
51 Correct 395 ms 38768 KB Output is correct
52 Correct 317 ms 36424 KB Output is correct
53 Correct 231 ms 30436 KB Output is correct
54 Correct 414 ms 43404 KB Output is correct
55 Correct 349 ms 44072 KB Output is correct
56 Correct 301 ms 42752 KB Output is correct
57 Correct 304 ms 38912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 5 ms 9812 KB Output is correct
3 Correct 6 ms 9940 KB Output is correct
4 Correct 6 ms 9928 KB Output is correct
5 Correct 5 ms 9940 KB Output is correct
6 Correct 6 ms 10196 KB Output is correct
7 Correct 7 ms 10132 KB Output is correct
8 Correct 6 ms 10196 KB Output is correct
9 Correct 6 ms 10196 KB Output is correct
10 Correct 138 ms 34884 KB Output is correct
11 Correct 166 ms 41284 KB Output is correct
12 Correct 186 ms 40372 KB Output is correct
13 Correct 188 ms 42368 KB Output is correct
14 Correct 136 ms 40900 KB Output is correct
15 Correct 129 ms 34372 KB Output is correct
16 Correct 217 ms 43456 KB Output is correct
17 Correct 222 ms 41112 KB Output is correct
18 Correct 239 ms 45780 KB Output is correct
19 Correct 193 ms 41560 KB Output is correct
20 Correct 186 ms 37028 KB Output is correct
21 Correct 223 ms 38148 KB Output is correct
22 Correct 188 ms 37468 KB Output is correct
23 Correct 208 ms 37948 KB Output is correct
24 Correct 193 ms 37856 KB Output is correct
25 Correct 189 ms 36824 KB Output is correct
26 Correct 209 ms 37544 KB Output is correct
27 Correct 200 ms 36548 KB Output is correct
28 Correct 6 ms 10068 KB Output is correct
29 Correct 6 ms 10100 KB Output is correct
30 Correct 6 ms 10196 KB Output is correct
31 Correct 6 ms 10068 KB Output is correct
32 Correct 6 ms 10068 KB Output is correct
33 Correct 6 ms 10196 KB Output is correct
34 Correct 7 ms 10196 KB Output is correct
35 Correct 6 ms 10196 KB Output is correct
36 Correct 7 ms 10068 KB Output is correct
37 Correct 16 ms 13396 KB Output is correct
38 Correct 187 ms 41792 KB Output is correct
39 Correct 180 ms 39512 KB Output is correct
40 Correct 194 ms 37068 KB Output is correct
41 Correct 175 ms 35948 KB Output is correct
42 Correct 159 ms 34924 KB Output is correct
43 Correct 139 ms 32776 KB Output is correct
44 Correct 172 ms 40776 KB Output is correct
45 Correct 175 ms 41984 KB Output is correct
46 Correct 143 ms 39664 KB Output is correct
47 Correct 159 ms 35532 KB Output is correct
48 Correct 24 ms 13392 KB Output is correct
49 Correct 371 ms 43256 KB Output is correct
50 Correct 410 ms 41092 KB Output is correct
51 Correct 368 ms 39828 KB Output is correct
52 Correct 395 ms 38768 KB Output is correct
53 Correct 317 ms 36424 KB Output is correct
54 Correct 231 ms 30436 KB Output is correct
55 Correct 414 ms 43404 KB Output is correct
56 Correct 349 ms 44072 KB Output is correct
57 Correct 301 ms 42752 KB Output is correct
58 Correct 304 ms 38912 KB Output is correct
59 Correct 101 ms 18668 KB Output is correct
60 Correct 347 ms 44236 KB Output is correct
61 Correct 347 ms 42816 KB Output is correct
62 Correct 369 ms 45812 KB Output is correct
63 Correct 334 ms 42932 KB Output is correct
64 Correct 5 ms 10068 KB Output is correct
65 Correct 5 ms 10180 KB Output is correct
66 Correct 6 ms 10068 KB Output is correct
67 Correct 5 ms 9940 KB Output is correct
68 Correct 5 ms 10068 KB Output is correct
69 Correct 6 ms 10228 KB Output is correct
70 Correct 6 ms 10176 KB Output is correct
71 Correct 7 ms 10164 KB Output is correct
72 Correct 6 ms 10224 KB Output is correct
73 Correct 6 ms 10196 KB Output is correct
74 Correct 138 ms 31920 KB Output is correct
75 Correct 178 ms 40520 KB Output is correct
76 Correct 206 ms 37528 KB Output is correct
77 Correct 148 ms 36980 KB Output is correct
78 Correct 66 ms 17168 KB Output is correct
79 Correct 60 ms 17240 KB Output is correct
80 Correct 130 ms 30668 KB Output is correct
81 Correct 204 ms 43796 KB Output is correct
82 Correct 214 ms 41876 KB Output is correct
83 Correct 228 ms 47484 KB Output is correct
84 Correct 136 ms 42604 KB Output is correct
85 Correct 193 ms 41264 KB Output is correct
86 Correct 70 ms 20444 KB Output is correct
87 Correct 441 ms 44844 KB Output is correct
88 Correct 409 ms 44736 KB Output is correct
89 Correct 281 ms 40168 KB Output is correct
90 Correct 145 ms 21664 KB Output is correct
91 Correct 148 ms 23880 KB Output is correct
92 Correct 234 ms 35688 KB Output is correct
93 Correct 395 ms 48364 KB Output is correct
94 Correct 342 ms 46452 KB Output is correct
95 Correct 430 ms 51320 KB Output is correct
96 Correct 317 ms 45312 KB Output is correct
97 Correct 377 ms 44372 KB Output is correct