题目链接:传送门

思路:

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

#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)的更多相关文章

  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. 初识TCP协议

    一.引言 发送一段TCP数据大致需要经过:用户封装 –> TCP封装 –> IP封装 –>帧封装 Note:用户封装没啥好说的,都是客户自己决定的,在一些简单的应用情况下,这个步骤可 ...

  2. 2019/4/17 Linux学习

    一.Linux的文件系统 其中/prov./srv./sys 文件为文件系统,技术不过硬不要去修改:二.关于Xshell.Xft1.服务器的端口可有65535个可设置,开的越多安全性越差:2.远程登录 ...

  3. vue-cli脚手架中webpack配置基础文件详解

    一.前言 原文:https://segmentfault.com/a/1190000014804826 vue-cli是构建vue单页应用的脚手架,输入一串指定的命令行从而自动生成vue.js+wep ...

  4. nginx开机启动

    centos 7以上是用Systemd进行系统初始化的 Systemd服务文件以.service结尾,比如现在要建立nginx为开机启动,如果用yum install命令安装的,yum命令会自动创建n ...

  5. 阿里云-CentOS如何挂载硬盘

    阿里云CentOS挂载硬盘 查看当前未挂载的硬盘 # fdisk -l 创建硬盘分区 # fdisk /dev/vdb 根据提示,依次输入"n","p" &qu ...

  6. 100-days: twenty-four

    Title: No word of a lie: scientists rate(评出) the world's biggest peddlers of bull(说瞎话的人,扯淡的人) bullsh ...

  7. 微信小程序记账本进度七

    最后大体上完成了,但是好像少了点功能,整体并不是特别华丽

  8. 解决在vscode中eslint在vue后缀文件中保存时无法自动格式化的问题

    在setting.json中加入如下内容 { "eslint.autoFixOnSave": true, "eslint.validate": [ " ...

  9. 《Java从入门到精通》学习总结2

    1. 在JAVA语言中对静态方法有两点规定: 在静态方法中不可以使用this关键字 在静态方法中不可以直接调用非静态方法 2. 不能将方法体内的局部变量声明为static的 3. 引用只是存放一个对象 ...

  10. Python-lambda表达式和推导式

    lambda表达式和推导式,可实现使用一行语句完成功能开发的效果,减少代码量. 1.lambda表达式 匿名函数,即没有名字的函数 1.1基本语法: lambda argument_list: exp ...