Your friend is developing a computer game. He has already decided how the game world should look like — it should consist of n locations connected by m 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 s and end in location t, 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 s to t 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 s or as t.
The first line contains two integers n and m (2≤n≤3⋅105, n−1≤m≤3⋅105) — the number of locations and passages, respectively.
Then m lines follow, each containing two integers x and y (1≤x,y≤n, x≠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.
Print one integer — the maximum number of bosses your friend can place, considering all possible choices for s and t.
5 5 1 2 2 3 3 1 4 1 5 2
2
4 3 1 2 4 3 3 2
3
题意:n个点m条边的无向图, 对于任意的两个点, 最多有多少条特殊边, 特殊边就是去掉这条边这两个点就无法互达(割边)
思路:tarjan缩点完,建图跑最长链(双dfs
#include#define ll long long#define pb push_backusing namespace std;const int maxn=300010;vector mp[maxn];vector G[maxn];int stc[maxn],scc[maxn],dfn[maxn],low[maxn],dis[maxn];int top,times,scc_cnt;void tarjan(int u,int fa){ stc[++top]=u; dfn[u]=low[u]=++times; for(auto x:mp[u]) { if(x==fa) continue; if(!dfn[x]) { tarjan(x,u); low[u]=min(low[u],low[x]); } else low[u]=min(low[u],dfn[x]); } if(dfn[u]==low[u]) { scc_cnt++; while(true) { int x=stc[top--]; scc[x]=scc_cnt; if(x==u) break; } }}void dfs(int u,int fa){ dis[u]=dis[fa]+1; for(auto x:G[u]) { if(x==fa) continue; dfs(x,u); }}int main(){ int n,m,u,v; scanf("%d %d",&n,&m); for(int i=1;i<=m;i++) { scanf("%d %d",&u,&v); mp[u].pb(v); mp[v].pb(u); } tarjan(1,0); for(int i=1;i<=n;i++) for(auto x:mp[i]) if(scc[x]!=scc[i]) G[scc[i]].pb(scc[x]); int s=0; dfs(1,0); for(int i=1;i<=scc_cnt;i++) if(dis[i]>dis[s]) s=i; dfs(s,0); int ans=0; for(int i=1;i<=scc_cnt;i++) ans=max(ans,dis[i]); cout< <<"\n"; return 0;}
PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~