Strategic Game 【无向图的最小顶点覆盖】

来源:互联网 发布:淘宝客服外包多少钱 编辑:程序博客网 时间:2024/05/17 00:17

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
4
0:(1) 1
1:(2) 2 3
2:(0)
3:(0)
5
3:(3) 1 4 2
1:(1) 0
2:(0)
0:(0)
4:(0)

Sample Output
1
2

无向图的 最小顶点覆盖==最大匹配/2

代码

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<map>#include<vector>#include<set>#define CLR(a,b) memset((a),(b),sizeof(a))#define inf 0x3f3f3f3f#define mod 100009#define LL long long#define MAXN  2000+10#define MAXM 20000+100#define ll o<<1#define rr o<<1|1#define lson o<<1,l,mid#define rson o<<1|1,mid+1,rusing namespace std;void read(int &x){    x=0;char c;    while((c=getchar())<'0');    do x=x*10+c-'0';while((c=getchar())>='0');}struct Edge {    int from,to,next;}edge[MAXM];int head[MAXN],top;int used[MAXN];int pipei[MAXN];int n,m;void init(){    memset(head,-1,sizeof(head));    memset(pipei,0,sizeof(pipei));    top=0;}void addedge(int a,int b){    Edge e={a,b,head[a]};    edge[top]=e;head[a]=top++;}void getmap(){     int a,b;    for(int i=0;i<n;i++)    {        int num;         scanf("%d:(%d)",&a,&num);        while(num--)        {            scanf("%d",&b);            addedge(a,b);            addedge(b,a);        }    }}int find(int x){    for(int i=head[x];i!=-1;i=edge[i].next)    {        Edge e=edge[i];        if(!used[e.to])        {            used[e.to]=1;            if(!pipei[e.to]||find(pipei[e.to]))            {                pipei[e.to]=x;                return 1;            }        }    }    return 0;}int main(){    while(scanf("%d",&n)!=EOF)    {    init();    getmap();    int sum=0;    for(int i=0;i<n;i++)    {        memset(used,0,sizeof(used));        sum+=find(i);      }     printf("%d\n",sum/2);     }    return 0;}
阅读全文
0 0
原创粉丝点击