forked from kaidul/Data_Structure_and_Algorithms_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin_vertex_cover.cpp
More file actions
32 lines (27 loc) · 803 Bytes
/
min_vertex_cover.cpp
File metadata and controls
32 lines (27 loc) · 803 Bytes
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
#define MAX 100001
int dp[MAX << 2][2];
int parent[MAX];
vector <int> adj[MAX];
int minVertexCover(int node, bool hasGuard) {
if( (int)adj[node].size() == 0 ) return 0;
if( dp[node][hasGuard] != -1 ) return dp[node][hasGuard];
int sum = 0;
for(int i = 0; i < (int)adj[node].size(); i++) {
int v = adj[node][i];
if( v != parent[node] ) {
parent[v] = node;
if(!hasGuard) {
sum += minVertexCover(v, true);
} else {
sum += min( minVertexCover(v, false), minVertexCover(v, true) );
}
}
}
return dp[node][hasGuard] = sum + hasGuard;
}
/*
usage:
result = min( minVertexCover(1, false), minVertexCover(1, true) );
if(n > 1) printf("%d\n", result);
else printf("1\n");
*/