#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. U4704 函数

    U4704 函数 题目背景 设gcd(a,b)为a和b的最大公约数,xor(a,b)为a异或b的结果. 最大公约数 异或 题目描述 kkk总是把gcd写成xor.今天数学考试恰好出到了gcd(a,b) ...

  2. javase - 点餐系统

    public class OrderMsg { public static void main(String[] args) throws Exception { /** * 订餐人姓名.选择菜品.送 ...

  3. luogu3225 [HNOI2012]矿场搭建

    题目大意 给出一个有$n(n\leq 500)$个节点的无向图,一个满足条件的点集$V$会使得对于图中的每一个节点$u$,满足路径起点为$u$终点$v\in V$的路径集合$P_u$中总存在至少两条路 ...

  4. Android View的onTouch和onClick和onLongClick事件

    这三个事件的调用顺序是: onTouch->onLongClick->onClick 先看这三个事件的处理函数: public boolean onTouch(View v, Motion ...

  5. EOJ 1501/UVa The Blocks Problem

    Many areas of Computer Science use simple, abstract domains for both analytical and empirical studie ...

  6. Agri-Net(prim)

    http://poj.org/problem?id=1258 #include<stdio.h> #include<string.h> ; <<; int map[ ...

  7. Redis学习笔记(二):Redis集群

    集群通过分片(sharding)来进行数据共享,并提供复制和故障转移功能.   1.节点 一个节点就是一个运行在集群模式下的Redis服务器.启动Redis服务器时,通过判断cluster-enabl ...

  8. MSSQL:查看作业情况

    select j.name 'Job名',        j.description '描述',        j.ENABLED job_enabled,        cast(js.last_r ...

  9. 工具分享3:VMware 10虚拟机、MS-DOS 7.1、安装教程(MS-DOS环境安装)

    VMware 10工具下载地址: 网页下载链接:http://www.xp510.com/xiazai/ossoft/desktools/22610.html MS-DOS .10镜像下载地址: 网页 ...

  10. ACM_小明滚出去?(求逆序数)

    小明滚出去? Time Limit: 2000/1000ms (Java/Others) Problem Description: 老师:“小明,写一个排序算法”: 小明: void mysort(i ...