답안 #393409

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
393409 2021-04-23T11:26:06 Z BartolM 자매 도시 (APIO20_swap) C++17
100 / 100
389 ms 45876 KB
#include "swap.h"
#define DEBUG 0
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;

const int INF=0x3f3f3f3f;
const int N=3e5+50;
const int LOG=18;

int n, m, uk;
vector <pip> v;
vector <int> ls[N];
int P[N], lanac[N], deg[N], dep[N];
int cnt[N][4], par[LOG+1][N];

int name(int x) {
    if (x==P[x]) return x;
    P[x]=name(P[x]);
    return P[x];
}

void check(int x, int val) {
    if (lanac[x]!=INF) return;
    if (cnt[x][3]) lanac[x]=val;
    else if (!cnt[x][1] && cnt[x][2]) lanac[x]=val;
    #if DEBUG
        if (lanac[x]!=INF) printf("%d ima lanac %d\n", x, val);
    #endif // DEBUG
}

void pov_deg(int x, int val) {
    if (deg[x]>=3) return;
    int y=name(x);
    cnt[y][deg[x]]--; deg[x]++; cnt[y][deg[x]]++;
    check(y, val);
}

void dfs(int node, int papi) {
    dep[node]=dep[papi]+1;
    par[0][node]=papi;
    lanac[node]=min(lanac[node], lanac[papi]);
    for (int sus:ls[node]) dfs(sus, node);
}

void build() {
    for (int i=1; i<=LOG; ++i) {
        for (int j=0; j<uk; ++j) {
            par[i][j]=par[i-1][par[i-1][j]];
        }
    }
}

int lca(int x, int y) {
    if (dep[x]<dep[y]) swap(x, y);
    for (int i=LOG; i>=0; --i) {
        if (dep[x]-(1<<i)>=dep[y]) x=par[i][x];
    }
    if (x==y) return x;
    for (int i=LOG; i>=0; --i) {
        if (par[i][x]!=par[i][y]) x=par[i][x], y=par[i][y];
    }
    return par[0][x];
}

void init(int NN, int MM, vector<int> U, vector<int> V, vector<int> W) {
    n=NN, m=MM;
    for (int i=0; i<m; ++i) v.pb(mp(W[i], mp(U[i], V[i])));
    sort(v.begin(), v.end());

    for (int i=0; i<2*n; ++i) P[i]=i;
    for (int i=0; i<n; ++i) cnt[i][0]=1;
    memset(lanac, INF, sizeof lanac);
    uk=n;

    for (pip pp:v) {
        #if DEBUG
            printf("edge: (%d <-> %d) %d\n", pp.Y.X, pp.Y.Y, pp.X);
        #endif
        int x=name(pp.Y.X), y=name(pp.Y.Y);
        int curr=x;
        if (x!=y) {
            curr=uk++;
            P[y]=P[x]=curr;

            #if DEBUG
                printf("parent od %d i %d je %d\n", x, y, curr);
            #endif // DEBUG

            ls[curr].pb(x); ls[curr].pb(y);
            for (int i=0; i<4; ++i) cnt[curr][i]=cnt[x][i]+cnt[y][i];
            lanac[curr]=min(lanac[x], lanac[y]);
            lanac[curr]=max(lanac[curr], pp.X);
        }
        pov_deg(pp.Y.X, pp.X); pov_deg(pp.Y.Y, pp.X);
        #if DEBUG
            printf("\n");
        #endif // DEBUG
    }
    #if DEBUG
        for (int i=0; i<uk; ++i) printf("node: %d, lanac: %d\n", i, lanac[i]);
    #endif
    dfs(name(0), name(0));
    build();
}

int getMinimumFuelCapacity(int x, int y) {
    int lc=lca(x, y);
    return lanac[lc]==INF ? -1 : lanac[lc];
}

# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8652 KB Output is correct
2 Correct 5 ms 8652 KB Output is correct
3 Correct 5 ms 8652 KB Output is correct
4 Correct 6 ms 8764 KB Output is correct
5 Correct 6 ms 8908 KB Output is correct
6 Correct 6 ms 8908 KB Output is correct
7 Correct 6 ms 8900 KB Output is correct
8 Correct 6 ms 8908 KB Output is correct
9 Correct 75 ms 31160 KB Output is correct
10 Correct 95 ms 36220 KB Output is correct
11 Correct 91 ms 35704 KB Output is correct
12 Correct 100 ms 37304 KB Output is correct
13 Correct 84 ms 38828 KB Output is correct
14 Correct 94 ms 31404 KB Output is correct
15 Correct 276 ms 40460 KB Output is correct
16 Correct 249 ms 39784 KB Output is correct
17 Correct 269 ms 41372 KB Output is correct
18 Correct 340 ms 43040 KB Output is correct
19 Correct 100 ms 18312 KB Output is correct
20 Correct 255 ms 41564 KB Output is correct
21 Correct 264 ms 40940 KB Output is correct
22 Correct 277 ms 42808 KB Output is correct
23 Correct 387 ms 44312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8652 KB Output is correct
2 Correct 5 ms 8652 KB Output is correct
3 Correct 364 ms 43676 KB Output is correct
4 Correct 369 ms 45088 KB Output is correct
5 Correct 380 ms 44440 KB Output is correct
6 Correct 362 ms 44700 KB Output is correct
7 Correct 389 ms 44832 KB Output is correct
8 Correct 372 ms 43400 KB Output is correct
9 Correct 377 ms 44324 KB Output is correct
10 Correct 371 ms 43124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8652 KB Output is correct
2 Correct 5 ms 8652 KB Output is correct
3 Correct 5 ms 8652 KB Output is correct
4 Correct 6 ms 8764 KB Output is correct
5 Correct 6 ms 8908 KB Output is correct
6 Correct 6 ms 8908 KB Output is correct
7 Correct 6 ms 8900 KB Output is correct
8 Correct 6 ms 8908 KB Output is correct
9 Correct 6 ms 8652 KB Output is correct
10 Correct 6 ms 8900 KB Output is correct
11 Correct 6 ms 8908 KB Output is correct
12 Correct 7 ms 8908 KB Output is correct
13 Correct 6 ms 8896 KB Output is correct
14 Correct 6 ms 8908 KB Output is correct
15 Correct 6 ms 8908 KB Output is correct
16 Correct 6 ms 8908 KB Output is correct
17 Correct 6 ms 8900 KB Output is correct
18 Correct 6 ms 8904 KB Output is correct
19 Correct 6 ms 8780 KB Output is correct
20 Correct 6 ms 8900 KB Output is correct
21 Correct 6 ms 8908 KB Output is correct
22 Correct 6 ms 8780 KB Output is correct
23 Correct 6 ms 8780 KB Output is correct
24 Correct 6 ms 8908 KB Output is correct
25 Correct 6 ms 8908 KB Output is correct
26 Correct 6 ms 8908 KB Output is correct
27 Correct 6 ms 8908 KB Output is correct
28 Correct 6 ms 8908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8652 KB Output is correct
2 Correct 5 ms 8652 KB Output is correct
3 Correct 5 ms 8652 KB Output is correct
4 Correct 5 ms 8652 KB Output is correct
5 Correct 6 ms 8764 KB Output is correct
6 Correct 6 ms 8908 KB Output is correct
7 Correct 6 ms 8908 KB Output is correct
8 Correct 6 ms 8900 KB Output is correct
9 Correct 6 ms 8908 KB Output is correct
10 Correct 75 ms 31160 KB Output is correct
11 Correct 95 ms 36220 KB Output is correct
12 Correct 91 ms 35704 KB Output is correct
13 Correct 100 ms 37304 KB Output is correct
14 Correct 84 ms 38828 KB Output is correct
15 Correct 6 ms 8900 KB Output is correct
16 Correct 6 ms 8908 KB Output is correct
17 Correct 7 ms 8908 KB Output is correct
18 Correct 6 ms 8896 KB Output is correct
19 Correct 6 ms 8908 KB Output is correct
20 Correct 6 ms 8908 KB Output is correct
21 Correct 6 ms 8908 KB Output is correct
22 Correct 6 ms 8900 KB Output is correct
23 Correct 6 ms 8904 KB Output is correct
24 Correct 6 ms 8780 KB Output is correct
25 Correct 6 ms 8900 KB Output is correct
26 Correct 6 ms 8908 KB Output is correct
27 Correct 6 ms 8780 KB Output is correct
28 Correct 6 ms 8780 KB Output is correct
29 Correct 6 ms 8908 KB Output is correct
30 Correct 6 ms 8908 KB Output is correct
31 Correct 6 ms 8908 KB Output is correct
32 Correct 6 ms 8908 KB Output is correct
33 Correct 6 ms 8908 KB Output is correct
34 Correct 17 ms 12544 KB Output is correct
35 Correct 102 ms 37016 KB Output is correct
36 Correct 104 ms 36980 KB Output is correct
37 Correct 100 ms 36920 KB Output is correct
38 Correct 102 ms 36660 KB Output is correct
39 Correct 99 ms 36528 KB Output is correct
40 Correct 91 ms 34096 KB Output is correct
41 Correct 104 ms 37304 KB Output is correct
42 Correct 103 ms 37064 KB Output is correct
43 Correct 85 ms 39152 KB Output is correct
44 Correct 106 ms 37284 KB Output is correct
45 Correct 119 ms 35760 KB Output is correct
46 Correct 100 ms 37052 KB Output is correct
47 Correct 100 ms 36904 KB Output is correct
48 Correct 107 ms 37816 KB Output is correct
49 Correct 76 ms 17232 KB Output is correct
50 Correct 62 ms 16940 KB Output is correct
51 Correct 102 ms 31228 KB Output is correct
52 Correct 141 ms 40756 KB Output is correct
53 Correct 147 ms 42052 KB Output is correct
54 Correct 156 ms 42668 KB Output is correct
55 Correct 87 ms 38816 KB Output is correct
56 Correct 148 ms 42416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8652 KB Output is correct
2 Correct 5 ms 8652 KB Output is correct
3 Correct 5 ms 8652 KB Output is correct
4 Correct 6 ms 8764 KB Output is correct
5 Correct 6 ms 8908 KB Output is correct
6 Correct 6 ms 8908 KB Output is correct
7 Correct 6 ms 8900 KB Output is correct
8 Correct 6 ms 8908 KB Output is correct
9 Correct 75 ms 31160 KB Output is correct
10 Correct 95 ms 36220 KB Output is correct
11 Correct 91 ms 35704 KB Output is correct
12 Correct 100 ms 37304 KB Output is correct
13 Correct 84 ms 38828 KB Output is correct
14 Correct 94 ms 31404 KB Output is correct
15 Correct 276 ms 40460 KB Output is correct
16 Correct 249 ms 39784 KB Output is correct
17 Correct 269 ms 41372 KB Output is correct
18 Correct 340 ms 43040 KB Output is correct
19 Correct 364 ms 43676 KB Output is correct
20 Correct 369 ms 45088 KB Output is correct
21 Correct 380 ms 44440 KB Output is correct
22 Correct 362 ms 44700 KB Output is correct
23 Correct 389 ms 44832 KB Output is correct
24 Correct 372 ms 43400 KB Output is correct
25 Correct 377 ms 44324 KB Output is correct
26 Correct 371 ms 43124 KB Output is correct
27 Correct 6 ms 8900 KB Output is correct
28 Correct 6 ms 8908 KB Output is correct
29 Correct 7 ms 8908 KB Output is correct
30 Correct 6 ms 8896 KB Output is correct
31 Correct 6 ms 8908 KB Output is correct
32 Correct 6 ms 8908 KB Output is correct
33 Correct 6 ms 8908 KB Output is correct
34 Correct 6 ms 8900 KB Output is correct
35 Correct 6 ms 8904 KB Output is correct
36 Correct 17 ms 12544 KB Output is correct
37 Correct 102 ms 37016 KB Output is correct
38 Correct 104 ms 36980 KB Output is correct
39 Correct 100 ms 36920 KB Output is correct
40 Correct 102 ms 36660 KB Output is correct
41 Correct 99 ms 36528 KB Output is correct
42 Correct 91 ms 34096 KB Output is correct
43 Correct 104 ms 37304 KB Output is correct
44 Correct 103 ms 37064 KB Output is correct
45 Correct 85 ms 39152 KB Output is correct
46 Correct 106 ms 37284 KB Output is correct
47 Correct 26 ms 12652 KB Output is correct
48 Correct 281 ms 41820 KB Output is correct
49 Correct 269 ms 41888 KB Output is correct
50 Correct 261 ms 41880 KB Output is correct
51 Correct 253 ms 41644 KB Output is correct
52 Correct 236 ms 40096 KB Output is correct
53 Correct 202 ms 33436 KB Output is correct
54 Correct 283 ms 42892 KB Output is correct
55 Correct 284 ms 41824 KB Output is correct
56 Correct 356 ms 43552 KB Output is correct
57 Correct 257 ms 42784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8652 KB Output is correct
2 Correct 5 ms 8652 KB Output is correct
3 Correct 5 ms 8652 KB Output is correct
4 Correct 5 ms 8652 KB Output is correct
5 Correct 6 ms 8764 KB Output is correct
6 Correct 6 ms 8908 KB Output is correct
7 Correct 6 ms 8908 KB Output is correct
8 Correct 6 ms 8900 KB Output is correct
9 Correct 6 ms 8908 KB Output is correct
10 Correct 75 ms 31160 KB Output is correct
11 Correct 95 ms 36220 KB Output is correct
12 Correct 91 ms 35704 KB Output is correct
13 Correct 100 ms 37304 KB Output is correct
14 Correct 84 ms 38828 KB Output is correct
15 Correct 94 ms 31404 KB Output is correct
16 Correct 276 ms 40460 KB Output is correct
17 Correct 249 ms 39784 KB Output is correct
18 Correct 269 ms 41372 KB Output is correct
19 Correct 340 ms 43040 KB Output is correct
20 Correct 364 ms 43676 KB Output is correct
21 Correct 369 ms 45088 KB Output is correct
22 Correct 380 ms 44440 KB Output is correct
23 Correct 362 ms 44700 KB Output is correct
24 Correct 389 ms 44832 KB Output is correct
25 Correct 372 ms 43400 KB Output is correct
26 Correct 377 ms 44324 KB Output is correct
27 Correct 371 ms 43124 KB Output is correct
28 Correct 6 ms 8900 KB Output is correct
29 Correct 6 ms 8908 KB Output is correct
30 Correct 7 ms 8908 KB Output is correct
31 Correct 6 ms 8896 KB Output is correct
32 Correct 6 ms 8908 KB Output is correct
33 Correct 6 ms 8908 KB Output is correct
34 Correct 6 ms 8908 KB Output is correct
35 Correct 6 ms 8900 KB Output is correct
36 Correct 6 ms 8904 KB Output is correct
37 Correct 17 ms 12544 KB Output is correct
38 Correct 102 ms 37016 KB Output is correct
39 Correct 104 ms 36980 KB Output is correct
40 Correct 100 ms 36920 KB Output is correct
41 Correct 102 ms 36660 KB Output is correct
42 Correct 99 ms 36528 KB Output is correct
43 Correct 91 ms 34096 KB Output is correct
44 Correct 104 ms 37304 KB Output is correct
45 Correct 103 ms 37064 KB Output is correct
46 Correct 85 ms 39152 KB Output is correct
47 Correct 106 ms 37284 KB Output is correct
48 Correct 26 ms 12652 KB Output is correct
49 Correct 281 ms 41820 KB Output is correct
50 Correct 269 ms 41888 KB Output is correct
51 Correct 261 ms 41880 KB Output is correct
52 Correct 253 ms 41644 KB Output is correct
53 Correct 236 ms 40096 KB Output is correct
54 Correct 202 ms 33436 KB Output is correct
55 Correct 283 ms 42892 KB Output is correct
56 Correct 284 ms 41824 KB Output is correct
57 Correct 356 ms 43552 KB Output is correct
58 Correct 257 ms 42784 KB Output is correct
59 Correct 100 ms 18312 KB Output is correct
60 Correct 255 ms 41564 KB Output is correct
61 Correct 264 ms 40940 KB Output is correct
62 Correct 277 ms 42808 KB Output is correct
63 Correct 387 ms 44312 KB Output is correct
64 Correct 6 ms 8780 KB Output is correct
65 Correct 6 ms 8900 KB Output is correct
66 Correct 6 ms 8908 KB Output is correct
67 Correct 6 ms 8780 KB Output is correct
68 Correct 6 ms 8780 KB Output is correct
69 Correct 6 ms 8908 KB Output is correct
70 Correct 6 ms 8908 KB Output is correct
71 Correct 6 ms 8908 KB Output is correct
72 Correct 6 ms 8908 KB Output is correct
73 Correct 6 ms 8908 KB Output is correct
74 Correct 119 ms 35760 KB Output is correct
75 Correct 100 ms 37052 KB Output is correct
76 Correct 100 ms 36904 KB Output is correct
77 Correct 107 ms 37816 KB Output is correct
78 Correct 76 ms 17232 KB Output is correct
79 Correct 62 ms 16940 KB Output is correct
80 Correct 102 ms 31228 KB Output is correct
81 Correct 141 ms 40756 KB Output is correct
82 Correct 147 ms 42052 KB Output is correct
83 Correct 156 ms 42668 KB Output is correct
84 Correct 87 ms 38816 KB Output is correct
85 Correct 148 ms 42416 KB Output is correct
86 Correct 78 ms 19992 KB Output is correct
87 Correct 250 ms 41968 KB Output is correct
88 Correct 267 ms 41812 KB Output is correct
89 Correct 348 ms 40976 KB Output is correct
90 Correct 175 ms 21768 KB Output is correct
91 Correct 196 ms 23824 KB Output is correct
92 Correct 271 ms 36368 KB Output is correct
93 Correct 300 ms 45032 KB Output is correct
94 Correct 334 ms 45876 KB Output is correct
95 Correct 339 ms 45492 KB Output is correct
96 Correct 363 ms 43792 KB Output is correct
97 Correct 346 ms 45172 KB Output is correct