HDU 1054 Hungary
Strategic Game
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
一定要注意 不能把first数组或者fa数组初值设成零,因为这里有0这个点。。因为这个查了好久的错。。。。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,tot=0,first[1505],nxt[1500*1505*2],v[1500*1500*2],fa[1505],ans=0;
bool vis[1505];
inline void add(int xx,int yy)
{
v[tot]=yy;
nxt[tot]=first[xx];
first[xx]=tot++;
}
bool dfs(int x)
{
for(int i=first[x];~i;i=nxt[i])
if(!vis[v[i]]){
vis[v[i]]=1;
if(fa[v[i]]==-1||dfs(fa[v[i]])){
fa[v[i]]=x;return true;
}
}
return false;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(first,-1,sizeof(first));
memset(fa,-1,sizeof(fa));
tot=ans=0;
for(int i=0;i<n;i++){
register int xx,yy,zz;
scanf("%d:(%d)",&xx,&yy);
for(int j=1;j<=yy;j++){
scanf("%d",&zz),add(zz,xx),add(xx,zz);
}
}
for(int i=0;i<n;i++){
memset(vis,0,sizeof(vis));
if(dfs(i)==1)ans++;
}
printf("%d\n",ans/2);
}
}
HDU 1054 Hungary的更多相关文章
- HDU - 1054 Strategic Game(二分图最小点覆盖/树形dp)
d.一颗树,选最少的点覆盖所有边 s. 1.可以转成二分图的最小点覆盖来做.不过转换后要把匹配数除以2,这个待细看. 2.也可以用树形dp c.匈牙利算法(邻接表,用vector实现): /* 用ST ...
- HDU 1054
http://acm.hdu.edu.cn/showproblem.php?pid=1054 二分图最少顶点覆盖,模板题,双向边最后结果/2 #include <iostream> #in ...
- HDU 1054 Strategic Game(最小路径覆盖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 题目大意:给你一棵树,选取树上最少的节点使得可以覆盖整棵树. 解题思路: 首先树肯定是二分图,因 ...
- hdu 1054 【树形dp】
http://acm.hdu.edu.cn/showproblem.php?pid=1054 给定一棵树,点能看住与其相连的边,问最少需要选定多少个点看住所有的边. 定义dp[maxn][2],dp[ ...
- hdu 1054 最小点覆盖
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 ...
- HDU 1054 Strategic Game(无向二分图的最大匹配)
( ̄▽ ̄)" //凡无向图,求匹配时都要除以2 #include<iostream> #include<cstdio> #include<algorithm&g ...
- HDU 1054 Strategic Game (最小点覆盖)【二分图匹配】
<题目链接> 题目大意:鲍勃喜欢玩电脑游戏,特别是战略游戏,但有时他无法找到解决方案,速度不够快,那么他很伤心.现在,他有以下的问题.他必须捍卫一个中世纪的城市,形成了树的道路.他把战士的 ...
- hdu 1054 Strategic Game (二分匹配)
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU - 1054 Strategic Game (二分图匹配模板题)
二分图匹配模板题 #include <bits/stdc++.h> #define FOPI freopen("in.txt", "r", stdi ...
随机推荐
- 用python写一个百度翻译
运行环境: python 3.6.0 今天处于练习的目的,就用 python 写了一个百度翻译,是如何做到的呢,其实呢就是拿到接口,通过这个接口去访问,不过中间确实是出现了点问题,不过都解决掉了 先晾 ...
- Unity中确定时间是否在一定范围内
NowTime = DateTime.Now.ToLocalTime(); Timeyear = DateTime.Now.ToLocalTime().ToString("yyyy-MM-d ...
- js可以随意拖拽的div的实现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 00.Python中下划线的5种含义
转自这里 先放上 简短的"速查表",罗列了五种Python下划线模式的含义: 模式 举例 含义 单前导下划线 _var 命名约定,仅供内部使用.通常不会由Python解释器强行执行 ...
- python爬虫17 | 听说你又被封 ip 了,你要学会伪装好自己,这次说说伪装你的头部
这两天 有小伙伴问小帅b 为什么我爬取 xx 网站的时候 不返回给我数据 而且还甩一句话给我 “系统检测到您频繁访问,请稍后再来” 小帅b看了一下他的代码 ): requests.get(url) 瞬 ...
- Thesis Viva checklist
This list gives you suggestions helpful in preparing to defend your thesis: I know my thesis thoroug ...
- 51nod1103 N的倍数
[题解] 先预处理出模N意义下的前缀和sum[i]. 1.如果sum[i]=0,那么1~i的数之和就是N的倍数 2.sum[i]%N总共有0~N-1这N种情况:根据1,如果sum[i]为0则必定有解: ...
- LA 4329 Ping pong
#include <iostream> #include <cstring> #include <cstdio> using namespace std; ; ; ...
- VScode使用简介
1.1 VSCode简介 VSCode官网:https://code.visualstudio.com/ 支持语音: 速度较快,对超大文件读写速度飞快(打开10M代码不到1s,Subline原生会卡近 ...
- N天学习一个Linux命令之hostnamectl
前言 安装了CentOS7,发现按照以前修改文件/etc/sysconfig/network HOSTNAME字段主机名的方式不生效了,查资料发现可以使用hostnamectl命令 用途 Contro ...