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
1415template <class Label , class W = int >
1516std::pair<std::vector<bool >, std::vector<bool >>
1617edge_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 }
0 commit comments