답안 #740090

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
740090 2023-05-12T06:14:41 Z becaido 자매 도시 (APIO20_swap) C++17
100 / 100
540 ms 331872 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "swap.h"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

const int SIZE = 4e5 + 5;
const int H = __lg(SIZE);

int n, m;
int val[SIZE];
array<int, 3> e[SIZE];
vector<int> adj[SIZE];

struct DSU {
    int sz, to[SIZE];
    deque<int> v[SIZE];
    void init() {
        sz = n;
        iota(to + 1, to + n + m + 1, 1);
        FOR (i, 1, n) v[i].pb(i);
    }
    int dsu(int x) {
        return x == to[x] ? x : (to[x] = dsu(to[x]));
    }
    void Merge(int a, int b, int w) {
        debug("merge", a, b, w);
        int da = dsu(a), db = dsu(b);
        if (da == db) {
            debug("b0");
            if (v[da].size() == 0) return;
            sz++;
            val[sz] = w;
            to[da] = sz;
            for (int x : v[da]) adj[sz].pb(x);
            v[da].clear();
            return;
        }
        if (v[db].size() == 0 || (b != v[db][0] && b != v[db].back())) swap(a, b), swap(da, db);
        if (v[da].size() == 0) {
            debug("b1");
            sz++;
            val[sz] = w;
            adj[sz].pb(da), to[da] = to[db] = sz;
            if (v[db].size() == 0) adj[sz].pb(db);
            else {
                for (int x : v[db]) adj[sz].pb(x);
                v[db].clear();
            }
            return;
        }
        if (a != v[da][0] && a != v[da].back()) {
            debug("b2");
            sz++;
            val[sz] = w;
            to[da] = to[db] = sz;
            for (int x : v[da]) adj[sz].pb(x);
            v[da].clear();
            if (v[db].size() == 0) adj[sz].pb(db);
            else {
                for (int x : v[db]) adj[sz].pb(x);
                v[db].clear();
            }
            return;
        }
        debug("b3");
        if (v[da].size() < v[db].size()) {
            swap(a, b);
            swap(da, db);
        }
        if (b == v[db][0]) reverse(v[db].begin(), v[db].end());
        if (a == v[da][0]) while (v[db].size()) v[da].emplace_front(v[db].back()), v[db].pop_back();
        else while (v[db].size()) v[da].pb(v[db].back()), v[db].pop_back();
        to[db] = da;
    }
} dsu;

int h[SIZE], to[SIZE][H + 1];
void dfs(int pos) {
    for (int np : adj[pos]) {
        h[np] = h[pos] + 1;
        to[np][0] = pos;
        dfs(np);
    }
}
int jump(int pos, int k) {
    int cnt = 0;
    while (k) {
        if (k & 1) pos = to[pos][cnt];
        cnt++;
        k >>= 1;
    }
    return pos;
}
int lca(int a, int b) {
    if (h[a] < h[b]) swap(a, b);
    a = jump(a, h[a] - h[b]);
    if (a == b) return a;
    for (int i = H; i >= 0; i--) if (to[a][i] != to[b][i]) {
        a = to[a][i];
        b = to[b][i];
    }
    return to[a][0];
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    n = N, m = M;
    FOR (i, 0, m - 1) e[i + 1] = {U[i] + 1, V[i] + 1, W[i]};
    sort(e + 1, e + m + 1, [](auto lhs, auto rhs) {
        return lhs[2] < rhs[2];
    });
    dsu.init();
    FOR (i, 1, m) {
        auto [a, b, w] = e[i];
        dsu.Merge(a, b, w);
    }
    FOR (i, 1, n + m) if (i == dsu.dsu(i)) h[i] = 1, dfs(i);
    FOR (j, 1, H) FOR (i, 1, n + m) to[i][j] = to[to[i][j - 1]][j - 1];
}

int getMinimumFuelCapacity(int a, int b) {
    a++, b++;
    if (dsu.dsu(a) != dsu.dsu(b) || dsu.v[dsu.dsu(a)].size() || dsu.v[dsu.dsu(b)].size()) return -1;
    return val[lca(a, b)];
}

/*
in1
5 6
0 1 4
0 2 4
1 2 1
1 3 2
1 4 10
2 3 3
3
1 2
2 4
0 1
out1
3
10
4
in2
3 2
0 1 5
0 2 5
1
1 2
out2
-1
*/

#ifdef WAIMAI
int main() {
  int N, M;
  assert(2 == scanf("%d %d", &N, &M));

  vector<int> U(M), V(M), W(M);
  for (int i = 0; i < M; ++i) {
    assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
  }

  int Q;
  assert(1 == scanf("%d", &Q));

  vector<int> X(Q), Y(Q);
  for (int i = 0; i < Q; ++i) {
    assert(2 == scanf("%d %d", &X[i], &Y[i]));
  }

  init(N, M, U, V, W);

  vector<int> minimum_fuel_capacities(Q);
  for (int i = 0; i < Q; ++i) {
    minimum_fuel_capacities[i] = getMinimumFuelCapacity(X[i], Y[i]);
  }

  for (int i = 0; i < Q; ++i) {
    printf("%d\n", minimum_fuel_capacities[i]);
  }

  return 0;
}
#endif

Compilation message

swap.cpp: In member function 'void DSU::Merge(int, int, int)':
swap.cpp:16:20: warning: statement has no effect [-Wunused-value]
   16 | #define debug(...) 7122
      |                    ^~~~
swap.cpp:46:9: note: in expansion of macro 'debug'
   46 |         debug("merge", a, b, w);
      |         ^~~~~
swap.cpp:16:20: warning: statement has no effect [-Wunused-value]
   16 | #define debug(...) 7122
      |                    ^~~~
swap.cpp:49:13: note: in expansion of macro 'debug'
   49 |             debug("b0");
      |             ^~~~~
swap.cpp:16:20: warning: statement has no effect [-Wunused-value]
   16 | #define debug(...) 7122
      |                    ^~~~
swap.cpp:60:13: note: in expansion of macro 'debug'
   60 |             debug("b1");
      |             ^~~~~
swap.cpp:16:20: warning: statement has no effect [-Wunused-value]
   16 | #define debug(...) 7122
      |                    ^~~~
swap.cpp:72:13: note: in expansion of macro 'debug'
   72 |             debug("b2");
      |             ^~~~~
swap.cpp:16:20: warning: statement has no effect [-Wunused-value]
   16 | #define debug(...) 7122
      |                    ^~~~
swap.cpp:85:9: note: in expansion of macro 'debug'
   85 |         debug("b3");
      |         ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 160 ms 278980 KB Output is correct
2 Correct 159 ms 279032 KB Output is correct
3 Correct 166 ms 278880 KB Output is correct
4 Correct 183 ms 279128 KB Output is correct
5 Correct 179 ms 279280 KB Output is correct
6 Correct 175 ms 279268 KB Output is correct
7 Correct 181 ms 279280 KB Output is correct
8 Correct 178 ms 279200 KB Output is correct
9 Correct 265 ms 306192 KB Output is correct
10 Correct 292 ms 312140 KB Output is correct
11 Correct 293 ms 311756 KB Output is correct
12 Correct 300 ms 313584 KB Output is correct
13 Correct 275 ms 301012 KB Output is correct
14 Correct 272 ms 306568 KB Output is correct
15 Correct 345 ms 316492 KB Output is correct
16 Correct 342 ms 315732 KB Output is correct
17 Correct 346 ms 317748 KB Output is correct
18 Correct 321 ms 305204 KB Output is correct
19 Correct 238 ms 289720 KB Output is correct
20 Correct 359 ms 318636 KB Output is correct
21 Correct 390 ms 317712 KB Output is correct
22 Correct 384 ms 320112 KB Output is correct
23 Correct 349 ms 307456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 160 ms 278980 KB Output is correct
2 Correct 159 ms 279032 KB Output is correct
3 Correct 452 ms 311080 KB Output is correct
4 Correct 451 ms 312412 KB Output is correct
5 Correct 446 ms 312028 KB Output is correct
6 Correct 484 ms 312168 KB Output is correct
7 Correct 507 ms 312204 KB Output is correct
8 Correct 458 ms 311012 KB Output is correct
9 Correct 469 ms 311808 KB Output is correct
10 Correct 489 ms 310776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 160 ms 278980 KB Output is correct
2 Correct 159 ms 279032 KB Output is correct
3 Correct 166 ms 278880 KB Output is correct
4 Correct 183 ms 279128 KB Output is correct
5 Correct 179 ms 279280 KB Output is correct
6 Correct 175 ms 279268 KB Output is correct
7 Correct 181 ms 279280 KB Output is correct
8 Correct 178 ms 279200 KB Output is correct
9 Correct 175 ms 278860 KB Output is correct
10 Correct 180 ms 279280 KB Output is correct
11 Correct 190 ms 279244 KB Output is correct
12 Correct 186 ms 279316 KB Output is correct
13 Correct 169 ms 279352 KB Output is correct
14 Correct 163 ms 279160 KB Output is correct
15 Correct 183 ms 279268 KB Output is correct
16 Correct 183 ms 279300 KB Output is correct
17 Correct 163 ms 279240 KB Output is correct
18 Correct 163 ms 279332 KB Output is correct
19 Correct 165 ms 279116 KB Output is correct
20 Correct 182 ms 279224 KB Output is correct
21 Correct 167 ms 279236 KB Output is correct
22 Correct 170 ms 279360 KB Output is correct
23 Correct 167 ms 279224 KB Output is correct
24 Correct 196 ms 279448 KB Output is correct
25 Correct 185 ms 279372 KB Output is correct
26 Correct 180 ms 279468 KB Output is correct
27 Correct 167 ms 279248 KB Output is correct
28 Correct 184 ms 279364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 175 ms 278860 KB Output is correct
2 Correct 160 ms 278980 KB Output is correct
3 Correct 159 ms 279032 KB Output is correct
4 Correct 166 ms 278880 KB Output is correct
5 Correct 183 ms 279128 KB Output is correct
6 Correct 179 ms 279280 KB Output is correct
7 Correct 175 ms 279268 KB Output is correct
8 Correct 181 ms 279280 KB Output is correct
9 Correct 178 ms 279200 KB Output is correct
10 Correct 265 ms 306192 KB Output is correct
11 Correct 292 ms 312140 KB Output is correct
12 Correct 293 ms 311756 KB Output is correct
13 Correct 300 ms 313584 KB Output is correct
14 Correct 275 ms 301012 KB Output is correct
15 Correct 180 ms 279280 KB Output is correct
16 Correct 190 ms 279244 KB Output is correct
17 Correct 186 ms 279316 KB Output is correct
18 Correct 169 ms 279352 KB Output is correct
19 Correct 163 ms 279160 KB Output is correct
20 Correct 183 ms 279268 KB Output is correct
21 Correct 183 ms 279300 KB Output is correct
22 Correct 163 ms 279240 KB Output is correct
23 Correct 163 ms 279332 KB Output is correct
24 Correct 165 ms 279116 KB Output is correct
25 Correct 182 ms 279224 KB Output is correct
26 Correct 167 ms 279236 KB Output is correct
27 Correct 170 ms 279360 KB Output is correct
28 Correct 167 ms 279224 KB Output is correct
29 Correct 196 ms 279448 KB Output is correct
30 Correct 185 ms 279372 KB Output is correct
31 Correct 180 ms 279468 KB Output is correct
32 Correct 167 ms 279248 KB Output is correct
33 Correct 184 ms 279364 KB Output is correct
34 Correct 194 ms 283212 KB Output is correct
35 Correct 296 ms 314340 KB Output is correct
36 Correct 296 ms 314124 KB Output is correct
37 Correct 289 ms 314056 KB Output is correct
38 Correct 301 ms 313216 KB Output is correct
39 Correct 302 ms 312596 KB Output is correct
40 Correct 295 ms 308412 KB Output is correct
41 Correct 299 ms 314476 KB Output is correct
42 Correct 302 ms 314228 KB Output is correct
43 Correct 268 ms 306236 KB Output is correct
44 Correct 450 ms 310560 KB Output is correct
45 Correct 344 ms 312840 KB Output is correct
46 Correct 293 ms 314324 KB Output is correct
47 Correct 298 ms 313496 KB Output is correct
48 Correct 312 ms 312768 KB Output is correct
49 Correct 244 ms 300468 KB Output is correct
50 Correct 248 ms 296584 KB Output is correct
51 Correct 300 ms 307692 KB Output is correct
52 Correct 328 ms 323080 KB Output is correct
53 Correct 360 ms 325900 KB Output is correct
54 Correct 355 ms 328020 KB Output is correct
55 Correct 268 ms 306088 KB Output is correct
56 Correct 345 ms 319844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 160 ms 278980 KB Output is correct
2 Correct 159 ms 279032 KB Output is correct
3 Correct 166 ms 278880 KB Output is correct
4 Correct 183 ms 279128 KB Output is correct
5 Correct 179 ms 279280 KB Output is correct
6 Correct 175 ms 279268 KB Output is correct
7 Correct 181 ms 279280 KB Output is correct
8 Correct 178 ms 279200 KB Output is correct
9 Correct 265 ms 306192 KB Output is correct
10 Correct 292 ms 312140 KB Output is correct
11 Correct 293 ms 311756 KB Output is correct
12 Correct 300 ms 313584 KB Output is correct
13 Correct 275 ms 301012 KB Output is correct
14 Correct 272 ms 306568 KB Output is correct
15 Correct 345 ms 316492 KB Output is correct
16 Correct 342 ms 315732 KB Output is correct
17 Correct 346 ms 317748 KB Output is correct
18 Correct 321 ms 305204 KB Output is correct
19 Correct 452 ms 311080 KB Output is correct
20 Correct 451 ms 312412 KB Output is correct
21 Correct 446 ms 312028 KB Output is correct
22 Correct 484 ms 312168 KB Output is correct
23 Correct 507 ms 312204 KB Output is correct
24 Correct 458 ms 311012 KB Output is correct
25 Correct 469 ms 311808 KB Output is correct
26 Correct 489 ms 310776 KB Output is correct
27 Correct 180 ms 279280 KB Output is correct
28 Correct 190 ms 279244 KB Output is correct
29 Correct 186 ms 279316 KB Output is correct
30 Correct 169 ms 279352 KB Output is correct
31 Correct 163 ms 279160 KB Output is correct
32 Correct 183 ms 279268 KB Output is correct
33 Correct 183 ms 279300 KB Output is correct
34 Correct 163 ms 279240 KB Output is correct
35 Correct 163 ms 279332 KB Output is correct
36 Correct 194 ms 283212 KB Output is correct
37 Correct 296 ms 314340 KB Output is correct
38 Correct 296 ms 314124 KB Output is correct
39 Correct 289 ms 314056 KB Output is correct
40 Correct 301 ms 313216 KB Output is correct
41 Correct 302 ms 312596 KB Output is correct
42 Correct 295 ms 308412 KB Output is correct
43 Correct 299 ms 314476 KB Output is correct
44 Correct 302 ms 314228 KB Output is correct
45 Correct 268 ms 306236 KB Output is correct
46 Correct 450 ms 310560 KB Output is correct
47 Correct 207 ms 283340 KB Output is correct
48 Correct 402 ms 319136 KB Output is correct
49 Correct 427 ms 319160 KB Output is correct
50 Correct 423 ms 318888 KB Output is correct
51 Correct 384 ms 318380 KB Output is correct
52 Correct 379 ms 315976 KB Output is correct
53 Correct 361 ms 305632 KB Output is correct
54 Correct 375 ms 320052 KB Output is correct
55 Correct 374 ms 319024 KB Output is correct
56 Correct 465 ms 310704 KB Output is correct
57 Correct 419 ms 315996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 175 ms 278860 KB Output is correct
2 Correct 160 ms 278980 KB Output is correct
3 Correct 159 ms 279032 KB Output is correct
4 Correct 166 ms 278880 KB Output is correct
5 Correct 183 ms 279128 KB Output is correct
6 Correct 179 ms 279280 KB Output is correct
7 Correct 175 ms 279268 KB Output is correct
8 Correct 181 ms 279280 KB Output is correct
9 Correct 178 ms 279200 KB Output is correct
10 Correct 265 ms 306192 KB Output is correct
11 Correct 292 ms 312140 KB Output is correct
12 Correct 293 ms 311756 KB Output is correct
13 Correct 300 ms 313584 KB Output is correct
14 Correct 275 ms 301012 KB Output is correct
15 Correct 272 ms 306568 KB Output is correct
16 Correct 345 ms 316492 KB Output is correct
17 Correct 342 ms 315732 KB Output is correct
18 Correct 346 ms 317748 KB Output is correct
19 Correct 321 ms 305204 KB Output is correct
20 Correct 452 ms 311080 KB Output is correct
21 Correct 451 ms 312412 KB Output is correct
22 Correct 446 ms 312028 KB Output is correct
23 Correct 484 ms 312168 KB Output is correct
24 Correct 507 ms 312204 KB Output is correct
25 Correct 458 ms 311012 KB Output is correct
26 Correct 469 ms 311808 KB Output is correct
27 Correct 489 ms 310776 KB Output is correct
28 Correct 180 ms 279280 KB Output is correct
29 Correct 190 ms 279244 KB Output is correct
30 Correct 186 ms 279316 KB Output is correct
31 Correct 169 ms 279352 KB Output is correct
32 Correct 163 ms 279160 KB Output is correct
33 Correct 183 ms 279268 KB Output is correct
34 Correct 183 ms 279300 KB Output is correct
35 Correct 163 ms 279240 KB Output is correct
36 Correct 163 ms 279332 KB Output is correct
37 Correct 194 ms 283212 KB Output is correct
38 Correct 296 ms 314340 KB Output is correct
39 Correct 296 ms 314124 KB Output is correct
40 Correct 289 ms 314056 KB Output is correct
41 Correct 301 ms 313216 KB Output is correct
42 Correct 302 ms 312596 KB Output is correct
43 Correct 295 ms 308412 KB Output is correct
44 Correct 299 ms 314476 KB Output is correct
45 Correct 302 ms 314228 KB Output is correct
46 Correct 268 ms 306236 KB Output is correct
47 Correct 450 ms 310560 KB Output is correct
48 Correct 207 ms 283340 KB Output is correct
49 Correct 402 ms 319136 KB Output is correct
50 Correct 427 ms 319160 KB Output is correct
51 Correct 423 ms 318888 KB Output is correct
52 Correct 384 ms 318380 KB Output is correct
53 Correct 379 ms 315976 KB Output is correct
54 Correct 361 ms 305632 KB Output is correct
55 Correct 375 ms 320052 KB Output is correct
56 Correct 374 ms 319024 KB Output is correct
57 Correct 465 ms 310704 KB Output is correct
58 Correct 419 ms 315996 KB Output is correct
59 Correct 238 ms 289720 KB Output is correct
60 Correct 359 ms 318636 KB Output is correct
61 Correct 390 ms 317712 KB Output is correct
62 Correct 384 ms 320112 KB Output is correct
63 Correct 349 ms 307456 KB Output is correct
64 Correct 165 ms 279116 KB Output is correct
65 Correct 182 ms 279224 KB Output is correct
66 Correct 167 ms 279236 KB Output is correct
67 Correct 170 ms 279360 KB Output is correct
68 Correct 167 ms 279224 KB Output is correct
69 Correct 196 ms 279448 KB Output is correct
70 Correct 185 ms 279372 KB Output is correct
71 Correct 180 ms 279468 KB Output is correct
72 Correct 167 ms 279248 KB Output is correct
73 Correct 184 ms 279364 KB Output is correct
74 Correct 344 ms 312840 KB Output is correct
75 Correct 293 ms 314324 KB Output is correct
76 Correct 298 ms 313496 KB Output is correct
77 Correct 312 ms 312768 KB Output is correct
78 Correct 244 ms 300468 KB Output is correct
79 Correct 248 ms 296584 KB Output is correct
80 Correct 300 ms 307692 KB Output is correct
81 Correct 328 ms 323080 KB Output is correct
82 Correct 360 ms 325900 KB Output is correct
83 Correct 355 ms 328020 KB Output is correct
84 Correct 268 ms 306088 KB Output is correct
85 Correct 345 ms 319844 KB Output is correct
86 Correct 243 ms 292352 KB Output is correct
87 Correct 379 ms 319124 KB Output is correct
88 Correct 394 ms 319032 KB Output is correct
89 Correct 421 ms 315476 KB Output is correct
90 Correct 328 ms 305628 KB Output is correct
91 Correct 345 ms 305648 KB Output is correct
92 Correct 390 ms 311976 KB Output is correct
93 Correct 421 ms 327300 KB Output is correct
94 Correct 504 ms 329776 KB Output is correct
95 Correct 462 ms 331872 KB Output is correct
96 Correct 540 ms 310896 KB Output is correct
97 Correct 503 ms 320192 KB Output is correct