답안 #916182

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
916182 2024-01-25T12:40:03 Z duckindog 자매 도시 (APIO20_swap) C++14
100 / 100
236 ms 42852 KB
#include<bits/stdc++.h>

using namespace std;

//#define LOCAL

#ifndef LOCAL
#include "swap.h"
#endif // LOCAL

const int N = 2e5 + 10;
struct edge {
  int u, v, w;
  edge() : u(0), v(0), w(0) {}
  edge(int u, int v, int w) : u(u), v(v), w(w) {}
} ED[N];
int par[N], id[N];
int root(int u, int ty = 0) {
  if (ty) return (id[u] < 0 ? u : id[u] = root(id[u], ty));
  return (par[u] < 0 ? u : par[u] = root(par[u]));
}
void add(int u, int v, int ty = 0) {
  u = root(u, ty); v = root(v, ty);
  if (u == v) return;
  if (ty) {
    if (u < v) swap(u, v);
    id[u] += id[v];
    id[v] = u;

  } else {
    if (par[u] > par[v]) swap(u, v);
    par[u] += par[v];
    par[v] = u;
  }
}

int n, m;
vector<int> ad[N];
bool mk[N];
int cnt[N], weight[N];
int low[N];

int f[N][17], st[N], ed[N], it;
void dfs(int u, int pre = -1) {
  if (mk[u]) low[u] = weight[u];
  st[u] = ++it;
  for (int i = 1; i <= 16; ++i)
    f[u][i] = f[f[u][i - 1]][i - 1];

  for (int v : ad[u]) {
    if (v == pre) continue;
    if (!mk[v]) low[v] = low[u];
    f[v][0] = u;
    dfs(v, u);
  }
  ed[u] = it;
}
bool anc(int u, int v) {
  return st[u] <= st[v] && ed[u] >= ed[v];
}
int lca(int u, int v) {
  if (anc(u, v)) return u;
  if (anc(v, u)) return v;
  for (int i = 16; i >= 0; --i)
    if (!anc(f[u][i], v)) u = f[u][i];
  return f[u][0];
}

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) {
    int u = U[i], v = V[i], w = W[i];
    ED[i] = {u, v, w};
  }
  sort(ED, ED + m, [&] (const auto& a, const auto& b) {
    return a.w < b.w;
  });

  memset(par, -1, sizeof par);
  memset(id, -1, sizeof id);
  int it = n - 1;

  for (int i = 0; i < m; ++i) {
    int u = ED[i].u, v = ED[i].v, w = ED[i].w;
    cnt[u] += 1; cnt[v] += 1;

    if (root(u) == root(v)) {
      int vertex = root(u, 1);
      if (mk[vertex]) continue;
      mk[vertex] = 1;
      weight[vertex] = w;
      continue;
    }

    add(u, v);
    it += 1;

    if (cnt[u] >= 3 || cnt[v] >= 3) {
      if (mk[it]) continue;
      mk[it] = 1;
      weight[it] = w;
    }

    u = root(u, 1); v = root(v, 1);

    ad[it].push_back(u); ad[it].push_back(v);
    mk[it] |= (mk[u] | mk[v]);
    add(it, u, 1); add(it, v, 1);
    weight[it] = w;
  }
  memset(low, -1, sizeof low);
  dfs(f[it][0] = it);

}

int getMinimumFuelCapacity(int X, int Y) {
  return low[lca(X, Y)];
}

#ifdef LOCAL
int32_t main() {
  cin.tie(0)->sync_with_stdio(0);

  if (fopen("duck.inp", "r")) {
    freopen("duck.inp", "r", stdin);
    freopen("duck.out", "w", stdout);
  }

  int n, m; cin >> n >> m;
  vector<int> U(m), V(m), W(m);
  for (int i = 0; i < m; ++i) cin >> U[i] >> V[i] >> W[i];
  init(n, m, U, V, W);

  int q; cin >> q;
  for (int i = 1; i <= q; ++i) {
    int X, Y; cin >> X >> Y;
    cout << getMinimumFuelCapacity(X, Y) << '\n';
  }


}
#endif // LOCAL
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 3 ms 13916 KB Output is correct
4 Correct 3 ms 13916 KB Output is correct
5 Correct 5 ms 13916 KB Output is correct
6 Correct 4 ms 13916 KB Output is correct
7 Correct 4 ms 13916 KB Output is correct
8 Correct 4 ms 13912 KB Output is correct
9 Correct 88 ms 28472 KB Output is correct
10 Correct 103 ms 31568 KB Output is correct
11 Correct 95 ms 31548 KB Output is correct
12 Correct 151 ms 31936 KB Output is correct
13 Correct 85 ms 35664 KB Output is correct
14 Correct 78 ms 28752 KB Output is correct
15 Correct 194 ms 33500 KB Output is correct
16 Correct 169 ms 33280 KB Output is correct
17 Correct 165 ms 33464 KB Output is correct
18 Correct 171 ms 37544 KB Output is correct
19 Correct 63 ms 20448 KB Output is correct
20 Correct 213 ms 34528 KB Output is correct
21 Correct 162 ms 34692 KB Output is correct
22 Correct 186 ms 34976 KB Output is correct
23 Correct 174 ms 38812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 236 ms 42232 KB Output is correct
4 Correct 155 ms 42420 KB Output is correct
5 Correct 173 ms 42492 KB Output is correct
6 Correct 164 ms 42276 KB Output is correct
7 Correct 153 ms 42444 KB Output is correct
8 Correct 152 ms 41860 KB Output is correct
9 Correct 152 ms 42528 KB Output is correct
10 Correct 152 ms 41872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 3 ms 13916 KB Output is correct
4 Correct 3 ms 13916 KB Output is correct
5 Correct 5 ms 13916 KB Output is correct
6 Correct 4 ms 13916 KB Output is correct
7 Correct 4 ms 13916 KB Output is correct
8 Correct 4 ms 13912 KB Output is correct
9 Correct 3 ms 13916 KB Output is correct
10 Correct 4 ms 13916 KB Output is correct
11 Correct 5 ms 13916 KB Output is correct
12 Correct 4 ms 13916 KB Output is correct
13 Correct 5 ms 13916 KB Output is correct
14 Correct 5 ms 13916 KB Output is correct
15 Correct 5 ms 13912 KB Output is correct
16 Correct 4 ms 13912 KB Output is correct
17 Correct 5 ms 13916 KB Output is correct
18 Correct 4 ms 13916 KB Output is correct
19 Correct 4 ms 13916 KB Output is correct
20 Correct 4 ms 13916 KB Output is correct
21 Correct 4 ms 13916 KB Output is correct
22 Correct 4 ms 13952 KB Output is correct
23 Correct 5 ms 13912 KB Output is correct
24 Correct 4 ms 13916 KB Output is correct
25 Correct 4 ms 13980 KB Output is correct
26 Correct 4 ms 13916 KB Output is correct
27 Correct 4 ms 13916 KB Output is correct
28 Correct 6 ms 13960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 3 ms 13916 KB Output is correct
4 Correct 3 ms 13916 KB Output is correct
5 Correct 3 ms 13916 KB Output is correct
6 Correct 5 ms 13916 KB Output is correct
7 Correct 4 ms 13916 KB Output is correct
8 Correct 4 ms 13916 KB Output is correct
9 Correct 4 ms 13912 KB Output is correct
10 Correct 88 ms 28472 KB Output is correct
11 Correct 103 ms 31568 KB Output is correct
12 Correct 95 ms 31548 KB Output is correct
13 Correct 151 ms 31936 KB Output is correct
14 Correct 85 ms 35664 KB Output is correct
15 Correct 4 ms 13916 KB Output is correct
16 Correct 5 ms 13916 KB Output is correct
17 Correct 4 ms 13916 KB Output is correct
18 Correct 5 ms 13916 KB Output is correct
19 Correct 5 ms 13916 KB Output is correct
20 Correct 5 ms 13912 KB Output is correct
21 Correct 4 ms 13912 KB Output is correct
22 Correct 5 ms 13916 KB Output is correct
23 Correct 4 ms 13916 KB Output is correct
24 Correct 4 ms 13916 KB Output is correct
25 Correct 4 ms 13916 KB Output is correct
26 Correct 4 ms 13916 KB Output is correct
27 Correct 4 ms 13952 KB Output is correct
28 Correct 5 ms 13912 KB Output is correct
29 Correct 4 ms 13916 KB Output is correct
30 Correct 4 ms 13980 KB Output is correct
31 Correct 4 ms 13916 KB Output is correct
32 Correct 4 ms 13916 KB Output is correct
33 Correct 6 ms 13960 KB Output is correct
34 Correct 12 ms 16728 KB Output is correct
35 Correct 116 ms 31792 KB Output is correct
36 Correct 107 ms 31712 KB Output is correct
37 Correct 103 ms 31828 KB Output is correct
38 Correct 107 ms 31928 KB Output is correct
39 Correct 97 ms 31828 KB Output is correct
40 Correct 87 ms 31416 KB Output is correct
41 Correct 106 ms 31684 KB Output is correct
42 Correct 113 ms 32052 KB Output is correct
43 Correct 87 ms 36948 KB Output is correct
44 Correct 104 ms 31988 KB Output is correct
45 Correct 96 ms 34012 KB Output is correct
46 Correct 105 ms 33156 KB Output is correct
47 Correct 100 ms 33316 KB Output is correct
48 Correct 103 ms 35152 KB Output is correct
49 Correct 58 ms 19284 KB Output is correct
50 Correct 46 ms 20736 KB Output is correct
51 Correct 87 ms 30548 KB Output is correct
52 Correct 122 ms 35208 KB Output is correct
53 Correct 134 ms 36692 KB Output is correct
54 Correct 139 ms 36408 KB Output is correct
55 Correct 88 ms 37800 KB Output is correct
56 Correct 140 ms 37996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 3 ms 13916 KB Output is correct
4 Correct 3 ms 13916 KB Output is correct
5 Correct 5 ms 13916 KB Output is correct
6 Correct 4 ms 13916 KB Output is correct
7 Correct 4 ms 13916 KB Output is correct
8 Correct 4 ms 13912 KB Output is correct
9 Correct 88 ms 28472 KB Output is correct
10 Correct 103 ms 31568 KB Output is correct
11 Correct 95 ms 31548 KB Output is correct
12 Correct 151 ms 31936 KB Output is correct
13 Correct 85 ms 35664 KB Output is correct
14 Correct 78 ms 28752 KB Output is correct
15 Correct 194 ms 33500 KB Output is correct
16 Correct 169 ms 33280 KB Output is correct
17 Correct 165 ms 33464 KB Output is correct
18 Correct 171 ms 37544 KB Output is correct
19 Correct 236 ms 42232 KB Output is correct
20 Correct 155 ms 42420 KB Output is correct
21 Correct 173 ms 42492 KB Output is correct
22 Correct 164 ms 42276 KB Output is correct
23 Correct 153 ms 42444 KB Output is correct
24 Correct 152 ms 41860 KB Output is correct
25 Correct 152 ms 42528 KB Output is correct
26 Correct 152 ms 41872 KB Output is correct
27 Correct 4 ms 13916 KB Output is correct
28 Correct 5 ms 13916 KB Output is correct
29 Correct 4 ms 13916 KB Output is correct
30 Correct 5 ms 13916 KB Output is correct
31 Correct 5 ms 13916 KB Output is correct
32 Correct 5 ms 13912 KB Output is correct
33 Correct 4 ms 13912 KB Output is correct
34 Correct 5 ms 13916 KB Output is correct
35 Correct 4 ms 13916 KB Output is correct
36 Correct 12 ms 16728 KB Output is correct
37 Correct 116 ms 31792 KB Output is correct
38 Correct 107 ms 31712 KB Output is correct
39 Correct 103 ms 31828 KB Output is correct
40 Correct 107 ms 31928 KB Output is correct
41 Correct 97 ms 31828 KB Output is correct
42 Correct 87 ms 31416 KB Output is correct
43 Correct 106 ms 31684 KB Output is correct
44 Correct 113 ms 32052 KB Output is correct
45 Correct 87 ms 36948 KB Output is correct
46 Correct 104 ms 31988 KB Output is correct
47 Correct 19 ms 16988 KB Output is correct
48 Correct 173 ms 34460 KB Output is correct
49 Correct 177 ms 34380 KB Output is correct
50 Correct 179 ms 34480 KB Output is correct
51 Correct 179 ms 34456 KB Output is correct
52 Correct 163 ms 34184 KB Output is correct
53 Correct 124 ms 29504 KB Output is correct
54 Correct 178 ms 35000 KB Output is correct
55 Correct 170 ms 34480 KB Output is correct
56 Correct 200 ms 38584 KB Output is correct
57 Correct 182 ms 35004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 3 ms 13916 KB Output is correct
4 Correct 3 ms 13916 KB Output is correct
5 Correct 3 ms 13916 KB Output is correct
6 Correct 5 ms 13916 KB Output is correct
7 Correct 4 ms 13916 KB Output is correct
8 Correct 4 ms 13916 KB Output is correct
9 Correct 4 ms 13912 KB Output is correct
10 Correct 88 ms 28472 KB Output is correct
11 Correct 103 ms 31568 KB Output is correct
12 Correct 95 ms 31548 KB Output is correct
13 Correct 151 ms 31936 KB Output is correct
14 Correct 85 ms 35664 KB Output is correct
15 Correct 78 ms 28752 KB Output is correct
16 Correct 194 ms 33500 KB Output is correct
17 Correct 169 ms 33280 KB Output is correct
18 Correct 165 ms 33464 KB Output is correct
19 Correct 171 ms 37544 KB Output is correct
20 Correct 236 ms 42232 KB Output is correct
21 Correct 155 ms 42420 KB Output is correct
22 Correct 173 ms 42492 KB Output is correct
23 Correct 164 ms 42276 KB Output is correct
24 Correct 153 ms 42444 KB Output is correct
25 Correct 152 ms 41860 KB Output is correct
26 Correct 152 ms 42528 KB Output is correct
27 Correct 152 ms 41872 KB Output is correct
28 Correct 4 ms 13916 KB Output is correct
29 Correct 5 ms 13916 KB Output is correct
30 Correct 4 ms 13916 KB Output is correct
31 Correct 5 ms 13916 KB Output is correct
32 Correct 5 ms 13916 KB Output is correct
33 Correct 5 ms 13912 KB Output is correct
34 Correct 4 ms 13912 KB Output is correct
35 Correct 5 ms 13916 KB Output is correct
36 Correct 4 ms 13916 KB Output is correct
37 Correct 12 ms 16728 KB Output is correct
38 Correct 116 ms 31792 KB Output is correct
39 Correct 107 ms 31712 KB Output is correct
40 Correct 103 ms 31828 KB Output is correct
41 Correct 107 ms 31928 KB Output is correct
42 Correct 97 ms 31828 KB Output is correct
43 Correct 87 ms 31416 KB Output is correct
44 Correct 106 ms 31684 KB Output is correct
45 Correct 113 ms 32052 KB Output is correct
46 Correct 87 ms 36948 KB Output is correct
47 Correct 104 ms 31988 KB Output is correct
48 Correct 19 ms 16988 KB Output is correct
49 Correct 173 ms 34460 KB Output is correct
50 Correct 177 ms 34380 KB Output is correct
51 Correct 179 ms 34480 KB Output is correct
52 Correct 179 ms 34456 KB Output is correct
53 Correct 163 ms 34184 KB Output is correct
54 Correct 124 ms 29504 KB Output is correct
55 Correct 178 ms 35000 KB Output is correct
56 Correct 170 ms 34480 KB Output is correct
57 Correct 200 ms 38584 KB Output is correct
58 Correct 182 ms 35004 KB Output is correct
59 Correct 63 ms 20448 KB Output is correct
60 Correct 213 ms 34528 KB Output is correct
61 Correct 162 ms 34692 KB Output is correct
62 Correct 186 ms 34976 KB Output is correct
63 Correct 174 ms 38812 KB Output is correct
64 Correct 4 ms 13916 KB Output is correct
65 Correct 4 ms 13916 KB Output is correct
66 Correct 4 ms 13916 KB Output is correct
67 Correct 4 ms 13952 KB Output is correct
68 Correct 5 ms 13912 KB Output is correct
69 Correct 4 ms 13916 KB Output is correct
70 Correct 4 ms 13980 KB Output is correct
71 Correct 4 ms 13916 KB Output is correct
72 Correct 4 ms 13916 KB Output is correct
73 Correct 6 ms 13960 KB Output is correct
74 Correct 96 ms 34012 KB Output is correct
75 Correct 105 ms 33156 KB Output is correct
76 Correct 100 ms 33316 KB Output is correct
77 Correct 103 ms 35152 KB Output is correct
78 Correct 58 ms 19284 KB Output is correct
79 Correct 46 ms 20736 KB Output is correct
80 Correct 87 ms 30548 KB Output is correct
81 Correct 122 ms 35208 KB Output is correct
82 Correct 134 ms 36692 KB Output is correct
83 Correct 139 ms 36408 KB Output is correct
84 Correct 88 ms 37800 KB Output is correct
85 Correct 140 ms 37996 KB Output is correct
86 Correct 48 ms 22536 KB Output is correct
87 Correct 178 ms 38532 KB Output is correct
88 Correct 172 ms 38572 KB Output is correct
89 Correct 203 ms 40312 KB Output is correct
90 Correct 113 ms 24788 KB Output is correct
91 Correct 118 ms 27432 KB Output is correct
92 Correct 149 ms 36524 KB Output is correct
93 Correct 202 ms 41488 KB Output is correct
94 Correct 219 ms 42576 KB Output is correct
95 Correct 213 ms 42124 KB Output is correct
96 Correct 191 ms 42852 KB Output is correct
97 Correct 211 ms 42592 KB Output is correct