답안 #977559

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
977559 2024-05-08T06:56:11 Z josanneo22 자매 도시 (APIO20_swap) C++17
100 / 100
312 ms 43424 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]];
}
 
/*
要么是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 2 ms 7768 KB Output is correct
2 Correct 2 ms 7928 KB Output is correct
3 Correct 2 ms 7772 KB Output is correct
4 Correct 2 ms 7772 KB Output is correct
5 Correct 3 ms 7772 KB Output is correct
6 Correct 3 ms 8024 KB Output is correct
7 Correct 2 ms 7772 KB Output is correct
8 Correct 2 ms 7772 KB Output is correct
9 Correct 68 ms 26760 KB Output is correct
10 Correct 75 ms 31952 KB Output is correct
11 Correct 73 ms 31804 KB Output is correct
12 Correct 82 ms 32348 KB Output is correct
13 Correct 74 ms 33732 KB Output is correct
14 Correct 68 ms 27168 KB Output is correct
15 Correct 199 ms 36192 KB Output is correct
16 Correct 212 ms 35760 KB Output is correct
17 Correct 204 ms 36748 KB Output is correct
18 Correct 261 ms 37676 KB Output is correct
19 Correct 92 ms 17708 KB Output is correct
20 Correct 196 ms 37112 KB Output is correct
21 Correct 193 ms 36996 KB Output is correct
22 Correct 218 ms 37936 KB Output is correct
23 Correct 312 ms 38952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7768 KB Output is correct
2 Correct 2 ms 7928 KB Output is correct
3 Correct 306 ms 40556 KB Output is correct
4 Correct 242 ms 41132 KB Output is correct
5 Correct 304 ms 41180 KB Output is correct
6 Correct 242 ms 40720 KB Output is correct
7 Correct 296 ms 41172 KB Output is correct
8 Correct 219 ms 40608 KB Output is correct
9 Correct 263 ms 41132 KB Output is correct
10 Correct 213 ms 38780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7768 KB Output is correct
2 Correct 2 ms 7928 KB Output is correct
3 Correct 2 ms 7772 KB Output is correct
4 Correct 2 ms 7772 KB Output is correct
5 Correct 3 ms 7772 KB Output is correct
6 Correct 3 ms 8024 KB Output is correct
7 Correct 2 ms 7772 KB Output is correct
8 Correct 2 ms 7772 KB Output is correct
9 Correct 2 ms 7772 KB Output is correct
10 Correct 2 ms 8024 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 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 2 ms 7876 KB Output is correct
25 Correct 3 ms 7872 KB Output is correct
26 Correct 2 ms 7772 KB Output is correct
27 Correct 2 ms 7772 KB Output is correct
28 Correct 3 ms 7768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7772 KB Output is correct
2 Correct 2 ms 7768 KB Output is correct
3 Correct 2 ms 7928 KB Output is correct
4 Correct 2 ms 7772 KB Output is correct
5 Correct 2 ms 7772 KB Output is correct
6 Correct 3 ms 7772 KB Output is correct
7 Correct 3 ms 8024 KB Output is correct
8 Correct 2 ms 7772 KB Output is correct
9 Correct 2 ms 7772 KB Output is correct
10 Correct 68 ms 26760 KB Output is correct
11 Correct 75 ms 31952 KB Output is correct
12 Correct 73 ms 31804 KB Output is correct
13 Correct 82 ms 32348 KB Output is correct
14 Correct 74 ms 33732 KB Output is correct
15 Correct 2 ms 8024 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 2 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 7772 KB Output is correct
28 Correct 2 ms 7772 KB Output is correct
29 Correct 2 ms 7876 KB Output is correct
30 Correct 3 ms 7872 KB Output is correct
31 Correct 2 ms 7772 KB Output is correct
32 Correct 2 ms 7772 KB Output is correct
33 Correct 3 ms 7768 KB Output is correct
34 Correct 10 ms 12316 KB Output is correct
35 Correct 76 ms 32040 KB Output is correct
36 Correct 76 ms 32208 KB Output is correct
37 Correct 75 ms 32196 KB Output is correct
38 Correct 73 ms 31976 KB Output is correct
39 Correct 80 ms 31940 KB Output is correct
40 Correct 74 ms 29264 KB Output is correct
41 Correct 77 ms 32500 KB Output is correct
42 Correct 75 ms 32196 KB Output is correct
43 Correct 70 ms 33736 KB Output is correct
44 Correct 78 ms 32464 KB Output is correct
45 Correct 84 ms 32076 KB Output is correct
46 Correct 78 ms 32200 KB Output is correct
47 Correct 82 ms 32312 KB Output is correct
48 Correct 90 ms 33516 KB Output is correct
49 Correct 49 ms 16064 KB Output is correct
50 Correct 41 ms 16580 KB Output is correct
51 Correct 68 ms 27848 KB Output is correct
52 Correct 98 ms 36036 KB Output is correct
53 Correct 124 ms 38992 KB Output is correct
54 Correct 110 ms 37820 KB Output is correct
55 Correct 72 ms 33600 KB Output is correct
56 Correct 103 ms 37264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7768 KB Output is correct
2 Correct 2 ms 7928 KB Output is correct
3 Correct 2 ms 7772 KB Output is correct
4 Correct 2 ms 7772 KB Output is correct
5 Correct 3 ms 7772 KB Output is correct
6 Correct 3 ms 8024 KB Output is correct
7 Correct 2 ms 7772 KB Output is correct
8 Correct 2 ms 7772 KB Output is correct
9 Correct 68 ms 26760 KB Output is correct
10 Correct 75 ms 31952 KB Output is correct
11 Correct 73 ms 31804 KB Output is correct
12 Correct 82 ms 32348 KB Output is correct
13 Correct 74 ms 33732 KB Output is correct
14 Correct 68 ms 27168 KB Output is correct
15 Correct 199 ms 36192 KB Output is correct
16 Correct 212 ms 35760 KB Output is correct
17 Correct 204 ms 36748 KB Output is correct
18 Correct 261 ms 37676 KB Output is correct
19 Correct 306 ms 40556 KB Output is correct
20 Correct 242 ms 41132 KB Output is correct
21 Correct 304 ms 41180 KB Output is correct
22 Correct 242 ms 40720 KB Output is correct
23 Correct 296 ms 41172 KB Output is correct
24 Correct 219 ms 40608 KB Output is correct
25 Correct 263 ms 41132 KB Output is correct
26 Correct 213 ms 38780 KB Output is correct
27 Correct 2 ms 8024 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 10 ms 12316 KB Output is correct
37 Correct 76 ms 32040 KB Output is correct
38 Correct 76 ms 32208 KB Output is correct
39 Correct 75 ms 32196 KB Output is correct
40 Correct 73 ms 31976 KB Output is correct
41 Correct 80 ms 31940 KB Output is correct
42 Correct 74 ms 29264 KB Output is correct
43 Correct 77 ms 32500 KB Output is correct
44 Correct 75 ms 32196 KB Output is correct
45 Correct 70 ms 33736 KB Output is correct
46 Correct 78 ms 32464 KB Output is correct
47 Correct 19 ms 12824 KB Output is correct
48 Correct 197 ms 36892 KB Output is correct
49 Correct 188 ms 36912 KB Output is correct
50 Correct 192 ms 37008 KB Output is correct
51 Correct 177 ms 36920 KB Output is correct
52 Correct 182 ms 36216 KB Output is correct
53 Correct 160 ms 29916 KB Output is correct
54 Correct 196 ms 37936 KB Output is correct
55 Correct 206 ms 36928 KB Output is correct
56 Correct 256 ms 38228 KB Output is correct
57 Correct 219 ms 38000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7772 KB Output is correct
2 Correct 2 ms 7768 KB Output is correct
3 Correct 2 ms 7928 KB Output is correct
4 Correct 2 ms 7772 KB Output is correct
5 Correct 2 ms 7772 KB Output is correct
6 Correct 3 ms 7772 KB Output is correct
7 Correct 3 ms 8024 KB Output is correct
8 Correct 2 ms 7772 KB Output is correct
9 Correct 2 ms 7772 KB Output is correct
10 Correct 68 ms 26760 KB Output is correct
11 Correct 75 ms 31952 KB Output is correct
12 Correct 73 ms 31804 KB Output is correct
13 Correct 82 ms 32348 KB Output is correct
14 Correct 74 ms 33732 KB Output is correct
15 Correct 68 ms 27168 KB Output is correct
16 Correct 199 ms 36192 KB Output is correct
17 Correct 212 ms 35760 KB Output is correct
18 Correct 204 ms 36748 KB Output is correct
19 Correct 261 ms 37676 KB Output is correct
20 Correct 306 ms 40556 KB Output is correct
21 Correct 242 ms 41132 KB Output is correct
22 Correct 304 ms 41180 KB Output is correct
23 Correct 242 ms 40720 KB Output is correct
24 Correct 296 ms 41172 KB Output is correct
25 Correct 219 ms 40608 KB Output is correct
26 Correct 263 ms 41132 KB Output is correct
27 Correct 213 ms 38780 KB Output is correct
28 Correct 2 ms 8024 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 10 ms 12316 KB Output is correct
38 Correct 76 ms 32040 KB Output is correct
39 Correct 76 ms 32208 KB Output is correct
40 Correct 75 ms 32196 KB Output is correct
41 Correct 73 ms 31976 KB Output is correct
42 Correct 80 ms 31940 KB Output is correct
43 Correct 74 ms 29264 KB Output is correct
44 Correct 77 ms 32500 KB Output is correct
45 Correct 75 ms 32196 KB Output is correct
46 Correct 70 ms 33736 KB Output is correct
47 Correct 78 ms 32464 KB Output is correct
48 Correct 19 ms 12824 KB Output is correct
49 Correct 197 ms 36892 KB Output is correct
50 Correct 188 ms 36912 KB Output is correct
51 Correct 192 ms 37008 KB Output is correct
52 Correct 177 ms 36920 KB Output is correct
53 Correct 182 ms 36216 KB Output is correct
54 Correct 160 ms 29916 KB Output is correct
55 Correct 196 ms 37936 KB Output is correct
56 Correct 206 ms 36928 KB Output is correct
57 Correct 256 ms 38228 KB Output is correct
58 Correct 219 ms 38000 KB Output is correct
59 Correct 92 ms 17708 KB Output is correct
60 Correct 196 ms 37112 KB Output is correct
61 Correct 193 ms 36996 KB Output is correct
62 Correct 218 ms 37936 KB Output is correct
63 Correct 312 ms 38952 KB Output is correct
64 Correct 2 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 7772 KB Output is correct
68 Correct 2 ms 7772 KB Output is correct
69 Correct 2 ms 7876 KB Output is correct
70 Correct 3 ms 7872 KB Output is correct
71 Correct 2 ms 7772 KB Output is correct
72 Correct 2 ms 7772 KB Output is correct
73 Correct 3 ms 7768 KB Output is correct
74 Correct 84 ms 32076 KB Output is correct
75 Correct 78 ms 32200 KB Output is correct
76 Correct 82 ms 32312 KB Output is correct
77 Correct 90 ms 33516 KB Output is correct
78 Correct 49 ms 16064 KB Output is correct
79 Correct 41 ms 16580 KB Output is correct
80 Correct 68 ms 27848 KB Output is correct
81 Correct 98 ms 36036 KB Output is correct
82 Correct 124 ms 38992 KB Output is correct
83 Correct 110 ms 37820 KB Output is correct
84 Correct 72 ms 33600 KB Output is correct
85 Correct 103 ms 37264 KB Output is correct
86 Correct 55 ms 17864 KB Output is correct
87 Correct 179 ms 36912 KB Output is correct
88 Correct 184 ms 36988 KB Output is correct
89 Correct 199 ms 35368 KB Output is correct
90 Correct 140 ms 21240 KB Output is correct
91 Correct 149 ms 23776 KB Output is correct
92 Correct 197 ms 32416 KB Output is correct
93 Correct 209 ms 40048 KB Output is correct
94 Correct 243 ms 43424 KB Output is correct
95 Correct 232 ms 41280 KB Output is correct
96 Correct 244 ms 38464 KB Output is correct
97 Correct 239 ms 41524 KB Output is correct