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;
} 

 

 

 

posted @ 2018-11-03 11:12  Exception2017  阅读(150)  评论(0编辑  收藏  举报