답안 #983331

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
983331 2024-05-15T10:17:31 Z ZHIRDILBILDIZ 자매 도시 (APIO20_swap) C++14
100 / 100
732 ms 89192 KB
#include <bits/stdc++.h>
#include "swap.h"
#define fi first
#define se second
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>

using namespace std;
const int N = 3e5 + 10;
struct edge {
    int from, to, w;
    edge () {}
    edge (int a, int b, int c) {
        from = a,
            to = b,
                w = c;
    }
};
bool cmp (edge e1, edge e2) {
    return e1.w < e2.w;
}
vector<edge> roads;

int SZ;
int kol;
int p[N];
int sz[N];
int st[N];
int ed[N];
int dist[N];
bool cycle[N];
vector<pii> gr[N];

void init_dsu () {
    for (int i = 0; i < N; ++i)
        p[i] = i,
            st[i] = i,
                ed[i] = i;
}
int get (int u) {
    if (u != p[u])
        p[u] = get(p[u]);
    return p[u];
}
void join (int u, int v, int w) {
    // cout << "join " << u << ' ' << v << '\n';
    int u1 = u, v1 = v;
    u = get(u);
    v = get(v);
    if (u == v) {
        ++kol;
        // cout << "add_road " << kol << ' ' << u << ' ' << w << '\n';
        gr[kol].push_back({u, w});
        p[u] = kol;
        cycle[kol] = 1;
        return;
    }
    // cout << "segments, u, v " << u << ' ' << v << ' ' << st[u] << ' ' << ed[u] << ' ' << st[v] << ' ' << ed[v] << '\n';
    ++kol;
    cycle[kol] = (cycle[u] | cycle[v]);
    if ((u1 == st[u] || u1 == ed[u]) && (v1 == st[v] || v1 == ed[v])) {
        set<int> all;
        all.insert(st[u]);
        all.insert(ed[u]);
        all.insert(st[v]);
        all.insert(ed[v]);

        if (st[u] != ed[u])
            all.erase(u1);
        if (st[v] != ed[v])
            all.erase(v1);


        st[kol] = (*all.begin());
        ed[kol] = (*all.rbegin());
    } else {
        cycle[kol] = 1;
    }
    // cout << "add_road " << kol << ' ' << u << ' ' << v << ' ' << w << '\n';
    gr[kol].push_back({u, w});
    gr[kol].push_back({v, w});

    p[u] = kol;
    p[v] = kol;
}

int timer;
int tin[N];
int tout[N];
int lc[N][20];
int mx[N][20];

void dfs(int city, int pr, int ls) {
    // cout << "dfs("<<city<<")\n";
    tin[city] = ++timer;

    lc[city][0] = pr;
    mx[city][0] = ls;

    for (int i = 1; i < 20; ++i) {
        lc[city][i] = lc[lc[city][i - 1]][i - 1];
        mx[city][i] = max(mx[city][i - 1], mx[lc[city][i - 1]][i - 1]);
    }

    for (auto i : gr[city]) {
        dist[i.fi] = dist[city] + 1;
        dfs(i.fi, city, i.se);
    }
    tout[city] = ++timer;
}
bool upper(int u, int v) {
    return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int get_on_dist (int u, int x) {
    int abu = 0;
    for (int i = 19; i >= 0; --i)
        if (abu + (1 << i) <= x && lc[u][i]) {
            u = lc[u][i];
            abu += (1 << i);
        }
    return u;
}
bool check_lca (int u, int v, int lca) {
    // cout << "check_lca u, v, lca = " << u << ' ' << v << ' ' << lca << ' ' << upper(lca, u) << ' ' << upper(lca, v) << '\n';
    if (upper(lca, u) && upper(lca, v))
        return 1;
    return 0;
}
int get_max (int lca, int v) {
    if (lca == v)
        return 0;
    int res = 0;
    for (int i = 19; i >= 0; --i)
        if (lc[v][i] && !upper(lc[v][i], lca)) {
            res = max(res, mx[v][i]);
            v = lc[v][i];
        }
    return max(res, mx[v][0]);
}

void init (int n, int m, vector<int> u, vector<int> v, vector<int> w) {

    kol = SZ = n;
    for (int i = 0; i < m; ++i) {
        u[i]++;
        v[i]++;
        edge e(u[i], v[i], w[i]);
        roads.push_back(e);
    }

    init_dsu();
    sort(roads.begin(), roads.end(), cmp);
    for (edge e : roads)
        join(e.from, e.to, e.w);
    // cout << "\ncycle\n";
    // for (int i=1;i<=kol;++i) {
    //     cout<<cycle[i] << ' ';
    // }
    // cout << '\n';

    dfs(kol, 0, 0);

}
int getMinimumFuelCapacity (int u, int v) {
    if (u == v)
        return -1;
    ++u;
    ++v;

    int l = 0, r = kol;
    while (l + 1 < r) {
        int mid = (l + r) >> 1;
        int lca = get_on_dist(u, mid);
        // cout << "mid, lca = " << l << ' ' << r << ' ' << mid << ' ' << lca << ' ' << check_lca(u, v, lca) << ' ' << cycle[lca] << '\n';
        if (!check_lca(u, v, lca) || !cycle[lca]) {
            l = mid;
        } else {
            r = mid;
        }
    }
    if (r == kol)
        return -1;
    int lca = get_on_dist(u, r);
    // cout << "u, v, lca = " << u << ' ' << v << ' ' << lca << '\n';
    return max(get_max(lca, u), get_max(lca, v));
}

// signed main () {
//     // ios_base::sync_with_stdio(0);
//     // cin.tie(0), cout.tie(0);

//     int n, m, q;
//     cin >> n >> m;
//     vector<int> u(m), v(m), w(m);
//     for (int i = 0; i < m; ++i)
//         cin >> u[i] >> v[i] >> w[i];
//     init(n, m, u, v, w);

//     cin >> q;
//     while (q--) {
//         int x, y;
//         cin >> x >> y;
//         cout << getMinimumFuelCapacity(x, y) << '\n';
//     }

//     return 0;
// }
// 5 6
// 0 1 4
// 0 2 4
// 2 1 1
// 2 3 3
// 1 3 2
// 1 4 10
//---------------------------
// 10 9
// 0 1 1
// 0 2 1
// 1 3 1
// 2 4 1
// 4 5 1
// 5 6 1
// 6 7 1
// 7 8 1
// 8 9 1
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19548 KB Output is correct
2 Correct 4 ms 19692 KB Output is correct
3 Correct 4 ms 19548 KB Output is correct
4 Correct 4 ms 19804 KB Output is correct
5 Correct 5 ms 19688 KB Output is correct
6 Correct 5 ms 19804 KB Output is correct
7 Correct 5 ms 19804 KB Output is correct
8 Correct 5 ms 19804 KB Output is correct
9 Correct 104 ms 50632 KB Output is correct
10 Correct 125 ms 58120 KB Output is correct
11 Correct 124 ms 58056 KB Output is correct
12 Correct 135 ms 58616 KB Output is correct
13 Correct 136 ms 61020 KB Output is correct
14 Correct 115 ms 50992 KB Output is correct
15 Correct 290 ms 62392 KB Output is correct
16 Correct 269 ms 62068 KB Output is correct
17 Correct 292 ms 62668 KB Output is correct
18 Correct 332 ms 65064 KB Output is correct
19 Correct 159 ms 31636 KB Output is correct
20 Correct 331 ms 63448 KB Output is correct
21 Correct 341 ms 63420 KB Output is correct
22 Correct 349 ms 64036 KB Output is correct
23 Correct 634 ms 66344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19548 KB Output is correct
2 Correct 4 ms 19692 KB Output is correct
3 Correct 642 ms 67404 KB Output is correct
4 Correct 609 ms 67872 KB Output is correct
5 Correct 732 ms 67800 KB Output is correct
6 Correct 560 ms 67776 KB Output is correct
7 Correct 651 ms 67912 KB Output is correct
8 Correct 638 ms 67352 KB Output is correct
9 Correct 625 ms 67708 KB Output is correct
10 Correct 639 ms 65412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19548 KB Output is correct
2 Correct 4 ms 19692 KB Output is correct
3 Correct 4 ms 19548 KB Output is correct
4 Correct 4 ms 19804 KB Output is correct
5 Correct 5 ms 19688 KB Output is correct
6 Correct 5 ms 19804 KB Output is correct
7 Correct 5 ms 19804 KB Output is correct
8 Correct 5 ms 19804 KB Output is correct
9 Correct 4 ms 19548 KB Output is correct
10 Correct 5 ms 19736 KB Output is correct
11 Correct 5 ms 19804 KB Output is correct
12 Correct 4 ms 19804 KB Output is correct
13 Correct 5 ms 19804 KB Output is correct
14 Correct 4 ms 19804 KB Output is correct
15 Correct 4 ms 19804 KB Output is correct
16 Correct 5 ms 19804 KB Output is correct
17 Correct 5 ms 20060 KB Output is correct
18 Correct 4 ms 19804 KB Output is correct
19 Correct 4 ms 19804 KB Output is correct
20 Correct 5 ms 19804 KB Output is correct
21 Correct 4 ms 19800 KB Output is correct
22 Correct 5 ms 20056 KB Output is correct
23 Correct 6 ms 19800 KB Output is correct
24 Correct 5 ms 22108 KB Output is correct
25 Correct 5 ms 22108 KB Output is correct
26 Correct 5 ms 21976 KB Output is correct
27 Correct 5 ms 19804 KB Output is correct
28 Correct 6 ms 22108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19548 KB Output is correct
2 Correct 4 ms 19548 KB Output is correct
3 Correct 4 ms 19692 KB Output is correct
4 Correct 4 ms 19548 KB Output is correct
5 Correct 4 ms 19804 KB Output is correct
6 Correct 5 ms 19688 KB Output is correct
7 Correct 5 ms 19804 KB Output is correct
8 Correct 5 ms 19804 KB Output is correct
9 Correct 5 ms 19804 KB Output is correct
10 Correct 104 ms 50632 KB Output is correct
11 Correct 125 ms 58120 KB Output is correct
12 Correct 124 ms 58056 KB Output is correct
13 Correct 135 ms 58616 KB Output is correct
14 Correct 136 ms 61020 KB Output is correct
15 Correct 5 ms 19736 KB Output is correct
16 Correct 5 ms 19804 KB Output is correct
17 Correct 4 ms 19804 KB Output is correct
18 Correct 5 ms 19804 KB Output is correct
19 Correct 4 ms 19804 KB Output is correct
20 Correct 4 ms 19804 KB Output is correct
21 Correct 5 ms 19804 KB Output is correct
22 Correct 5 ms 20060 KB Output is correct
23 Correct 4 ms 19804 KB Output is correct
24 Correct 4 ms 19804 KB Output is correct
25 Correct 5 ms 19804 KB Output is correct
26 Correct 4 ms 19800 KB Output is correct
27 Correct 5 ms 20056 KB Output is correct
28 Correct 6 ms 19800 KB Output is correct
29 Correct 5 ms 22108 KB Output is correct
30 Correct 5 ms 22108 KB Output is correct
31 Correct 5 ms 21976 KB Output is correct
32 Correct 5 ms 19804 KB Output is correct
33 Correct 6 ms 22108 KB Output is correct
34 Correct 17 ms 24560 KB Output is correct
35 Correct 129 ms 58392 KB Output is correct
36 Correct 129 ms 58308 KB Output is correct
37 Correct 130 ms 58308 KB Output is correct
38 Correct 128 ms 58280 KB Output is correct
39 Correct 124 ms 58052 KB Output is correct
40 Correct 111 ms 55380 KB Output is correct
41 Correct 130 ms 58824 KB Output is correct
42 Correct 126 ms 58396 KB Output is correct
43 Correct 110 ms 61248 KB Output is correct
44 Correct 128 ms 58700 KB Output is correct
45 Correct 149 ms 72300 KB Output is correct
46 Correct 130 ms 58304 KB Output is correct
47 Correct 129 ms 58508 KB Output is correct
48 Correct 147 ms 63172 KB Output is correct
49 Correct 105 ms 64292 KB Output is correct
50 Correct 84 ms 55296 KB Output is correct
51 Correct 125 ms 64968 KB Output is correct
52 Correct 179 ms 77744 KB Output is correct
53 Correct 204 ms 81088 KB Output is correct
54 Correct 195 ms 84924 KB Output is correct
55 Correct 116 ms 61136 KB Output is correct
56 Correct 182 ms 82384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19548 KB Output is correct
2 Correct 4 ms 19692 KB Output is correct
3 Correct 4 ms 19548 KB Output is correct
4 Correct 4 ms 19804 KB Output is correct
5 Correct 5 ms 19688 KB Output is correct
6 Correct 5 ms 19804 KB Output is correct
7 Correct 5 ms 19804 KB Output is correct
8 Correct 5 ms 19804 KB Output is correct
9 Correct 104 ms 50632 KB Output is correct
10 Correct 125 ms 58120 KB Output is correct
11 Correct 124 ms 58056 KB Output is correct
12 Correct 135 ms 58616 KB Output is correct
13 Correct 136 ms 61020 KB Output is correct
14 Correct 115 ms 50992 KB Output is correct
15 Correct 290 ms 62392 KB Output is correct
16 Correct 269 ms 62068 KB Output is correct
17 Correct 292 ms 62668 KB Output is correct
18 Correct 332 ms 65064 KB Output is correct
19 Correct 642 ms 67404 KB Output is correct
20 Correct 609 ms 67872 KB Output is correct
21 Correct 732 ms 67800 KB Output is correct
22 Correct 560 ms 67776 KB Output is correct
23 Correct 651 ms 67912 KB Output is correct
24 Correct 638 ms 67352 KB Output is correct
25 Correct 625 ms 67708 KB Output is correct
26 Correct 639 ms 65412 KB Output is correct
27 Correct 5 ms 19736 KB Output is correct
28 Correct 5 ms 19804 KB Output is correct
29 Correct 4 ms 19804 KB Output is correct
30 Correct 5 ms 19804 KB Output is correct
31 Correct 4 ms 19804 KB Output is correct
32 Correct 4 ms 19804 KB Output is correct
33 Correct 5 ms 19804 KB Output is correct
34 Correct 5 ms 20060 KB Output is correct
35 Correct 4 ms 19804 KB Output is correct
36 Correct 17 ms 24560 KB Output is correct
37 Correct 129 ms 58392 KB Output is correct
38 Correct 129 ms 58308 KB Output is correct
39 Correct 130 ms 58308 KB Output is correct
40 Correct 128 ms 58280 KB Output is correct
41 Correct 124 ms 58052 KB Output is correct
42 Correct 111 ms 55380 KB Output is correct
43 Correct 130 ms 58824 KB Output is correct
44 Correct 126 ms 58396 KB Output is correct
45 Correct 110 ms 61248 KB Output is correct
46 Correct 128 ms 58700 KB Output is correct
47 Correct 41 ms 24648 KB Output is correct
48 Correct 370 ms 63148 KB Output is correct
49 Correct 385 ms 63148 KB Output is correct
50 Correct 368 ms 63132 KB Output is correct
51 Correct 406 ms 63352 KB Output is correct
52 Correct 415 ms 62780 KB Output is correct
53 Correct 408 ms 51116 KB Output is correct
54 Correct 362 ms 63944 KB Output is correct
55 Correct 345 ms 63148 KB Output is correct
56 Correct 676 ms 65588 KB Output is correct
57 Correct 490 ms 64100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19548 KB Output is correct
2 Correct 4 ms 19548 KB Output is correct
3 Correct 4 ms 19692 KB Output is correct
4 Correct 4 ms 19548 KB Output is correct
5 Correct 4 ms 19804 KB Output is correct
6 Correct 5 ms 19688 KB Output is correct
7 Correct 5 ms 19804 KB Output is correct
8 Correct 5 ms 19804 KB Output is correct
9 Correct 5 ms 19804 KB Output is correct
10 Correct 104 ms 50632 KB Output is correct
11 Correct 125 ms 58120 KB Output is correct
12 Correct 124 ms 58056 KB Output is correct
13 Correct 135 ms 58616 KB Output is correct
14 Correct 136 ms 61020 KB Output is correct
15 Correct 115 ms 50992 KB Output is correct
16 Correct 290 ms 62392 KB Output is correct
17 Correct 269 ms 62068 KB Output is correct
18 Correct 292 ms 62668 KB Output is correct
19 Correct 332 ms 65064 KB Output is correct
20 Correct 642 ms 67404 KB Output is correct
21 Correct 609 ms 67872 KB Output is correct
22 Correct 732 ms 67800 KB Output is correct
23 Correct 560 ms 67776 KB Output is correct
24 Correct 651 ms 67912 KB Output is correct
25 Correct 638 ms 67352 KB Output is correct
26 Correct 625 ms 67708 KB Output is correct
27 Correct 639 ms 65412 KB Output is correct
28 Correct 5 ms 19736 KB Output is correct
29 Correct 5 ms 19804 KB Output is correct
30 Correct 4 ms 19804 KB Output is correct
31 Correct 5 ms 19804 KB Output is correct
32 Correct 4 ms 19804 KB Output is correct
33 Correct 4 ms 19804 KB Output is correct
34 Correct 5 ms 19804 KB Output is correct
35 Correct 5 ms 20060 KB Output is correct
36 Correct 4 ms 19804 KB Output is correct
37 Correct 17 ms 24560 KB Output is correct
38 Correct 129 ms 58392 KB Output is correct
39 Correct 129 ms 58308 KB Output is correct
40 Correct 130 ms 58308 KB Output is correct
41 Correct 128 ms 58280 KB Output is correct
42 Correct 124 ms 58052 KB Output is correct
43 Correct 111 ms 55380 KB Output is correct
44 Correct 130 ms 58824 KB Output is correct
45 Correct 126 ms 58396 KB Output is correct
46 Correct 110 ms 61248 KB Output is correct
47 Correct 128 ms 58700 KB Output is correct
48 Correct 41 ms 24648 KB Output is correct
49 Correct 370 ms 63148 KB Output is correct
50 Correct 385 ms 63148 KB Output is correct
51 Correct 368 ms 63132 KB Output is correct
52 Correct 406 ms 63352 KB Output is correct
53 Correct 415 ms 62780 KB Output is correct
54 Correct 408 ms 51116 KB Output is correct
55 Correct 362 ms 63944 KB Output is correct
56 Correct 345 ms 63148 KB Output is correct
57 Correct 676 ms 65588 KB Output is correct
58 Correct 490 ms 64100 KB Output is correct
59 Correct 159 ms 31636 KB Output is correct
60 Correct 331 ms 63448 KB Output is correct
61 Correct 341 ms 63420 KB Output is correct
62 Correct 349 ms 64036 KB Output is correct
63 Correct 634 ms 66344 KB Output is correct
64 Correct 4 ms 19804 KB Output is correct
65 Correct 5 ms 19804 KB Output is correct
66 Correct 4 ms 19800 KB Output is correct
67 Correct 5 ms 20056 KB Output is correct
68 Correct 6 ms 19800 KB Output is correct
69 Correct 5 ms 22108 KB Output is correct
70 Correct 5 ms 22108 KB Output is correct
71 Correct 5 ms 21976 KB Output is correct
72 Correct 5 ms 19804 KB Output is correct
73 Correct 6 ms 22108 KB Output is correct
74 Correct 149 ms 72300 KB Output is correct
75 Correct 130 ms 58304 KB Output is correct
76 Correct 129 ms 58508 KB Output is correct
77 Correct 147 ms 63172 KB Output is correct
78 Correct 105 ms 64292 KB Output is correct
79 Correct 84 ms 55296 KB Output is correct
80 Correct 125 ms 64968 KB Output is correct
81 Correct 179 ms 77744 KB Output is correct
82 Correct 204 ms 81088 KB Output is correct
83 Correct 195 ms 84924 KB Output is correct
84 Correct 116 ms 61136 KB Output is correct
85 Correct 182 ms 82384 KB Output is correct
86 Correct 137 ms 40136 KB Output is correct
87 Correct 404 ms 63276 KB Output is correct
88 Correct 415 ms 63152 KB Output is correct
89 Correct 538 ms 65472 KB Output is correct
90 Correct 353 ms 71448 KB Output is correct
91 Correct 408 ms 70436 KB Output is correct
92 Correct 643 ms 69252 KB Output is correct
93 Correct 547 ms 81924 KB Output is correct
94 Correct 654 ms 85164 KB Output is correct
95 Correct 416 ms 89192 KB Output is correct
96 Correct 667 ms 65840 KB Output is correct
97 Correct 649 ms 75720 KB Output is correct