NBUT 1635 Explosion(最小顶点覆盖)
[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(最小顶点覆盖)的更多相关文章
- POJ2226 Muddy Fields 二分匹配 最小顶点覆盖 好题
在一个n*m的草地上,.代表草地,*代表水,现在要用宽度为1,长度不限的木板盖住水, 木板可以重叠,但是所有的草地都不能被木板覆盖. 问至少需要的木板数. 这类题的建图方法: 把矩阵作为一个二分图,以 ...
- BZOJ 3140 消毒(最小顶点覆盖)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3140 题意:最近在生物实验室工作的小T遇到了大麻烦. 由于实验室最近升级的缘故,他的分格 ...
- poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)
http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- hdoj 1150 Machine Schedule【匈牙利算法+最小顶点覆盖】
Machine Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP
分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...
- hdu1054(最小顶点覆盖)
传送门:Strategic Game 题意:用尽量少的顶点来覆盖所有的边. 分析:最小顶点覆盖裸题,最小顶点覆盖=最大匹配数(双向图)/2. #include <cstdio> #incl ...
- hdu 1150 Machine Schedule(最小顶点覆盖)
pid=1150">Machine Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- poj2594最小顶点覆盖+传递闭包
传递闭包最开始是在Floyd-Warshall算法里面出现的,当时这算法用的很少就被我忽视了.. 传递闭包是指如果i能到达k,并且k能到达j,那么i就能到达j Have you ever read a ...
- hdu1151有向图的最小顶点覆盖
有向图的最小路径覆盖=V-二分图最大匹配. Consider a town where all the streets are one-way and each street leads from o ...
随机推荐
- eclipse4.4 tomcat jrebel使用
1.下载jrebel破解包 2.去eclipse -->Help -->EclipseMarketplace 下载eclipse jrebel 也可以下载离线安装包,然后eclipse - ...
- CodeChef DISTNUM2 Easy Queries 节点数组线段树
Description You are given an array A consisting of N positive integers. You have to answer Q queries ...
- HDU 4292 Food 最大流
Food Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- scp 使用
[spark-hadoop@master ~]$ sudo scp /etc/profile spark-hadoop@slave1:/etc spark-hadoop@slave1's passwo ...
- Centos升级内核 --已验证
Docekr 对内核由要求,3.10以上 Centos 6.5内核达不到要求 [linux@localhost Desktop]$ sudo service docker statusdocker d ...
- JavaScript表单提交四种方式
总结JavaScript表单提交四种方式 <!DOCTYPE html> <html> <head> <title>JavaScript表单提交四种方式 ...
- Spark Streaming中向flume拉取数据
在这里看到的解决方法 https://issues.apache.org/jira/browse/SPARK-1729 请是个人理解,有问题请大家留言. 其实本身flume是不支持像KAFKA一样的发 ...
- RequiredFieldValidator 控件 CompareValidator 控件
RequiredFieldValidator 控件 验证关联控件非空 ControlToValidate 属性用来关联被验证控件 ErrorMEssage 触发控件后显示的错误信息 CompareVa ...
- SU sugabor命令学习
不足之处,欢迎批评指正.
- 分享Kali Linux 2016.2第36周镜像虚拟机
分享Kali Linux 2016.2第36周镜像虚拟机 9月9日,Kali Linux官方发布Kali Linux 2016.2周更新镜像.今天以64位镜像安装了一个虚拟机,分享给大家.该虚拟机 ...