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 ...
随机推荐
- Github-素材篇
my github: http://github.com/yaochao
- Android开发-无法新建Activity及新建后编译错误
下载了其他的工程后,新建Activity时无法进行下一步,报错:“This template requires a minimum SDK version of at least 7, and the ...
- jquery用append添加按钮之后,按钮监听无法使用的解决方法
<!DOCTYPE html><html><head><meta charset="utf-8"> <title>< ...
- asp.net忘记密码功能
//调用接口 post public string GetResponseByPost(string mobile, string messcode, string values, string ut ...
- LR12.53—第6课:运行负载测试
第6课:运行负载测试 当你运行一个负载测试,LoadRunner将生成系统上的负载.然后,您可以使用LoadRunner的监控器和图形负载下,观察系统的性能. 在这一课中,你将涵盖以下主题: 一目了然 ...
- How to acquire an Android phone with locked bootloader?
As we know that some devices come with locked bootloaders like Sony, HUAWEI, hTC...If you try to unl ...
- C#操作access数据库
未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序解决办法 去http://download.microsoft.com/download/7/0/3/703ffbcb- ...
- NodeJS学习三之API
Node采用V8引擎处理JavaScript脚本,最大特点就是单线程运行,一次只能运行一个任务.这导致Node大量采用异步操作(asynchronous opertion),即任务不是马上执行,而是插 ...
- java内存分配和String类型的深度解析
[尊重原创文章出自:http://my.oschina.net/xiaohui249/blog/170013] 摘要 从整体上介绍java内存的概念.构成以及分配机制,在此基础上深度解析java中的S ...
- CI框架(一)
CI工作流程: 所有的入口都从根目录下的index.php进入,确定应用所在目录后,加载 codeigniter/CodeIgniter.php 文件,该文件会顺序加载以下文件执行整个流 ...