/*
Amrutansu Garanaik
Will work on tree only
tree is represented using stucture
*/
#include <stdio.h>
#include <list>
#include <vector>
#include <string.h>
#include <stdlib.h>
#define MAX 10000
using namespace std;
struct tree
{
int min_vertex;
vector <struct tree *> child;
};
int vertex_cover(struct tree *root)
{
if(root == NULL)
return 0;
if((root->child).size() == 0) //no children
return 0;
if(root->min_vertex != -1)
return root->min_vertex;
int with_root = 1;
for(int i = 0; i < (root->child).size(); i++)
with_root += vertex_cover(root->child[i]);
int without_root = (root->child).size();
for(int i = 0; i < (root->child).size(); i++)
{
for(int j = 0; j < (root->child[i]->child).size(); j++)
without_root += vertex_cover((root->child[i]->child)[j]);
}
root->min_vertex = with_root < without_root ? with_root : without_root;
return root->min_vertex;
}
struct tree * newNode()
{
struct tree *temp = (struct tree *) malloc(sizeof(struct tree));
temp->min_vertex = -1;
temp->child.clear();
return temp;
}
int main()
{
struct tree *root = NULL;
/*root = newNode();
(root->child).push_back(newNode());
(root->child).push_back(newNode());
(root->child[0]->child).push_back(newNode());
(root->child[0]->child).push_back(newNode());
(root->child[1]->child).push_back(newNode());
(root->child[0]->child[1]->child).push_back(newNode());
(root->child[0]->child[1]->child).push_back(newNode());
*/
root = newNode();
(root->child).push_back(newNode());
(root->child).push_back(newNode());
(root->child).push_back(newNode());
(root->child[1]->child).push_back(newNode());
(root->child[1]->child).push_back(newNode());
//(root->child[1]->child).push_back(newNode());
(root->child[1]->child[0]->child).push_back(newNode());
(root->child[1]->child[0]->child).push_back(newNode());
(root->child[2]->child).push_back(newNode());
(root->child[2]->child).push_back(newNode());
(root->child[2]->child).push_back(newNode());
printf("%d\n",vertex_cover(root));
}