CF1000E We Need More Bosses (tarjan + dfs)

E. We Need More Bosses

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

5 5
1 2
2 3
3 1
4 1
5 2

Output

Copy

2

Input

Copy

4 3
1 2
4 3
3 2

Output

Copy

3

题解:

给一个无向联通图 , 求最长路径  S T必须经过的长度。可以 tarjan缩点,dfs两遍求树的直径。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=3e5+10;
struct E{int x,y,next;} mm[N<<1][2];
int h[N][2],dfn[N],low[N],a[N],f[N][2],len[2],stack[N];
int n,m,top,num,cnt,mn,rt;
inline void ins(int x,int y,int s){
	int now=++len[s]; mm[now][s].x=x;mm[now][s].y=y;mm[now][s].next=h[x][s];h[x][s]=len[s];
}
void tarjan(int x,int fa){
	low[x]=dfn[x]=++num;f[x][0]=f[x][1]=1;stack[++top]=x;
	
	for(int k=h[x][0] ; k ;k=mm[k][0].next){
		int y=mm[k][0].y;if(y==fa) continue;
		if(!f[y][0]) tarjan(y,x),low[x]=min(low[x],low[y]);
		else if(f[y][1]) low[x]=min(low[x],dfn[y]); 
	}
	if(dfn[x]==low[x]){
		int y;++cnt;
		do{y=stack[top--];f[y][1]=0;a[y]=cnt;
		}while(x!=y);
	} 
}
void dfs(int x,int fa,int val){
	if(val>mn) mn=val,rt=x;
	for(int k=h[x][1];k;k=mm[k][1].next){
		int y=mm[k][1].y;if(y==fa) continue;
		dfs(y,x,val+1);
	}
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,x,y;i<=m;i++)
		scanf("%d%d",&x,&y),ins(x,y,0),ins(y,x,0);
	tarjan(1,0);
	for(int x=1,y;x<=n;x++)
		for(int k=h[x][0] ; k ; k=mm[k][0].next){
			y=mm[k][0].y;if(a[x] != a[y]) ins(a[x] , a[y] ,1);
		}
	rt=1;mn=0;
	dfs(1,0,0);
	dfs(rt,0,0);
	printf("%d\n",mn);
	return 0;
} 

 

 

 

转载于:https://www.cnblogs.com/Exception2017/p/10252106.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值