• [1635] Explosion

  • 时间限制: 10000 ms 内存限制: 65535 K
  • 问题描述
  • there is a country which contains n cities connected by n - 1 roads(just like a tree). If you place TNT in one city, all the roads connect these city will be destroyed, now i want to destroy all the roads with the least number of TNT, can you help me ?

  • 输入
  • Input starts with an integer T(T <= 500), denoting the number of test case.
    For each test case, first line contains n(1 <= n <= 1000), denoting the number of cities, next n - 1lines following and each line contains two different cities denoting these two cities connect directly. You can assume the input guarantee the relation among cities is a tree.

  • 输出
  • For each test case, print the least number of TNT that i need to destroy all the n - 1 roads.

  • 样例输入
  • 2
    5
    1 2
    2 3
    3 4
    4 5
  • 样例输出
  • 2

题目链接:NBUT 1635

又是一道没人写的水题……由于题目中说like a tree,因此可以归为二分图,然后就套公式,在二分图中最小顶点覆盖数=最大匹配数。(另外这题应该是可以用树形DP做然而并不会……以后再说- -|||)

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1010;
struct edge
{
int to;
int pre;
};
edge E[N<<1];
int head[N],ne;
int vis[N],match[N];
void add(int s,int t)
{
E[ne].to=t;
E[ne].pre=head[s];
head[s]=ne++;
}
void init()
{
CLR(head,-1);
ne=0;
CLR(match,-1);
}
int dfs(int now)
{
for (int i=head[now]; ~i; i=E[i].pre)
{
int v=E[i].to;
if(!vis[v])
{
vis[v]=1;
if(match[v]==-1||dfs(match[v]))
{
match[v]=now;
return 1;
}
}
}
return 0;
}
int hun(int n)
{
int r=0;
for (int i=1; i<=n; ++i)
{
CLR(vis,0);
if(dfs(i))
++r;
}
return r;
}
int main(void)
{
int tcase,n,a,b,i,j,ans;
scanf("%d",&tcase);
while (tcase--)
{
init();
scanf("%d",&n);
for (i=0; i<n-1; ++i)
{
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
printf("%d\n",hun(n)>>1);
}
return 0;
}

NBUT 1635 Explosion(最小顶点覆盖)的更多相关文章

  1. POJ2226 Muddy Fields 二分匹配 最小顶点覆盖 好题

    在一个n*m的草地上,.代表草地,*代表水,现在要用宽度为1,长度不限的木板盖住水, 木板可以重叠,但是所有的草地都不能被木板覆盖. 问至少需要的木板数. 这类题的建图方法: 把矩阵作为一个二分图,以 ...

  2. BZOJ 3140 消毒(最小顶点覆盖)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3140 题意:最近在生物实验室工作的小T遇到了大麻烦. 由于实验室最近升级的缘故,他的分格 ...

  3. poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)

    http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  4. hdoj 1150 Machine Schedule【匈牙利算法+最小顶点覆盖】

    Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP

    分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...

  6. hdu1054(最小顶点覆盖)

    传送门:Strategic Game 题意:用尽量少的顶点来覆盖所有的边. 分析:最小顶点覆盖裸题,最小顶点覆盖=最大匹配数(双向图)/2. #include <cstdio> #incl ...

  7. hdu 1150 Machine Schedule(最小顶点覆盖)

    pid=1150">Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/327 ...

  8. poj2594最小顶点覆盖+传递闭包

    传递闭包最开始是在Floyd-Warshall算法里面出现的,当时这算法用的很少就被我忽视了.. 传递闭包是指如果i能到达k,并且k能到达j,那么i就能到达j Have you ever read a ...

  9. hdu1151有向图的最小顶点覆盖

    有向图的最小路径覆盖=V-二分图最大匹配. Consider a town where all the streets are one-way and each street leads from o ...

随机推荐

  1. Adaboost算法结合Haar-like特征

    Adaboost算法结合Haar-like特征 一.Haar-like特征 目前通常使用的Haar-like特征主要包括Paul Viola和Michal Jones在人脸检测中使用的由Papageo ...

  2. cf455a(简单dp)

    题意:给出一个长度为n的数列,元素为a1, a2, ...an:删除ai,ai+1,ai-1 可以得到ai积分,输出最多可以得到多少积分: 题解:开一个数组a存取数列,a[i]表示元素i的个数,所以删 ...

  3. 自定义viewgroup实现ArcMenu

    最终效果如下 实现思路 通过效果图,会有几个问题: a.动画效果如何实现 可以看出动画是从顶点外外发射的,可能有人说,那还不简单,默认元素都在定点位置,然后TraslateAnimation就好了:这 ...

  4. grep -n 显示行号

    [root@86 ~]# grep -n "StartDiscoverers" /usr/local/zabbix/etc/zabbix_server.conf 176:### O ...

  5. TCP的长连接和短连接

    TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层. 在网络层有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议. 在传输层中有TCP协议与UDP协议. 在应用层有FTP. ...

  6. centos7下安装vsftpd配置

    0. 首先安装ftp服务 yum install -y ftp 1. 通过yum install -y vsftp安装vsftp 2.    修改vi /etc/vsftpd/vsftpd.conf, ...

  7. wp7 BaseDictionary<TKey, TValue>

    /// <summary>/// Represents a dictionary mapping keys to values./// </summary>/// /// &l ...

  8. hdu 4024 二分

    转自:http://www.cnblogs.com/kuangbin/archive/2012/08/23/2653003.html   一种是直接根据公式计算的,另外一种是二分算出来的.两种方法速度 ...

  9. HDU 4513 吉哥系列故事——完美队形II manacher

    吉哥系列故事——完美队形II Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希 ...

  10. JVM的数据类型

    Java虚拟机是通过某些数据类型来执行计算的,数据类型可以分为两种:基本类型和引用类型,基本类型的变量持有原始值,而引用类型的变量持有引用值. Java语言中的所有基本类型同样也都是Java虚拟机中的 ...