hdu---(1054)Strategic Game(最小覆盖边)
Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5034 Accepted Submission(s): 2297
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
Recommend
代码: 这里是最少覆盖边,开始一直搞最小覆盖点,然后各种不得劲,.....后面看了以下提示,然后就明白了。但是改了之后有开始tie,没办法,只能最后用起了邻接表(我用 的是vector来替代的) 最小覆盖点 =顶点数- 最大匹配数 、 最小覆盖边= 等于(无向图)最大匹配/2;
代码:
1 #include<cstring>
2 #include<cstdio>
3 #include<vector>
4 #include<iostream>
5 using namespace std;
6 const int maxn=1550;
7 vector<vector<int> >grid(maxn);
8 bool vis[maxn];
9 int savx[maxn];
10 int n;
11 int km(int x){
12 vector<int>::iterator it;
13 for(it=grid[x].begin();it<grid[x].end();it++){
14 if(!vis[*it]){
15 vis[*it]=1;
16 if(savx[*it]==-1||km(savx[*it])){
17 savx[*it]=x;
18 return 1;
19 }
20 }
21 }
22 return 0;
23 }
24
25 int main(){
26 int ans=0;
27 int a,b,c;
28 int km(int );
29 while(scanf("%d",&n)!=EOF){
30 ans=0;
31 memset(savx,-1,sizeof(savx));
32 for(int i=0;i<n;i++)
33 grid[i].clear();
34 for(int i=0;i<n;i++){
35 scanf("%d:(%d)",&a,&b);
36 for(int j=0;j<b;j++){
37 scanf("%d",&c);
38 grid[a].push_back(c);
39 grid[c].push_back(a);
40 }
41 }
42 for(int i=0;i<n;i++){
43 memset(vis,0,sizeof(vis));
44 ans+=km(i);
45 }
46 printf("%dn",ans/2);
47 }
48 return 0;
49 }
- 为 WordPress 后台管理菜单自定义排序
- WordPress 添加个性化的博客宠物(妹纸篇)
- WordPress 添加个性化的博客宠物(汉纸篇)
- WordPress 退出(登出)的时候跳转到首页
- WordPress免插件仅代码实现面包屑导航
- 开源的作业调度框架 - Quartz.NET
- Windows Server 2003网络负载均衡的实现
- 使用 ETW 对 .NET 应用程序进行性能诊断
- ORB_SLAM论文解读
- Mono.Addins 插件式框架
- 通过多说服务器缓存加速Gravatar 头像,解决被墙问题
- asp.net mvc脚手架代码生成工具
- Page.FindControl方法找不到指定控件的原因
- Silverlight 2 DispatcherTimer和通过XAML创建UI元素
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 聊聊dubbo-go的TpsLimitFilter
- 聊聊dubbo-go的TokenFilter
- 你这磨人的小妖精——选中文本并标注的实现过程
- VUE跨页面传值的精妙
- 一文解决如何提取TCGA配对表达矩阵
- CSS中的伪类选择器、颜色、度量单位、文本字体及文本样式设置
- CSS盒模型、边框和背景、表格和列表、颜色和透明度、阴影和轮廓及长度单位rem
- C++核心准则CP.110:不要自已为初始化编写双重检查锁定代码
- BOSS直聘招聘信息获取之爬虫程序数据处理
- BOSS直聘招聘信息获取之爬虫程序分析
- 历时4个多月,学习了这 66 个CSS 特效
- ArrayList、LinkedList哪家强,据说90%人都不知道
- Windows创建克隆隐藏账号
- 学员分享-aspera踩坑记录
- BOSS直聘招聘信息获取之爬虫工具分析