题目:

Strategic Game

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 110 Accepted Submission(s): 75
 
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
 
 
Source
Southeastern Europe 2000
 
Recommend
JGShining

题目分析:

二分图,求最小顶点覆盖。简单题。这道题须要注意的是,用邻接矩阵来做会超时,数组开得太大,要用

邻接表来做。

最小顶点覆盖 = 最大匹配数/2

代码例如以下:

/*
* a.cpp
*
* Created on: 2015年3月13日
* Author: Administrator
*/ #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector> using namespace std; const int maxn = 1501;
//bool map[maxn][maxn];//这个是使用邻接矩阵实现时所须要用到的数据结构
vector<int> map[maxn];//这个是使用邻接表实现时所须要用到的数据结构.这时候map[i]里面村的就是愿意和结点i匹配的结点集合了
bool useif[maxn];//用于标记某个节点是否已经匹配
int link[maxn];//link[i] = t .表示结点i匹配的结点是t int n;//节点数 /**
* 用邻接表实现的,推断结点t能否找到匹配的节点
*/
bool can(int t){
int i; int size = map[t].size();//获取元一个结点t匹配的结点的个数
for(i = 0 ; i < size ; ++i){//遍历每个愿意和结点t匹配的结点
int index = map[t][i];//获取愿意和节点t匹配的结点的序号
if(useif[index] == false){//假设该节点还没有匹配
useif[index] = true;//将该节点标记为已经匹配
if(link[index] == - 1 || can(link[index])){//假设该节点还没有匹配的结点||该匹配结点能找到其它的匹配结点
link[index] = t;//那么僵该节点的匹配结点设置为t return true;//返回true,表示可以t找到匹配的结点
}
}
} return false;//返回false,表示结点t无法找到匹配的节点
} /**
* 求最大匹配数
*/
int max_match(){
int num = 0; int i;
for(i = 0 ; i < n ; ++i){//遍历全部节点,求最大匹配数
memset(useif,false,sizeof(useif));
if(can(i) == true){
num++;
}
} return num;
} int main(){
while(scanf("%d",&n)!=EOF){
// memset(map,false,sizeof(map));//使用邻接矩阵实现求最大匹配数时,清空map的写法
int i; for(i = 0 ; i < n ; ++i){//使用邻接矩阵求最大匹配数时,清空map的写法
map[i].clear();
}
memset(link,-1,sizeof(link)); int a,b;
for(i = 0 ; i < n ; ++i){
scanf("%d:(%d)",&a,&b); int c;
int j;
for(j = 0 ; j < b ; ++j){
scanf("%d",&c);
// map[a][c] = true;//邻接矩阵建立关系的写法
// map[c][a] = true;
/**
* 这道题和Grils And Boys 的差别就在于
* 假设0 (2): 1 2 --->这个表示0与1和2相互认识
* 在Grils And Boys中,0会出如今1和2的描写叙述中如
* 1 (...): 0 ...
* 2 (...): 0 ...
*
* 可是这道题中0 (2): 1 2 表示的不过0认识1和2
* 但1和2不一定认识0.
* 在做的时候我们要把它转换成相互认识来做
*/
map[a].push_back(c);//邻接表建立关系的写法
map[c].push_back(a);
}
} //最小顶点覆盖 = 最大匹配数/2
printf("%d\n",max_match()/2);
} return 0;
}

(hdu step 6.3.1)Strategic Game(求用最少顶点数把全部边都覆盖,使用的是邻接表)的更多相关文章

  1. hdu 4587 2013南京邀请赛B题/ / 求割点后连通分量数变形。

    题意:求一个无向图的,去掉两个不同的点后最多有几个连通分量. 思路:枚举每个点,假设去掉该点,然后对图求割点后连通分量数,更新最大的即可.算法相对简单,但是注意几个细节: 1:原图可能不连通. 2:有 ...

  2. hdu 6214 Smallest Minimum Cut(最小割的最少边数)

    题目大意是给一张网络,网络可能存在不同边集的最小割,求出拥有最少边集的最小割,最少的边是多少条? 思路:题目很好理解,就是找一个边集最少的最小割,一个方法是在建图的时候把边的容量处理成C *(E+1 ...

  3. (hdu step 6.3.3)Air Raid(最小路径覆盖:求用最少边把全部的顶点都覆盖)

    题目: Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  4. (hdu step 7.1.7)Wall(求凸包的周长——求将全部点围起来的最小凸多边形的周长)

    题目: Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  5. hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割

    view code//hdu 3987 #include <iostream> #include <cstdio> #include <algorithm> #in ...

  6. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  7. (hdu step 6.3.5)Card Game Cheater(匹配的最大数:a与b打牌,问b赢a多少次)

    称号: Card Game Cheater Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  8. hdu 3746 Cyclic Nacklace(next数组求最小循环节)

    题意:给出一串字符串,可以在字符串的开头的结尾添加字符,求添加最少的字符,使这个字符串是循环的(例如:abcab 在结尾添加1个c变为 abcabc 既可). 思路:求出最小循环节,看总长能不能整除. ...

  9. HDU 4864 Task (贪心+STL多集(二分)+邻接表存储)(杭电多校训练赛第一场1004)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4864 解题报告:有n台机器用来完成m个任务,每个任务有一个难度值和一个需要完成的时间,每台机器有一个可 ...

随机推荐

  1. jquery插件-validate

    1.引入js,css 下载地址:http://plugins.jquery.com/validate/ 2.设置验证规则:input的class添加以下验证属性 3.设置不符合规则的提示信息:添加da ...

  2. Python 手册——Python的非正式介绍

    在后面的例子中,区分输入和输出的方法是看是否有提示符(“>>> ”和“.. ”):想要重复这些例子的话,你就要在提示符显示后输入所有的一切:没有以提示符开始的行,是解释器输出的信息. ...

  3. resolvconf: Error: /etc/resolv.conf isn't a symlink, not doing anything.

    一.问题出现的原因 resolv.conf默认是个软链接,resolvconf默认会检查resolv.conf不是软链接就报错 默认的情况是这样的: #ls -l /etc/resolv.conflr ...

  4. 优先级和lisp式前缀表达式

    if(f(a,b) > 0) # 1 if(f(a, b>0)) # 2 上面在调用f的时候,1使用括号表明了参数的优先级,不然会出现2这样的情况. 而使用lisp式的前缀表达式则不会有不 ...

  5. HJA的异或值

    HJA的异或值 查看 提交 统计 提问 总时间限制:  20000ms 内存限制:  512000kB 描述 形态形成场(Morphogenetic Field)假说是Rupert Sheldrake ...

  6. 安卓天天练练(十)ListView

    ListView不能和ScrollView同时使用,因为它已经包含了滚动支持. 还有个Gallery http://blog.csdn.net/dazlly/article/details/78639 ...

  7. iOS获取图片的Base64String,兼容Android,java,web,jpg(jpeg),png

    呃呃呃……需求的来源又是同学,对!又是! 废话不哆嗦,怎么把一张图在iOS上转一个Base64String出来,稍微了解的,或者随便搜一下,都能搞定一大堆,但是!!!! 自己(iOS)转自己用,完全没 ...

  8. PHP Array函数分类

    一. 数组操作的基本函数1.数组的键名和值array_values($arr);              获得数组的值array_keys($arr);                 获得数组的键 ...

  9. Android+clipse导入工程提示:invalid project description

    今天遇到一个奇怪的问题.一个android的工程用eclipse导入的时候,提示错误.错误为:invalid project description . details为xxxx project ov ...

  10. HDU 1150 Machine Schedule

    题目大意: 有两台机器A和B以及K个需要运行的任务.A机器有N种不同的模式,B机器有M种不同的模式,而每个任务都恰好在一台机器上运行. 如果它在机器A上运行,则机器A需要设置为模式xi,如果它在机器B ...