100114J
经过思考后,很明显,我们可以看出应该是求出两条最长的链,链是指挂在连通块上的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+T5dYUU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iEUIYuRoDYGIeMwezSIQEYxNgFEgDeGFDQEAJpIRAEwkIwCYSEYAMJGMAGAiGQHARDICgIlkBAATyQgAJpIRAEwkIwCYSEYAMJGMAGAiGQHARDICgIlkBAATyQgAJpIRAEwkIwCYSEYAMP0fGEFwisNJJ+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的更多相关文章
随机推荐
- 异常总结<经典例题>
public class Test1 { public static void main(String[] args) { try { add(1); System.out.println(" ...
- Flex数据绑定陷阱(一)
Flex数据绑定陷阱:常见的误用和错误 当构建Flex或者Adobe AIR程序时,将一个对象的值自动的传递给另一个对象这种处理是数据绑定最常 用并最有用的特征之一. 尽管如此,同时数据绑定会减缓程序 ...
- tyvj[1089]smrtfun
描述 现有N个物品,第i个物品有两个属性A_i和B_i.在其中选取若干个物品,使得sum{A_i + B_i}最大,同时sum{A_i},sum{B_i}均非负(sum{}表示求和). 输入格式 ...
- Js经典相册
Js经典相册 点击下载
- SPM12manual,统计部分(8-10)笔记
fMRI model specifictaion GLM based 包括以下步骤:①明确GLM设计矩阵:②用经典或贝叶斯方法估计GLM参数:③利用对比向量检查结果,生成统计参数图(SPMs)以及后验 ...
- BZOJ 1096 【ZJOI2007】 仓库建设
Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在露天,以节省费用.突然有一天, ...
- IE6/IE7/IE8/Firefox/Chrome/Safari的CSS hack兼容一览表
浏览器兼容问题一直是前段开发工程师比较头痛的问题,熟悉了里面的规则也就变得简单了,这里有一份资料可以分享给大家,大家平时开发过程中遵循这个规律的话,会变得轻松多了: 各浏览器CSS hack兼容表: ...
- 1002. A+B for Polynomials
1002. A+B for Polynomials (25) This time, you are supposed to find A+B where A and B are two polynom ...
- 浅谈设计模式--组合模式(Composite Pattern)
组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...
- 在项目中代替DevExpress(一)
从Delphi时代开始一直都是DevExpress系列控件的忠实用户,到现在已经有10多个年头了.DevExpress里面的控件基本从头到尾都用过一次,而且也开发过很多基于DevExpress的子控件 ...