hdu5452 Minimum Cut
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5452
题意:给你一个图和它的生成树,要你在树上删一条边,问你最少删多少条边使得图不联通(开始时图一定联通)
解:
对每一条非树边对它两点之间的树上链的边+1,答案就是树上边的最小边权+1。处理上开始用了树状数组=TLE,其实由于只查询一次,用数组维护一下就好
/*
* Problem:
* Author: SHJWUDP
* Created Time: 2015/9/23 星期三 19:32:28
* File Name: 1001.cpp
* State:
* Memo:
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm> using namespace std; struct Graph {
struct Edge {
int u, v;
};
int n, m;
vector<Edge> edges;
vector<vector<int>> G;
Graph(int _n):n(_n), G(_n){}
void addEdge(int u, int v) {
edges.push_back({u, v});
m=edges.size();
G[u].push_back(m-);
}
vector<int>& operator[](int x) {
return G[x];
}
}; struct LinkCutTree {
Graph G;
vector<int> fa, siz, son, dep, top;
vector<int> w;
int id;
vector<int> ans;
LinkCutTree(int n):G(n){}
void init() {
fa.resize(G.n);
siz.resize(G.n);
son.resize(G.n);
dep.resize(G.n);
top.resize(G.n);
w.resize(G.n);
id=; int root=;
fa[root]=-;
dfs1(root, );
dfs2(root, root);
ans.assign(G.n+, );
}
int dfs1(int u, int d) {
siz[u]=; dep[u]=d; son[u]=-;
for(auto i : G[u]) {
const auto& e=G.edges[i];
if(e.v==fa[u]) continue;
fa[e.v]=u;
siz[u]+=dfs1(e.v, d+);
if(son[u]==- || siz[son[u]]<siz[e.v]) son[u]=e.v;
}
return siz[u];
}
void dfs2(int u, int tp) {
w[u]=id++; top[u]=tp;
if(son[u]!=-) dfs2(son[u], tp);
for(auto i : G[u]) {
const auto & e=G.edges[i];
if(e.v==fa[u] || e.v==son[u]) continue;
dfs2(e.v, e.v);
}
}
void update(int u, int v) {
int f1=top[u], f2=top[v];
while(f1!=f2) {
if(dep[f1]<dep[f2]) swap(f1, f2), swap(u, v);
// cout<<"\tup: "<<w[f1]<<"\t"<<w[u]+1<<endl;
++ans[w[f1]]; --ans[w[u]+];
u=fa[f1]; f1=top[u];
}
if(u==v) return;
if(dep[u]>dep[v]) swap(u, v);
// cout<<"\tup: "<<w[u]<<"\t"<<w[v]<<endl;
++ans[w[son[u]]]; --ans[w[v]+];
}
}; int n, m;
int main() {
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif
int T, now=;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
LinkCutTree lct(n+);
Graph & G=lct.G;
for(int i=; i<n-; ++i) {
int a, b;
scanf("%d%d", &a, &b);
G.addEdge(a, b);
G.addEdge(b, a);
}
lct.init();
for(int i=n-; i<m; ++i) {
int a, b;
scanf("%d%d", &a, &b);
lct.update(a, b);
}
int ans=0x7f7f7f7f;
for(int i=; i<n; ++i) {
lct.ans[i]+=lct.ans[i-];
ans=min(ans, lct.ans[i]);
}
printf("Case #%d: %d\n", ++now, ans+);
}
return ;
}
hdu5452 Minimum Cut的更多相关文章
- POJ Minimum Cut
Minimum Cut Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 9302 Accepted: 3902 Case ...
- POJ 2914 Minimum Cut
Minimum Cut Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 9319 Accepted: 3910 Case ...
- hdu 5452 Minimum Cut 树形dp
Minimum Cut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=54 ...
- POJ 2914 Minimum Cut 最小割图论
Description Given an undirected graph, in which two vertices can be connected by multiple edges, wha ...
- HDU 6214.Smallest Minimum Cut 最少边数最小割
Smallest Minimum Cut Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Oth ...
- HDU 6214 Smallest Minimum Cut(最少边最小割)
Problem Description Consider a network G=(V,E) with source s and sink t. An s-t cut is a partition o ...
- Smallest Minimum Cut HDU - 6214(最小割集)
Smallest Minimum Cut Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Oth ...
- HDU - 6214:Smallest Minimum Cut(最小割边最小割)
Consider a network G=(V,E) G=(V,E) with source s s and sink t t . An s-t cut is a partition of nodes ...
- hdu 6214 Smallest Minimum Cut[最大流]
hdu 6214 Smallest Minimum Cut[最大流] 题意:求最小割中最少的边数. 题解:对边权乘个比边大点的数比如300,再加1 ,最后,最大流对300取余就是边数啦.. #incl ...
随机推荐
- eclipse提高效率 MAC
1添加代码提示,在java contentaasist里面加所有字母 2行注释, 块注释mac里是command ctrl加\,鼠标行注释shift+\ 3.快速添加注释doc comman ...
- ShellExecute 使用方法
ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件.打开一个目录.打印一个文件等等),并对外部程序有一定的控制. 有几个API函数都可以实现这些功能,但是在大多数情况下She ...
- PHP性能优化大全(转)
PHP优化对于PHP的优化主要是对php.ini中的相关主要参数进行合理调整和设置,以下我们就来看看php.ini中的一些对性能影响较大的参数应该如何设置. # vi /etc/php.ini (1) ...
- 1不等于1?numeric、decimal、float 和 real 数据类型的区别
大家有没有在SQL中遇见1不等于1(1<>1)的情形!?下面会有一个例子演示这个情形. 先简单介绍一下标题中的四种数值数据类型. 在T-SQL中,numeric和decimal是精确数值数 ...
- python Django教程 之 模型(数据库)、自定义Field、数据表更改、QuerySet API
python Django教程 之 模型(数据库).自定义Field.数据表更改.QuerySet API 一.Django 模型(数据库) Django 模型是与数据库相关的,与数据库相关的代码 ...
- C语言PIC18 serial bootloader和C#语言bootloader PC端串口通信程序
了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 新PIC18 Boot ...
- zend framework2 下载及安装
1.安装XAMPP 2.安装zend studio 3.在GITHUB上下载一个zendframework模板,插入到IDE中 4.将下载的zend framework2文件夹解压放在vendor文件 ...
- EBS中OPM成本更新处理流程及对应的表结构、SLA表
OPM成本更新流程: 1.跑实际成本处理 功能作用:计算成本 2.成本更新 功能作用:更新成本 3.OPM会计预处理程序->活动->提交流程 功能作用:是创建会计事件 ...
- 格式化namenode,造成无法启动datanode
一个常见的问题:格式化namenode,造成无法启动datanode的问题. 问题描述: 无法启动datanode,查看日志,datanote尝试n次启动无效后,会出现这个语句 INFO ...
- [jv-convert] Error 1,[all-recursive] Error 1
g++4.6编译是报错: make profiledbootstrap /var/tmp/gcc4/gcc-4.0.1/sparc-sun-solaris2.8/gcc/gcj -B/var/tmp/ ...