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的位置 ...
随机推荐
- 转: Ext.data.Store 修改Post请求
Extjs 4.0版本 var Store = Ext.create('Ext.data.Store', { pageSize: pageSize, model: 'Ext.data.Model名称' ...
- Working with Excel Files in Python
Working with Excel Files in Python from: http://www.python-excel.org/ This site contains pointers to ...
- 2017-11-11 Sa Oct 消参
2017-11-11 Sa Oct 消参 Prior versions: 2017-11-04 Sa Oct 消参 2017-11-10 Fr Oct 消参 2017-11-04 Sa $ P(-3, ...
- 吴裕雄 python 数据可视化
import pandas as pd df = pd.read_csv("F:\\python3_pachongAndDatareduce\\data\\pandas data\\taob ...
- nagios监控mysql及邮件报警
1.使用默认监控命令check_http命令+相关的参数来实现,如下: 在command.cfg添加如下关键词监控命令:check_http_word,参数解析:-I指定IP或者主机名,-u指定URL ...
- 这可能是史上最全的windows10装ubuntu双系统教程
一. 先搞清楚自己电脑的类型: A MBR传统bios+单硬盘 B MBR传统bios+双硬盘(SSD固态硬盘+机械硬盘) C UEFI新式bios+单硬盘 D UEFI新式bios+双硬盘( ...
- Jenkins解析日志(log-parser-plugin)
Jenkins打包机打包时产生了大量的日志,当报错时,不方便查看error日志 因为日志量太大,查看全部log的时候整个web页面会卡死,所以引用log-parser-plugin可以增加过滤条件显示 ...
- jmeter连接oracle数据库配置
1导入加载ojdbc.jar包(2种方法) 1)直接拷贝目录 2.
- Python入门day04_函数与装饰器
一.函数入门 什么是函数: # 函数:函数是一系列代码的集,用来完成特定功能的代码块,类似于工具,可以重复不但的去使用 为什么要有函数: # 优点:# 1. 避免代码的冗余 # 2. 让程序代码结构更 ...
- java_29打印流
1打印流 PrintStream 和PrintWriter 不负责数据源 只负责数据目的 2.方法 public class Demo { public static void main(St ...