#pragma comment(linker,"/STACK:102400000,102400000")//总是爆栈加上这个就么么哒了
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
#define N 210000
#define inf 99999999
struct node {
int u,v,w,next;
}bian[N*20],biantwo[N*20];
int head[N],yong,dfn[N],low[N],yongtwo,index,top,cnt,stac[N],visit[N];
int suo[N];
void init(){
memset(head,-1,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(visit,0,sizeof(visit));
index=0;top=0;cnt=0;yong=0;
yongtwo=0;
}
void addedge(int u,int v) {
bian[yong].u=u;
bian[yong].v=v;
bian[yong].next=head[u];
head[u]=yong++;
}
void addedgetwo(int u,int v,int w) {
biantwo[yongtwo].u=u;
biantwo[yongtwo].v=v;
biantwo[yongtwo].w=w;
biantwo[yongtwo].next=head[u];
head[u]=yongtwo++;
}
int Min(int a,int b) {
return a>b?b:a;
}
void tarjan(int u,int pre) {//双联通缩点
int i;
dfn[u]=low[u]=++index;
stac[++top]=u;
visit[u]=1;
for(i=head[u];i!=-1;i=bian[i].next) {
int v=bian[i].v;
if(i==(pre^1))continue;
if(!dfn[v]) {
tarjan(v,i);
low[u]=Min(low[u],low[v]);
}
else
if(visit[v])
low[u]=Min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
++cnt;
int t;
do{
t=stac[top--];
suo[t]=cnt;
visit[t]=0;
}while(t!=u);
}
}
int dis[N],n;
int bfs(int u) {//求树的直径
int i,vis[N],j;
queue<int>q;
memset(vis,0,sizeof(vis));
for(i=1;i<=cnt;i++)
dis[i]=inf;
q.push(u);
vis[u]=1;
dis[u]=0;
while(!q.empty()) {
int d=q.front();
q.pop();
for(i=head[d];i!=-1;i=biantwo[i].next) {
int v=biantwo[i].v;
if(vis[v]==0&&dis[v]>dis[d]+biantwo[i].w){
dis[v]=dis[d]+biantwo[i].w;
vis[v]=1;
q.push(v);
}
}
}
int mi=inf,temp;
//printf("%d %d\n",dis[1],dis[2]);
for(i=1;i<=cnt;i++)
if(mi>dis[i]) {
mi=dis[i];
temp=i;
}
// printf("%d\n",mi);
return temp;
}
int main() {
int m,i,j;
while(scanf("%d%d",&n,&m),n||m) {
init();
while(m--){
scanf("%d%d",&i,&j);
addedge(i,j);
addedge(j,i);
}
tarjan(1,-1);
memset(head,-1,sizeof(head));
for(i=0;i<yong;i++) {
int u=bian[i].u,v=bian[i].v;
if(suo[u]!=suo[v]) {
addedgetwo(suo[u],suo[v],-1);
addedgetwo(suo[v],suo[u],-1);
}
}
printf("%d\n",cnt+dis[bfs(bfs(1))]-1);
}
return 0;
}

hdu 4612 双联通缩点+树形dp的更多相关文章

  1. hdu 4612 (双联通+树形DP)

    加一条边后最少还有多少个桥,先Tarjan双联通缩点, 然后建树,求出树的直径,在直径起点终点加一条边去的桥最多, #pragma comment(linker, "/STACK:10240 ...

  2. hdu 2242双联通分量+树形dp

    /*先求出双联通缩点,然后进行树形dp*/ #include<stdio.h> #include<string.h> #include<math.h> #defin ...

  3. 洛谷 P2515 [HAOI2010]软件安装(缩点+树形dp)

    题面 luogu 题解 缩点+树形dp 依赖关系可以看作有向边 因为有环,先缩点 缩点后,有可能图不联通. 我们可以新建一个结点连接每个联通块. 然后就是树形dp了 Code #include< ...

  4. poj 3694双联通缩点+LCA

    题意:给你一个无向连通图,每次加一条边后,问图中桥的数目. 思路:先将图进行双联通缩点,则缩点后图的边就是桥,然后dfs记录节点深度,给出(u,v)使其节点深度先降到同一等级,然后同时降等级直到汇合到 ...

  5. 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)

    layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...

  6. Codeforces 1000 组合数可行线段倒dp 边双联通缩点求树直径

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) using namespace std ...

  7. HDU 2242 连通分量缩点+树形dp

    题目大意是: 所有点在一个连通图上,希望去掉一条边得到两个连通图,且两个图上所有点的权值的差最小,如果没有割边,则输出impossible 这道题需要先利用tarjan算法将在同一连通分量中的点缩成一 ...

  8. HDU 1520.Anniversary party 基础的树形dp

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. HDU 3586 Information Disturbing(二分+树形dp)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...

随机推荐

  1. HDU 5533/ 2015长春区域 G.Dancing Stars on Me 暴力

    Dancing Stars on Me Problem Description The sky was brushed clean by the wind and the stars were col ...

  2. Fisher 线性判别

    Multiplying both sides of this result by wT and adding w0, and making use of y(x)=wTx+w0 and  y(xΓ)= ...

  3. web.xml中load-on-startup

    <servlet> <servlet-name>SystemInit</servlet-name> <servlet-class>sjgl.system ...

  4. oen /var/run/nginx.pid failed

    nginx: [error] open() "/var/run/nginx.pid" failed (2: No such file or directory) [root@TES ...

  5. EasyUI DataGrid组织事件冒泡

    在事件内部需要阻止的地方添加如下代码 ]; 解释: arguments.callee是获得我自定义的事件处理方法OnSelected的方法体. .caller是获得调用OnSelected的上层方法( ...

  6. B - Link/Cut Tree

    Problem description Programmer Rostislav got seriously interested in the Link/Cut Tree data structur ...

  7. RPC与REST

    RPC与REST (摘自网络,个人理解)

  8. 3A课程笔记分享_StudyJams_2017

    课程3A-面向对象编程(上) 概述 面向对象的思想在当今的软件开发中占据着主导地位. Java是一门完全面向对象的语言,是一种天然的分布式互联网软件的开发语言,在当今企业级应用中占据绝对领先地位,也是 ...

  9. mysqlslap: Error when connecting to server: 2001 Can't create UNIX socket (24) 解决方法

    在用mysqlslap对mysql进行压力测试遇到mysqlslap: Error when connecting to server: 2001 Can't create UNIX socket ( ...

  10. ★Java面向对象(一)——————————基本概念

    package boll; /* 用Java语言对现实生活中的事物进行描述. 通过类的形式来体现, 怎么描述呢? 对于事物的描述通常只有两个方面,一个是属性,一个是行为. 只要明确该事物的行为和属性并 ...