-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f91796a
commit fec4131
Showing
5 changed files
with
98 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef CP_ALGO_DATA_STRUCTURES_DSU_HPP | ||
#define CP_ALGO_DATA_STRUCTURES_DSU_HPP | ||
#include <numeric> | ||
#include <vector> | ||
namespace cp_algo::data_structures { | ||
struct disjoint_set_union { | ||
disjoint_set_union (int n): par(n) { | ||
std::iota(begin(par), end(par), 0); | ||
} | ||
int get(int v) { | ||
return v == par[v] ? v : par[v] = get(par[v]); | ||
} | ||
bool uni(int a, int b) { | ||
a = get(a); | ||
b = get(b); | ||
par[a] = b; | ||
return a != b; | ||
} | ||
private: | ||
std::vector<int> par; | ||
}; | ||
using dsu = disjoint_set_union; | ||
} | ||
#endif // CP_ALGO_DATA_STRUCTURES_DSU_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef CP_ALGO_GRAPH_MST_HPP | ||
#define CP_ALGO_GRAPH_MST_HPP | ||
#include "base.hpp" | ||
#include "../data_structures/dsu.hpp" | ||
#include <algorithm> | ||
namespace cp_algo::graph { | ||
template<weighted_edge_type edge_t> | ||
auto mst(graph<undirected, edge_t> const& g) { | ||
std::vector<std::pair<int64_t, edge_index>> edges; | ||
g.call_edges([&](edge_index e) { | ||
edges.emplace_back(g.edge(e).w, e); | ||
}); | ||
std::ranges::sort(edges); | ||
data_structures::dsu me(g.n()); | ||
int64_t total = 0; | ||
std::vector<edge_index> mst; | ||
for(auto [w, e]: edges) { | ||
if(me.uni(g.edge(e ^ 1).to, g.edge(e).to)) { | ||
total += w; | ||
mst.push_back(e); | ||
} | ||
} | ||
return std::pair{total, mst}; | ||
} | ||
} | ||
#endif // CP_ALGO_GRAPH_MST_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// @brief Minimum Spanning Tree | ||
#define PROBLEM "https://judge.yosupo.jp/problem/minimum_spanning_tree" | ||
#pragma GCC optimize("Ofast,unroll-loops") | ||
#include "cp-algo/graph/mst.hpp" | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
using namespace cp_algo::graph; | ||
|
||
void solve() { | ||
int n, m; | ||
cin >> n >> m; | ||
graph<undirected, weighted_edge> g(n); | ||
g.read_edges(m); | ||
auto [X, E] = mst(g); | ||
cout << X << "\n"; | ||
for(int e: E) {cout << e / 2 << " ";} | ||
} | ||
|
||
signed main() { | ||
//freopen("input.txt", "r", stdin); | ||
ios::sync_with_stdio(0); | ||
cin.tie(0); | ||
int t = 1; | ||
//cin >> t; | ||
while(t--) { | ||
solve(); | ||
} | ||
} |