답안 #979577

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
979577 2024-05-11T07:55:52 Z bubble_xd 자매 도시 (APIO20_swap) C++17
13 / 100
351 ms 55368 KB
#include "swap.h"

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

const int nax = 500005;
int fa[nax], front[nax], back[nax], val[nax];
bool chain[nax];
int find(int x) {
  if (x == fa[x]) return x;
  fa[x] = find(fa[x]);
  return fa[x];
}
int N, M;
vector<pair<int, pair<int, int>>> edges; // (W, index)
vector<int> G[nax];
int id;
const int K = 23;
int sp[nax][K], dep[nax];

void dfs(int u, int f) {
  if (!u) return;
  dep[u] = dep[f] + 1;
  sp[u][0] = f;
  for (int i = 1; i < K; i++) sp[u][i] = sp[sp[u][i - 1]][i - 1];
  for (auto child : G[u]) dfs(child, u);
}

int lca(int u, int v) {
  if (dep[v] >= dep[u]) swap(u, v);
  for (int i = K - 1; i >= 0; i--) if (dep[sp[u][i]] >= dep[v]) u = sp[u][i];
  if (u == v) return u;
  for (int i = K - 1; i >= 0; i--) if (dep[sp[u][i]] != dep[sp[v][i]]) u = sp[u][i], v = sp[v][i];
  return sp[u][0];
}
void init(int _N, int _M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
  N = _N; M = _M;
  for (int i = 0; i < M; i++) 
    edges.push_back(make_pair(W[i], make_pair(U[i] + 1, V[i] + 1)));
  sort(edges.begin(), edges.end());
  for (int i = 0; i < nax; i++) fa[i] = i;
  for (int i = 1; i <= N; i++) {
    chain[i] = true;
    front[i] = back[i] = i;
  }
  id = N;
  for (int i = 0; i < M; i++) {
    int w = edges[i].first;
    int u = edges[i].second.first;
    int v = edges[i].second.second;

    int A = find(u), B = find(v);
    if (A == B) {
      if (chain[A]) {
        int new_id = ++id;
        fa[A] = new_id;
        val[new_id] = w;
        G[new_id].push_back(A);
      }
      continue;
    }
    int new_id = ++id;
    fa[A] = new_id;
    fa[B] = new_id;
    val[new_id] = w;
    G[new_id].push_back(A);
    G[new_id].push_back(B);
    if (chain[A] && chain[B]) {
      if (front[A] == v || back[A] == v) swap(A, B);
      // connect u -> v 
      // x -> u -> v -> y
      if (front[A] == u) swap(front[A], back[A]);
      if (back[B] == v) swap(front[B], back[B]);
      if (back[A] == u && front[B] == v) {
        chain[new_id] = true;
        front[new_id] = front[A];
        back[new_id] = back[B];
      }
    }
  }
  dfs(id, 0);
}

int getMinimumFuelCapacity(int X, int Y) {
  X++; Y++;
  int l = lca(X, Y);
  if (!chain[l]) return val[l];
  for (int i = K - 1; i >= 0; i--) {
    if (sp[l][i] && chain[sp[l][i]]) {
      l = sp[l][i];
    }
  }
  if (l == id) return -1;
  return val[sp[l][0]];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 24156 KB Output is correct
2 Correct 5 ms 24156 KB Output is correct
3 Correct 5 ms 24200 KB Output is correct
4 Correct 5 ms 24152 KB Output is correct
5 Correct 5 ms 24156 KB Output is correct
6 Correct 5 ms 24156 KB Output is correct
7 Correct 6 ms 24256 KB Output is correct
8 Correct 6 ms 24156 KB Output is correct
9 Correct 79 ms 43820 KB Output is correct
10 Correct 96 ms 49736 KB Output is correct
11 Correct 98 ms 49052 KB Output is correct
12 Correct 106 ms 49352 KB Output is correct
13 Correct 84 ms 51056 KB Output is correct
14 Correct 89 ms 43976 KB Output is correct
15 Correct 257 ms 51040 KB Output is correct
16 Correct 247 ms 50804 KB Output is correct
17 Correct 268 ms 51180 KB Output is correct
18 Correct 290 ms 52592 KB Output is correct
19 Correct 108 ms 31056 KB Output is correct
20 Correct 232 ms 52292 KB Output is correct
21 Correct 234 ms 52084 KB Output is correct
22 Correct 250 ms 52576 KB Output is correct
23 Correct 351 ms 54060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 24156 KB Output is correct
2 Correct 5 ms 24156 KB Output is correct
3 Correct 252 ms 55056 KB Output is correct
4 Correct 234 ms 55344 KB Output is correct
5 Correct 257 ms 55248 KB Output is correct
6 Correct 257 ms 55344 KB Output is correct
7 Correct 244 ms 55368 KB Output is correct
8 Correct 228 ms 54900 KB Output is correct
9 Correct 274 ms 55112 KB Output is correct
10 Correct 230 ms 52784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 24156 KB Output is correct
2 Correct 5 ms 24156 KB Output is correct
3 Correct 5 ms 24200 KB Output is correct
4 Correct 5 ms 24152 KB Output is correct
5 Correct 5 ms 24156 KB Output is correct
6 Correct 5 ms 24156 KB Output is correct
7 Correct 6 ms 24256 KB Output is correct
8 Correct 6 ms 24156 KB Output is correct
9 Correct 5 ms 24152 KB Output is correct
10 Incorrect 6 ms 24156 KB Output isn't correct
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 24152 KB Output is correct
2 Correct 6 ms 24156 KB Output is correct
3 Correct 5 ms 24156 KB Output is correct
4 Correct 5 ms 24200 KB Output is correct
5 Correct 5 ms 24152 KB Output is correct
6 Correct 5 ms 24156 KB Output is correct
7 Correct 5 ms 24156 KB Output is correct
8 Correct 6 ms 24256 KB Output is correct
9 Correct 6 ms 24156 KB Output is correct
10 Correct 79 ms 43820 KB Output is correct
11 Correct 96 ms 49736 KB Output is correct
12 Correct 98 ms 49052 KB Output is correct
13 Correct 106 ms 49352 KB Output is correct
14 Correct 84 ms 51056 KB Output is correct
15 Incorrect 6 ms 24156 KB Output isn't correct
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 24156 KB Output is correct
2 Correct 5 ms 24156 KB Output is correct
3 Correct 5 ms 24200 KB Output is correct
4 Correct 5 ms 24152 KB Output is correct
5 Correct 5 ms 24156 KB Output is correct
6 Correct 5 ms 24156 KB Output is correct
7 Correct 6 ms 24256 KB Output is correct
8 Correct 6 ms 24156 KB Output is correct
9 Correct 79 ms 43820 KB Output is correct
10 Correct 96 ms 49736 KB Output is correct
11 Correct 98 ms 49052 KB Output is correct
12 Correct 106 ms 49352 KB Output is correct
13 Correct 84 ms 51056 KB Output is correct
14 Correct 89 ms 43976 KB Output is correct
15 Correct 257 ms 51040 KB Output is correct
16 Correct 247 ms 50804 KB Output is correct
17 Correct 268 ms 51180 KB Output is correct
18 Correct 290 ms 52592 KB Output is correct
19 Correct 252 ms 55056 KB Output is correct
20 Correct 234 ms 55344 KB Output is correct
21 Correct 257 ms 55248 KB Output is correct
22 Correct 257 ms 55344 KB Output is correct
23 Correct 244 ms 55368 KB Output is correct
24 Correct 228 ms 54900 KB Output is correct
25 Correct 274 ms 55112 KB Output is correct
26 Correct 230 ms 52784 KB Output is correct
27 Incorrect 6 ms 24156 KB Output isn't correct
28 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 24152 KB Output is correct
2 Correct 6 ms 24156 KB Output is correct
3 Correct 5 ms 24156 KB Output is correct
4 Correct 5 ms 24200 KB Output is correct
5 Correct 5 ms 24152 KB Output is correct
6 Correct 5 ms 24156 KB Output is correct
7 Correct 5 ms 24156 KB Output is correct
8 Correct 6 ms 24256 KB Output is correct
9 Correct 6 ms 24156 KB Output is correct
10 Correct 79 ms 43820 KB Output is correct
11 Correct 96 ms 49736 KB Output is correct
12 Correct 98 ms 49052 KB Output is correct
13 Correct 106 ms 49352 KB Output is correct
14 Correct 84 ms 51056 KB Output is correct
15 Correct 89 ms 43976 KB Output is correct
16 Correct 257 ms 51040 KB Output is correct
17 Correct 247 ms 50804 KB Output is correct
18 Correct 268 ms 51180 KB Output is correct
19 Correct 290 ms 52592 KB Output is correct
20 Correct 252 ms 55056 KB Output is correct
21 Correct 234 ms 55344 KB Output is correct
22 Correct 257 ms 55248 KB Output is correct
23 Correct 257 ms 55344 KB Output is correct
24 Correct 244 ms 55368 KB Output is correct
25 Correct 228 ms 54900 KB Output is correct
26 Correct 274 ms 55112 KB Output is correct
27 Correct 230 ms 52784 KB Output is correct
28 Incorrect 6 ms 24156 KB Output isn't correct
29 Halted 0 ms 0 KB -