LOJ-10096(强连通+bfs)
题目链接:传送门
思路:
强连通缩点,重建图,然后广搜找最长路径。
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- const int maxn = 1e6+;
- int head[maxn],next[maxn],ver[maxn],tot;
- int low[maxn],num[maxn],tim,co[maxn],col,si[maxn];
- int vis[maxn],dis[maxn];
- int st[maxn],top,a[maxn],in[maxn],que[maxn],w,t;
- int xx[maxn],yy[maxn],nu[maxn],ans,m,n,vv[maxn];
- int MIN(int x,int y)
- {
- return x<y?x:y;
- }
- int MAX(int x,int y)
- {
- return x>y?x:y;
- }
- void Init()
- {
- memset(vis,,sizeof(vis));
- memset(in,,sizeof(in));
- memset(vv,,sizeof(vv));
- top=;tim=;tot=;col=;w=;t=;
- }
- void addedge(int u,int v)
- {
- ver[++tot]=v;next[tot]=head[u];head[u]=tot;
- }
- void Tarjan(int u)
- {
- low[u]=num[u]=++tim;
- st[++top]=u;
- for(int i=head[u];i;i=next[i]){
- int v=ver[i];
- if(!num[v]){
- Tarjan(v);
- low[u]=MIN(low[u],low[v]);
- }
- else if(!co[v]) low[u]=MIN(low[u],num[v]);
- }
- if(low[u]==num[u]){
- col++;
- co[u]=col;
- si[col]=a[u];
- vv[col]=MAX(vv[col],vis[u]);
- while(st[top]!=u){
- co[st[top]]=col;
- vv[col]=MAX(vv[col],vis[st[top]]);
- si[col]+=a[st[top]];
- top--;
- }
- top--;
- }
- }
- bool cmp(int x,int y)
- {
- if(xx[x]!=xx[y]) return xx[x]<xx[y];
- else return yy[x]<yy[y];
- }
- void Remove()
- {
- memset(head,,sizeof(head));
- tot=;
- for(int i=;i<=m;i++){
- nu[i]=i;
- xx[i]=co[xx[i]];
- yy[i]=co[yy[i]];
- }
- sort(nu+,nu+m+,cmp);
- }
- void Build()
- {
- for(int i=;i<=m;i++){
- int z=nu[i];
- if(xx[z]!=yy[z]&&(xx[z]!=xx[nu[i-]]||yy[z]!=yy[nu[i-]]))
- addedge(xx[z],yy[z]),in[yy[z]]++;
- }
- }
- int main(void)
- {
- int i,j,ss,pp,tp;
- scanf("%d%d",&n,&m);
- Init();
- for(i=;i<=m;i++){
- scanf("%d%d",&xx[i],&yy[i]);
- addedge(xx[i],yy[i]);
- }
- for(i=;i<=n;i++) scanf("%d",&a[i]);
- scanf("%d%d",&ss,&pp);
- for(i=;i<=pp;i++){
- scanf("%d",&tp);vis[tp]=;
- }
- for(i=;i<=n;i++)
- if(!num[i]) Tarjan(i);
- Remove();
- Build();
- que[++w]=co[ss];
- dis[co[ss]]=si[co[ss]];
- while(t<w){
- int u=que[++t];
- for(i=head[u];i;i=next[i]){
- int v=ver[i];
- if(dis[v]<dis[u]+si[v]) dis[v]=dis[u]+si[v],que[++w]=v;
- }
- }
- int mx=;
- for(i=;i<=col;i++)
- if(vv[i]==) mx=MAX(mx,dis[i]);
- printf("%d\n",mx);
- return ;
- }
LOJ-10096(强连通+bfs)的更多相关文章
- loj 1426(dfs + bfs)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1426 思路:首先我们预处理出每一个"*"在某一方向上最终能到达的位 ...
- loj 1046(bfs)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26766 思路:由于数据不是很大,我们可以枚举骑士最后聚集的位置,然 ...
- 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。
问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...
- 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 ...
- 洛谷P1262 间谍网络[强连通分量 BFS]
题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美元,他们就愿意交出手中掌握的全部情报 ...
- loj 1377 (bfs)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1377 思路:这道题只要处理好遇到"*"这种情况就可以搞定了.我们可 ...
- loj 1185(bfs)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26898 思路:我们可以给定有直接边相连的两点的距离为1,那么就是求 ...
- loj 1165(bfs+康托展开)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26879 思路:题目意思很简单,就是通过一些位置的交换,最后变成有序 ...
- loj 1055(bfs)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26772 思路:注意判重就行,开个6维数组记录3个robots的位置 ...
随机推荐
- 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 ...
- css第二天
二丶 1.字体属性font: 字体名称(font-family)字体大小(font-size):pc中通常,字体大小表示为12px,14px.移动设备中通常表示为0.57rem.字体粗细(font-w ...
- 使用Chrome浏览器访问谷歌和维基百科
安装chrome插件 谷歌访问助手即可 就这么简单
- Locust 学习一 :初识
之前就听过Locust是基于python的一款很好用的开源性能测试框架,一直没机会实践,正好这次项目上有个接口压测的小任务,就拿来练练手 安装:py -3 -m pip install locusti ...
- AttributeError: 'LoginForm' object has no attribute 'is_bound' , object has no attribute 'is_bound'
'LoginForm' object has no attribute 'is_bound' 可能原因: 啥子问题?? 都是 jquery.js 文件.....
- AHB协议整理 AMBA
本文对AHB协议作了简单整理,整理自两篇文章: AHB总线协议 AHB重点难点总结 1. 简介 AHB总线规范是AMBA总线规范的一部分,AMBA总线规范是ARM公司提出的总线规范,被大多数SoC设计 ...
- mysql 中启动服务的命令 、登录命令、退出命令 mysql 的常用命令
1.cmd 以管理员执行 下面命令 启动服务 :net start mysql57 关闭 服务:net stop mysql57 查看mysql 的版本信息 : mysql -V 指定主机地址登录: ...
- Win10系统下安装Gradle-3.5
1. 下载 从官网https://gradle.org/releases/上下载3.5版本 2.解压 解压到D盘,路径是:D:\gradle-3.5 3.配置系统环境变量 首先依赖JAVA_HOME这 ...
- python websocket 再线聊天室的 Demo
服务端 import tornado.web import tornado.ioloop import tornado.httpserver import tornado.options import ...
- 聚簇索引(Clustered Index)和非聚簇索引 (Non- Clustered Index)
本文转自https://my.oschina.net/u/1866821/blog/297673 索引的重要性数据库性能优化中索引绝对是一个重量级的因素,可以说,索引使用不当,其它优化措施将毫无意义. ...