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的位置 ...
随机推荐
- spring中Constructor、@Autowired、@PostConstruct的顺序
其实从依赖注入的字面意思就可以知道,要将对象p注入到对象a,那么首先就必须得生成对象p与对象a,才能执行注入.所以,如果一个类A中有个成员变量p被@Autowired注解,那么@Autowired注入 ...
- 读取文件不是真实的具体路径 setZh.ini
读取 c:\windows\Syswow64\XX\XX.ini 时内容不正确. 发现真实文件为: C:\Users\用户名\AppData\Local\VirtualStore\Windows\Sy ...
- python day10 函数(第二篇)
2019.4.10 S21 day10笔记总结 一.内容回顾 1.面试题相关: 1.py2和py3的区别 2.运算符的计算 :3 or 9 and 8 3.字符串的反转 4.is和==的区别 5.v1 ...
- Java框架spring Boot学习笔记(七):@Configuration,@bean注解
@Configuration作用在类上,相当于一个xml文件 @bean作用于方法上,相当于xml配置中的<bean>标签 一个例子: 新建一个Springboot工程 新建一个User类 ...
- 浏览器登录EBS系统遇到问题总结
1.可以登录EBS系统,但是职责位置为空白 这是兼容性视图问题,将ERP域名加入兼容性视图列表即可. 加入即可: 2.可以登录EBS系统,但是职责位置显示 “undefined” 这也是兼容性视图问题 ...
- 985. Sum of Even Numbers After Queries
We have an array A of integers, and an array queries of queries. For the i-th query val = queries[i] ...
- C++ map 使用erase在windows下崩溃,在linux下没有问题的原因
注意:此程序在win环境下会出现上述描述的问题:在mac环境下第一种方式是正常运行的.Map.erase有3个重载函数: void erase(iterator position); size_typ ...
- echarts 图表重新加载,原来的数据依然存在图表上
问题 在做一个全国地图上一些饼图,并且向省一级的地图钻取的时候,原来的饼图依然显示 原因 echars所有添加的图表都在一个series属性集合中,并且同一个echars对象默认是合并之前的数据的,所 ...
- Openssl asn1parse命令
一.简介 asn1parse命令是一种用来诊断ASN.1结构的工具,也能用于从ASN1.1数据中提取数据 二.语法 openssl asn1parse [-inform PEM|DER] [-in f ...
- scrapy Mongodb 储存
pipelines.py # -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your ...