题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612

  简单图论题,先求图的边双连通分量,注意,此题有重边(admin还逗比的说没有重边),在用targan算法求的时候,处理反向边需要标记边,然后缩点,在树上求最长链。。

  此题在比赛的时候,我的模板数组开小,WA一下午,sd。。。。

 //STATUS:C++_AC_734MS_37312KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=,M=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Edge{
int u,v;
}e[M],e2[M];
int first2[N],next2[M],mt2;
//bool iscut[M];
int first[N],next[M],pre[N],low[N],bccno[N];
int n,m,mt,bcnt,dfs_clock;
stack<int> s; int T;
int vis[N]; void adde(int a,int b)
{
e[mt].u=a;e[mt].v=b;
next[mt]=first[a];first[a]=mt++;
e[mt].u=b;e[mt].v=a;
next[mt]=first[b];first[b]=mt++;
}
void adde2(int a,int b)
{
e2[mt2].u=a;e2[mt2].v=b;
next2[mt2]=first2[a];first2[a]=mt2++;
e2[mt2].u=b;e2[mt2].v=a;
next2[mt2]=first2[b];first2[b]=mt2++;
} void dfs(int u,int fa)
{
int i,v;
pre[u]=low[u]=++dfs_clock;
s.push(u);
int cnt=;
for(i=first[u];i!=-;i=next[i]){
v=e[i].v;
if(!pre[v]){
dfs(v,u);
low[u]=Min(low[u],low[v]);
// if(low[v]>pre[u])iscut[i]=true; //存在割边
}
else if(fa==v){ //反向边更新
if(cnt)low[u]=Min(low[u],pre[v]);
cnt++;
}
else low[u]=Min(low[u],pre[v]);
}
if(low[u]==pre[u]){ //充分必要条件
int x=-;
bcnt++;
while(x!=u){
x=s.top();s.pop();
bccno[x]=bcnt;
}
}
} void find_bcc()
{
int i;
bcnt=dfs_clock=;//mem(iscut,0);
mem(pre,);mem(bccno,);
for(i=;i<=n;i++){
if(!pre[i])dfs(i,-);
}
} int hig;
int dfs2(int u,int p)
{
int max1=,max2=;
for (int i=first2[u];i!=-;i=next2[i])
{
int v=e2[i].v;
if (v==p) continue;
int tmp=dfs2(v,u)+;
if (max1<tmp) max2=max1,max1=tmp;
else if (max2<tmp) max2=tmp;
}
hig=Max(hig,max1+max2);
return max1;
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,a,b,tot;
while(~scanf("%d%d",&n,&m) && (n || m))
{
mt=;mem(first,-);
for(i=;i<m;i++){
scanf("%d%d",&a,&b);
adde(a,b);
} find_bcc();
mem(first2,-);mt2=;
for(i=;i<mt;i+=){
if(bccno[e[i].u]!=bccno[e[i].v]){
adde2(bccno[e[i].u],bccno[e[i].v]);
}
}
hig=;
dfs2(,-); printf("%d\n",bcnt--hig);
}
return ;
}

HDU-4612 Warm up 边双连通分量+缩点+最长链的更多相关文章

  1. HDU 4612 Warm up (边双连通分量+缩点+树的直径)

    <题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...

  2. HDU 4612 Warm up(双连通分量缩点+求树的直径)

    思路:强连通分量缩点,建立一颗新的树,然后求树的最长直径,然后加上一条边能够去掉的桥数,就是直径的长度. 树的直径长度的求法:两次bfs可以求,第一次随便找一个点u,然后进行bfs搜到的最后一个点v, ...

  3. HDU 4612 Warm up (边双连通分量+DP最长链)

    [题意]给定一个无向图,问在允许加一条边的情况下,最少的桥的个数 [思路]对图做一遍Tarjan找出桥,把双连通分量缩成一个点,这样原图就成了一棵树,树的每条边都是桥.然后在树中求最长链,这样在两端点 ...

  4. hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Su ...

  5. hdu4612 Warm up[边双连通分量缩点+树的直径]

    给你一个连通图,你可以任意加一条边,最小化桥的数目. 添加一条边,发现在边双内是不会减少桥的.只有在边双与边双之间加边才有效.于是,跑一遍边双并缩点,然后就变成一棵树,这样要加一条非树边,路径上的点( ...

  6. 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 ...

  7. hdu 4612 Warm up 双连通+树形dp思想

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total S ...

  8. HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)

    Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...

  9. Hdu 4612 Warm up (双连通分支+树的直径)

    题目链接: Hdu 4612 Warm up 题目描述: 给一个无向连通图,问加上一条边后,桥的数目最少会有几个? 解题思路: 题目描述很清楚,题目也很裸,就是一眼看穿怎么做的,先求出来双连通分量,然 ...

随机推荐

  1. [扫描线]POJ2932 Coneology

    题意:有n个圆 依次给了半径和圆心坐标  保证输入的圆不相交(只有 相离 和 内含/外含 的情况)   问 有几个圆 不内含在其他圆中,并分别列出这几个圆的编号(1~n) (n的范围是[1, 4000 ...

  2. [topcoder]AvoidRoads

    二维动态规划.和某一道leetcode的题目差不多.就是多了blocks的数组或集合. 本次解题的心得有:1.根据题意使用集合表示阻碍:2.使用字符串的形式表示整数的pair,简洁明了:3.p1到p2 ...

  3. linux2.6中的工作队列接口 workqueue_struct

    http://blog.csdn.net/sfrysh/article/details/5801786 工作队列接口 工作队列接口是在2.5的开发过程中引入的,用于取代任务队列接口(用于调 度内核任务 ...

  4. 123. Best Time to Buy and Sell Stock III

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  5. 转:JavaScript中函数与对象的关系

    来自:http://www.nowamagic.net/javascript/js_RelationOfFunctionAndObject.php 在ajax兴起以前,很多人写JavaScript可以 ...

  6. Windows平台下的session0创建进程的问题与解决办法

    很多博客都有记载如何在session0下创建进程的办法,也就是使用CreateProcessAsUser.但是这个要求服务的进程有SE_INCREASE_QUOTA_NAME和SE_ASSIGNPRI ...

  7. C#.Net 如何动态加载与卸载程序集(.dll或者.exe)4-----Net下的AppDomain编程 [摘录]

    最近在对AppDomain编程时遇到了一个问题,卸载AppDomain后,在内存中还保留它加载的DLL的数据,所以即使卸载掉AppDomain,还是无法更新它加载的DLL.看来只有关闭整个进程来更新D ...

  8. 转载:C++ vector 类学习笔记

    声明:本文转载自http://blog.csdn.net/whz_zb/article/details/6827999 vector简介 vector是STL中最常见的容器,它是一种顺序容器,支持随机 ...

  9. C++ 空类默认产生成员函数

    class Empty { Empty(){...} //默认构造函数 ~Empty(){...} //默认析构函数 Empty(const Empty&){...} //拷贝构造函数 Emp ...

  10. poj 2503 Babelfish (查找 map)

    题目:http://poj.org/problem?id=2503 不知道为什么 poj  的 数据好像不是100000,跟周赛的不一样 2000MS的代码: #include <iostrea ...