Your friend is developing a computer game. He has already decided how the game world should look like — it should consist of nn locations connected by mm two-way passages. The passages are designed in such a way that it should be possible to get from any location to any other location.

Of course, some passages should be guarded by the monsters (if you just can go everywhere without any difficulties, then it's not fun, right?). Some crucial passages will be guarded by really fearsome monsters, requiring the hero to prepare for battle and designing his own tactics of defeating them (commonly these kinds of monsters are called bosses). And your friend wants you to help him place these bosses.

The game will start in location ss and end in location tt, but these locations are not chosen yet. After choosing these locations, your friend will place a boss in each passage such that it is impossible to get from ss to tt without using this passage. Your friend wants to place as much bosses as possible (because more challenges means more fun, right?), so he asks you to help him determine the maximum possible number of bosses, considering that any location can be chosen as ss or as tt.

Input

The first line contains two integers nn and mm (2≤n≤3⋅1052≤n≤3⋅105, n−1≤m≤3⋅105n−1≤m≤3⋅105) — the number of locations and passages, respectively.

Then mm lines follow, each containing two integers xx and yy (1≤x,y≤n1≤x,y≤n, x≠yx≠y) describing the endpoints of one of the passages.

It is guaranteed that there is no pair of locations directly connected by two or more passages, and that any location is reachable from any other location.

Output

Print one integer — the maximum number of bosses your friend can place, considering all possible choices for ss and tt.

Examples

Input
5 5
1 2
2 3
3 1
4 1
5 2
Output
2
Input
4 3
1 2
4 3
3 2
Output
3

题意:一条路径上必经的边为关键边,现在让你找一条路径,使得其关键边最多,输出最多的数量。

思路:如果一条路径上面有环,那么这个环的任意一条边都不是关键边。所以我们缩点,那么就算在一棵树上找最多的关键边,显然就算求直径。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
int From[maxn],Laxt[maxn],To[maxn<<],Next[maxn<<],cnt;
int low[maxn],dfn[maxn],times,q[maxn],head,scc_cnt,scc[maxn];
vector<int>G[maxn];
int dis[maxn],S,T,ans;
void add(int u,int v)
{
Next[++cnt]=Laxt[u]; From[cnt]=u;
Laxt[u]=cnt; To[cnt]=v;
}
void tarjan(int u,int fa)
{
dfn[u]=low[u]=++times;
q[++head]=u;
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]==fa) continue;
if(!dfn[To[i]]) {
tarjan(To[i],u);
low[u]=min(low[u],low[To[i]]);
}
else low[u]=min(low[u],dfn[To[i]]);
}
if(low[u]==dfn[u]){
scc_cnt++;
while(true){
int x=q[head--];
scc[x]=scc_cnt;
if(x==u) break;
}
}
}
void dfs(int u,int f)
{
dis[u]=dis[f]+;
for(int i=;i<G[u].size();i++){
if(G[u][i]!=f) dfs(G[u][i],u);
}
}
int main()
{
int N,M,u,v,i,j;
scanf("%d%d",&N,&M);
for(i=;i<=M;i++){
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
tarjan(,);
for(i=;i<=N;i++){
for(j=Laxt[i];j;j=Next[j]){
if(scc[i]!=scc[To[j]])
G[scc[i]].push_back(scc[To[j]]);
}
}
dfs(,);
for(i=;i<=scc_cnt;i++) if(dis[i]>dis[S]) S=i;
dfs(S,);
for(i=;i<=scc_cnt;i++) ans=max(ans,dis[i]-);
printf("%d\n",ans);
return ;
}

对于无向图的缩点:

void tarjan(int u,int fa)
{
dfn[u]=low[u]=++times;
q[++head]=u;
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]==fa) continue;
if(!dfn[To[i]]) {
tarjan(To[i],u);
low[u]=min(low[u],low[To[i]]);
}
else low[u]=min(low[u],dfn[To[i]]);
}
if(low[u]==dfn[u]){
scc_cnt++;
while(true){
int x=q[head--];
scc[x]=scc_cnt;
if(x==u) break;
}
}
}

对于有向图的缩点:二者唯一的区别就算有向图考虑横边,所以有个instack的判断。

void tarjan(int u)
{
instk[u]=;
q[++head]=u;
dfn[u]=low[u]=++times;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(!dfn[v]) {
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instk[v])low[u]=min(low[u],dfn[v]);//无向图与有向图的区别
}
if(dfn[u]==low[u]){
scc_cnt++;
while(true){
int x=q[head--];
scc[x]=scc_cnt;
V[scc_cnt]+=w[x];
instk[x]=;
if(x==u) break;
}
}
}

CodeForces - 1000E :We Need More Bosses(无向图缩点+树的直径)的更多相关文章

  1. hdu-4612(无向图缩点+树的直径)

    题意:给你n个点和m条边的无向图,问你如果多加一条边的话,那么这个图最少的桥是什么 解题思路:无向图缩点和树的直径,用并查集缩点: #include<iostream> #include& ...

  2. cf1000E We Need More Bosses (tarjan缩点+树的直径)

    题意:无向联通图,求一条最长的路径,路径长度定义为u到v必须经过的边的个数 如果把强联通分量都缩成一个点以后,每个点内部的边都是可替代的:而又因为这是个无向图,缩完点以后就是棵树,跑两遍dfs求直径即 ...

  3. Codeforces 1000E We Need More Bosses (边双连通+最长链)

    <题目链接> 题目大意:给定一个$n$个节点$m$条边的无向图,问你对任意两点,最多有多少条特殊边,特殊边指删除这条边后,这两个点不能够到达. 解题分析: 特殊变其实就是指割边,题意就是问 ...

  4. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  5. Codeforces 734E Anton and Tree(缩点+树的直径)

    题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ...

  6. HDU4612 Warm up 边双(重边)缩点+树的直径

    题意:一个连通无向图,问你增加一条边后,让原图桥边最少 分析:先边双缩点,因为连通,所以消环变树,每一个树边都是桥,现在让你增加一条边,让桥变少(即形成环) 所以我们选择一条树上最长的路径,连接两端, ...

  7. hdu4612 Warm up 缩点+树的直径

    题意抽象后为:给定一个无向图 问添加一条边的情况下最少能有多少个桥. 桥的定义:删除该边后原图变为多个连通块. 数据规模:点数N(2<=N<=200000),边数M(1<=M< ...

  8. F - Warm up HDU - 4612 tarjan缩点 + 树的直径 + 对tajan的再次理解

    题目链接:https://vjudge.net/contest/67418#problem/F 题目大意:给你一个图,让你加一条边,使得原图中的桥尽可能的小.(谢谢梁学长的帮忙) 我对重边,tarja ...

  9. HDU4612:Warm up(缩点+树的直径)

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

随机推荐

  1. VueJS样式绑定之内联样式v-bind:style

    我们可以在 v-bind:style 直接设置样式: 直接添加样式属性 HTML <!DOCTYPE html> <html> <head> <meta ch ...

  2. Robot framework 引入 Selenium2Library 类库:

    在用robotframework-selenium2library做web自动化测试时候,首先要将Selenium2Library导入到Test Suite中,在导入Selenium2Library时 ...

  3. Freebsd的ports命令

    安装 make clean 卸载 make deinstall 重装 make reinstall 清理 make clean 列出配置单 make config 恢复默认的配置单 make rmco ...

  4. wifi认证Portal开发系列(二):FreeRadius的安装和测试、关联Mysql

    注:本次安装是基于FreeRadius 3版本进行安装配置的,在配置Mysql的过程中,与2版本有些不同.操作系统是CentOS 7 一.准备工作 工具的安装 #安装rz.sz命令用于文件上传 yum ...

  5. 【Unity3D】【NGUI】Atlas的动态创建

    NGUI版本号:3.6.5 1.參见SZUIAtlasMakerRuntimeTest设置对应的值以上值须要提前设置好 2.没有检查是否atlas可以正确创建,自己可以改,增加返回值 3.代码都是在N ...

  6. 输出 pdf

    jar 包 :core-renderer.jar  iText-2.0.8.jar   iTextAsian.jar 方式1: import java.io.FileNotFoundException ...

  7. CGI的基本原理

    一.基本原理 CGI:通用网关接口(Common Gateway Interface)是一个Webserver主机提供信息服务的标准接口.通过CGI接口,Webserver就行获取client提交的信 ...

  8. EasyDarwin支持GB28181协议开发

    本文转自:http://blog.csdn.net/gavin1010/article/details/77926853 EasyGB28181服务器开发 背景 当前的安防行业,除了私有协议,普遍使用 ...

  9. vue 向后台提交数据

    新建template 表单 <template> <div class="add-group content"> <table class=" ...

  10. Cordova插件相关常用命令

    一,插件相关常用命令   1,查看所有已经安装的插件 1 cordova plugin ls 2,安装插件(以camera插件为例) 1 cordova plugin add cordova-plug ...