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 ...
随机推荐
- nyoj 82 迷宫寻宝(二)
http://acm.nyist.net/JudgeOnline/problem.php?pid=83 题目解法主要在于判断两线段是否相交,思路是穷举所有地图四周的点,其中每一个边界上的点和终点构成一 ...
- 转换实例存储支持为EBS支持的AMI
转换实例存储支持为EBS支持的AMI 注:不能将实例存储支持的Windows AMI 转换为 EBS 支持的 AMI.并且,你只能转换你所拥有的 AMI. 1. 从一个EBS支持的AMI启动一个Ama ...
- FreeBSD从零开始---安装后配置(一)
一.安装后配置 上次我们说到FreeBSD的安装,这次我们说FreeBSD安装后的配置和简单优化方法. 安装完BSD只是服务器提供服务这条万里长征路的开始,还需要一些基本的设定和优化.不过实际 ...
- 【转】Hibernate级联注解CascadeType参数详解
cascade(级联) 级联在编写触发器时经常用到,触发器的作用是当 主控表信息改变时,用来保证其关联表中数据同步更新.若对触发器来修改或删除关联表相记录,必须要删除对应的关联表信息,否则,会存有脏数 ...
- extentreports报告插件之extentX之服务搭建(三)
之前两个章节已经写完再extentreports报告插件与testng 的集成,但是发现 每次测试完后,生成的报告都要在单独发送,每个项目都有一份报告,如果项目多的话,管理起来就会很冗余. 这个给大家 ...
- java comet
http://www.javaworld.com/article/2077995/java-concurrency/asynchronous-processing-support-in-servlet ...
- OGC学习课程
1.引言 由于项目需要,需要学习OGC相关地图标准,包括WMS.WFS.GML.SLD等,只是国内相关书籍大家都懂的,特向Google大师请教,得一秘籍<Open Web Mapping> ...
- eaby技术架构变迁
如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 最近在infoq上面看到 ebay介绍其系统架构变迁以及系统设计分享方面的讲座,其中 ...
- 装tomcat和nginx心得
开机启动tomcat 1:在/etc/rc.d/init.d目录下生成一个文件tomcat8080 2:在文件里添加如下内 #!/bin/bash #2345 linux运行级别 #10开机启动优先级 ...
- tomcat gzip compression not working for large js files
solution 1: <Connector port="8080" protocol="HTTP/1.1" connectionTimeout=&quo ...