Strategic Game

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

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
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define MAX 1550
  4. int map[MAX][MAX],vis[MAX],point[MAX];
  5. int t;
  6. int find(int x)
  7. {
  8. int i;
  9. for(i=0;i<t;i++)
  10. {
  11. if(map[x][i]&&!vis[i])
  12. {
  13. vis[i]=1;
  14. if(point[i]==0||find(point[i]))
  15. {
  16. point[i]=x;
  17. return 1;
  18. }
  19. }
  20. }
  21. return 0;
  22. }
  23. int main()
  24. {
  25. int i,j,s,n,m;
  26. int a;
  27. while(scanf("%d",&t)!=EOF)
  28. {
  29. memset(map,0,sizeof(map));
  30. memset(point,0,sizeof(point));
  31. for(i=0;i<t;i++)
  32. {
  33. scanf("%d:(%d)",&n,&m);
  34. for(j=0;j<m;j++)
  35. {
  36. scanf("%d",&a);
  37. map[n][a]=map[a][n]=1;//此处要将map[n][a]和map[a][n]同时标记
  38. }
  39. }
  40. s=0;
  41. for(i=0;i<t;i++)
  42. {
  43. memset(vis,0,sizeof(vis));
  44. if(find(i))
  45. s++;
  46. }
  47. printf("%d\n",s/2);//因为上边双向同时标记所以s算重复了 所以要除以2
  48. }
  49. return 0;
  50. }

hdoj 1054 Strategic Game【匈牙利算法+最小顶点覆盖】的更多相关文章

  1. hdoj 1150 Machine Schedule【匈牙利算法+最小顶点覆盖】

    Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. HDU1054 Strategic Game——匈牙利算法

    Strategic Game Bob enjoys playing computer games, especially strategic games, but sometimes he canno ...

  3. HDU - 1054 Strategic Game(二分图最小点覆盖/树形dp)

    d.一颗树,选最少的点覆盖所有边 s. 1.可以转成二分图的最小点覆盖来做.不过转换后要把匹配数除以2,这个待细看. 2.也可以用树形dp c.匈牙利算法(邻接表,用vector实现): /* 用ST ...

  4. poj3041 Asteroids 匈牙利算法 最小点集覆盖问题=二分图最大匹配

    /** 题目:poj3041 Asteroids 链接:http://poj.org/problem?id=3041 题意:给定n*n的矩阵,'X'表示障碍物,'.'表示空格;你有一把枪,每一发子弹可 ...

  5. POJ 3041 Asteroids(二分图 && 匈牙利算法 && 最小点覆盖)

    嗯... 题目链接:http://poj.org/problem?id=3041 这道题的思想比较奇特: 把x坐标.y坐标分别看成是二分图两边的点,如果(x,y)上有行星,则将(x,y)之间连一条边, ...

  6. hdoj 1083 Courses【匈牙利算法】

    Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  7. Asteroids POJ - 3041 匈牙利算法+最小点覆盖König定理

    题意: 给出一个N*N的地图N   地图里面有K个障碍     你每次可以选择一条直线 消除这条直线上的所有障碍  (直线只能和列和行平行) 问最少要消除几次 题解: 如果(x,y)上有一个障碍 则把 ...

  8. Strategic Game HDU - 1054(最小顶点覆盖)

    最小顶点覆盖:用最少的点,让每条边都至少和其中一个点关联: ...以为自己很聪明..用边连边...最后还是点连点  哎.... hc 写的  匈牙利足够///// #include <iostr ...

  9. HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP

    分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...

随机推荐

  1. jQuery EasyUI 提示框(Messager)用法

    jQuery EasyUI 提示框(Messager)不仅强大,而且也不用任何的HTML代码,只需要按照<jQuery EasyUI框架使用文档>包含必要文件后,在$(function() ...

  2. 规则引擎-BRMS在企业开发中的应用

    1. 什么是规则复杂企业级项目的开发以及其中随外部条件不断变化的业务规则(business logic),迫切需要分离商业决策者的商业决策逻辑和应用开发者的技术决策,并把这些商业决策放在中心数据库或其 ...

  3. IDEA插件开发基础

    由于简易ORM的需要,想要做一些代码自动生成功能(通过右键菜单辅助) 半自动编写代码,故考虑需要开发IDE插件(我司现使用IDEA) 1.例子代码http://confluence.jetbrains ...

  4. Laravel学习第一天(创建laravel项目、路由、视图、blade模板)

    创建laravel项目 composer create-project laravel/laravel learnlv 4.1.* 查看帮助:composer create-project    使用 ...

  5. linux系统文件的颜色代表的意思

    1.蓝色代表目录 2.绿色代表可执行文件 3.红色代表可压缩文件 4.白色代表其他文件 5.浅蓝色代表链接文件 6.黄色代码设备 7.红色闪烁表示链接的文件有问题

  6. 以查询方式实现1s定时

    以查询控制器的控制位状态来实现1s定时. #include <reg52.h> sbit LED = P0^; unsigned ; void main () { LED = ; // 点 ...

  7. POJ 1129 Channel Allocation 四色定理dfs

    题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...

  8. POJ 3267 The Cow Lexicon 简单DP

    题目链接: http://poj.org/problem?id=3267 从后往前遍历,dp[i]表示第i个字符到最后一个字符删除的字符个数. 状态转移方程为: dp[i] = dp[i+1] + 1 ...

  9. SVN - 配置

    版本控制器 1.创建文件夹 svn 2.打开终端 进入该文件夹 3.输入 svnadmin 如果有错 xcrun: error: active developer path ("/Appli ...

  10. 第十二周项目一 教师兼干部类】 共建虚基类person

    项目1 - 教师兼干部类]分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部).要求: (1)在两个基类中都包含姓名.年 ...