답안 #1068137

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1068137 2024-08-21T07:57:16 Z Requiem 자매 도시 (APIO20_swap) C++17
100 / 100
246 ms 56644 KB
#include "swap.h"

#include<bits/stdc++.h>
//#define int long long
#define fast ios_base::sync_with_stdio(NULL), cin.tie(NULL), cout.tie(NULL)
#define endl '\n'
#define TASKNAME "swapcities"
#define pb push_back
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORD(i, a, b) for(int i = a; i >= b; i--)
#define se second
#define inf 1e9 + 2
#define fi first
using namespace std;

typedef pair<int, int> ii;
typedef pair<int, ii> iii;

/**
Cho đồ thị liên thông n đỉnh m cạnh.
Cạnh i nối 2 đỉnh ui vi có trọng số là wi.

Ta có q truy vấn:
Xi và Yi.

Ta cần tìm 2 đường đi từ Xi đến Yi và từ Yi đến Xi cho 2 xe di chuyển sao cho:
- 2 xe không đến 1 thành phố vào cùng 1 thời điểm
- 2 xe không dược đi theo 2 chiều ngược nhau của cùng 1 cạnh cùng 1 lúc.
- Trọng số của cạnh lớn nhất trên đường đi là nhỏ nhất.
- 1 xe có thể đứng đợi tại 1 thành phố nào đó.

subtask 1: bậc đồ thị 0 quá 2.
Trường hợp 1: Cây đường thẳng thì bỏ qua.
Trường hợp 2: đồ thị vòng thì tôi đơn giản là max các cạnh

subtask 2: đồ thị hình sao.
TH1: u = 0, v != 0 thì tôi sẽ chọn 1 đỉnh khác u nhỏ nhất để trốn.
TH2: u != 0 và v != 0 thì tôi chọn 1 đỉnh khác u và v nhỏ nhất để trốn.

subtask 3, 4: q <= 5, n <= 1000, m <= 1000. Mình đang nghĩ đến chặt nhị phân.
Giả sử mình có 1 cái đồ thị con mà cạnh lớn nhất <= w.
Thì đồ thị nó thỏa nếu như nó không phải là 1 đường thẳng hoặc giữa đường đi đó có những
đỉnh khác chìa ra.

subtask 5: m = n - 1. sub cây.
Mình đang nghĩ đến nhảy nhị phân.
up[0][i]: giá trị nhỏ nhất trong cây con của thằng par[i] mà không đi qua i.
up[j][i]: gia tri nho nhat trong cay con cua thang par[i] ma khong di vao cay con chua nut i.
Xử lý riêng khi đến nút lca.

subtask 6: không còn ràng buộc.
Giả sử mình chặt nhị phân W.
Nếu như ta biết được 1 đường đi từ u đến v thì ta muốn biết là liệu ta có thể đến được
thằng nào nằm ngoài đường đi đó không.

Thì mình sẽ biết là ngoài những đỉnh nằm trên đường đi từ u đến v hay không
thì tôi còn có thể đến được đỉnh nào khác không.

Dựng reachability tree lên thì ta thấy.
**/

const int MAXN = 3e5 + 9;
iii edge[MAXN];
vector<vector<int>> reachTree;
int value[MAXN], weight[MAXN], directPar[MAXN], deg[MAXN];

template<typename T> bool maximize(T &a, T b){ if (a < b) {a = b; return true; } return false;}
template<typename T> bool minimize(T &a, T b){ if (a > b) {a = b; return true; } return false;}

struct DisjointSetUnion{
    vector<int> lab;
    DisjointSetUnion(int _sz = 0){
        lab.resize(_sz + 9, -1);
        fill(lab.begin(), lab.end(), -1);
    }

    int root(int u){
        return ((lab[u] < 0) ? u : lab[u] = root(lab[u]));
    }

    bool unite(int u, int v){
        u = root(u);
        v = root(v);
        if (u != v){
            lab[u] += lab[v];
            lab[v] = u;
            return true;
        }
        return false;
    }
};

struct eulerTour{
    vector<int> etour, in, out, depth;
    vector<vector<int>> P;
    int _sz = 0;

    void dfs(vector<vector<int>> &g, int u, int p){
        in[u] = 1;
        P[0][u] = p;
        for(auto v: g[u]){
            if (v == p) continue;
            depth[v] = depth[u] + 1;
            dfs(g, v, u);
        }
    }

    void init(){
        in.resize(_sz + 3);
        depth.resize(_sz + 3, 0);
        P.resize(30, vector<int>(_sz + 3, 0));
    }


    int getlca(int u, int v){
        if (depth[u] < depth[v]) swap(u, v);
        FORD(i, 20, 0){
            if (P[i][u] != -1 and depth[P[i][u]] >= depth[v]){
                u = P[i][u];
            }
        }

        if (u == v) return u;

        FORD(i, 20, 0){
            if (P[i][u] != -1 and P[i][v] != -1 and P[i][u] != P[i][v]){
                u = P[i][u];
                v = P[i][v];
            }
        }
        return P[0][v];
    }
    void build(vector<vector<int>> &g){
        _sz = g.size();
        init();

        FORD(i, g.size() - 1, 0){
            if (!in[i]) dfs(g, i, -1);
        }


        FOR(i, 1, 20){
            FOR(j, 0, _sz - 1){
                if (P[i - 1][j] == -1) P[i][j] = -1;
                else P[i][j] = P[i - 1][P[i - 1][j]];
            }
        }
    }

    int Query(int x, int y){
        int acs = getlca(x, y);
        if (value[acs] == 1) return weight[acs];
        FORD(i, 20, 0){
            if (P[i][acs] != -1 and value[P[i][acs]] == 0){
                acs = P[i][acs];
            }
        }
        if (P[0][acs] == -1 or value[P[0][acs]] == 0) return -1;
        else return weight[P[0][acs]];
        return 0;
    }
};

eulerTour etour;
DisjointSetUnion DSU;

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    FOR(i, 0, M - 1){
        edge[i] = {W[i], {U[i], V[i]}};
    }

    sort(edge, edge + M);
    reachTree.resize(M);
    DSU = DisjointSetUnion(N + M);
    FOR(i, 0, M - 1){
        int u = edge[i].se.fi;
        int v = edge[i].se.se;
        weight[i] = edge[i].fi;
        deg[u]++;
        deg[v]++;

        int rt1 = DSU.root(u);
        int rt2 = DSU.root(v);
        if (deg[v] > 2 or deg[u] > 2 or value[rt1 - N]) value[i] = 1;
        if (deg[u] > 2 or deg[v] > 2 or value[rt2 - N]) value[i] = 1;

        if (rt1 == rt2) value[i] = 1;
        if (DSU.unite(i + N, rt1)){
            if (rt1 >= N) {
                reachTree[i].pb(rt1 - N);
            }
            else directPar[rt1] = i;
        }

        if (DSU.unite(i + N, rt2)){
            if (rt2 >= N) {
                reachTree[i].pb(rt2 - N);
            }
            else directPar[rt2] = i;
        }
//        printf("%d %d\n", i, value[i]);

    }
    etour.build(reachTree);
}

int getMinimumFuelCapacity(int X, int Y) {
  int x = directPar[X];
  int y = directPar[Y];
  if (DSU.root(X) != DSU.root(Y)) return -1;
  int res = etour.Query(x, y);
  return res;
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 37 ms 20012 KB Output is correct
10 Correct 53 ms 24404 KB Output is correct
11 Correct 47 ms 23888 KB Output is correct
12 Correct 52 ms 25300 KB Output is correct
13 Correct 51 ms 27848 KB Output is correct
14 Correct 45 ms 20412 KB Output is correct
15 Correct 160 ms 28500 KB Output is correct
16 Correct 151 ms 27728 KB Output is correct
17 Correct 156 ms 29160 KB Output is correct
18 Correct 214 ms 31764 KB Output is correct
19 Correct 75 ms 9308 KB Output is correct
20 Correct 163 ms 29368 KB Output is correct
21 Correct 160 ms 28832 KB Output is correct
22 Correct 165 ms 30516 KB Output is correct
23 Correct 223 ms 33072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 139 ms 33056 KB Output is correct
4 Correct 140 ms 34220 KB Output is correct
5 Correct 154 ms 33764 KB Output is correct
6 Correct 136 ms 34000 KB Output is correct
7 Correct 146 ms 33948 KB Output is correct
8 Correct 141 ms 32696 KB Output is correct
9 Correct 139 ms 33872 KB Output is correct
10 Correct 139 ms 32568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 720 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 37 ms 20012 KB Output is correct
11 Correct 53 ms 24404 KB Output is correct
12 Correct 47 ms 23888 KB Output is correct
13 Correct 52 ms 25300 KB Output is correct
14 Correct 51 ms 27848 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 720 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 860 KB Output is correct
31 Correct 1 ms 860 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 860 KB Output is correct
34 Correct 7 ms 3676 KB Output is correct
35 Correct 53 ms 25168 KB Output is correct
36 Correct 47 ms 25168 KB Output is correct
37 Correct 47 ms 25424 KB Output is correct
38 Correct 49 ms 25172 KB Output is correct
39 Correct 48 ms 25016 KB Output is correct
40 Correct 42 ms 22868 KB Output is correct
41 Correct 52 ms 25424 KB Output is correct
42 Correct 49 ms 25040 KB Output is correct
43 Correct 42 ms 28104 KB Output is correct
44 Correct 51 ms 25808 KB Output is correct
45 Correct 85 ms 40212 KB Output is correct
46 Correct 55 ms 25296 KB Output is correct
47 Correct 53 ms 25424 KB Output is correct
48 Correct 62 ms 29224 KB Output is correct
49 Correct 74 ms 44132 KB Output is correct
50 Correct 62 ms 33912 KB Output is correct
51 Correct 67 ms 35244 KB Output is correct
52 Correct 85 ms 42152 KB Output is correct
53 Correct 107 ms 46740 KB Output is correct
54 Correct 108 ms 52800 KB Output is correct
55 Correct 43 ms 28156 KB Output is correct
56 Correct 98 ms 46776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 37 ms 20012 KB Output is correct
10 Correct 53 ms 24404 KB Output is correct
11 Correct 47 ms 23888 KB Output is correct
12 Correct 52 ms 25300 KB Output is correct
13 Correct 51 ms 27848 KB Output is correct
14 Correct 45 ms 20412 KB Output is correct
15 Correct 160 ms 28500 KB Output is correct
16 Correct 151 ms 27728 KB Output is correct
17 Correct 156 ms 29160 KB Output is correct
18 Correct 214 ms 31764 KB Output is correct
19 Correct 139 ms 33056 KB Output is correct
20 Correct 140 ms 34220 KB Output is correct
21 Correct 154 ms 33764 KB Output is correct
22 Correct 136 ms 34000 KB Output is correct
23 Correct 146 ms 33948 KB Output is correct
24 Correct 141 ms 32696 KB Output is correct
25 Correct 139 ms 33872 KB Output is correct
26 Correct 139 ms 32568 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 7 ms 3676 KB Output is correct
37 Correct 53 ms 25168 KB Output is correct
38 Correct 47 ms 25168 KB Output is correct
39 Correct 47 ms 25424 KB Output is correct
40 Correct 49 ms 25172 KB Output is correct
41 Correct 48 ms 25016 KB Output is correct
42 Correct 42 ms 22868 KB Output is correct
43 Correct 52 ms 25424 KB Output is correct
44 Correct 49 ms 25040 KB Output is correct
45 Correct 42 ms 28104 KB Output is correct
46 Correct 51 ms 25808 KB Output is correct
47 Correct 14 ms 3928 KB Output is correct
48 Correct 156 ms 29488 KB Output is correct
49 Correct 152 ms 29748 KB Output is correct
50 Correct 138 ms 29852 KB Output is correct
51 Correct 133 ms 29760 KB Output is correct
52 Correct 136 ms 28308 KB Output is correct
53 Correct 130 ms 22872 KB Output is correct
54 Correct 150 ms 30680 KB Output is correct
55 Correct 157 ms 29488 KB Output is correct
56 Correct 199 ms 32560 KB Output is correct
57 Correct 150 ms 31024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 37 ms 20012 KB Output is correct
11 Correct 53 ms 24404 KB Output is correct
12 Correct 47 ms 23888 KB Output is correct
13 Correct 52 ms 25300 KB Output is correct
14 Correct 51 ms 27848 KB Output is correct
15 Correct 45 ms 20412 KB Output is correct
16 Correct 160 ms 28500 KB Output is correct
17 Correct 151 ms 27728 KB Output is correct
18 Correct 156 ms 29160 KB Output is correct
19 Correct 214 ms 31764 KB Output is correct
20 Correct 139 ms 33056 KB Output is correct
21 Correct 140 ms 34220 KB Output is correct
22 Correct 154 ms 33764 KB Output is correct
23 Correct 136 ms 34000 KB Output is correct
24 Correct 146 ms 33948 KB Output is correct
25 Correct 141 ms 32696 KB Output is correct
26 Correct 139 ms 33872 KB Output is correct
27 Correct 139 ms 32568 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 7 ms 3676 KB Output is correct
38 Correct 53 ms 25168 KB Output is correct
39 Correct 47 ms 25168 KB Output is correct
40 Correct 47 ms 25424 KB Output is correct
41 Correct 49 ms 25172 KB Output is correct
42 Correct 48 ms 25016 KB Output is correct
43 Correct 42 ms 22868 KB Output is correct
44 Correct 52 ms 25424 KB Output is correct
45 Correct 49 ms 25040 KB Output is correct
46 Correct 42 ms 28104 KB Output is correct
47 Correct 51 ms 25808 KB Output is correct
48 Correct 14 ms 3928 KB Output is correct
49 Correct 156 ms 29488 KB Output is correct
50 Correct 152 ms 29748 KB Output is correct
51 Correct 138 ms 29852 KB Output is correct
52 Correct 133 ms 29760 KB Output is correct
53 Correct 136 ms 28308 KB Output is correct
54 Correct 130 ms 22872 KB Output is correct
55 Correct 150 ms 30680 KB Output is correct
56 Correct 157 ms 29488 KB Output is correct
57 Correct 199 ms 32560 KB Output is correct
58 Correct 150 ms 31024 KB Output is correct
59 Correct 75 ms 9308 KB Output is correct
60 Correct 163 ms 29368 KB Output is correct
61 Correct 160 ms 28832 KB Output is correct
62 Correct 165 ms 30516 KB Output is correct
63 Correct 223 ms 33072 KB Output is correct
64 Correct 1 ms 720 KB Output is correct
65 Correct 1 ms 604 KB Output is correct
66 Correct 1 ms 604 KB Output is correct
67 Correct 1 ms 860 KB Output is correct
68 Correct 1 ms 604 KB Output is correct
69 Correct 1 ms 860 KB Output is correct
70 Correct 1 ms 860 KB Output is correct
71 Correct 1 ms 860 KB Output is correct
72 Correct 1 ms 604 KB Output is correct
73 Correct 1 ms 860 KB Output is correct
74 Correct 85 ms 40212 KB Output is correct
75 Correct 55 ms 25296 KB Output is correct
76 Correct 53 ms 25424 KB Output is correct
77 Correct 62 ms 29224 KB Output is correct
78 Correct 74 ms 44132 KB Output is correct
79 Correct 62 ms 33912 KB Output is correct
80 Correct 67 ms 35244 KB Output is correct
81 Correct 85 ms 42152 KB Output is correct
82 Correct 107 ms 46740 KB Output is correct
83 Correct 108 ms 52800 KB Output is correct
84 Correct 43 ms 28156 KB Output is correct
85 Correct 98 ms 46776 KB Output is correct
86 Correct 60 ms 14792 KB Output is correct
87 Correct 160 ms 29748 KB Output is correct
88 Correct 136 ms 29768 KB Output is correct
89 Correct 209 ms 32720 KB Output is correct
90 Correct 160 ms 50592 KB Output is correct
91 Correct 182 ms 46288 KB Output is correct
92 Correct 209 ms 38632 KB Output is correct
93 Correct 190 ms 46308 KB Output is correct
94 Correct 246 ms 51076 KB Output is correct
95 Correct 235 ms 56644 KB Output is correct
96 Correct 190 ms 32816 KB Output is correct
97 Correct 222 ms 41476 KB Output is correct