题目链接:传送门

思路:

强连通缩点,重建图,然后广搜找最长路径。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. const int maxn = 1e6+;
  7. int head[maxn],next[maxn],ver[maxn],tot;
  8. int low[maxn],num[maxn],tim,co[maxn],col,si[maxn];
  9. int vis[maxn],dis[maxn];
  10. int st[maxn],top,a[maxn],in[maxn],que[maxn],w,t;
  11. int xx[maxn],yy[maxn],nu[maxn],ans,m,n,vv[maxn];
  12. int MIN(int x,int y)
  13. {
  14. return x<y?x:y;
  15. }
  16. int MAX(int x,int y)
  17. {
  18. return x>y?x:y;
  19. }
  20. void Init()
  21. {
  22. memset(vis,,sizeof(vis));
  23. memset(in,,sizeof(in));
  24. memset(vv,,sizeof(vv));
  25. top=;tim=;tot=;col=;w=;t=;
  26. }
  27. void addedge(int u,int v)
  28. {
  29. ver[++tot]=v;next[tot]=head[u];head[u]=tot;
  30. }
  31. void Tarjan(int u)
  32. {
  33. low[u]=num[u]=++tim;
  34. st[++top]=u;
  35. for(int i=head[u];i;i=next[i]){
  36. int v=ver[i];
  37. if(!num[v]){
  38. Tarjan(v);
  39. low[u]=MIN(low[u],low[v]);
  40. }
  41. else if(!co[v]) low[u]=MIN(low[u],num[v]);
  42. }
  43. if(low[u]==num[u]){
  44. col++;
  45. co[u]=col;
  46. si[col]=a[u];
  47. vv[col]=MAX(vv[col],vis[u]);
  48. while(st[top]!=u){
  49. co[st[top]]=col;
  50. vv[col]=MAX(vv[col],vis[st[top]]);
  51. si[col]+=a[st[top]];
  52. top--;
  53. }
  54. top--;
  55. }
  56. }
  57. bool cmp(int x,int y)
  58. {
  59. if(xx[x]!=xx[y]) return xx[x]<xx[y];
  60. else return yy[x]<yy[y];
  61. }
  62. void Remove()
  63. {
  64. memset(head,,sizeof(head));
  65. tot=;
  66. for(int i=;i<=m;i++){
  67. nu[i]=i;
  68. xx[i]=co[xx[i]];
  69. yy[i]=co[yy[i]];
  70. }
  71. sort(nu+,nu+m+,cmp);
  72. }
  73. void Build()
  74. {
  75. for(int i=;i<=m;i++){
  76. int z=nu[i];
  77. if(xx[z]!=yy[z]&&(xx[z]!=xx[nu[i-]]||yy[z]!=yy[nu[i-]]))
  78. addedge(xx[z],yy[z]),in[yy[z]]++;
  79. }
  80. }
  81. int main(void)
  82. {
  83. int i,j,ss,pp,tp;
  84. scanf("%d%d",&n,&m);
  85. Init();
  86. for(i=;i<=m;i++){
  87. scanf("%d%d",&xx[i],&yy[i]);
  88. addedge(xx[i],yy[i]);
  89. }
  90. for(i=;i<=n;i++) scanf("%d",&a[i]);
  91. scanf("%d%d",&ss,&pp);
  92. for(i=;i<=pp;i++){
  93. scanf("%d",&tp);vis[tp]=;
  94. }
  95.  
  96. for(i=;i<=n;i++)
  97. if(!num[i]) Tarjan(i);
  98. Remove();
  99. Build();
  100. que[++w]=co[ss];
  101. dis[co[ss]]=si[co[ss]];
  102. while(t<w){
  103. int u=que[++t];
  104. for(i=head[u];i;i=next[i]){
  105. int v=ver[i];
  106. if(dis[v]<dis[u]+si[v]) dis[v]=dis[u]+si[v],que[++w]=v;
  107. }
  108. }
  109. int mx=;
  110. for(i=;i<=col;i++)
  111. if(vv[i]==) mx=MAX(mx,dis[i]);
  112. printf("%d\n",mx);
  113. return ;
  114. }

LOJ-10096(强连通+bfs)的更多相关文章

  1. loj 1426(dfs + bfs)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1426 思路:首先我们预处理出每一个"*"在某一方向上最终能到达的位 ...

  2. loj 1046(bfs)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26766 思路:由于数据不是很大,我们可以枚举骑士最后聚集的位置,然 ...

  3. 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。

    问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...

  4. PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

    1013 Battle Over Cities (25 分)   It is vitally important to have all the cities connected by highway ...

  5. 洛谷P1262 间谍网络[强连通分量 BFS]

    题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美元,他们就愿意交出手中掌握的全部情报 ...

  6. loj 1377 (bfs)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1377 思路:这道题只要处理好遇到"*"这种情况就可以搞定了.我们可 ...

  7. loj 1185(bfs)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26898 思路:我们可以给定有直接边相连的两点的距离为1,那么就是求 ...

  8. loj 1165(bfs+康托展开)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26879 思路:题目意思很简单,就是通过一些位置的交换,最后变成有序 ...

  9. loj 1055(bfs)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26772 思路:注意判重就行,开个6维数组记录3个robots的位置 ...

随机推荐

  1. JavaScript遍历集合(for...of/for...in/forEach)

    var arr = [1,2,3]; var map = new Map(); map.set('baylor',22); var s = new Set(); s.add([1,2,3]); for ...

  2. css第二天

    二丶 1.字体属性font: 字体名称(font-family)字体大小(font-size):pc中通常,字体大小表示为12px,14px.移动设备中通常表示为0.57rem.字体粗细(font-w ...

  3. 使用Chrome浏览器访问谷歌和维基百科

    安装chrome插件 谷歌访问助手即可 就这么简单

  4. Locust 学习一 :初识

    之前就听过Locust是基于python的一款很好用的开源性能测试框架,一直没机会实践,正好这次项目上有个接口压测的小任务,就拿来练练手 安装:py -3 -m pip install locusti ...

  5. AttributeError: 'LoginForm' object has no attribute 'is_bound' , object has no attribute 'is_bound'

    'LoginForm' object has no attribute 'is_bound' 可能原因: 啥子问题?? 都是 jquery.js 文件.....

  6. AHB协议整理 AMBA

    本文对AHB协议作了简单整理,整理自两篇文章: AHB总线协议 AHB重点难点总结 1. 简介 AHB总线规范是AMBA总线规范的一部分,AMBA总线规范是ARM公司提出的总线规范,被大多数SoC设计 ...

  7. mysql 中启动服务的命令 、登录命令、退出命令 mysql 的常用命令

    1.cmd 以管理员执行 下面命令 启动服务 :net start mysql57 关闭 服务:net stop mysql57 查看mysql 的版本信息 : mysql -V 指定主机地址登录: ...

  8. Win10系统下安装Gradle-3.5

    1. 下载 从官网https://gradle.org/releases/上下载3.5版本 2.解压 解压到D盘,路径是:D:\gradle-3.5 3.配置系统环境变量 首先依赖JAVA_HOME这 ...

  9. python websocket 再线聊天室的 Demo

    服务端 import tornado.web import tornado.ioloop import tornado.httpserver import tornado.options import ...

  10. 聚簇索引(Clustered Index)和非聚簇索引 (Non- Clustered Index)

    本文转自https://my.oschina.net/u/1866821/blog/297673 索引的重要性数据库性能优化中索引绝对是一个重量级的因素,可以说,索引使用不当,其它优化措施将毫无意义. ...