博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codefoeces-1000E We Need More Bosses(tarjan+最长链)
阅读量:5345 次
发布时间:2019-06-15

本文共 3473 字,大约阅读时间需要 11 分钟。

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 n locations connected by 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.

Input

The first line contains two integers n and m (2n3105, n1m3105) — the number of locations and passages, respectively.

Then m lines follow, each containing two integers x and y (1x,yn, xy) 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 s and t.

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

题意: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;}
View Code

 

 

 

PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

转载于:https://www.cnblogs.com/MengX/p/9439276.html

你可能感兴趣的文章
上海淮海中路上苹果旗舰店门口欲砸一台IMAC电脑维权
查看>>
Google透露Android Market恶意程序扫描服务
查看>>
给mysql数据库字段值拼接前缀或后缀。 concat()函数
查看>>
迷宫问题
查看>>
【FZSZ2017暑假提高组Day9】猜数游戏(number)
查看>>
泛型子类_属性类型_重写方法类型
查看>>
对闭包的理解
查看>>
练习10-1 使用递归函数计算1到n之和(10 分
查看>>
Oracle MySQL yaSSL 不明细节缓冲区溢出漏洞2
查看>>
Code Snippet
查看>>
zoj 1232 Adventure of Super Mario
查看>>
组合数学 UVa 11538 Chess Queen
查看>>
oracle job
查看>>
Redis常用命令
查看>>
[转载]电脑小绝技
查看>>
windos系统定时执行批处理文件(bat文件)
查看>>
thinkphp如何实现伪静态
查看>>
BZOJ 2243: [SDOI2011]染色( 树链剖分 )
查看>>
BZOJ 1925: [Sdoi2010]地精部落( dp )
查看>>
c++中的string常用函数用法总结!
查看>>