Skip to content

Commit 5a0cca0

Browse files
committed
fix min spanning forest
1 parent 646d7b6 commit 5a0cca0

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

combinatorial_opt/edge_disjoint_min_spanning_forests.hpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
// Reference: https://www.slideshare.net/tmaehara/ss-17402143
1111
// Verified:
1212
// - https://www.codechef.com/submit/HAMEL
13-
// - https://community.topcoder.com/stat?c=problem_statement&pm=14909&rd=17198
13+
// - https://archive.topcoder.com/ProblemStatement/pm/14909
14+
// - https://uoj.ac/problem/168
1415
template <class Label, class W = int>
1516
std::pair<std::vector<bool>, std::vector<bool>>
1617
edge_disjoint_min_spanning_forests(const std::vector<std::pair<Label, Label>> &edges,
@@ -61,11 +62,11 @@ edge_disjoint_min_spanning_forests(const std::vector<std::pair<Label, Label>> &e
6162
return prv;
6263
};
6364

64-
std::vector<int> prveid(M, -1), visited(N);
65-
std::vector<std::vector<int>> L(2);
65+
std::vector<int> prveid(M, -1);
66+
std::vector<std::vector<int>> L(2), visited(2, std::vector<int>(N));
6667
std::vector<std::vector<std::pair<int, int>>> prv(2);
6768
for (const int e : es) {
68-
if (nb_accepted_edges > 2 * (N - 1)) break;
69+
if (nb_accepted_edges >= 2 * (N - 1)) break;
6970
const int u = uvs[e].first, v = uvs[e].second;
7071

7172
bool found = false;
@@ -81,8 +82,9 @@ edge_disjoint_min_spanning_forests(const std::vector<std::pair<Label, Label>> &e
8182
}
8283
if (found) continue;
8384

84-
visited.assign(N, 0);
85-
visited[u] = 1;
85+
visited[0].assign(N, 0);
86+
visited[1].assign(N, 0);
87+
visited[0][u] = visited[1][u] = 1;
8688
L[0] = {e}, L[1] = {};
8789

8890
int ehead = -1;
@@ -98,11 +100,11 @@ edge_disjoint_min_spanning_forests(const std::vector<std::pair<Label, Label>> &e
98100
ehead = exy;
99101
break;
100102
}
101-
if (!visited[x]) std::swap(x, y);
102-
while (!visited[y]) {
103+
if (!visited[i][x]) std::swap(x, y);
104+
while (!visited[i][y]) {
103105
int nxty = prv[i][y].first, nxte = prv[i][y].second;
104106
L[i ^ 1].push_back(nxte);
105-
visited[y] = 1;
107+
visited[i][y] = 1;
106108
y = nxty;
107109
prveid[nxte] = exy;
108110
}

combinatorial_opt/edge_disjoint_min_spanning_forests.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ tie(I1, I2) = edge_disjoint_min_spanning_forests(edges, w); // 重み付き
3131
## 問題例
3232
3333
- [Hamel Paths \| CodeChef](https://www.codechef.com/problems/HAMEL) $n \le 64, m \le n(n - 1) / 2$, 最大 64 ケース.
34-
- [2018 TCO Round 3A 1000 ColoringEdgesDiv1](https://community.topcoder.com/stat?c=problem_statement&pm=14909&rd=17198) $n \le 1000, m = 3n/2$
34+
- [2018 TCO Round 3A 1000 ColoringEdgesDiv1](https://archive.topcoder.com/ProblemStatement/pm/14909) $n \le 1000, m = 3n/2$
35+
- [【UR #11】元旦老人与丛林 - 题目 - Universal Online Judge](https://uoj.ac/problem/168) $n \le 2000, m \le 4000$
3536
3637
## 参考文献・リンク
3738

0 commit comments

Comments
 (0)