답안 #928398

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
928398 2024-02-16T10:08:50 Z VinhLuu 자매 도시 (APIO20_swap) C++17
100 / 100
428 ms 94432 KB
#include <bits/stdc++.h>
//#define int long long
//#define ll long long
#define fi first
#define se second
#define pb push_back
using namespace std;

typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 3e5 + 5;
const int oo = 2e9;
//const int mod = 1e9 + 7;

struct e{
    int u, v, w;
} p[N];

bool comp(e g1, e g2){
    return g1.w < g2.w;
}

int n, m, d[N], in[2][N], out[2][N], dp[2][N][22], mx[2][N][22], demin[2], demout[2], root, pa[N], sz[N], f[N], deg[N];
vector<pii> g[2][N];

int fin(int u){
    return pa[u] == u ? u : fin(pa[u]);
}

void dfs(int t,int u,int v,int ts){
    in[t][u] = ++demin[t];
    if(u == root) for(int i = 0; i <= 17; i ++) dp[t][u][i] = u;
    else{
        dp[t][u][0] = v;
        mx[t][u][0] = ts;
        for(int i = 1; i <= 16; i ++){
            dp[t][u][i] = dp[t][dp[t][u][i - 1]][i - 1];
            mx[t][u][i] = max(mx[t][u][i - 1], mx[t][dp[t][u][i - 1]][i - 1]);
        }
    }
    for(auto jj : g[t][u]){
        int j = jj.fi;
        int w = jj.se;
        if(j == v) continue;
        if(t == 0) f[j] = min(f[j], max(f[u], w));
        dfs(t, j, u, w);
    }
    out[t][u] = ++demout[t];
}

bool kt(int t,int u,int v){
    return in[t][u] <= in[t][v] && out[t][u] >= out[t][v];
}

int lca(int t,int u,int v){
    if(kt(t, u, v)) return u;
    else{
        int kq = u;
        for(int i = 16; i >= 0; i --){
            if(kt(t, dp[t][u][i], v)) kq = dp[t][u][i];
            else u = dp[t][u][i];
        }
        return kq;
    }
}

int get(int u,int v){
    int ret = 0, k = lca(1, u, v), tmp = u;
    for(int i = 16; i >= 0; i --){
        if(!kt(1, dp[1][u][i], v) || dp[1][u][i] == k){
            ret = max(ret, mx[1][u][i]);
            u = dp[1][u][i];
        }
    }
    u = tmp;
    for(int i = 16; i >= 0; i --){
        if(!kt(1, dp[1][v][i], u) || dp[1][v][i] == k){
            ret = max(ret, mx[1][v][i]);
            v = dp[1][v][i];
        }
    }
    return ret;
}

void dsu(int x,int y,int w){
    int u = x, v = y;
    x = fin(x);
    y = fin(y);
    if(x == y){
        f[x] = min(f[x], w);
//        cerr << u << " " << v << " " << x << " " << f[x] << "change\n";
        return;
    }
    if(sz[x] < sz[y]) swap(x, y);
    sz[x] += sz[y];
    pa[y] = x;
//    cerr << x << " " << y << " " << w << " f\n";
    g[0][x].push_back({y, w});
    g[1][u].pb({v, w});
    g[1][v].pb({u, w});
    f[x] = min(f[x], max(w, f[y]));
    d[x] = max(d[x], d[y]);
//    re[x] = max({re[x], re[y], w});
}

void init(int N,int M, vector<int> U, vector<int> V, vector<int> W){
    n = N;
    m = M;
    for(int i = 1; i <= n; i ++){
        f[i] = oo;
        pa[i] = i;
        sz[i] = 1;
    }

    for(int i = 0; i < m; i ++){
        p[i + 1].u = U[i] + 1;
        p[i + 1].v = V[i] + 1;
        p[i + 1].w = W[i];
    }
    sort(p + 1, p + m + 1, comp);
    for(int i = 1; i <= m; i ++){
        if(p[i].u == p[i].v) continue;
        dsu(p[i].u, p[i].v, p[i].w);
        int u = p[i].u, v = p[i].v, w = p[i].w;
        deg[u]++;
        deg[v]++;

        int j = fin(u);
        d[j] = max({d[j], deg[u], deg[v]});
        if(d[j] >= 3) f[j] = min(f[j], w);
//        cerr << u << " " << v << " " << w << " " << j << " " << f[j] << " gggg\n";
    }

    root = fin(1);
//    cerr << root << " g\n";
    dfs(0, root, 0, 0);
    dfs(1, root, 0, 0);
}

int getMinimumFuelCapacity(int x,int y){
    x++;
    y++;

    int t = lca(0, x, y), kq = max(f[t], get(x, y));
//    cerr << x << " " << y << " " << t << " " << f[t] << " " << get(x, y) << "f\n";
    if(f[t] == (int)oo) return -1;
    return max(f[t], get(x, y));
}

//#define LOCAL

#ifdef LOCAL
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    #define task "v"
    if(fopen(task ".inp","r")){
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }

    cin >> n >> m;
    vector<int> u, v, w;
    for(int i = 1; i <= m; i ++){
        int x, y, t; cin >> x >> y >> t;
        u.pb(x - 1);
        v.pb(y - 1);
        w.pb(t);
    }
    init(n, m, u, v, w);
    int rq = 0;
    cin >> rq;
//    cerr << rq << "fifai\n";
    while(rq--){
        int x, y;
        cin >> x >> y;
//        cerr << x << " " << y << " rr\n";
        x--;
        y--;

        cout << getMinimumFuelCapacity(x, y) << "\n";
    }
}
#endif // LOCAL


Compilation message

swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:144:27: warning: unused variable 'kq' [-Wunused-variable]
  144 |     int t = lca(0, x, y), kq = max(f[t], get(x, y));
      |                           ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 33368 KB Output is correct
2 Correct 7 ms 33372 KB Output is correct
3 Correct 6 ms 33372 KB Output is correct
4 Correct 6 ms 33372 KB Output is correct
5 Correct 6 ms 33368 KB Output is correct
6 Correct 6 ms 33372 KB Output is correct
7 Correct 7 ms 33372 KB Output is correct
8 Correct 6 ms 33372 KB Output is correct
9 Correct 96 ms 71864 KB Output is correct
10 Correct 177 ms 82400 KB Output is correct
11 Correct 161 ms 83284 KB Output is correct
12 Correct 153 ms 85276 KB Output is correct
13 Correct 121 ms 81168 KB Output is correct
14 Correct 116 ms 74068 KB Output is correct
15 Correct 389 ms 85708 KB Output is correct
16 Correct 351 ms 84600 KB Output is correct
17 Correct 425 ms 89768 KB Output is correct
18 Correct 411 ms 90432 KB Output is correct
19 Correct 137 ms 44516 KB Output is correct
20 Correct 408 ms 88880 KB Output is correct
21 Correct 369 ms 83048 KB Output is correct
22 Correct 428 ms 90036 KB Output is correct
23 Correct 371 ms 93024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 33368 KB Output is correct
2 Correct 7 ms 33372 KB Output is correct
3 Correct 253 ms 76340 KB Output is correct
4 Correct 247 ms 77208 KB Output is correct
5 Correct 270 ms 75104 KB Output is correct
6 Correct 251 ms 77044 KB Output is correct
7 Correct 292 ms 75164 KB Output is correct
8 Correct 257 ms 76264 KB Output is correct
9 Correct 249 ms 75060 KB Output is correct
10 Correct 248 ms 74124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 33368 KB Output is correct
2 Correct 7 ms 33372 KB Output is correct
3 Correct 6 ms 33372 KB Output is correct
4 Correct 6 ms 33372 KB Output is correct
5 Correct 6 ms 33368 KB Output is correct
6 Correct 6 ms 33372 KB Output is correct
7 Correct 7 ms 33372 KB Output is correct
8 Correct 6 ms 33372 KB Output is correct
9 Correct 6 ms 33372 KB Output is correct
10 Correct 7 ms 33368 KB Output is correct
11 Correct 7 ms 33580 KB Output is correct
12 Correct 6 ms 33372 KB Output is correct
13 Correct 6 ms 33372 KB Output is correct
14 Correct 6 ms 33368 KB Output is correct
15 Correct 6 ms 33372 KB Output is correct
16 Correct 6 ms 33372 KB Output is correct
17 Correct 6 ms 33624 KB Output is correct
18 Correct 6 ms 33368 KB Output is correct
19 Correct 7 ms 33372 KB Output is correct
20 Correct 6 ms 33516 KB Output is correct
21 Correct 7 ms 33372 KB Output is correct
22 Correct 6 ms 33372 KB Output is correct
23 Correct 6 ms 33372 KB Output is correct
24 Correct 7 ms 33540 KB Output is correct
25 Correct 6 ms 33372 KB Output is correct
26 Correct 7 ms 33372 KB Output is correct
27 Correct 6 ms 33628 KB Output is correct
28 Correct 6 ms 33376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 33372 KB Output is correct
2 Correct 10 ms 33368 KB Output is correct
3 Correct 7 ms 33372 KB Output is correct
4 Correct 6 ms 33372 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 6 ms 33368 KB Output is correct
7 Correct 6 ms 33372 KB Output is correct
8 Correct 7 ms 33372 KB Output is correct
9 Correct 6 ms 33372 KB Output is correct
10 Correct 96 ms 71864 KB Output is correct
11 Correct 177 ms 82400 KB Output is correct
12 Correct 161 ms 83284 KB Output is correct
13 Correct 153 ms 85276 KB Output is correct
14 Correct 121 ms 81168 KB Output is correct
15 Correct 7 ms 33368 KB Output is correct
16 Correct 7 ms 33580 KB Output is correct
17 Correct 6 ms 33372 KB Output is correct
18 Correct 6 ms 33372 KB Output is correct
19 Correct 6 ms 33368 KB Output is correct
20 Correct 6 ms 33372 KB Output is correct
21 Correct 6 ms 33372 KB Output is correct
22 Correct 6 ms 33624 KB Output is correct
23 Correct 6 ms 33368 KB Output is correct
24 Correct 7 ms 33372 KB Output is correct
25 Correct 6 ms 33516 KB Output is correct
26 Correct 7 ms 33372 KB Output is correct
27 Correct 6 ms 33372 KB Output is correct
28 Correct 6 ms 33372 KB Output is correct
29 Correct 7 ms 33540 KB Output is correct
30 Correct 6 ms 33372 KB Output is correct
31 Correct 7 ms 33372 KB Output is correct
32 Correct 6 ms 33628 KB Output is correct
33 Correct 6 ms 33376 KB Output is correct
34 Correct 15 ms 37212 KB Output is correct
35 Correct 130 ms 82772 KB Output is correct
36 Correct 136 ms 80200 KB Output is correct
37 Correct 117 ms 77652 KB Output is correct
38 Correct 110 ms 74520 KB Output is correct
39 Correct 115 ms 74092 KB Output is correct
40 Correct 100 ms 70224 KB Output is correct
41 Correct 154 ms 82500 KB Output is correct
42 Correct 127 ms 82260 KB Output is correct
43 Correct 113 ms 87732 KB Output is correct
44 Correct 118 ms 76740 KB Output is correct
45 Correct 115 ms 70616 KB Output is correct
46 Correct 127 ms 79700 KB Output is correct
47 Correct 131 ms 77996 KB Output is correct
48 Correct 109 ms 76240 KB Output is correct
49 Correct 56 ms 41912 KB Output is correct
50 Correct 47 ms 40968 KB Output is correct
51 Correct 89 ms 65344 KB Output is correct
52 Correct 149 ms 86624 KB Output is correct
53 Correct 138 ms 84820 KB Output is correct
54 Correct 166 ms 90684 KB Output is correct
55 Correct 119 ms 89664 KB Output is correct
56 Correct 136 ms 83660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 33368 KB Output is correct
2 Correct 7 ms 33372 KB Output is correct
3 Correct 6 ms 33372 KB Output is correct
4 Correct 6 ms 33372 KB Output is correct
5 Correct 6 ms 33368 KB Output is correct
6 Correct 6 ms 33372 KB Output is correct
7 Correct 7 ms 33372 KB Output is correct
8 Correct 6 ms 33372 KB Output is correct
9 Correct 96 ms 71864 KB Output is correct
10 Correct 177 ms 82400 KB Output is correct
11 Correct 161 ms 83284 KB Output is correct
12 Correct 153 ms 85276 KB Output is correct
13 Correct 121 ms 81168 KB Output is correct
14 Correct 116 ms 74068 KB Output is correct
15 Correct 389 ms 85708 KB Output is correct
16 Correct 351 ms 84600 KB Output is correct
17 Correct 425 ms 89768 KB Output is correct
18 Correct 411 ms 90432 KB Output is correct
19 Correct 253 ms 76340 KB Output is correct
20 Correct 247 ms 77208 KB Output is correct
21 Correct 270 ms 75104 KB Output is correct
22 Correct 251 ms 77044 KB Output is correct
23 Correct 292 ms 75164 KB Output is correct
24 Correct 257 ms 76264 KB Output is correct
25 Correct 249 ms 75060 KB Output is correct
26 Correct 248 ms 74124 KB Output is correct
27 Correct 7 ms 33368 KB Output is correct
28 Correct 7 ms 33580 KB Output is correct
29 Correct 6 ms 33372 KB Output is correct
30 Correct 6 ms 33372 KB Output is correct
31 Correct 6 ms 33368 KB Output is correct
32 Correct 6 ms 33372 KB Output is correct
33 Correct 6 ms 33372 KB Output is correct
34 Correct 6 ms 33624 KB Output is correct
35 Correct 6 ms 33368 KB Output is correct
36 Correct 15 ms 37212 KB Output is correct
37 Correct 130 ms 82772 KB Output is correct
38 Correct 136 ms 80200 KB Output is correct
39 Correct 117 ms 77652 KB Output is correct
40 Correct 110 ms 74520 KB Output is correct
41 Correct 115 ms 74092 KB Output is correct
42 Correct 100 ms 70224 KB Output is correct
43 Correct 154 ms 82500 KB Output is correct
44 Correct 127 ms 82260 KB Output is correct
45 Correct 113 ms 87732 KB Output is correct
46 Correct 118 ms 76740 KB Output is correct
47 Correct 28 ms 39216 KB Output is correct
48 Correct 355 ms 90124 KB Output is correct
49 Correct 352 ms 84920 KB Output is correct
50 Correct 376 ms 84232 KB Output is correct
51 Correct 341 ms 83312 KB Output is correct
52 Correct 322 ms 78340 KB Output is correct
53 Correct 239 ms 68612 KB Output is correct
54 Correct 379 ms 88428 KB Output is correct
55 Correct 361 ms 87640 KB Output is correct
56 Correct 352 ms 94432 KB Output is correct
57 Correct 316 ms 84148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 33372 KB Output is correct
2 Correct 10 ms 33368 KB Output is correct
3 Correct 7 ms 33372 KB Output is correct
4 Correct 6 ms 33372 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 6 ms 33368 KB Output is correct
7 Correct 6 ms 33372 KB Output is correct
8 Correct 7 ms 33372 KB Output is correct
9 Correct 6 ms 33372 KB Output is correct
10 Correct 96 ms 71864 KB Output is correct
11 Correct 177 ms 82400 KB Output is correct
12 Correct 161 ms 83284 KB Output is correct
13 Correct 153 ms 85276 KB Output is correct
14 Correct 121 ms 81168 KB Output is correct
15 Correct 116 ms 74068 KB Output is correct
16 Correct 389 ms 85708 KB Output is correct
17 Correct 351 ms 84600 KB Output is correct
18 Correct 425 ms 89768 KB Output is correct
19 Correct 411 ms 90432 KB Output is correct
20 Correct 253 ms 76340 KB Output is correct
21 Correct 247 ms 77208 KB Output is correct
22 Correct 270 ms 75104 KB Output is correct
23 Correct 251 ms 77044 KB Output is correct
24 Correct 292 ms 75164 KB Output is correct
25 Correct 257 ms 76264 KB Output is correct
26 Correct 249 ms 75060 KB Output is correct
27 Correct 248 ms 74124 KB Output is correct
28 Correct 7 ms 33368 KB Output is correct
29 Correct 7 ms 33580 KB Output is correct
30 Correct 6 ms 33372 KB Output is correct
31 Correct 6 ms 33372 KB Output is correct
32 Correct 6 ms 33368 KB Output is correct
33 Correct 6 ms 33372 KB Output is correct
34 Correct 6 ms 33372 KB Output is correct
35 Correct 6 ms 33624 KB Output is correct
36 Correct 6 ms 33368 KB Output is correct
37 Correct 15 ms 37212 KB Output is correct
38 Correct 130 ms 82772 KB Output is correct
39 Correct 136 ms 80200 KB Output is correct
40 Correct 117 ms 77652 KB Output is correct
41 Correct 110 ms 74520 KB Output is correct
42 Correct 115 ms 74092 KB Output is correct
43 Correct 100 ms 70224 KB Output is correct
44 Correct 154 ms 82500 KB Output is correct
45 Correct 127 ms 82260 KB Output is correct
46 Correct 113 ms 87732 KB Output is correct
47 Correct 118 ms 76740 KB Output is correct
48 Correct 28 ms 39216 KB Output is correct
49 Correct 355 ms 90124 KB Output is correct
50 Correct 352 ms 84920 KB Output is correct
51 Correct 376 ms 84232 KB Output is correct
52 Correct 341 ms 83312 KB Output is correct
53 Correct 322 ms 78340 KB Output is correct
54 Correct 239 ms 68612 KB Output is correct
55 Correct 379 ms 88428 KB Output is correct
56 Correct 361 ms 87640 KB Output is correct
57 Correct 352 ms 94432 KB Output is correct
58 Correct 316 ms 84148 KB Output is correct
59 Correct 137 ms 44516 KB Output is correct
60 Correct 408 ms 88880 KB Output is correct
61 Correct 369 ms 83048 KB Output is correct
62 Correct 428 ms 90036 KB Output is correct
63 Correct 371 ms 93024 KB Output is correct
64 Correct 7 ms 33372 KB Output is correct
65 Correct 6 ms 33516 KB Output is correct
66 Correct 7 ms 33372 KB Output is correct
67 Correct 6 ms 33372 KB Output is correct
68 Correct 6 ms 33372 KB Output is correct
69 Correct 7 ms 33540 KB Output is correct
70 Correct 6 ms 33372 KB Output is correct
71 Correct 7 ms 33372 KB Output is correct
72 Correct 6 ms 33628 KB Output is correct
73 Correct 6 ms 33376 KB Output is correct
74 Correct 115 ms 70616 KB Output is correct
75 Correct 127 ms 79700 KB Output is correct
76 Correct 131 ms 77996 KB Output is correct
77 Correct 109 ms 76240 KB Output is correct
78 Correct 56 ms 41912 KB Output is correct
79 Correct 47 ms 40968 KB Output is correct
80 Correct 89 ms 65344 KB Output is correct
81 Correct 149 ms 86624 KB Output is correct
82 Correct 138 ms 84820 KB Output is correct
83 Correct 166 ms 90684 KB Output is correct
84 Correct 119 ms 89664 KB Output is correct
85 Correct 136 ms 83660 KB Output is correct
86 Correct 74 ms 48652 KB Output is correct
87 Correct 344 ms 85428 KB Output is correct
88 Correct 399 ms 85176 KB Output is correct
89 Correct 302 ms 78580 KB Output is correct
90 Correct 178 ms 46376 KB Output is correct
91 Correct 192 ms 49776 KB Output is correct
92 Correct 242 ms 68356 KB Output is correct
93 Correct 368 ms 90244 KB Output is correct
94 Correct 335 ms 89140 KB Output is correct
95 Correct 425 ms 93012 KB Output is correct
96 Correct 402 ms 94332 KB Output is correct
97 Correct 323 ms 87140 KB Output is correct