-
Notifications
You must be signed in to change notification settings - Fork 1
/
randomized_mvc.cpp
63 lines (57 loc) · 1.02 KB
/
randomized_mvc.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <cstdlib>
#include <ctime>
using namespace std;
void remove_edges(vector<vector<int>> &graph, int n)
{
auto it = graph.begin();
while (it != graph.end())
{
if ((*it)[0] == n || (*it)[1] == n)
{
it = graph.erase(it);
}
else
{
it++;
}
}
}
vector<int> mvc(vector<vector<int>> &graph)
{
vector<int> ans;
srand(time(0));
while (!graph.empty())
{
int i = rand() % 5;
int u = graph[i][0];
int v = graph[i][1];
ans.push_back(u);
ans.push_back(v);
remove_edges(graph, u);
remove_edges(graph, v);
}
return ans;
}
int main()
{
set<int> ans;
vector<vector<int>> graph{
{0, 1},
{0, 2},
{1, 3},
{3, 4},
};
int n = graph.size();
vector<int> s = mvc(graph);
for (auto x : s)
{
cout << x << " ";
}
return 0;
}
// output
// 0 1 3 4