답안 #894734

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
894734 2023-12-28T20:24:44 Z ShaShi 자매 도시 (APIO20_swap) C++17
43 / 100
166 ms 68200 KB
#include "swap.h"
#include <bits/stdc++.h>
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define kill(x) cout << x << endl, exit(0);
#define pb push_back
#define mp make_pair
#define pii pair<int, int>


using namespace std;
typedef long long ll;
typedef long double ld;


const int MAXN = (int)1e6 + 7;
const int LG = 18;
const int MOD = 998244353;
const int INF = (int)1e18 + 7;



int n, m, t, flag, k, u, v, w, ans, ans2, tmp, tmp2, tmp3, tmp4, q, timer;
int arr[MAXN], num[MAXN];
int par[MAXN], love[MAXN], dp[MAXN], deg[MAXN];
int h[MAXN], dad[MAXN], pow2dad[MAXN][LG];
vector<pair<int, pii> > edge;
vector<int> adj[MAXN];


/* DSU */

inline int get_par(int v) {
    return (par[v] == v? v : par[v] = get_par(par[v]));
}


void unite(int u, int v, int w) {
    int u2, v2;
    u2 = u;
    v2 = v;

    deg[u2]++, deg[v2]++;

    u = get_par(u), v = get_par(v);

    if (u == v) {
        if (!love[u]) {
            love[u] = 1;
            num[u] = w;
        }
    } else {
        par[u] = par[v] = flag;
        par[flag] = flag;
        adj[flag].pb(u), adj[flag].pb(v);
        num[flag] = w;

        if (love[u] || love[v]) {
            love[flag] = 1;
        } else {
            love[flag] = (deg[u2] == 3 || deg[v2] == 3);
        }

        // cout << "@" << flag << " " << u << " " << v << endl;

        flag++;
    }

}

/* DSU */

/* LCA */

inline int up(int v, int k) {
    for (int i=LG-1; i>=0; i--) {
        if ((1<<i) <= k) {
            k -= (1<<i);
            v = pow2dad[v][i];
        }
    }

    return v;
}


int LCA(int u, int v) {
    if (h[u] < h[v]) swap(u, v);

    u = up(u, h[u]-h[v]);

    for (int i=LG-1; i>=0; i--) {
        if (h[v] >= (1<<i) && pow2dad[v][i] != pow2dad[u][i]) {
            v = pow2dad[v][i];
            u = pow2dad[u][i];
        }
    }

    if (u == v) return u;
    return dad[v];
}


/* LCA */


void DFS(int v) {
    for (int u:adj[v]) {
        if (u != dad[v]) {
            dad[u] = v;
            h[u] = h[v]+1;
            DFS(u);

            love[v] |= love[u];
        }
    }

    dp[v] = (love[v]? v : dp[dad[v]]);
}

void DFS2(int v) {
    dp[v] = (love[v]? v : dp[dad[v]]);

    for (int u:adj[v]) if (u != dad[v]) DFS2(u);
}


void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
	n = N;
	m = M;

	for (int i=0; i<m; i++) {
        // cin >> u >> v >> w;
		u = U[i];
		v = V[i];
		w = W[i];

        edge.pb(mp(w, mp(u, v)));
    }

    sort(all(edge));
    flag = n+1;

    for (int i=1; i<=n; i++) par[i] = i;
    
    for (int i=0; i<m; i++) unite(edge[i].S.F, edge[i].S.S, edge[i].F);
    flag--;

    DFS(flag);
    DFS2(flag);

    for (int i=1; i<=flag; i++) pow2dad[i][0] = dad[i];
    for (int j=1; j<LG; j++) for (int i=1; i<=flag; i++) pow2dad[i][j] = pow2dad[pow2dad[i][j-1]][j-1];
    num[0]--;
}

int getMinimumFuelCapacity(int X, int Y) {
	return num[dp[LCA(X, Y)]];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 31324 KB Output is correct
2 Correct 6 ms 31324 KB Output is correct
3 Correct 7 ms 31576 KB Output is correct
4 Correct 7 ms 31324 KB Output is correct
5 Correct 7 ms 31324 KB Output is correct
6 Correct 7 ms 31392 KB Output is correct
7 Correct 7 ms 31320 KB Output is correct
8 Correct 6 ms 31320 KB Output is correct
9 Correct 58 ms 50632 KB Output is correct
10 Correct 72 ms 56180 KB Output is correct
11 Correct 62 ms 56004 KB Output is correct
12 Correct 69 ms 58664 KB Output is correct
13 Correct 62 ms 62624 KB Output is correct
14 Correct 58 ms 50884 KB Output is correct
15 Correct 128 ms 62284 KB Output is correct
16 Correct 122 ms 60024 KB Output is correct
17 Correct 124 ms 62764 KB Output is correct
18 Correct 148 ms 66608 KB Output is correct
19 Correct 60 ms 40212 KB Output is correct
20 Correct 143 ms 63380 KB Output is correct
21 Correct 121 ms 61312 KB Output is correct
22 Correct 127 ms 64196 KB Output is correct
23 Correct 166 ms 67888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 31324 KB Output is correct
2 Correct 6 ms 31324 KB Output is correct
3 Incorrect 147 ms 68200 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 31324 KB Output is correct
2 Correct 6 ms 31324 KB Output is correct
3 Correct 7 ms 31576 KB Output is correct
4 Correct 7 ms 31324 KB Output is correct
5 Correct 7 ms 31324 KB Output is correct
6 Correct 7 ms 31392 KB Output is correct
7 Correct 7 ms 31320 KB Output is correct
8 Correct 6 ms 31320 KB Output is correct
9 Correct 7 ms 31320 KB Output is correct
10 Correct 7 ms 31320 KB Output is correct
11 Correct 7 ms 31528 KB Output is correct
12 Correct 7 ms 31324 KB Output is correct
13 Correct 6 ms 31396 KB Output is correct
14 Correct 7 ms 31324 KB Output is correct
15 Correct 7 ms 31364 KB Output is correct
16 Correct 7 ms 31324 KB Output is correct
17 Correct 6 ms 31324 KB Output is correct
18 Correct 7 ms 31392 KB Output is correct
19 Correct 7 ms 31324 KB Output is correct
20 Correct 7 ms 31324 KB Output is correct
21 Correct 7 ms 31360 KB Output is correct
22 Correct 7 ms 31324 KB Output is correct
23 Correct 6 ms 31324 KB Output is correct
24 Correct 7 ms 31576 KB Output is correct
25 Correct 7 ms 31576 KB Output is correct
26 Correct 7 ms 31580 KB Output is correct
27 Correct 7 ms 31360 KB Output is correct
28 Correct 7 ms 31580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 31320 KB Output is correct
2 Correct 6 ms 31324 KB Output is correct
3 Correct 6 ms 31324 KB Output is correct
4 Correct 7 ms 31576 KB Output is correct
5 Correct 7 ms 31324 KB Output is correct
6 Correct 7 ms 31324 KB Output is correct
7 Correct 7 ms 31392 KB Output is correct
8 Correct 7 ms 31320 KB Output is correct
9 Correct 6 ms 31320 KB Output is correct
10 Correct 58 ms 50632 KB Output is correct
11 Correct 72 ms 56180 KB Output is correct
12 Correct 62 ms 56004 KB Output is correct
13 Correct 69 ms 58664 KB Output is correct
14 Correct 62 ms 62624 KB Output is correct
15 Correct 7 ms 31320 KB Output is correct
16 Correct 7 ms 31528 KB Output is correct
17 Correct 7 ms 31324 KB Output is correct
18 Correct 6 ms 31396 KB Output is correct
19 Correct 7 ms 31324 KB Output is correct
20 Correct 7 ms 31364 KB Output is correct
21 Correct 7 ms 31324 KB Output is correct
22 Correct 6 ms 31324 KB Output is correct
23 Correct 7 ms 31392 KB Output is correct
24 Correct 7 ms 31324 KB Output is correct
25 Correct 7 ms 31324 KB Output is correct
26 Correct 7 ms 31360 KB Output is correct
27 Correct 7 ms 31324 KB Output is correct
28 Correct 6 ms 31324 KB Output is correct
29 Correct 7 ms 31576 KB Output is correct
30 Correct 7 ms 31576 KB Output is correct
31 Correct 7 ms 31580 KB Output is correct
32 Correct 7 ms 31360 KB Output is correct
33 Correct 7 ms 31580 KB Output is correct
34 Correct 15 ms 34840 KB Output is correct
35 Correct 72 ms 58372 KB Output is correct
36 Correct 94 ms 58312 KB Output is correct
37 Correct 68 ms 58312 KB Output is correct
38 Correct 67 ms 58252 KB Output is correct
39 Correct 70 ms 58184 KB Output is correct
40 Correct 63 ms 55200 KB Output is correct
41 Correct 69 ms 58572 KB Output is correct
42 Correct 67 ms 58304 KB Output is correct
43 Correct 62 ms 63384 KB Output is correct
44 Correct 76 ms 58568 KB Output is correct
45 Correct 85 ms 56764 KB Output is correct
46 Correct 75 ms 58304 KB Output is correct
47 Correct 69 ms 58456 KB Output is correct
48 Correct 75 ms 60616 KB Output is correct
49 Correct 55 ms 39740 KB Output is correct
50 Correct 51 ms 38388 KB Output is correct
51 Correct 68 ms 52168 KB Output is correct
52 Correct 94 ms 62108 KB Output is correct
53 Correct 102 ms 64776 KB Output is correct
54 Correct 107 ms 64708 KB Output is correct
55 Correct 66 ms 62916 KB Output is correct
56 Correct 98 ms 64964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 31324 KB Output is correct
2 Correct 6 ms 31324 KB Output is correct
3 Correct 7 ms 31576 KB Output is correct
4 Correct 7 ms 31324 KB Output is correct
5 Correct 7 ms 31324 KB Output is correct
6 Correct 7 ms 31392 KB Output is correct
7 Correct 7 ms 31320 KB Output is correct
8 Correct 6 ms 31320 KB Output is correct
9 Correct 58 ms 50632 KB Output is correct
10 Correct 72 ms 56180 KB Output is correct
11 Correct 62 ms 56004 KB Output is correct
12 Correct 69 ms 58664 KB Output is correct
13 Correct 62 ms 62624 KB Output is correct
14 Correct 58 ms 50884 KB Output is correct
15 Correct 128 ms 62284 KB Output is correct
16 Correct 122 ms 60024 KB Output is correct
17 Correct 124 ms 62764 KB Output is correct
18 Correct 148 ms 66608 KB Output is correct
19 Incorrect 147 ms 68200 KB Output isn't correct
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 31320 KB Output is correct
2 Correct 6 ms 31324 KB Output is correct
3 Correct 6 ms 31324 KB Output is correct
4 Correct 7 ms 31576 KB Output is correct
5 Correct 7 ms 31324 KB Output is correct
6 Correct 7 ms 31324 KB Output is correct
7 Correct 7 ms 31392 KB Output is correct
8 Correct 7 ms 31320 KB Output is correct
9 Correct 6 ms 31320 KB Output is correct
10 Correct 58 ms 50632 KB Output is correct
11 Correct 72 ms 56180 KB Output is correct
12 Correct 62 ms 56004 KB Output is correct
13 Correct 69 ms 58664 KB Output is correct
14 Correct 62 ms 62624 KB Output is correct
15 Correct 58 ms 50884 KB Output is correct
16 Correct 128 ms 62284 KB Output is correct
17 Correct 122 ms 60024 KB Output is correct
18 Correct 124 ms 62764 KB Output is correct
19 Correct 148 ms 66608 KB Output is correct
20 Incorrect 147 ms 68200 KB Output isn't correct
21 Halted 0 ms 0 KB -