summaryrefslogtreecommitdiff
path: root/graph/havelHakimi.cpp
blob: 9fb9846a9612c3548df4c08bc153d60634f8d461 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;
}