답안 #976047

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
976047 2024-05-06T06:20:22 Z josanneo22 자매 도시 (APIO20_swap) C++17
100 / 100
304 ms 37540 KB
#include "swap.h"

#include <vector>
#include <bits/stdc++.h>
using namespace std;

const int nax = 300500, K = 22;
int N, M;
vector<pair<int, pair<int, int>>> edges;  

int fa[nax];
int child[nax][2];
int val[nax];
int dep[nax], sp[nax][K];
int st[nax], ed[nax];
bool chain[nax];

int find(int x) {
   if (x == fa[x]) return x;
   fa[x] = find(fa[x]);
   return fa[x];  
}

void dfs(int u, int f) {
   if (!u) return;
   // cout << "DFS : " << u << ' ' << f << '\n';
   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 (int t = 0; t < 2; t++) {
      int v = child[u][t];
      dfs(v, u);
   }
}

int lca(int X, int Y) {
   if (dep[Y] >= dep[X]) swap(X, Y);
   for (int i = K - 1; i >= 0; i--) if (dep[sp[X][i]] >= dep[Y]) X = sp[X][i];
   if (X == Y) return X;
   for (int i = K - 1; i >= 0; i--) if (sp[X][i] != sp[Y][i]) X = sp[X][i], Y = sp[Y][i];
   return sp[X][0];
}
int id;
void init(int _N, int _M, std::vector<int> _U, std::vector<int> _V, std::vector<int> _W) {
   N = _N; M = _M;
   id = N;
   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;
      st[i] = ed[i] = i;
   }
   for (int i = 0; i < edges.size(); 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);
      // cout << A << ' ' << B << '\n';
      if (A == B) {
         if (chain[A]) {
            int g = ++id;
            fa[A] = g;
            val[g] = w;
            child[g][0] = A;
         }
         continue;
      }
      int g = ++id;
      fa[A] = g; fa[B] = g;
      val[g] = w;
      child[g][0] = A; child[g][1] = B;
      if (chain[A] && chain[B]) {
         // ...u, v...
         if (st[A] == v || ed[A] == v) swap(A, B);
         if (st[A] == u) swap(st[A], ed[A]);
         if (ed[B] == v) swap(st[B], ed[B]);
         if (ed[A] == u && st[B] == v) {
            chain[g] = true;
            st[g] = st[A];
            ed[g] = ed[B];
         }
      }
   }
   dfs(id, 0);
   // for (int i = 1; i <= 10; i++) cout << val[i] << ' ';
   //    cout << '\n';
}

int getMinimumFuelCapacity(int X, int Y) {
   X++; Y++;
   // cout << X << ' ' << Y << '\n';
   int l = lca(X, Y);
   // cout << "lca : " << l << '\n';
   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]];
}

// #include "grader.cpp"

/*
要么是deg >= 3, 要么是cycle
考虑什么时候不能:链

我们可以直接维持kruskal重构树,
   让chain[i] = true/false
   u, v -> new node
   然后new node点权 = w
   那么答案就是
   在lca上倍增直到chain[i] = true为此
   然后 ans = cal[fa[lca]]
   判断chain[i] 可以用看u -> v是不是两个corner
*/

Compilation message

swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:56:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |    for (int i = 0; i < edges.size(); i++) {
      |                    ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7768 KB Output is correct
2 Correct 2 ms 7772 KB Output is correct
3 Correct 1 ms 7772 KB Output is correct
4 Correct 2 ms 7772 KB Output is correct
5 Correct 2 ms 7772 KB Output is correct
6 Correct 2 ms 7772 KB Output is correct
7 Correct 2 ms 7768 KB Output is correct
8 Correct 2 ms 7772 KB Output is correct
9 Correct 65 ms 25288 KB Output is correct
10 Correct 75 ms 29836 KB Output is correct
11 Correct 69 ms 29848 KB Output is correct
12 Correct 76 ms 30228 KB Output is correct
13 Correct 72 ms 31428 KB Output is correct
14 Correct 66 ms 25284 KB Output is correct
15 Correct 197 ms 32056 KB Output is correct
16 Correct 186 ms 31560 KB Output is correct
17 Correct 189 ms 32060 KB Output is correct
18 Correct 304 ms 33316 KB Output is correct
19 Correct 92 ms 15740 KB Output is correct
20 Correct 194 ms 33096 KB Output is correct
21 Correct 193 ms 32820 KB Output is correct
22 Correct 204 ms 33584 KB Output is correct
23 Correct 264 ms 34608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7768 KB Output is correct
2 Correct 2 ms 7772 KB Output is correct
3 Correct 199 ms 37000 KB Output is correct
4 Correct 222 ms 37440 KB Output is correct
5 Correct 217 ms 37468 KB Output is correct
6 Correct 210 ms 37168 KB Output is correct
7 Correct 214 ms 37272 KB Output is correct
8 Correct 214 ms 36892 KB Output is correct
9 Correct 220 ms 37188 KB Output is correct
10 Correct 209 ms 34892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7768 KB Output is correct
2 Correct 2 ms 7772 KB Output is correct
3 Correct 1 ms 7772 KB Output is correct
4 Correct 2 ms 7772 KB Output is correct
5 Correct 2 ms 7772 KB Output is correct
6 Correct 2 ms 7772 KB Output is correct
7 Correct 2 ms 7768 KB Output is correct
8 Correct 2 ms 7772 KB Output is correct
9 Correct 2 ms 7768 KB Output is correct
10 Correct 2 ms 7772 KB Output is correct
11 Correct 2 ms 7772 KB Output is correct
12 Correct 2 ms 7772 KB Output is correct
13 Correct 2 ms 7772 KB Output is correct
14 Correct 2 ms 7772 KB Output is correct
15 Correct 2 ms 7772 KB Output is correct
16 Correct 2 ms 7772 KB Output is correct
17 Correct 2 ms 7772 KB Output is correct
18 Correct 2 ms 7772 KB Output is correct
19 Correct 3 ms 7772 KB Output is correct
20 Correct 2 ms 7772 KB Output is correct
21 Correct 2 ms 7772 KB Output is correct
22 Correct 2 ms 7628 KB Output is correct
23 Correct 2 ms 7772 KB Output is correct
24 Correct 3 ms 7636 KB Output is correct
25 Correct 2 ms 7772 KB Output is correct
26 Correct 2 ms 7772 KB Output is correct
27 Correct 2 ms 7844 KB Output is correct
28 Correct 3 ms 7884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7768 KB Output is correct
2 Correct 3 ms 7768 KB Output is correct
3 Correct 2 ms 7772 KB Output is correct
4 Correct 1 ms 7772 KB Output is correct
5 Correct 2 ms 7772 KB Output is correct
6 Correct 2 ms 7772 KB Output is correct
7 Correct 2 ms 7772 KB Output is correct
8 Correct 2 ms 7768 KB Output is correct
9 Correct 2 ms 7772 KB Output is correct
10 Correct 65 ms 25288 KB Output is correct
11 Correct 75 ms 29836 KB Output is correct
12 Correct 69 ms 29848 KB Output is correct
13 Correct 76 ms 30228 KB Output is correct
14 Correct 72 ms 31428 KB Output is correct
15 Correct 2 ms 7772 KB Output is correct
16 Correct 2 ms 7772 KB Output is correct
17 Correct 2 ms 7772 KB Output is correct
18 Correct 2 ms 7772 KB Output is correct
19 Correct 2 ms 7772 KB Output is correct
20 Correct 2 ms 7772 KB Output is correct
21 Correct 2 ms 7772 KB Output is correct
22 Correct 2 ms 7772 KB Output is correct
23 Correct 2 ms 7772 KB Output is correct
24 Correct 3 ms 7772 KB Output is correct
25 Correct 2 ms 7772 KB Output is correct
26 Correct 2 ms 7772 KB Output is correct
27 Correct 2 ms 7628 KB Output is correct
28 Correct 2 ms 7772 KB Output is correct
29 Correct 3 ms 7636 KB Output is correct
30 Correct 2 ms 7772 KB Output is correct
31 Correct 2 ms 7772 KB Output is correct
32 Correct 2 ms 7844 KB Output is correct
33 Correct 3 ms 7884 KB Output is correct
34 Correct 11 ms 12060 KB Output is correct
35 Correct 76 ms 30448 KB Output is correct
36 Correct 74 ms 30404 KB Output is correct
37 Correct 74 ms 30460 KB Output is correct
38 Correct 74 ms 30416 KB Output is correct
39 Correct 72 ms 30160 KB Output is correct
40 Correct 64 ms 27500 KB Output is correct
41 Correct 85 ms 30284 KB Output is correct
42 Correct 72 ms 30408 KB Output is correct
43 Correct 69 ms 31756 KB Output is correct
44 Correct 74 ms 30408 KB Output is correct
45 Correct 86 ms 28028 KB Output is correct
46 Correct 72 ms 30408 KB Output is correct
47 Correct 73 ms 30408 KB Output is correct
48 Correct 83 ms 31436 KB Output is correct
49 Correct 51 ms 13272 KB Output is correct
50 Correct 42 ms 14288 KB Output is correct
51 Correct 66 ms 25288 KB Output is correct
52 Correct 96 ms 33176 KB Output is correct
53 Correct 113 ms 35420 KB Output is correct
54 Correct 115 ms 33988 KB Output is correct
55 Correct 83 ms 31872 KB Output is correct
56 Correct 101 ms 33544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7768 KB Output is correct
2 Correct 2 ms 7772 KB Output is correct
3 Correct 1 ms 7772 KB Output is correct
4 Correct 2 ms 7772 KB Output is correct
5 Correct 2 ms 7772 KB Output is correct
6 Correct 2 ms 7772 KB Output is correct
7 Correct 2 ms 7768 KB Output is correct
8 Correct 2 ms 7772 KB Output is correct
9 Correct 65 ms 25288 KB Output is correct
10 Correct 75 ms 29836 KB Output is correct
11 Correct 69 ms 29848 KB Output is correct
12 Correct 76 ms 30228 KB Output is correct
13 Correct 72 ms 31428 KB Output is correct
14 Correct 66 ms 25284 KB Output is correct
15 Correct 197 ms 32056 KB Output is correct
16 Correct 186 ms 31560 KB Output is correct
17 Correct 189 ms 32060 KB Output is correct
18 Correct 304 ms 33316 KB Output is correct
19 Correct 199 ms 37000 KB Output is correct
20 Correct 222 ms 37440 KB Output is correct
21 Correct 217 ms 37468 KB Output is correct
22 Correct 210 ms 37168 KB Output is correct
23 Correct 214 ms 37272 KB Output is correct
24 Correct 214 ms 36892 KB Output is correct
25 Correct 220 ms 37188 KB Output is correct
26 Correct 209 ms 34892 KB Output is correct
27 Correct 2 ms 7772 KB Output is correct
28 Correct 2 ms 7772 KB Output is correct
29 Correct 2 ms 7772 KB Output is correct
30 Correct 2 ms 7772 KB Output is correct
31 Correct 2 ms 7772 KB Output is correct
32 Correct 2 ms 7772 KB Output is correct
33 Correct 2 ms 7772 KB Output is correct
34 Correct 2 ms 7772 KB Output is correct
35 Correct 2 ms 7772 KB Output is correct
36 Correct 11 ms 12060 KB Output is correct
37 Correct 76 ms 30448 KB Output is correct
38 Correct 74 ms 30404 KB Output is correct
39 Correct 74 ms 30460 KB Output is correct
40 Correct 74 ms 30416 KB Output is correct
41 Correct 72 ms 30160 KB Output is correct
42 Correct 64 ms 27500 KB Output is correct
43 Correct 85 ms 30284 KB Output is correct
44 Correct 72 ms 30408 KB Output is correct
45 Correct 69 ms 31756 KB Output is correct
46 Correct 74 ms 30408 KB Output is correct
47 Correct 19 ms 12316 KB Output is correct
48 Correct 187 ms 32812 KB Output is correct
49 Correct 203 ms 33072 KB Output is correct
50 Correct 181 ms 32828 KB Output is correct
51 Correct 176 ms 32820 KB Output is correct
52 Correct 167 ms 32304 KB Output is correct
53 Correct 144 ms 26044 KB Output is correct
54 Correct 190 ms 33576 KB Output is correct
55 Correct 215 ms 32812 KB Output is correct
56 Correct 254 ms 34184 KB Output is correct
57 Correct 196 ms 33432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7768 KB Output is correct
2 Correct 3 ms 7768 KB Output is correct
3 Correct 2 ms 7772 KB Output is correct
4 Correct 1 ms 7772 KB Output is correct
5 Correct 2 ms 7772 KB Output is correct
6 Correct 2 ms 7772 KB Output is correct
7 Correct 2 ms 7772 KB Output is correct
8 Correct 2 ms 7768 KB Output is correct
9 Correct 2 ms 7772 KB Output is correct
10 Correct 65 ms 25288 KB Output is correct
11 Correct 75 ms 29836 KB Output is correct
12 Correct 69 ms 29848 KB Output is correct
13 Correct 76 ms 30228 KB Output is correct
14 Correct 72 ms 31428 KB Output is correct
15 Correct 66 ms 25284 KB Output is correct
16 Correct 197 ms 32056 KB Output is correct
17 Correct 186 ms 31560 KB Output is correct
18 Correct 189 ms 32060 KB Output is correct
19 Correct 304 ms 33316 KB Output is correct
20 Correct 199 ms 37000 KB Output is correct
21 Correct 222 ms 37440 KB Output is correct
22 Correct 217 ms 37468 KB Output is correct
23 Correct 210 ms 37168 KB Output is correct
24 Correct 214 ms 37272 KB Output is correct
25 Correct 214 ms 36892 KB Output is correct
26 Correct 220 ms 37188 KB Output is correct
27 Correct 209 ms 34892 KB Output is correct
28 Correct 2 ms 7772 KB Output is correct
29 Correct 2 ms 7772 KB Output is correct
30 Correct 2 ms 7772 KB Output is correct
31 Correct 2 ms 7772 KB Output is correct
32 Correct 2 ms 7772 KB Output is correct
33 Correct 2 ms 7772 KB Output is correct
34 Correct 2 ms 7772 KB Output is correct
35 Correct 2 ms 7772 KB Output is correct
36 Correct 2 ms 7772 KB Output is correct
37 Correct 11 ms 12060 KB Output is correct
38 Correct 76 ms 30448 KB Output is correct
39 Correct 74 ms 30404 KB Output is correct
40 Correct 74 ms 30460 KB Output is correct
41 Correct 74 ms 30416 KB Output is correct
42 Correct 72 ms 30160 KB Output is correct
43 Correct 64 ms 27500 KB Output is correct
44 Correct 85 ms 30284 KB Output is correct
45 Correct 72 ms 30408 KB Output is correct
46 Correct 69 ms 31756 KB Output is correct
47 Correct 74 ms 30408 KB Output is correct
48 Correct 19 ms 12316 KB Output is correct
49 Correct 187 ms 32812 KB Output is correct
50 Correct 203 ms 33072 KB Output is correct
51 Correct 181 ms 32828 KB Output is correct
52 Correct 176 ms 32820 KB Output is correct
53 Correct 167 ms 32304 KB Output is correct
54 Correct 144 ms 26044 KB Output is correct
55 Correct 190 ms 33576 KB Output is correct
56 Correct 215 ms 32812 KB Output is correct
57 Correct 254 ms 34184 KB Output is correct
58 Correct 196 ms 33432 KB Output is correct
59 Correct 92 ms 15740 KB Output is correct
60 Correct 194 ms 33096 KB Output is correct
61 Correct 193 ms 32820 KB Output is correct
62 Correct 204 ms 33584 KB Output is correct
63 Correct 264 ms 34608 KB Output is correct
64 Correct 3 ms 7772 KB Output is correct
65 Correct 2 ms 7772 KB Output is correct
66 Correct 2 ms 7772 KB Output is correct
67 Correct 2 ms 7628 KB Output is correct
68 Correct 2 ms 7772 KB Output is correct
69 Correct 3 ms 7636 KB Output is correct
70 Correct 2 ms 7772 KB Output is correct
71 Correct 2 ms 7772 KB Output is correct
72 Correct 2 ms 7844 KB Output is correct
73 Correct 3 ms 7884 KB Output is correct
74 Correct 86 ms 28028 KB Output is correct
75 Correct 72 ms 30408 KB Output is correct
76 Correct 73 ms 30408 KB Output is correct
77 Correct 83 ms 31436 KB Output is correct
78 Correct 51 ms 13272 KB Output is correct
79 Correct 42 ms 14288 KB Output is correct
80 Correct 66 ms 25288 KB Output is correct
81 Correct 96 ms 33176 KB Output is correct
82 Correct 113 ms 35420 KB Output is correct
83 Correct 115 ms 33988 KB Output is correct
84 Correct 83 ms 31872 KB Output is correct
85 Correct 101 ms 33544 KB Output is correct
86 Correct 54 ms 16340 KB Output is correct
87 Correct 185 ms 32832 KB Output is correct
88 Correct 207 ms 33152 KB Output is correct
89 Correct 191 ms 31260 KB Output is correct
90 Correct 140 ms 15616 KB Output is correct
91 Correct 151 ms 20552 KB Output is correct
92 Correct 175 ms 27800 KB Output is correct
93 Correct 204 ms 35992 KB Output is correct
94 Correct 273 ms 37540 KB Output is correct
95 Correct 230 ms 35952 KB Output is correct
96 Correct 242 ms 34340 KB Output is correct
97 Correct 231 ms 34944 KB Output is correct