답안 #341242

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
341242 2020-12-29T09:34:03 Z phathnv 자매 도시 (APIO20_swap) C++11
100 / 100
450 ms 42992 KB
#include <bits/stdc++.h>
#include "swap.h"

#define X first
#define Y second
#define mp make_pair

using namespace std;

typedef pair<int, int> ii;

const int maxN = 100000;
const int maxM = 200000;

bool vst[maxN];
vector <ii> adj[maxN];
vector <int> adjEdge[maxN];
int nEdge = 0;

void buildEdge(int u, int w){
    for(int v : adjEdge[u])
        if (!vst[v]){
            adj[u].push_back(mp(v, w));
            adj[v].push_back(mp(u, w));
            vst[v] = 1;
            nEdge++;
            buildEdge(v, w);
        }
}

struct DSU{
    int rnk[maxN], root[maxN], deg[maxN];
    int cnt[maxN][4];
    bool ok[maxN];

    void reset(int n){
        for(int i = 0; i < n; i++){
            root[i] = i;
            deg[i] = rnk[i] = 0;
            cnt[i][0] = 1;
            cnt[i][1] = cnt[i][2] = cnt[i][3] = 0;
            ok[i] = 0;
        }
    }
    int getRoot(int u){
        if (u == root[u])
            return u;
        root[u] = getRoot(root[u]);
        return root[u];
    }
    void updateDeg(int u){
        int ru = getRoot(u);
        cnt[ru][deg[u]]--;
        deg[u]++;
        cnt[ru][deg[u]]++;
    }
    bool check(int u){
        u = getRoot(u);
        if (cnt[u][3] > 0 || cnt[u][0] + cnt[u][1] == 0)
            return 1;
        return 0;
    }
    void addEdge(int u, int v, int w){
        if (deg[u] < 3)
            updateDeg(u);
        if (deg[v] < 3)
            updateDeg(v);
        u = getRoot(u);
        v = getRoot(v);
        if (u != v){
            if (rnk[u] > rnk[v])
                swap(u, v);
            if (rnk[u] == rnk[v])
                rnk[u]++;
            root[v] = u;
            adjEdge[u].push_back(v);

            for(int i = 0; i < 4; i++)
                cnt[u][i] += cnt[v][i];
            ok[u] = ok[u] || ok[v];
        }
        ok[u] = ok[u] || check(u);
        if (ok[u])
            buildEdge(u, w);
    }
} G;

int dist[maxN][17], par[maxN][17], h[maxN];

void dfs(int u, int p){
    for(int i = 1; i <= 16; i++){
        par[u][i] = par[par[u][i - 1]][i - 1];
        dist[u][i] = max(dist[u][i - 1], dist[par[u][i - 1]][i - 1]);
    }
    for(ii e : adj[u])
        if (e.X != p){
            par[e.X][0] = u;
            dist[e.X][0] = e.Y;
            h[e.X] = h[u] + 1;
            dfs(e.X, u);
        }
}

struct edge{
    int u, v, w;
};

int n, m;
edge edges[maxM];

void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {
    n = N;
    m = M;
    for(int i = 0; i < m; i++){
        edges[i].u = U[i];
        edges[i].v = V[i];
        edges[i].w = W[i];
    }
    sort(edges, edges + m, [](edge &a, edge & b){
            return a.w < b.w;
         });
    G.reset(n);
    for(int i = 0; i < m; i++)
        G.addEdge(edges[i].u, edges[i].v, edges[i].w);
    dfs(0, -1);
}

int getDist(int u, int v){
    int res = 0;
    if (h[u] > h[v])
        swap(u, v);
    for(int i = 16; i >= 0; i--)
        if (h[u] <= h[v] - (1 << i)){
            res = max(res, dist[v][i]);
            v = par[v][i];
        }
    if (v == u)
        return res;
    for(int i = 16; i >= 0; i--)
        if (par[u][i] != par[v][i]){
            res = max(res, dist[u][i]);
            res = max(res, dist[v][i]);
            u = par[u][i];
            v = par[v][i];
        }
    res = max(res, dist[u][0]);
    res = max(res, dist[v][0]);
    return res;
}

int getMinimumFuelCapacity(int X, int Y) {
    if (nEdge == 0)
        return -1;
    return getDist(X, Y);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 5 ms 5100 KB Output is correct
6 Correct 4 ms 5228 KB Output is correct
7 Correct 5 ms 5228 KB Output is correct
8 Correct 4 ms 5228 KB Output is correct
9 Correct 55 ms 13280 KB Output is correct
10 Correct 73 ms 15084 KB Output is correct
11 Correct 75 ms 14968 KB Output is correct
12 Correct 81 ms 15468 KB Output is correct
13 Correct 71 ms 16492 KB Output is correct
14 Correct 63 ms 13676 KB Output is correct
15 Correct 140 ms 19320 KB Output is correct
16 Correct 138 ms 18972 KB Output is correct
17 Correct 171 ms 19540 KB Output is correct
18 Correct 135 ms 20564 KB Output is correct
19 Correct 117 ms 14700 KB Output is correct
20 Correct 274 ms 37252 KB Output is correct
21 Correct 346 ms 36516 KB Output is correct
22 Correct 288 ms 38352 KB Output is correct
23 Correct 435 ms 42992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 363 ms 39820 KB Output is correct
4 Correct 365 ms 41168 KB Output is correct
5 Correct 359 ms 40576 KB Output is correct
6 Correct 356 ms 41168 KB Output is correct
7 Correct 359 ms 41064 KB Output is correct
8 Correct 377 ms 39704 KB Output is correct
9 Correct 358 ms 40724 KB Output is correct
10 Correct 355 ms 39460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 5 ms 5100 KB Output is correct
7 Correct 4 ms 5228 KB Output is correct
8 Correct 5 ms 5228 KB Output is correct
9 Correct 4 ms 5228 KB Output is correct
10 Correct 4 ms 5356 KB Output is correct
11 Correct 4 ms 5356 KB Output is correct
12 Correct 5 ms 5356 KB Output is correct
13 Correct 4 ms 5228 KB Output is correct
14 Correct 5 ms 5356 KB Output is correct
15 Correct 5 ms 5356 KB Output is correct
16 Correct 4 ms 5356 KB Output is correct
17 Correct 5 ms 5356 KB Output is correct
18 Correct 4 ms 5356 KB Output is correct
19 Correct 5 ms 5228 KB Output is correct
20 Correct 5 ms 5356 KB Output is correct
21 Correct 4 ms 5356 KB Output is correct
22 Correct 5 ms 5228 KB Output is correct
23 Correct 4 ms 5228 KB Output is correct
24 Correct 5 ms 5356 KB Output is correct
25 Correct 5 ms 5484 KB Output is correct
26 Correct 5 ms 5632 KB Output is correct
27 Correct 6 ms 5356 KB Output is correct
28 Correct 6 ms 5356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 5 ms 5100 KB Output is correct
7 Correct 4 ms 5228 KB Output is correct
8 Correct 5 ms 5228 KB Output is correct
9 Correct 4 ms 5228 KB Output is correct
10 Correct 55 ms 13280 KB Output is correct
11 Correct 73 ms 15084 KB Output is correct
12 Correct 75 ms 14968 KB Output is correct
13 Correct 81 ms 15468 KB Output is correct
14 Correct 71 ms 16492 KB Output is correct
15 Correct 4 ms 5356 KB Output is correct
16 Correct 4 ms 5356 KB Output is correct
17 Correct 5 ms 5356 KB Output is correct
18 Correct 4 ms 5228 KB Output is correct
19 Correct 5 ms 5356 KB Output is correct
20 Correct 5 ms 5356 KB Output is correct
21 Correct 4 ms 5356 KB Output is correct
22 Correct 5 ms 5356 KB Output is correct
23 Correct 4 ms 5356 KB Output is correct
24 Correct 18 ms 8812 KB Output is correct
25 Correct 145 ms 32748 KB Output is correct
26 Correct 152 ms 32620 KB Output is correct
27 Correct 144 ms 32556 KB Output is correct
28 Correct 149 ms 32364 KB Output is correct
29 Correct 170 ms 32236 KB Output is correct
30 Correct 148 ms 29804 KB Output is correct
31 Correct 140 ms 33132 KB Output is correct
32 Correct 148 ms 32620 KB Output is correct
33 Correct 178 ms 37308 KB Output is correct
34 Correct 178 ms 32876 KB Output is correct
35 Correct 5 ms 5228 KB Output is correct
36 Correct 5 ms 5356 KB Output is correct
37 Correct 4 ms 5356 KB Output is correct
38 Correct 5 ms 5228 KB Output is correct
39 Correct 4 ms 5228 KB Output is correct
40 Correct 5 ms 5356 KB Output is correct
41 Correct 5 ms 5484 KB Output is correct
42 Correct 5 ms 5632 KB Output is correct
43 Correct 6 ms 5356 KB Output is correct
44 Correct 6 ms 5356 KB Output is correct
45 Correct 165 ms 31852 KB Output is correct
46 Correct 140 ms 32768 KB Output is correct
47 Correct 155 ms 32596 KB Output is correct
48 Correct 177 ms 33516 KB Output is correct
49 Correct 73 ms 13676 KB Output is correct
50 Correct 62 ms 13420 KB Output is correct
51 Correct 139 ms 27500 KB Output is correct
52 Correct 171 ms 36500 KB Output is correct
53 Correct 230 ms 38380 KB Output is correct
54 Correct 205 ms 38168 KB Output is correct
55 Correct 183 ms 38252 KB Output is correct
56 Correct 218 ms 38516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 5 ms 5100 KB Output is correct
6 Correct 4 ms 5228 KB Output is correct
7 Correct 5 ms 5228 KB Output is correct
8 Correct 4 ms 5228 KB Output is correct
9 Correct 55 ms 13280 KB Output is correct
10 Correct 73 ms 15084 KB Output is correct
11 Correct 75 ms 14968 KB Output is correct
12 Correct 81 ms 15468 KB Output is correct
13 Correct 71 ms 16492 KB Output is correct
14 Correct 63 ms 13676 KB Output is correct
15 Correct 140 ms 19320 KB Output is correct
16 Correct 138 ms 18972 KB Output is correct
17 Correct 171 ms 19540 KB Output is correct
18 Correct 135 ms 20564 KB Output is correct
19 Correct 363 ms 39820 KB Output is correct
20 Correct 365 ms 41168 KB Output is correct
21 Correct 359 ms 40576 KB Output is correct
22 Correct 356 ms 41168 KB Output is correct
23 Correct 359 ms 41064 KB Output is correct
24 Correct 377 ms 39704 KB Output is correct
25 Correct 358 ms 40724 KB Output is correct
26 Correct 355 ms 39460 KB Output is correct
27 Correct 4 ms 5356 KB Output is correct
28 Correct 4 ms 5356 KB Output is correct
29 Correct 5 ms 5356 KB Output is correct
30 Correct 4 ms 5228 KB Output is correct
31 Correct 5 ms 5356 KB Output is correct
32 Correct 5 ms 5356 KB Output is correct
33 Correct 4 ms 5356 KB Output is correct
34 Correct 5 ms 5356 KB Output is correct
35 Correct 4 ms 5356 KB Output is correct
36 Correct 18 ms 8812 KB Output is correct
37 Correct 145 ms 32748 KB Output is correct
38 Correct 152 ms 32620 KB Output is correct
39 Correct 144 ms 32556 KB Output is correct
40 Correct 149 ms 32364 KB Output is correct
41 Correct 170 ms 32236 KB Output is correct
42 Correct 148 ms 29804 KB Output is correct
43 Correct 140 ms 33132 KB Output is correct
44 Correct 148 ms 32620 KB Output is correct
45 Correct 178 ms 37308 KB Output is correct
46 Correct 178 ms 32876 KB Output is correct
47 Correct 28 ms 9068 KB Output is correct
48 Correct 296 ms 37456 KB Output is correct
49 Correct 293 ms 37528 KB Output is correct
50 Correct 286 ms 37408 KB Output is correct
51 Correct 312 ms 37468 KB Output is correct
52 Correct 313 ms 35864 KB Output is correct
53 Correct 248 ms 29408 KB Output is correct
54 Correct 334 ms 38384 KB Output is correct
55 Correct 290 ms 37632 KB Output is correct
56 Correct 411 ms 42636 KB Output is correct
57 Correct 341 ms 38480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 5 ms 5100 KB Output is correct
7 Correct 4 ms 5228 KB Output is correct
8 Correct 5 ms 5228 KB Output is correct
9 Correct 4 ms 5228 KB Output is correct
10 Correct 55 ms 13280 KB Output is correct
11 Correct 73 ms 15084 KB Output is correct
12 Correct 75 ms 14968 KB Output is correct
13 Correct 81 ms 15468 KB Output is correct
14 Correct 71 ms 16492 KB Output is correct
15 Correct 63 ms 13676 KB Output is correct
16 Correct 140 ms 19320 KB Output is correct
17 Correct 138 ms 18972 KB Output is correct
18 Correct 171 ms 19540 KB Output is correct
19 Correct 135 ms 20564 KB Output is correct
20 Correct 363 ms 39820 KB Output is correct
21 Correct 365 ms 41168 KB Output is correct
22 Correct 359 ms 40576 KB Output is correct
23 Correct 356 ms 41168 KB Output is correct
24 Correct 359 ms 41064 KB Output is correct
25 Correct 377 ms 39704 KB Output is correct
26 Correct 358 ms 40724 KB Output is correct
27 Correct 355 ms 39460 KB Output is correct
28 Correct 4 ms 5356 KB Output is correct
29 Correct 4 ms 5356 KB Output is correct
30 Correct 5 ms 5356 KB Output is correct
31 Correct 4 ms 5228 KB Output is correct
32 Correct 5 ms 5356 KB Output is correct
33 Correct 5 ms 5356 KB Output is correct
34 Correct 4 ms 5356 KB Output is correct
35 Correct 5 ms 5356 KB Output is correct
36 Correct 4 ms 5356 KB Output is correct
37 Correct 18 ms 8812 KB Output is correct
38 Correct 145 ms 32748 KB Output is correct
39 Correct 152 ms 32620 KB Output is correct
40 Correct 144 ms 32556 KB Output is correct
41 Correct 149 ms 32364 KB Output is correct
42 Correct 170 ms 32236 KB Output is correct
43 Correct 148 ms 29804 KB Output is correct
44 Correct 140 ms 33132 KB Output is correct
45 Correct 148 ms 32620 KB Output is correct
46 Correct 178 ms 37308 KB Output is correct
47 Correct 178 ms 32876 KB Output is correct
48 Correct 28 ms 9068 KB Output is correct
49 Correct 296 ms 37456 KB Output is correct
50 Correct 293 ms 37528 KB Output is correct
51 Correct 286 ms 37408 KB Output is correct
52 Correct 312 ms 37468 KB Output is correct
53 Correct 313 ms 35864 KB Output is correct
54 Correct 248 ms 29408 KB Output is correct
55 Correct 334 ms 38384 KB Output is correct
56 Correct 290 ms 37632 KB Output is correct
57 Correct 411 ms 42636 KB Output is correct
58 Correct 341 ms 38480 KB Output is correct
59 Correct 117 ms 14700 KB Output is correct
60 Correct 274 ms 37252 KB Output is correct
61 Correct 346 ms 36516 KB Output is correct
62 Correct 288 ms 38352 KB Output is correct
63 Correct 435 ms 42992 KB Output is correct
64 Correct 5 ms 5228 KB Output is correct
65 Correct 5 ms 5356 KB Output is correct
66 Correct 4 ms 5356 KB Output is correct
67 Correct 5 ms 5228 KB Output is correct
68 Correct 4 ms 5228 KB Output is correct
69 Correct 5 ms 5356 KB Output is correct
70 Correct 5 ms 5484 KB Output is correct
71 Correct 5 ms 5632 KB Output is correct
72 Correct 6 ms 5356 KB Output is correct
73 Correct 6 ms 5356 KB Output is correct
74 Correct 165 ms 31852 KB Output is correct
75 Correct 140 ms 32768 KB Output is correct
76 Correct 155 ms 32596 KB Output is correct
77 Correct 177 ms 33516 KB Output is correct
78 Correct 73 ms 13676 KB Output is correct
79 Correct 62 ms 13420 KB Output is correct
80 Correct 139 ms 27500 KB Output is correct
81 Correct 171 ms 36500 KB Output is correct
82 Correct 230 ms 38380 KB Output is correct
83 Correct 205 ms 38168 KB Output is correct
84 Correct 183 ms 38252 KB Output is correct
85 Correct 218 ms 38516 KB Output is correct
86 Correct 85 ms 16160 KB Output is correct
87 Correct 287 ms 37604 KB Output is correct
88 Correct 286 ms 37576 KB Output is correct
89 Correct 401 ms 37128 KB Output is correct
90 Correct 175 ms 18372 KB Output is correct
91 Correct 193 ms 20496 KB Output is correct
92 Correct 317 ms 32176 KB Output is correct
93 Correct 341 ms 41176 KB Output is correct
94 Correct 389 ms 42668 KB Output is correct
95 Correct 402 ms 42180 KB Output is correct
96 Correct 450 ms 41168 KB Output is correct
97 Correct 398 ms 41320 KB Output is correct