HDU - 1054 Strategic Game(二分图最小点覆盖/树形dp)
d.一颗树,选最少的点覆盖所有边
s.
1.可以转成二分图的最小点覆盖来做。不过转换后要把匹配数除以2,这个待细看。
2.也可以用树形dp
c.匈牙利算法(邻接表,用vector实现):
/* 用STL中的vector建立邻接表实现匈牙利算法
效率比较高
处理点比较多的效率很高。1500的点都没有问题
*/
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std; const int MAXN=;//这个值要超过两边个数的较大者,因为有linker
int linker[MAXN];
bool used[MAXN];
vector<int>G[MAXN];
int uN;
bool dfs(int u)
{
int sz=G[u].size();
for(int i=; i<sz; i++)
{
if(!used[G[u][i]])
{
used[G[u][i]]=true;
if(linker[G[u][i]]==-||dfs(linker[G[u][i]]))
{
linker[G[u][i]]=u;
return true;
}
}
}
return false;
} int hungary()
{
int u;
int res=;
memset(linker,-,sizeof(linker));
for(u=; u<uN; u++)
{
memset(used,false,sizeof(used));
if(dfs(u)) res++;
}
return res;
} int main(){ int n,k;
int u,v;
int i,j; while(~scanf("%d",&n)){ for(i=;i<MAXN;++i){
G[i].clear();
} for(i=;i<n;++i){
scanf("%d:(%d)",&u,&k);
for(j=;j<k;++j){
scanf("%d",&v);
G[u].push_back(v);
G[v].push_back(u);
}
} uN=n; printf("%d\n",hungary()/);
} return ;
}
c2.树形dp
/*
HDU 1054 G++ 312ms 560K
*/ #include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=;
struct Node
{
int father,brother,child;
int yes;//该结点放置
int no;//该结点不放置
}t[MAXN];
void DFS(int x)
{
int child=t[x].child;
while(child)
{
DFS(child);
t[x].yes+=min(t[child].yes,t[child].no);
//父亲结点放置了,儿子结点可以放置也可以不放置
t[x].no+=t[child].yes;
//父亲结点没有放置,儿子结点必须放置
child=t[child].brother;
}
}
bool used[MAXN];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
int root,k,v;
while(scanf("%d",&n)!=EOF)
{
memset(used,false,sizeof(used));
int Root;//根结点
for(int i=;i<n;i++)
{
scanf("%d:(%d)",&root,&k);
root++;//编号从1开始
if(i==)Root=root;
if(!used[root])
{
used[root]=true;
t[root].brother=t[root].father=t[root].child=;
t[root].yes=;
t[root].no=;
}
while(k--)
{
scanf("%d",&v);
v++;
if(!used[v])
{
used[v]=true;
t[v].brother=t[v].father=t[v].child=;
t[v].yes=;
t[v].no=;
}
t[v].brother=t[root].child;
t[v].father=root;
t[root].child=v;
} }
DFS(Root);
printf("%d\n",min(t[Root].yes,t[Root].no)); }
return ;
}
HDU - 1054 Strategic Game(二分图最小点覆盖/树形dp)的更多相关文章
- HDU 1054 Strategic Game(最小点覆盖+树形dp)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=106048#problem/B 题意:给出一些点相连,找出最小的点数覆盖所有的 ...
- HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP
分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...
- HDU 1054 Strategic Game(最小路径覆盖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 题目大意:给你一棵树,选取树上最少的节点使得可以覆盖整棵树. 解题思路: 首先树肯定是二分图,因 ...
- HDU 1150 Machine Schedule (二分图最小点覆盖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1150 有两个机器a和b,分别有n个模式和m个模式.下面有k个任务,每个任务需要a的一个模式或者b的一个 ...
- POJ2226 Muddy Fields(二分图最小点覆盖集)
题目给张R×C的地图,地图上*表示泥地..表示草地,问最少要几块宽1长任意木板才能盖住所有泥地,木板可以重合但不能盖住草地. 把所有行和列连续的泥地(可以放一块木板铺满的)看作点且行和列连续泥地分别作 ...
- POJ1325 Machine Schedule(二分图最小点覆盖集)
最小点覆盖集就是在一个有向图中选出最少的点集,使其覆盖所有的边. 二分图最小点覆盖集=二分图最大匹配(二分图最大边独立集) 这题A机器的n种模式作为X部的点,B机器的m种模式作为Y部的点: 每个任务就 ...
- hihoCoder #1127:二分图最小点覆盖和最大独立集
题目大意:求二分图最小点覆盖和最大独立集. 题目分析:如果选中一个点,那么与这个点相连的所有边都被覆盖,使所有边都被覆盖的最小点集称为最小点覆盖,它等于最大匹配:任意两个点之间都没有边相连的最大点集称 ...
- [POJ] 2226 Muddy Fields(二分图最小点覆盖)
题目地址:http://poj.org/problem?id=2226 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...
- 二分图 最小点覆盖 poj 3041
题目链接:Asteroids - POJ 3041 - Virtual Judge https://vjudge.net/problem/POJ-3041 第一行输入一个n和一个m表示在n*n的网格 ...
随机推荐
- chromedriver错误信息提示
The open chrome driver window displays: Starting ChromeDriver (v2.8.241075) on port 10820 [8804:7492 ...
- webstorm(三):webstorm的一些waring提示
一.Attribute key is not allowed here 二.Comparison this.loginType != 'username' may cause unexpected t ...
- commons.apache
1.ToStringBuilder //对象及其属性一行显示 System.out.println(ToStringBuilder.reflectionToString(u)); System.out ...
- SpringBoot中mybatis的自动生成
1.在pom文件中加入自动生成的插件 <!-- mybatis generator 自动生成代码插件 --> <plugin> <groupId>org.mybat ...
- MongoDB学习day10--数据库导入导出
在 Mongodb 中我们使用 mongodump 命令来备份 MongoDB 数据. 该命令可以导出所有数据到指定目录中.mongodump 命令可以通过参数指定导出的数据量级转存的服务器. 使用m ...
- Linux下Shell脚本运行程序不输出日志到终端
使用: 脚本路径/脚本名 >/dev/>& 说明: 可以简单的理解/dev/null是Linux下的回收站 >默认是把标准输出重定向 2>&1是把出错输出也定向 ...
- go语言学习之路五:Go语言内存分配机制make&new
Go有两种分配内存的机制,规则很简单,下面来简单介绍一下.1.new函数New()函数可以给一个值类型的数据分配内存(不知道什么是值类型请前往切片那一部分),调用成功后返回一个初始化的内存块指针,同时 ...
- BUPT复试专题—List(2015)
题目描述 在该LIST上实现3种操作 1.append x在该LIST末尾添加x,x是32位整数 2.pop删除该LIST末尾的数 3.find i寻找第i个数,若i为负数表示寻找倒数第i个数,例如i ...
- es中插入数据
es中插入数据 学习了:https://www.imooc.com/video/15769/0 分为指定Id和自动生成Id两种: 1,指定Id使用PUT操作 PUT http://127.0.0.1: ...
- iOS常用网络库收集
一 ASIHttpRequest二 AFNetworking 三 AFDownloadRequestOperationA progressive download operation for AFNe ...