HDU 4612——Warm up——————【边双连通分量、树的直径】
Warm up
Description
If we can isolate some planets from others by breaking only one channel , the channel is called a bridge of the transportation system.
People don't like to be isolated. So they ask what's the minimal number of bridges they can have if they decide to build a new channel.
Note that there could be more than one channel between two planets.
Input
Each case starts with two positive integers N and M , indicating the number of planets and the number of channels.
(2<=N<=200000, 1<=M<=1000000)
Next M lines each contains two positive integers A and B, indicating a channel between planet A and B in the system. Planets are numbered by 1..N.
A line with two integers '0' terminates the input.
Output
Sample Input
Sample Output
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;
const int maxn = 200100;
struct Edge{
int from,to,dist,next;
Edge(){}
Edge(int _to,int _next):to(_to),next(_next){}
}edges[maxn*10];
int head[maxn], tot;
int dfs_clock, dfn[maxn], brinum;
int Stack[maxn], instack[maxn], top, ebccno[maxn], ebcc_cnt;
int deg[maxn];
vector<int>G[maxn];
void init(){
tot = 0;
brinum = dfs_clock = 0;
top = 0;
ebcc_cnt = 0;
memset(deg,0,sizeof(deg));
memset(head,-1,sizeof(head));
}
void AddEdge(int _u,int _v){
edges[tot] = Edge(_v,head[_u]);
head[_u] = tot++;
}
int dfs(int u,int fa){
int lowu = dfn[u] = ++dfs_clock;
Stack[++top] = u;
// instack[u] = 1;
for(int i = head[u]; i != -1; i = edges[i].next){
int v = edges[i].to;
if(!dfn[v]){
int lowv = dfs(v,i);
lowu = min(lowu,lowv);
if(lowv > dfn[u]){
brinum++;
}
}else if(dfn[v] < dfn[u] && (fa^1) != i){//这里用边的编号来标记是否是同一条边的回边
lowu = min(lowu,dfn[v]);
}
}
if(dfn[u] == lowu){ //找到一个边双连通分量
ebcc_cnt++;
for(;;){
int v = Stack[top--];
// instack[v] = 0;
ebccno[v] = ebcc_cnt; //给每个点划分一个分量标号
if(u == v){
break;
}
}
}
// low[u] = lowu;
return lowu;
}
void find_ebcc(int n){
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
for(int i = 1; i <= n; i++){
if(!dfn[i]){
dfs(i,-1);
}
}
}
int pos, Maxd;
void dfs1(int u,int dep,int fa){ //求树的直径
if(dep > Maxd){
Maxd = dep;
pos = u;
}
for(int i = 0; i < G[u].size(); i++){
int v = G[u][i];
if(fa == v){ continue; }
dfs1(v,dep+1,u);
}
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){
init();
for(int i = 0; i <= n; i++){
G[i].clear();
}
int a,b;
for(int i = 0; i < m; i++){
scanf("%d%d",&a,&b);
AddEdge(a,b);
AddEdge(b,a);
}
find_ebcc(n);
for(int i = 1; i <= n; i++){
for(int j = head[i]; j != -1; j = edges[j].next){
int v = edges[j].to;
if(ebccno[i] != ebccno[v]){ //重新构图,形成树
G[ebccno[i]].push_back(ebccno[v]);
}
}
}
pos = 1, Maxd = 0;
dfs1(1,0,-1);
int st = pos; Maxd = 0;
dfs1(pos,0,-1);
printf("%d\n",brinum - Maxd);
}
return 0;
}
HDU 4612——Warm up——————【边双连通分量、树的直径】的更多相关文章
- HDU 4612 Warm up (边双连通分量+缩点+树的直径)
<题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...
- HDU 4612 Warm up(双连通分量缩点+求树的直径)
思路:强连通分量缩点,建立一颗新的树,然后求树的最长直径,然后加上一条边能够去掉的桥数,就是直径的长度. 树的直径长度的求法:两次bfs可以求,第一次随便找一个点u,然后进行bfs搜到的最后一个点v, ...
- HDU 4612 Warm up (边双连通分量+DP最长链)
[题意]给定一个无向图,问在允许加一条边的情况下,最少的桥的个数 [思路]对图做一遍Tarjan找出桥,把双连通分量缩成一个点,这样原图就成了一棵树,树的每条边都是桥.然后在树中求最长链,这样在两端点 ...
- hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Su ...
- 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。
问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...
- Hdu 4612 Warm up (双连通分支+树的直径)
题目链接: Hdu 4612 Warm up 题目描述: 给一个无向连通图,问加上一条边后,桥的数目最少会有几个? 解题思路: 题目描述很清楚,题目也很裸,就是一眼看穿怎么做的,先求出来双连通分量,然 ...
- HDU 4612 Warm up(2013多校2 1002 双连通分量)
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Su ...
- [HDOJ4612]Warm up(双连通分量,缩点,树直径)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 所有图论题都要往树上考虑 题意:给一张图,仅允许添加一条边,问能干掉的最多条桥有多少. 必须解决 ...
- 【HDU 4612 Warm up】BCC 树的直径
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 题意:一个包含n个节点m条边的无向连通图(无自环,可能有重边).求添加一条边后最少剩余的桥的数 ...
随机推荐
- MapReduce-自定义 InputFormat 生成 SequenceFile
Hadoop 框架自带的 InputFormat 类型不能满足所有应用场景,需要自定义 InputFormat 来解决实际问题. 无论 HDFS 还是 MapReduce,在处理小文件时效率都非常低, ...
- OpenCV自带dnn的Example研究(6)— text_detection
这个博客系列,简单来说,今天我们就是要研究 https://docs.opencv.org/master/examples.html下的 6个文件,看看在最新的OpenCV中,它们是如何发挥作用的. ...
- (multi)set的某些操作
(multi)set的某些操作 我们可以把multiset当作平衡树用~ 注意,必须定义小于运算符. s.begin() 返回指向第一个元素的迭代器. s.end() 返回指向最后元素的后面那个虚拟元 ...
- vue axios ajax引擎
0.调用 1.axios api axios.request(config) axios.get(url[,config]) axios.delete(url[,config]) axios.head ...
- boost库checked_delete的使用
在查看boost库时发现一个小文件checked_delete.hpp里面几个小函数,它的作用用很简短的话来说:防止未定义的行为造成delete时的内存泄露.实现如下: template<cla ...
- storm本地可以运行集群出错遇到的问题
storm本地运行和集群运行是存在区别的: 本地可以读取本地文件系统及java项目中的文件,但是提交集群后就不能读取了,storm只是将topology提交到了集群,所以只能在main方法中将需要读取 ...
- 在使用jquery时,(e.target).closest("li")是什么意思
今天在群里有童鞋问以下代码中: $(function(){ $(document).bind("click", function (e) { $(e.target).closest ...
- c#随笔-正则
- UVALive 3645 时序模型
按航班拆点 注意返边的条件 #include<bits/stdc++.h> using namespace std; const int maxn = 1e6+11; const int ...
- Codeforces Round #529 (Div. 3) C. Powers Of Two
http://codeforces.com/contest/1095/problem/C 题意:给n找出k个2的幂,加起来正好等于n.例如 9,4:9 = 1 + 2 + 2 + 4 思路:首先任何数 ...