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这个点。。因为这个查了好久的错。。。。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5. int n,tot=0,first[1505],nxt[1500*1505*2],v[1500*1500*2],fa[1505],ans=0;
  6. bool vis[1505];
  7. inline void add(int xx,int yy)
  8. {
  9. v[tot]=yy;
  10. nxt[tot]=first[xx];
  11. first[xx]=tot++;
  12. }
  13. bool dfs(int x)
  14. {
  15. for(int i=first[x];~i;i=nxt[i])
  16. if(!vis[v[i]]){
  17. vis[v[i]]=1;
  18. if(fa[v[i]]==-1||dfs(fa[v[i]])){
  19. fa[v[i]]=x;return true;
  20. }
  21. }
  22. return false;
  23. }
  24. int main()
  25. {
  26. while(scanf("%d",&n)!=EOF)
  27. {
  28. memset(first,-1,sizeof(first));
  29. memset(fa,-1,sizeof(fa));
  30. tot=ans=0;
  31. for(int i=0;i<n;i++){
  32. register int xx,yy,zz;
  33. scanf("%d:(%d)",&xx,&yy);
  34. for(int j=1;j<=yy;j++){
  35. scanf("%d",&zz),add(zz,xx),add(xx,zz);
  36. }
  37. }
  38. for(int i=0;i<n;i++){
  39. memset(vis,0,sizeof(vis));
  40. if(dfs(i)==1)ans++;
  41. }
  42. printf("%d\n",ans/2);
  43. }
  44. }

HDU 1054 Hungary的更多相关文章

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

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

  2. HDU 1054

    http://acm.hdu.edu.cn/showproblem.php?pid=1054 二分图最少顶点覆盖,模板题,双向边最后结果/2 #include <iostream> #in ...

  3. HDU 1054 Strategic Game(最小路径覆盖)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 题目大意:给你一棵树,选取树上最少的节点使得可以覆盖整棵树. 解题思路: 首先树肯定是二分图,因 ...

  4. hdu 1054 【树形dp】

    http://acm.hdu.edu.cn/showproblem.php?pid=1054 给定一棵树,点能看住与其相连的边,问最少需要选定多少个点看住所有的边. 定义dp[maxn][2],dp[ ...

  5. 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 ...

  6. HDU 1054 Strategic Game(无向二分图的最大匹配)

    ( ̄▽ ̄)" //凡无向图,求匹配时都要除以2 #include<iostream> #include<cstdio> #include<algorithm&g ...

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

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

  8. hdu 1054 Strategic Game (二分匹配)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. HDU - 1054 Strategic Game (二分图匹配模板题)

    二分图匹配模板题 #include <bits/stdc++.h> #define FOPI freopen("in.txt", "r", stdi ...

随机推荐

  1. kvm virt-install 使用小结

    简介: virt-install 能够为KVM.Xen或其它支持libvrit API的hypervisor创建虚拟机并完成GuestOS安装. 此外,它能够基于串行控制台.VNC或SDL支持文本或图 ...

  2. 转:LINQ教程一:LINQ简介

    原文地址:https://www.cnblogs.com/dotnet261010/p/8278793.html 一.为什么要使用LINQ 要理解为什么使用LINQ,先来看下面一个例子.假设有一个整数 ...

  3. 【LeetCode】2、Add Two Numbers

    题目等级:Medium 题目描述:   You are given two non-empty linked lists representing two non-negative integers. ...

  4. Jenkins+Ant+Jmeter自动化集成测试实例

    通过学习Jmeter自动化测试,接触到了Ant命令和其构建文件build.xml文件的编写,与此同时,通过将测试项目集成在jenkins上,进一步学习了jenkins的一些环境配置知识.以下是自己的初 ...

  5. 10.mysql事务管理及python操作

    在用户操作MySQL过程中,对于一般简单的业务逻辑或中小型程序而言,无需考虑应用MySQL事务.但在比较复杂的情况下,往往用户在执行某些数据操作过程中,需要通过一组SQL语句执行多项并行业务逻辑或程序 ...

  6. 6.3.1 使用 pickle 模块读写二进制文件

    Python 标准库 pickle 提供的 dump() 方法 用于将数据进行序列化并写入文件(dump() 方法的protocol 参数为True 时可以实现压缩的效果),而load() 用于读取二 ...

  7. 对vuex的浅解

    vuex是什么? 官网的解释是 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex 也 ...

  8. 费用最少的一款赛门铁克SSL证书

    Symantec Secure Site SSL证书,验证域名所有权和企业信息,属于Symantec Class 3企业(OV)验证 级SSL证书,为40位/56位/128/256位自适应加密,目前连 ...

  9. java中redis的分布式锁工具类

    使用方式 try { if(PublicLock.getLock(lockKey)){ //这里写代码逻辑,执行完后需要释放锁 PublicLock.freeLock(lockKey); } } ca ...

  10. js es6 Object.freeze

    将对象冻结,使用Object.freeze方法 const foo = Object.freeze({}); // 常规模式时,下面一行不起作用: // 严格模式时,该行会报错 foo.prop = ...