hdoj 1054 Strategic Game【匈牙利算法+最小顶点覆盖】
Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5783 Accepted Submission(s): 2677
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:
#include<stdio.h>
#include<string.h>
#define MAX 1550
int map[MAX][MAX],vis[MAX],point[MAX];
int t;
int find(int x)
{
int i;
for(i=0;i<t;i++)
{
if(map[x][i]&&!vis[i])
{
vis[i]=1;
if(point[i]==0||find(point[i]))
{
point[i]=x;
return 1;
}
}
}
return 0;
}
int main()
{
int i,j,s,n,m;
int a;
while(scanf("%d",&t)!=EOF)
{
memset(map,0,sizeof(map));
memset(point,0,sizeof(point));
for(i=0;i<t;i++)
{
scanf("%d:(%d)",&n,&m);
for(j=0;j<m;j++)
{
scanf("%d",&a);
map[n][a]=map[a][n]=1;//此处要将map[n][a]和map[a][n]同时标记
}
}
s=0;
for(i=0;i<t;i++)
{
memset(vis,0,sizeof(vis));
if(find(i))
s++;
}
printf("%d\n",s/2);//因为上边双向同时标记所以s算重复了 所以要除以2
}
return 0;
}
hdoj 1054 Strategic Game【匈牙利算法+最小顶点覆盖】的更多相关文章
- hdoj 1150 Machine Schedule【匈牙利算法+最小顶点覆盖】
Machine Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU1054 Strategic Game——匈牙利算法
Strategic Game Bob enjoys playing computer games, especially strategic games, but sometimes he canno ...
- HDU - 1054 Strategic Game(二分图最小点覆盖/树形dp)
d.一颗树,选最少的点覆盖所有边 s. 1.可以转成二分图的最小点覆盖来做.不过转换后要把匹配数除以2,这个待细看. 2.也可以用树形dp c.匈牙利算法(邻接表,用vector实现): /* 用ST ...
- poj3041 Asteroids 匈牙利算法 最小点集覆盖问题=二分图最大匹配
/** 题目:poj3041 Asteroids 链接:http://poj.org/problem?id=3041 题意:给定n*n的矩阵,'X'表示障碍物,'.'表示空格;你有一把枪,每一发子弹可 ...
- POJ 3041 Asteroids(二分图 && 匈牙利算法 && 最小点覆盖)
嗯... 题目链接:http://poj.org/problem?id=3041 这道题的思想比较奇特: 把x坐标.y坐标分别看成是二分图两边的点,如果(x,y)上有行星,则将(x,y)之间连一条边, ...
- hdoj 1083 Courses【匈牙利算法】
Courses Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- Asteroids POJ - 3041 匈牙利算法+最小点覆盖König定理
题意: 给出一个N*N的地图N 地图里面有K个障碍 你每次可以选择一条直线 消除这条直线上的所有障碍 (直线只能和列和行平行) 问最少要消除几次 题解: 如果(x,y)上有一个障碍 则把 ...
- Strategic Game HDU - 1054(最小顶点覆盖)
最小顶点覆盖:用最少的点,让每条边都至少和其中一个点关联: ...以为自己很聪明..用边连边...最后还是点连点 哎.... hc 写的 匈牙利足够///// #include <iostr ...
- HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP
分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...
随机推荐
- vs2005用正则表达式统计有效代码行数
正则表达式:^:b*[^:b#/]+.*$ 需要注意:#开头和/开头或者空行都不计入代码量. 如果需要只统计代码文件的代码量,可以按住Ctrl+Shift+F之后选择查找文件的类型. Form:htt ...
- [python]字符串方法
字符串的方法及注释 字符串的方法及注释 capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 cente ...
- VS2012 独占编辑 设置
用VS2008建立的项目就有(禁止其他用户签出和签入) 为什么VS2012的项目就没有了呢??(管理员说他什么都没设置)VS2012: 两个项目同样是用VS2012打开的,而第一个是用VS2008创建 ...
- Map集合的四种遍历
Map集合遍历 Map<String,Integer> m = new HashMap<String,Integer>(); m.put("one",100 ...
- 集合 ArrayList 向下转型 遍历
List list=new ArrayList(); Person p1=new Person("lisi1",21); Person p2=new Person("l ...
- KEIL4.12中添加ULINK2的支持
转载自:http://www.amobbs.com/thread-4767650-1-1.html 如果你用KEIL4.12,但却没有Ulink2下载器,只有早先用的Ulink下载器,那么你按照下面三 ...
- Response.Redirect:无法在发送 HTTP 标头之后进行重定向
URL:http://blog.163.com/asp_neter/blog/static/17510918820107258107558/ 错误出现语句:“Response.Redirect(&qu ...
- /etc/passwd 与 /etc/shadow
/etc/passwd是用户数据库,其中的域给出了用户名.加密口令和用户的其他信息. /etc/shadow是在安装了影子(shadow)口令软件的系统上的影子口令文件.影子口令文件将/etc/pas ...
- 【网络流24题】No.18 分配问题 (二分图最佳匹配 费用流|KM)
[题意] 有 n 件工作要分配给 n 个人做.第 i 个人做第 j 件工作产生的效益为 cij . 试设计一个将n 件工作分配给 n 个人做的分配方案, 使产生的总效益最大. 输入文件示例input. ...
- linux和windows双系统导致的时间日
我的博客:www.while0.com系统中有两种时间区分,一为UTC,另一为LT(地方时)两者的区别为时区不同,UTC就是0时区的时间,而我们当地是用的北京时间要慢8小时.linux采用的UTC时间 ...