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 ...
随机推荐
- ajax异步请求详解
1.XMLHttpRequst的出现才有了异步处理 2.创建XmlHttpRequest对象 var request=new XMLHttpRequest(); 注意:如果要兼容IE6以下浏览器则需要 ...
- 线索二叉树【C语言】
#include<stdio.h> #include<stdlib.h> typedef char ElemType; typedef enum{Link,Thread} Po ...
- MySQL多表连接操作
select * from userinfo ,dapartment where userinfo.part_id = dapartment.id; --左连接: 左边全部显示 select * fr ...
- 一个电商项目的Web服务化改造
一个电商项目的Web服务化改造 项目,早期是随便瞎做的,没啥架构,连基本的设计也没. 有需求,实现需求,再反复修改. 大致就是这么做的. 最近,项目要重新架构,和某boss协商的结果是,采用阿里开源的 ...
- tipsText表单验证(注册)
注册表单验证脚本 <script src="/assets/skins/js/jquery-1.11.2.min.js"></script> <scr ...
- IE7浏览器下去除flash动画边框问题
<object width="100%" height="100%" data="/templates/default/swf/guide.sw ...
- hdu 4950
#include<stdio.h> int main(){ __int64 h,a,b,k,j=0; while(scanf("%I64d%I64d%I64d%I64d" ...
- NEMA-0183(GPRMC GPGGA)详细解释
NEMA-0183(GPRMC GPGGA)详细解释 nmea数据如下: $GPGGA,121252.000,3937.3032,N,11611.6046,E,1,05,2.0,45.9,M,-5. ...
- 【ACM】hdu_zs3_1008_Train Problem I_201308100835
Train Problem I Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Tota ...
- firedac的TFDStoredProc动态创建并调用存储过程
1)中间件执行存储过程 sp.Close; sp.StoredProcName := procName; sp.Prepare; // 生成存储过程的参数列表,无任何OUTPUT的存储过程,也会自动 ...