经过思考后,很明显,我们可以看出应该是求出两条最长的链,链是指挂在连通块上的aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbMAAAEOCAIAAACM2257AAATBElEQVR4nO3dL3MqSRfH8YitW8isQ1KrIlP7CpDIyMhIJPK6yMgrkbwEJBK5Mi/hkUgkkkeQmkyaoek/p7tP93w/bmth5vQ5Pb8LSRgezgCAnx5KFwAA6pCMAGAiGQHARDICgIlkBAATyQgAJpIRAEwkIwCYSEYAMJGMAGAiGQHARDICgIlkBAATyQgAJpIRAEwkIwCYSEYAMJGMAGAiGQHARDICgIlkBAATyQgAJpIRAEwkIwCYEibjww3pzggAIuRz6lYgEpEAaiEZT+6ZSD4C0EwmmMIykXwEoJNAJMXHIuEIQJXYPPINO8IRgH5RYRQTcIQjALXCk0gk1whHAApJJmPZ4wCAFBVxRjgCUCUkg1IEGeEIQI+0ybjb7Waz2cPDw2w2W61Wh8PB/bABhQGACIHfmVgefInFzq9fv/7555/NZhN/ZABIJ20yfnx8XD/+r7/+cjy4b20AICI2GR2ftd1un5+fu2fdeltNMgLQIFMyns/n0+nUPWu9XrscnHAEUIRf9ETGVvfE+Xye6BQAEK9MMlqeSzICKK5MMv7777+JTgEA8cok42QySXQKAIiXLxl3ux3vpgFUIV8yTqdTkhFAFfIlo+NzSUYAxWlPRoJSGwaEMYhKN68rweWJ9quOa7IIJoIRyvcZGJcnBl+EXKIp0PyxYY6dUSTjLb5rHw9aPRIM9Ja099qxPNHlyAX5tqUZdHUMmOld+e7pffcpLscUnGiwgI5VIX75I29gFUJ3/ehmqiUZ43udZOCefGvWQ3xdrTaqanI7vf2x5vuGLMvjU7c4yS7wJLsiWenKrrcnjRHZpbXvcy9iyejb1phDSXHcLqnlWaxvB6o7Czqpt5/a/SxIuC+WZ00mk+5hr6+vlkOFrEOa195Kp8gyc54rwxqbV3B3Fdy6qSX5cd7gE7vvhHl9fe3f37u6hnptvnQSLST+sKrO2B4lm8elHsGz5Jf2Fx2+T5dYUUlezUkn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YuRoDYGIeMwezSIQEYxNgFEgDeGFDQEAJpIRAEwkIwCYSEYAMJGMAGAiGQHARDICgIlkBAATyQgAJpIRAEwkIwCYSEYAMJGMAGAiGQHARDICgIlkBAATyQgAJpIRAEwkIwCYSEYAMP0fGEFwisNJJ+QAAAAASUVORK5CYII=" alt="" />

1,5可以称作一条链,但是,图里会有连通块,也就是环或者几个环相交在一起,这时就很难求链。这时,需要进行缩点。

缩点是把连通块变成一个点,大概是通过tarjan求出桥,也就是删掉这条边之后,图变得不连通,求出桥之后,把这些边删掉

然后通过dfs把每个连通块标记成一个颜色,也就是一个新的点,这时,所有的点都不联通,都有了新的编号,那些割点也是

然后就两次bfs求树的直径就可以了

#include<iostream>
#include<cstring>
#include<Vector>
#include<Queue>
#include<cstdio>
#define N 10010
using namespace std;
int n,m,all_edge=-,time,x,cnt;
int to[*N],next[*N],head[*N],color[*N],num[N],d[N],low[N],dfn[N],cut[*N];
queue<int>q;
vector<int>graph[N];
inline int min(int x,int y){return x<y?x:y;}
inline void ins(int u,int v){to[++all_edge]=v;next[all_edge]=head[u];head[u]=all_edge;}
inline void insert(int u,int v){ins(u,v);ins(v,u);}
void tarjan(int u,int fa)
{
dfn[u]=low[u]=++time;
for(int i=head[u];~i;i=next[i])
{
int v=to[i];
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u]){cut[i]=cut[i^]=;}
} else if(v!=fa)low[u]=min(low[u],low[v]);
}
}
void dfs(int u)
{
color[u]=cnt;
for(int i=head[u];~i;i=next[i])
{
int v=to[i];
if(!color[v]&&!cut[i]) dfs(v);
}
}
void bfs(int point)
{
int MAX=;x=;
memset(d,-,sizeof(d));
q.push(point);
d[point]=;
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=;i<graph[u].size();i++)
{
int v=graph[u][i];
if(d[v]==-)
{
d[v]=d[u]+;
q.push(v);
if(d[v]>MAX){MAX=d[v];x=v;}
}
}
}
cout<<num[x]<<" ";
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
cin>>n>>m;memset(head,-,sizeof(head));
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
insert(u,v);
}
tarjan(,-);
for(int i=;i<=n;i++) if(!color[i])
{
++cnt;
num[cnt]=i;
dfs(i);
}
for(int i=;i<all_edge;i+=) if(cut[i])
{
int u=color[to[i^]],v=color[to[i]];
graph[u].push_back(v);
graph[v].push_back(u);
}
/* cout<<"---------"<<endl;
for(int i=1;i<=cnt;i++)
{
cout<<"i="<<i<<":";
for(int j=0;j<graph[i].size();j++)
{
cout<<graph[i][j]<<" ";
}
cout<<endl;
}
cout<<"---------"<<endl;*/
bfs();
bfs(x);
fclose(stdin);
fclose(stdout);
return ;
}

100114J的更多相关文章

随机推荐

  1. UIPanelResetHelper(UIScrollView滚动复位)

    原理 如果我们的UI中有滑动列表,并且列表比较长,那么不知道你们是否有这样需求,每次页面打开时,列表的滑动状态都恢复到默认状态. 如果要复位,其实就是修改UIPanel 的属性到初始状态.此组件做的工 ...

  2. C# 事件编程在游戏开发的应用

    2D碰撞检测:http://wenku.baidu.com/view/45544cfcfab069dc50220145.html 1.Action System.Action 等于快捷创建一个委托 2 ...

  3. 收集入侵Windows系统的证据

    随着网络的不断扩大,网络安全更加会成为人们的一个焦点,同时也成为是否能进一步投入到更深更广领域的一个基石.当然网络的安全也是一个动态的概念,世界上没有绝对安全的网络,只有相对安全的网络.相对安全环境的 ...

  4. 如何在CTF中当搅屎棍

    论如何在CTF比赛中搅屎 0×00 前言 不能搅屎的CTF不是好CTF,不能搅屎的题目不是好题目. 我很赞成phithon神的一句话,"比赛就是和他人竞争的过程,通过各种手段阻止对手拿分我觉 ...

  5. luogu10125回文数[noip1999 Day1 T1]

    题目描述 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数. 例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数. 又如:对于10进制数 ...

  6. angular的uiRouter服务学习(5) --- $state.includes()方法

    $state.includes方法用于判断当前激活状态是否是指定的状态或者是指定状态的子状态. $state.includes(stateOrName,params,options) $state.i ...

  7. gedit脚本

    明天ctsc,赶紧学了一下gedit的配置 以下假设你只在/home/zzq下写代码(用户名自己改) 首先在/home/zzq下建一个runner.sh,内容如下: #!/bin/bash echo ...

  8. wechat开发

    1.easywechat安装 2.weichat打通服务器 function getTest(Request $request){ $token = 'zhenhaokeji'; $data = $r ...

  9. IE 和Firefox的js兼容性总结

    IE 和Firefox的js兼容性总结 12 August 2010 11:39 Thursday by 小屋 标签: 浏览器 方法 属性 IT 写法 一.函数和方法差异 1 . getYear()方 ...

  10. Windows 8.1 应用开发 – 触控操作

    与WPF相同Windows 8.1应用中也具有高级触控操作(Manipulation),其中包含了三种常见的触屏手势:平移.缩放.旋转,通过以下四种事件可为控件实现各种触控操作:Manipulatio ...