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
#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【匈牙利算法+最小顶点覆盖】的更多相关文章

  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. 深入了解relative

    1.relative是自身定位,距原本位置的偏移 2.无侵入布局: 挪动位置,原本位置还在占据,并不会影响其他元素的布局   应用: 实现鼠标拖拽,比自身api好用 3.top/bottom 和 le ...

  2. 纯原生js移动端日期选择插件

    最近在项目上需要使用日期选择插件,由于是移动端的项目,对请求资源还是蛮节约的,可是百度上一搜,诶~全是基于jquery.zepto的,本来类库就很大,特别像mobiscroll这种样式文件一大堆又丑又 ...

  3. div+css3实现漂亮的多彩标签云,鼠标移动会有动画

    div+css3实现漂亮的多彩标签云,鼠标移动会有动画 点击运行效果 <style> .dict { margin: 20px 0;clear:both ;text-align:left; ...

  4. 使用PHP在共享内存中存储数据集

    我们可以使用共享内存作为一种独特的存储选项,提供快速读/写操作和进程互操作性等优势. 对于 Web 应用程序,这意味着: 缓存存储(数据库查询.Web 服务数据.外部数据) 会话存储 应用程序之间的数 ...

  5. Spring Mvc Controller间跳转 重定向 传参 (转)

    原文链接:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ 1. 需求背景     需求:spring MVC框架con ...

  6. 防止apche列出目录以及下载文件

    1.修改httpd.conf,将override none改为override all 2.在需要设置权限的目录上传.htaccess文件,.htaccess文件内容如下: <FilesMatc ...

  7. 搭建LNMP发布ecshop系统及压测启用opcache缓存与否的情况

    安装环境:CENTOS6.5,nginx1.6.2,php-5.5.18,mysql5.5.38 在安装软件之前安装epel源,就可以直接用yum安装libmcrypt,mhash,mcrypt等ph ...

  8. 用Eclipse 开发Dynamic Web Project应用程序 【转】

    简介:本文仅简单介绍基于Eclipse开发Dynamic Web Project应用下的JSP,Servlet及TOMCAT数据源的配置和开发. 软件环境: Eclipse Java EE IDE f ...

  9. simhash--文本排重

    转载自 https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/06.12.md http: ...

  10. Java多线程初学者指南(8):从线程返回数据的两种方法

    从线程中返回数据和向线程传递数据类似.也可以通过类成员以及回调函数来返回数据.但类成员在返回数据和传递数据时有一些区别,下面让我们来看看它们区别在哪. 一.通过类变量和方法返回数据 使用这种方法返回数 ...