hdu 1054 Strategic Game 最少点覆盖 二分图最大匹配
2016-03-13 19:53
519 查看
Strategic GameTime Limit:10000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
SubmitStatus
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 <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int MAXN=5010;
const int MAXM=50010;
struct Edge
{
int to,next;
} edge[MAXM];
int head[MAXN],tol;
void init()
{
tol=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v)
{
edge[tol].to=v;
edge[tol].next=head[u];
head[u]=tol++;
}
int linker[MAXN];
bool used[MAXM];
int uN;
bool dfs(int u)
{
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].to;
if(!used[v])
{
used[v]=true;
if(linker[v]==-1||dfs(linker[v]))
{
linker[v]=u;
return true;
}
}
}
return false;
}
int hungary()
{
int res=0;
memset(linker,-1,sizeof(linker));
for(int u=0; u<uN; u++)
{
memset(used,false,sizeof(used));
if(dfs(u))
res++;
}
return res;
}
void getdata(char *data,int &node,int &num)
{
int i;
node=num=0;
for(i=0; data[i]!=':'; i++)
{
//cout<<data[i]<<endl;
node*=10;
node+=data[i]-'0';
}
for(i+=2; data[i]!=')'; i++)
{
num*=10;
num+=data[i]-'0';
}
}
int main()
{
char data[100];
int node,num,to,i,j;
while(~scanf("%d",&uN))
{
init();
for(j=0; j<uN; j++)
{
scanf("%s",data);
getdata(data,node,num);
//printf("%d %d\n",node,num);
for(i=0; i<num; i++)
{
scanf("%d",&to);
addedge(node,to);
addedge(to,node);
}
}
int ans=hungary();
printf("%d\n",ans/2);
}
return 0;
}
& %I64u
SubmitStatus
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 <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int MAXN=5010;
const int MAXM=50010;
struct Edge
{
int to,next;
} edge[MAXM];
int head[MAXN],tol;
void init()
{
tol=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v)
{
edge[tol].to=v;
edge[tol].next=head[u];
head[u]=tol++;
}
int linker[MAXN];
bool used[MAXM];
int uN;
bool dfs(int u)
{
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].to;
if(!used[v])
{
used[v]=true;
if(linker[v]==-1||dfs(linker[v]))
{
linker[v]=u;
return true;
}
}
}
return false;
}
int hungary()
{
int res=0;
memset(linker,-1,sizeof(linker));
for(int u=0; u<uN; u++)
{
memset(used,false,sizeof(used));
if(dfs(u))
res++;
}
return res;
}
void getdata(char *data,int &node,int &num)
{
int i;
node=num=0;
for(i=0; data[i]!=':'; i++)
{
//cout<<data[i]<<endl;
node*=10;
node+=data[i]-'0';
}
for(i+=2; data[i]!=')'; i++)
{
num*=10;
num+=data[i]-'0';
}
}
int main()
{
char data[100];
int node,num,to,i,j;
while(~scanf("%d",&uN))
{
init();
for(j=0; j<uN; j++)
{
scanf("%s",data);
getdata(data,node,num);
//printf("%d %d\n",node,num);
for(i=0; i<num; i++)
{
scanf("%d",&to);
addedge(node,to);
addedge(to,node);
}
}
int ans=hungary();
printf("%d\n",ans/2);
}
return 0;
}
相关文章推荐
- AD 报ID:1054错误处理过程
- 【HDU 5366】The mook jong 详解
- 【HDU 2136】Largest prime factor 详细图解
- 【HDU 1568】Fibonacci 数学公式 详解
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- 2015-11-11 hdu新生赛 A题(AC)