Strategic Game

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

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1054

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

题意:

问在图中最少放置多少个士兵,可以看守所有的边。

题解:

最小点覆盖模板。

但由于数据量较大,边数较多,邻接矩阵要超时,我便改用vector存储。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std; const int N = ;
int check[N],match[N];
int n,ans,dfn;
vector<int> link[N]; inline int dfs(int x){
for(int i=;i<link[x].size();i++){
int now = link[x][i];
if(check[now]!=dfn){
check[now]=dfn;
if(match[now]==- || dfs(match[now])){
match[now]=x;
return ;
}
}
}
return ;
} int main(){
while(scanf("%d",&n)!=EOF){
for(int i=;i<n;i++) link[i].clear();
mem(link,);mem(match,-);ans=dfn=;
for(int i=,u,k;i<n;i++){
scanf("%d:(%d)",&u,&k);
for(int j=,v;j<=k;j++){
scanf("%d",&v);
link[u].push_back(v);
link[v].push_back(u);
}
}
for(int i=;i<n;i++){
dfn++;
if(dfs(i)) ans++;
}
printf("%d\n",ans/);
}
return ;
}

HDU1054 Strategic Game(最小点覆盖)的更多相关文章

  1. HDU1054 Strategic Game —— 最小点覆盖 or 树形DP

    题目链接:https://vjudge.net/problem/HDU-1054 Strategic Game Time Limit: 20000/10000 MS (Java/Others)     ...

  2. POJ1463 Strategic game (最小点覆盖 or 树dp)

    题目链接:http://poj.org/problem?id=1463 给你一棵树形图,问最少多少个点覆盖所有的边. 可以用树形dp做,任选一点,自底向上回溯更新. dp[i][0] 表示不选i点 覆 ...

  3. HDU 1054 Strategic Game 最小点覆盖

     最小点覆盖概念:选取最小的点数覆盖二分图中的所有边. 最小点覆盖 = 最大匹配数. 证明:首先假设我们求的最大匹配数为m,那么最小点覆盖必然 >= m,因为仅仅是这m条边就至少需要m个点.然后 ...

  4. HDU 1054 Strategic Game (最小点覆盖)【二分图匹配】

    <题目链接> 题目大意:鲍勃喜欢玩电脑游戏,特别是战略游戏,但有时他无法找到解决方案,速度不够快,那么他很伤心.现在,他有以下的问题.他必须捍卫一个中世纪的城市,形成了树的道路.他把战士的 ...

  5. HDU 1054 Strategic Game(最小点覆盖+树形dp)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=106048#problem/B 题意:给出一些点相连,找出最小的点数覆盖所有的 ...

  6. Strategic game POJ - 1463 【最小点覆盖集】

    Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solu ...

  7. LA 2038 Strategic game(最小点覆盖,树形dp,二分匹配)

    题意即求一个最小顶点覆盖. 对于没有孤立点的图G=(V,E),最大独立集+最小顶点覆盖= V.(往最大独立集加点) 问题可以变成求树上的最大独立集合. 每个结点的选择和其父节点选不选有关, dp(u, ...

  8. 最小点覆盖 hdu--1054

    点击打开题目链接 最小点覆盖=最大二分匹配的 (单向图) ; 最小点覆盖=最大二分匹配的一半 (双向图) ; 证明 所以我们只需求最大匹配,用 匈牙利算法 求出最大匹配,除以二得到答案 具体算法都已经 ...

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

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

随机推荐

  1. STL——泛型编程

    1.指针的算术运算 对于一个存储int数据的vector,我们要查找某值是否存在于其中,采用下标操作的做法如下: int* find(const vector<int> &vec, ...

  2. vuejs中的计算属性和监视

    计算属性 1.在computed属性对象中定义计算属性的方法,在页面上使用{{方法名}}来显示计算结果 2.通过getter/setter实现对属性数据的显示和监视 3.计算属性存在缓存,多次读取只执 ...

  3. nyoj 525 一道水题【字符串(分割)】

    参考:https://blog.csdn.net/dxx_111/article/details/48154687 #include <iostream> #include <cst ...

  4. IDEA常用操作(一)

    1.视图的调整 左下右的侧边栏如何关闭?——右击选择remove from sidebar 面板上(左下右)的导航栏视图如何隐藏——可以在左下角悬停显示,单击隐藏/开启侧边栏 想打开其它视图怎么办?— ...

  5. Electron入门应用打包exe(windows)

    最近在学习nodejs,得知Electron是通过将Chromium和Node.js合并到同一个运行时环境中,用HTML,CSS和JavaScript来构建跨平台桌面应用程序的一门技术.对于之前一直从 ...

  6. springmvc+mybatis的两种配置和应用方式

    一.不用写dao层实现的方式 1.导入依赖包,我的pom.xml文件配置如下: <project xmlns="http://maven.apache.org/POM/4.0.0&qu ...

  7. Scala function programming

    1. Arbitrary multi parameters funcs sum(1,2,3,4,5) = sum(1 to 5: _*)the equal '=' can be ignored if ...

  8. ES6 语法糖

    重新认识ES6中的语法糖:https://segmentfault.com/a/1190000010159725

  9. 第十九章 Python os模块,pathlib 判断文件是目录还是文件

    OS模块 os.path.abspath() :返回path规范化的绝对路径 import sys import os BASE_DIR = os.path.dirname(os.path.dirna ...

  10. Docker容器-入门级

    1.1 容器简介 1.1.1 什么是 Linux 容器 Linux容器是与系统其他部分隔离开的一系列进程,从另一个镜像运行,并由该镜像提供支持进程所需的全部文件.容器提供的镜像包含了应用的所有依赖项, ...