Strategic Game LA 2038


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string> 
#include <sstream>
#include <utility>
#include <ctime>
 
using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::greater;
using std::endl;

const int MAXN(1510);
const int MAXM(MAXN*MAXN*2);

struct EDGE
{
	int v;
	EDGE *next;
};

EDGE *first[MAXN];
EDGE edge[MAXM];
EDGE *rear;

void init()
{
	memset(first, 0, sizeof(first));
	rear = edge;
}

void insert(int tu, int tv)
{
	for(EDGE *i = first[tu]; i; i = i->next)
		if(i->v == tv)
			return;
	rear->v = tv;
	rear->next = first[tu];
	first[tu] = rear++;
}

bool vis[MAXN][2];
int table[MAXN][2];

int dfs(int f, int ls, int cur)
{
	if(vis[cur][ls])
		return table[cur][ls];
	int &tt = table[cur][ls];
	tt = 1;
	for(EDGE *i = first[cur]; i; i = i->next)
		if(i->v != f)
			tt += dfs(cur, 1, i->v);
	if(ls)
	{
		int temp = 0;
		for(EDGE *i = first[cur]; i; i = i->next)
		if(i->v != f)
			temp += dfs(cur, 0, i->v);
		tt = min(tt, temp);
	}
	vis[cur][ls] = true;
	return tt;
}

int main()
{
	int n;
	while(~scanf("%d", &n))
	{
		init();
		int tu, tv, tn;
		for(int i = 0; i < n; ++i)
		{
			scanf("%d", &tu);

			while(getchar() != '(');

			scanf("%d", &tn);

			while(getchar() != ')');

			for(int j = 0; j < tn; ++j)
			{
				scanf("%d", &tv);
				insert(tu, tv);
				insert(tv, tu);
			}
		}
		memset(vis, 0, sizeof(vis));
		printf("%d\n", dfs(-1, 1, 0));
	}
	return 0;
}


你可能感兴趣的:(Strategic Game LA 2038)