-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumHeightTreesOptimise.cpp
More file actions
47 lines (43 loc) · 1.29 KB
/
MinimumHeightTreesOptimise.cpp
File metadata and controls
47 lines (43 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
if (n == 1) return {0};
vector<unordered_set<int>> adjList(n);
for (vector<int> adj : edges) {
adjList[adj[0]].insert(adj[1]);
adjList[adj[1]].insert(adj[0]);
}
vector<int> visited(n, false);
vector<int> leaves;
for (int i = 0; i < n; i++) {
// To the parent
if (adjList[i].size() == 1) {
leaves.push_back(i);
visited[i] = true;
}
}
int remainingNode = n;
while (remainingNode > 2) {
remainingNode -= leaves.size();
vector<int> newLeaves;
// Remove the current leaves
for (int leafe : leaves) {
for (int nb : adjList[leafe]) {
adjList[nb].erase(leafe);
if (adjList[nb].size() == 1) {
newLeaves.push_back(nb);
}
}
}
leaves = newLeaves;
}
return leaves;
}
int main() {
int n;
cin >> n;
vector<vector<int>> edges(n, vector<int>(2));
for (int i = 0; i < n; i++) cin >> edges[i][0] >> edges[i][1];
vector<int> out = findMinHeightTrees(n, edges);
for (int x : out) cout << x << " ";
}