hdoj 1054 Strategic Game

来源:互联网 发布:vscode svn插件 编辑:程序博客网 时间:2024/05/29 17:41

Strategic Game

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5357    Accepted Submission(s): 2477


Problem Description
Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?

Your program should find the minimum number of soldiers that Bob has to put for a given tree.

The input file contains several data sets in text format. Each data set represents a tree with the following description:

the number of nodes
the description of each node in the following format
node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifier
or
node_identifier:(0)

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.

For example for the tree:



the solution is one soldier ( at the node 1).

The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following table:
 

Sample Input
40:(1) 11:(2) 2 32:(0)3:(0)53:(3) 1 4 21:(1) 02:(0)0:(0)4:(0)
 

Sample Output
12
 
醉了醉了。。。因为一个细节 TLE 4次,最后师傅帮我找出了毛病。。。最小顶点覆盖数 = 最大匹配数
 
代码:
 
#include<stdio.h>#include<string.h>#define max 1500+1int used[max],map[max][max],pipei[max];int n;int find(int x){    int i;    for(i=0;i<n;i++)    {        if(!used[i]&&map[x][i])        {            used[i]=1;            if(!pipei[i]||find(pipei[i]))            {                pipei[i]=x;                return 1;            }        }    }    return 0;}int main(){    int i,j,sum,num;    int a,b;    while(scanf("%d",&n)!=EOF)    {        memset(map,0,sizeof(map));        memset(pipei,0,sizeof(pipei));        for(i=0;i<n;i++)        {            scanf("%d:(%d)",&a,&num);            while(num--)            {                scanf("%d",&b);                map[a][b]=map[b][a]=1;            }        }        sum=0;        for(i=0;i<n;i++)        {            memset(used,0,sizeof(used));            if(find(i))            sum++;        }        printf("%d\n",sum/2);    }    return 0;}

 
邻接表:594ms
 
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#define MAX 1500+10using namespace std;vector<int >match[MAX];int pipei[MAX],used[MAX];int find(int x){    int i, y;    for(i = 0; i < match[x].size(); i++)    {        y = match[x][i];        if(!used[y])        {            used[y] = 1;            if(pipei[y] == -1 || find(pipei[y]))            {                pipei[y] = x;                return 1;            }        }    }    return 0;}int main(){    int n, i, j;    int sum;    int x, y, num;    while(~scanf("%d", &n))    {        for(i = 0; i < n; i++)        {            match[i].clear();        }        for(i = 0; i < n; i++)        {            scanf("%d:(%d)",&x, &num);            //match[x].clear();            while(num--)            {                scanf("%d", &y);                match[x].push_back(y);                match[y].push_back(x);            }        }        memset(pipei, -1, sizeof(pipei));        sum = 0;        for(i = 0; i < n; i++)        {            memset(used, 0, sizeof(used));            sum += find(i);        }        printf("%d\n", sum/2);    }    return 0;}

 
0 0