本来就是自己负责图论,结果水了= =

题目其实很裸,就是求桥的数量,只是要新加上一条边罢了。做法:先缩点、再在树上搜最长链(第一场多校的hdu 4607Park Visit就考了最长链,小样,套个马甲以为就认不出你了),加边后求桥数就可以了。

犯了一大三小四个错误-_-

竟然真的去套模板求桥数了。。竟然没注意到树边都是桥,用树边减链边就可以了。

然后,重新建图的Build()把n传进去了。

再然后,发现读数据从0~m-1,Build()里竟然是1~m。

再再然后,原来存边的su[],sv[]数组开成了 MAXN。

 #pragma comment(linker, "/STACK:10240000000000,10240000000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std; const int MAXM=;
const int MAXN=; struct Edge{
int u,v,next;
int vis;
Edge(){}
Edge(int _u,int _v,int _next):u(_u),v(_v),next(_next),vis(){}
}edge[MAXM<<]; int brige[MAXM<<]; struct Q{
int p,c;
}q[MAXN]; int head[MAXN],tol;
int stk[MAXN],top;
int low[MAXN],dfn[MAXN],TT;
int belong[MAXN],scc; int vis[MAXN]; int su[MAXM],sv[MAXM]; int son; void init()
{
tol=;
memset(head,-,sizeof(head));
} void add(int u,int v)
{
edge[tol]=Edge(u,v,head[u]);
head[u]=tol++;
} void tarjan(int u)
{
int v;
stk[++top]=u;
low[u]=dfn[u]=++TT;
for(int i=head[u];i!=-;i=edge[i].next)
{
if(edge[i].vis)
continue;
edge[i].vis=edge[i^].vis=; v=edge[i].v;
if(!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}else {
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u]){
scc++;
do{
v=stk[top--];
//ins[v]=0;
belong[v]=scc;
}while(v!=u);
}
} void Build(int m)
{
init();
for(int i=;i<m;i++)
{
int u=su[i],v=sv[i];
if(belong[u]!=belong[v]){
add(belong[u],belong[v]);
add(belong[v],belong[u]);
}
}
} int bfs(int x)
{
int l,r,u; l=r=;
q[r].p=x;
q[r++].c=;
memset(vis,,sizeof(vis));
vis[x]++;
while(l<r)
{
u=q[l].p;
int c=q[l++].c;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(!vis[v]){
q[r].p=v;
q[r++].c=c+;
vis[v]=;
}
}
}
return u;
} int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
if(!n&&!m)
return ;
init();
for(int i=;i<m;i++)
{
scanf("%d%d",&su[i],&sv[i]);
add(su[i],sv[i]);
add(sv[i],su[i]);
}
scc=;top=;TT=;
memset(dfn,,sizeof(dfn));
for(int i=;i<=n;i++)
if(!dfn[i])
tarjan(i); Build(m); int u=bfs();
int v=bfs(u);
printf("%d\n",scc-q[scc-].c);
}
return ;
}
/*
8 9
1 2
2 3
3 4
4 1
5 6
6 7
7 8
8 5
4 5 8 8
1 2
2 3
3 4
5 6
6 7
7 8
8 5
4 5 8 8
1 2
2 3
3 4
2 5
5 6
6 7
7 8
8 6 8 9
1 2
2 3
3 4
2 5
5 6
6 7
7 8
8 6
4 5 7 8
1 2
2 3
3 4
4 5
5 6
6 1
3 7
7 4
*/

hdu 4612 Warm up(缩点+树上最长链)的更多相关文章

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

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

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

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

  3. hdu 4607 Park Visit(树上最长链)

    求树上最长链:两遍搜索. 第一次从树上任意点开始,最远点必然是某一条最长链上的端点u. 第二次从u开始,最远点即该最长链的另一端点. 先在最长链上走,不足再去走支链. 把询问数m错打成n,狠狠wa了一 ...

  4. [HDU4607]Park Visit(树上最长链)

    HDU#4607. Park Visit 题目描述 Claire and her little friend, ykwd, are travelling in Shevchenko's Park! T ...

  5. HDU 4612 Warm up tarjan缩环+求最长链

    Warm up Problem Description   N planets are connected by M bidirectional channels that allow instant ...

  6. hdu 4612 Warm up 双连通缩点+树的直径

    首先双连通缩点建立新图(顺带求原图的总的桥数,事实上因为原图是一个强连通图,所以桥就等于缩点后的边) 此时得到的图类似树结构,对于新图求一次直径,也就是最长链. 我们新建的边就一定是连接这条最长链的首 ...

  7. HDU4612 Warm up —— 边双联通分量 + 重边 + 缩点 + 树上最长路

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4612 Warm up Time Limit: 10000/5000 MS (Java/Ot ...

  8. HDU 4612——Warm up——————【边双连通分量、树的直径】

    Warm up Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Stat ...

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

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

随机推荐

  1. C#根据日期DateTime和持续时间int找到日期

    protected DateTime GetFinish(DateTime start, int duration) { return start.AddDays(duration); } prote ...

  2. js函数:setInterval()/clearInterval()——js网页计时器

    一.setInterval()/clearInterval()技术学习 都是window对象的方法,可以直接使用. setInterval(function(){},1000);:每1000毫秒执行一 ...

  3. PAT-乙级-1055. 集体照 (25)

    1055. 集体照 (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 拍集体照时队形很重要,这里对给定的N ...

  4. [nowCoder] 子数组最大乘积

    给定一个double类型的数组arr,其中的元素可正可负可0,返回子数组累乘的最大乘积.例如arr=[-2.5,4,0,3,0.5,8,-1],子数组[3,0.5,8]累乘可以获得最大的乘积12,所以 ...

  5. EBP的妙用[无法使用ESP定律时]

    1.了解EBP寄存器 在寄存器里面有很多寄存器虽然他们的功能和使用没有任何的区别,但是在长期的编程和使用 中,在程序员习惯中已经默认的给每个寄存器赋上了特殊的含义,比如:EAX一般用来做返回值,ECX ...

  6. Aizu 2325 Mysterious Maze

    走迷宫 ~ 不同的是题目给了你转向的方向序列 dis[x][y]表示到(x,y) 使用了最少的转向次数 #include<cstdio> #include<cstring> # ...

  7. spoj 147

    dfs枚举真值 #include <cstdio> #include <cstring> #include <cstdlib> #include <stack ...

  8. Installing Lua in Mac

    Lua is distributed in source form. You need to build it before using it. Building Lua should be stra ...

  9. [转载]Spring Annotation Based Configuration

    Annotation injection is performed before XML injection, thus the latter configuration will override ...

  10. Unique Binary Search Tree II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...