summaryrefslogtreecommitdiff
path: root/graph/havelHakimi.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'graph/havelHakimi.cpp')
-rw-r--r--graph/havelHakimi.cpp19
1 files changed, 19 insertions, 0 deletions
diff --git a/graph/havelHakimi.cpp b/graph/havelHakimi.cpp
new file mode 100644
index 0000000..9fb9846
--- /dev/null
+++ b/graph/havelHakimi.cpp
@@ -0,0 +1,19 @@
+vector<vector<int>> havelHakimi(const vector<int>& deg) {
+ priority_queue<pair<int, int>> pq;
+ for (int i = 0; i < sz(deg); i++) pq.push({deg[i], i});
+ vector<vector<int>> adj;
+ while (!pq.empty()) {
+ auto v = pq.top(); pq.pop();
+ if (sz(pq) < v.first) return {}; //ERROR
+ vector<pair<int, int>> todo;
+ for (int i = 0; i < v.first; i++) {
+ auto u = pq.top(); pq.pop();
+ adj[v.second].push_back(u.second);
+ adj[u.second].push_back(v.second);
+ u.first--;
+ if (u.first > 0) todo.push_back(u);
+ }
+ for (auto e : todo) pq.push(e);
+ }
+ return adj;
+}