답안 #931500

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
931500 2024-02-22T00:38:09 Z duckindog 자매 도시 (APIO20_swap) C++17
100 / 100
200 ms 46616 KB
#include<bits/stdc++.h>
 
using namespace std;
 
//#define 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 13988 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 14012 KB Output is correct
6 Correct 3 ms 13916 KB Output is correct
7 Correct 4 ms 13916 KB Output is correct
8 Correct 3 ms 13984 KB Output is correct
9 Correct 62 ms 30292 KB Output is correct
10 Correct 83 ms 33752 KB Output is correct
11 Correct 86 ms 33620 KB Output is correct
12 Correct 85 ms 33876 KB Output is correct
13 Correct 71 ms 37968 KB Output is correct
14 Correct 67 ms 30548 KB Output is correct
15 Correct 156 ms 37780 KB Output is correct
16 Correct 148 ms 38048 KB Output is correct
17 Correct 148 ms 37984 KB Output is correct
18 Correct 152 ms 41976 KB Output is correct
19 Correct 61 ms 22252 KB Output is correct
20 Correct 180 ms 38884 KB Output is correct
21 Correct 141 ms 38924 KB Output is correct
22 Correct 152 ms 39436 KB Output is correct
23 Correct 163 ms 43472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13988 KB Output is correct
3 Correct 146 ms 45808 KB Output is correct
4 Correct 160 ms 46616 KB Output is correct
5 Correct 155 ms 46380 KB Output is correct
6 Correct 138 ms 46260 KB Output is correct
7 Correct 167 ms 46444 KB Output is correct
8 Correct 143 ms 45828 KB Output is correct
9 Correct 166 ms 46024 KB Output is correct
10 Correct 152 ms 45692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13988 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 14012 KB Output is correct
6 Correct 3 ms 13916 KB Output is correct
7 Correct 4 ms 13916 KB Output is correct
8 Correct 3 ms 13984 KB Output is correct
9 Correct 3 ms 13912 KB Output is correct
10 Correct 3 ms 13916 KB Output is correct
11 Correct 3 ms 13912 KB Output is correct
12 Correct 4 ms 13916 KB Output is correct
13 Correct 3 ms 13916 KB Output is correct
14 Correct 3 ms 13916 KB Output is correct
15 Correct 3 ms 14024 KB Output is correct
16 Correct 4 ms 13968 KB Output is correct
17 Correct 3 ms 13916 KB Output is correct
18 Correct 3 ms 13996 KB Output is correct
19 Correct 3 ms 13916 KB Output is correct
20 Correct 3 ms 13916 KB Output is correct
21 Correct 4 ms 13912 KB Output is correct
22 Correct 3 ms 13916 KB Output is correct
23 Correct 3 ms 13916 KB Output is correct
24 Correct 3 ms 13912 KB Output is correct
25 Correct 3 ms 13912 KB Output is correct
26 Correct 4 ms 13916 KB Output is correct
27 Correct 4 ms 13916 KB Output is correct
28 Correct 3 ms 13980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13912 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 3 ms 13988 KB Output is correct
4 Correct 3 ms 13916 KB Output is correct
5 Correct 3 ms 13916 KB Output is correct
6 Correct 3 ms 14012 KB Output is correct
7 Correct 3 ms 13916 KB Output is correct
8 Correct 4 ms 13916 KB Output is correct
9 Correct 3 ms 13984 KB Output is correct
10 Correct 62 ms 30292 KB Output is correct
11 Correct 83 ms 33752 KB Output is correct
12 Correct 86 ms 33620 KB Output is correct
13 Correct 85 ms 33876 KB Output is correct
14 Correct 71 ms 37968 KB Output is correct
15 Correct 3 ms 13916 KB Output is correct
16 Correct 3 ms 13912 KB Output is correct
17 Correct 4 ms 13916 KB Output is correct
18 Correct 3 ms 13916 KB Output is correct
19 Correct 3 ms 13916 KB Output is correct
20 Correct 3 ms 14024 KB Output is correct
21 Correct 4 ms 13968 KB Output is correct
22 Correct 3 ms 13916 KB Output is correct
23 Correct 3 ms 13996 KB Output is correct
24 Correct 3 ms 13916 KB Output is correct
25 Correct 3 ms 13916 KB Output is correct
26 Correct 4 ms 13912 KB Output is correct
27 Correct 3 ms 13916 KB Output is correct
28 Correct 3 ms 13916 KB Output is correct
29 Correct 3 ms 13912 KB Output is correct
30 Correct 3 ms 13912 KB Output is correct
31 Correct 4 ms 13916 KB Output is correct
32 Correct 4 ms 13916 KB Output is correct
33 Correct 3 ms 13980 KB Output is correct
34 Correct 10 ms 16988 KB Output is correct
35 Correct 86 ms 33620 KB Output is correct
36 Correct 98 ms 33616 KB Output is correct
37 Correct 87 ms 33560 KB Output is correct
38 Correct 84 ms 33676 KB Output is correct
39 Correct 85 ms 33812 KB Output is correct
40 Correct 75 ms 33060 KB Output is correct
41 Correct 87 ms 33872 KB Output is correct
42 Correct 87 ms 33620 KB Output is correct
43 Correct 73 ms 38740 KB Output is correct
44 Correct 88 ms 34120 KB Output is correct
45 Correct 92 ms 35120 KB Output is correct
46 Correct 87 ms 33616 KB Output is correct
47 Correct 85 ms 33692 KB Output is correct
48 Correct 91 ms 36016 KB Output is correct
49 Correct 47 ms 20304 KB Output is correct
50 Correct 41 ms 21420 KB Output is correct
51 Correct 69 ms 31572 KB Output is correct
52 Correct 114 ms 36724 KB Output is correct
53 Correct 113 ms 38260 KB Output is correct
54 Correct 122 ms 38228 KB Output is correct
55 Correct 75 ms 38224 KB Output is correct
56 Correct 117 ms 39652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13988 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 14012 KB Output is correct
6 Correct 3 ms 13916 KB Output is correct
7 Correct 4 ms 13916 KB Output is correct
8 Correct 3 ms 13984 KB Output is correct
9 Correct 62 ms 30292 KB Output is correct
10 Correct 83 ms 33752 KB Output is correct
11 Correct 86 ms 33620 KB Output is correct
12 Correct 85 ms 33876 KB Output is correct
13 Correct 71 ms 37968 KB Output is correct
14 Correct 67 ms 30548 KB Output is correct
15 Correct 156 ms 37780 KB Output is correct
16 Correct 148 ms 38048 KB Output is correct
17 Correct 148 ms 37984 KB Output is correct
18 Correct 152 ms 41976 KB Output is correct
19 Correct 146 ms 45808 KB Output is correct
20 Correct 160 ms 46616 KB Output is correct
21 Correct 155 ms 46380 KB Output is correct
22 Correct 138 ms 46260 KB Output is correct
23 Correct 167 ms 46444 KB Output is correct
24 Correct 143 ms 45828 KB Output is correct
25 Correct 166 ms 46024 KB Output is correct
26 Correct 152 ms 45692 KB Output is correct
27 Correct 3 ms 13916 KB Output is correct
28 Correct 3 ms 13912 KB Output is correct
29 Correct 4 ms 13916 KB Output is correct
30 Correct 3 ms 13916 KB Output is correct
31 Correct 3 ms 13916 KB Output is correct
32 Correct 3 ms 14024 KB Output is correct
33 Correct 4 ms 13968 KB Output is correct
34 Correct 3 ms 13916 KB Output is correct
35 Correct 3 ms 13996 KB Output is correct
36 Correct 10 ms 16988 KB Output is correct
37 Correct 86 ms 33620 KB Output is correct
38 Correct 98 ms 33616 KB Output is correct
39 Correct 87 ms 33560 KB Output is correct
40 Correct 84 ms 33676 KB Output is correct
41 Correct 85 ms 33812 KB Output is correct
42 Correct 75 ms 33060 KB Output is correct
43 Correct 87 ms 33872 KB Output is correct
44 Correct 87 ms 33620 KB Output is correct
45 Correct 73 ms 38740 KB Output is correct
46 Correct 88 ms 34120 KB Output is correct
47 Correct 16 ms 17500 KB Output is correct
48 Correct 150 ms 38628 KB Output is correct
49 Correct 146 ms 38532 KB Output is correct
50 Correct 153 ms 38592 KB Output is correct
51 Correct 153 ms 38476 KB Output is correct
52 Correct 146 ms 38128 KB Output is correct
53 Correct 122 ms 33084 KB Output is correct
54 Correct 149 ms 39352 KB Output is correct
55 Correct 152 ms 38556 KB Output is correct
56 Correct 171 ms 42928 KB Output is correct
57 Correct 153 ms 39564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13912 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 3 ms 13988 KB Output is correct
4 Correct 3 ms 13916 KB Output is correct
5 Correct 3 ms 13916 KB Output is correct
6 Correct 3 ms 14012 KB Output is correct
7 Correct 3 ms 13916 KB Output is correct
8 Correct 4 ms 13916 KB Output is correct
9 Correct 3 ms 13984 KB Output is correct
10 Correct 62 ms 30292 KB Output is correct
11 Correct 83 ms 33752 KB Output is correct
12 Correct 86 ms 33620 KB Output is correct
13 Correct 85 ms 33876 KB Output is correct
14 Correct 71 ms 37968 KB Output is correct
15 Correct 67 ms 30548 KB Output is correct
16 Correct 156 ms 37780 KB Output is correct
17 Correct 148 ms 38048 KB Output is correct
18 Correct 148 ms 37984 KB Output is correct
19 Correct 152 ms 41976 KB Output is correct
20 Correct 146 ms 45808 KB Output is correct
21 Correct 160 ms 46616 KB Output is correct
22 Correct 155 ms 46380 KB Output is correct
23 Correct 138 ms 46260 KB Output is correct
24 Correct 167 ms 46444 KB Output is correct
25 Correct 143 ms 45828 KB Output is correct
26 Correct 166 ms 46024 KB Output is correct
27 Correct 152 ms 45692 KB Output is correct
28 Correct 3 ms 13916 KB Output is correct
29 Correct 3 ms 13912 KB Output is correct
30 Correct 4 ms 13916 KB Output is correct
31 Correct 3 ms 13916 KB Output is correct
32 Correct 3 ms 13916 KB Output is correct
33 Correct 3 ms 14024 KB Output is correct
34 Correct 4 ms 13968 KB Output is correct
35 Correct 3 ms 13916 KB Output is correct
36 Correct 3 ms 13996 KB Output is correct
37 Correct 10 ms 16988 KB Output is correct
38 Correct 86 ms 33620 KB Output is correct
39 Correct 98 ms 33616 KB Output is correct
40 Correct 87 ms 33560 KB Output is correct
41 Correct 84 ms 33676 KB Output is correct
42 Correct 85 ms 33812 KB Output is correct
43 Correct 75 ms 33060 KB Output is correct
44 Correct 87 ms 33872 KB Output is correct
45 Correct 87 ms 33620 KB Output is correct
46 Correct 73 ms 38740 KB Output is correct
47 Correct 88 ms 34120 KB Output is correct
48 Correct 16 ms 17500 KB Output is correct
49 Correct 150 ms 38628 KB Output is correct
50 Correct 146 ms 38532 KB Output is correct
51 Correct 153 ms 38592 KB Output is correct
52 Correct 153 ms 38476 KB Output is correct
53 Correct 146 ms 38128 KB Output is correct
54 Correct 122 ms 33084 KB Output is correct
55 Correct 149 ms 39352 KB Output is correct
56 Correct 152 ms 38556 KB Output is correct
57 Correct 171 ms 42928 KB Output is correct
58 Correct 153 ms 39564 KB Output is correct
59 Correct 61 ms 22252 KB Output is correct
60 Correct 180 ms 38884 KB Output is correct
61 Correct 141 ms 38924 KB Output is correct
62 Correct 152 ms 39436 KB Output is correct
63 Correct 163 ms 43472 KB Output is correct
64 Correct 3 ms 13916 KB Output is correct
65 Correct 3 ms 13916 KB Output is correct
66 Correct 4 ms 13912 KB Output is correct
67 Correct 3 ms 13916 KB Output is correct
68 Correct 3 ms 13916 KB Output is correct
69 Correct 3 ms 13912 KB Output is correct
70 Correct 3 ms 13912 KB Output is correct
71 Correct 4 ms 13916 KB Output is correct
72 Correct 4 ms 13916 KB Output is correct
73 Correct 3 ms 13980 KB Output is correct
74 Correct 92 ms 35120 KB Output is correct
75 Correct 87 ms 33616 KB Output is correct
76 Correct 85 ms 33692 KB Output is correct
77 Correct 91 ms 36016 KB Output is correct
78 Correct 47 ms 20304 KB Output is correct
79 Correct 41 ms 21420 KB Output is correct
80 Correct 69 ms 31572 KB Output is correct
81 Correct 114 ms 36724 KB Output is correct
82 Correct 113 ms 38260 KB Output is correct
83 Correct 122 ms 38228 KB Output is correct
84 Correct 75 ms 38224 KB Output is correct
85 Correct 117 ms 39652 KB Output is correct
86 Correct 46 ms 23128 KB Output is correct
87 Correct 157 ms 38432 KB Output is correct
88 Correct 146 ms 38584 KB Output is correct
89 Correct 161 ms 40296 KB Output is correct
90 Correct 114 ms 24640 KB Output is correct
91 Correct 117 ms 27548 KB Output is correct
92 Correct 142 ms 36524 KB Output is correct
93 Correct 183 ms 41600 KB Output is correct
94 Correct 200 ms 42564 KB Output is correct
95 Correct 188 ms 41908 KB Output is correct
96 Correct 159 ms 43048 KB Output is correct
97 Correct 189 ms 42656 KB Output is correct