After retirement as contestant from WHU ACM Team, flymouse volunteered to do the odds and ends such as cleaning out the computer lab for training as extension of his contribution to the team. When Christmas came, flymouse played Father Christmas to give gifts to the team members. The team members lived in distinct rooms in different buildings on the campus. To save vigor, flymouse decided to choose only one of those rooms as the place to start his journey and follow directed paths to visit one room after another and give out gifts en passant until he could reach no more unvisited rooms.

During the days on the team, flymouse left different impressions on his teammates at the time. Some of them, like LiZhiXu, with whom flymouse shared a lot of candies, would surely sing flymouse’s deeds of generosity, while the others, like snoopy, would never let flymouse off for his idleness. flymouse was able to use some kind of comfort index to quantitize whether better or worse he would feel after hearing the words from the gift recipients (positive for better and negative for worse). When arriving at a room, he chould choose to enter and give out a gift and hear the words from the recipient, or bypass the room in silence. He could arrive at a room more than once but never enter it a second time. He wanted to maximize the the sum of comfort indices accumulated along his journey.

Input

The input contains several test cases. Each test cases start with two integers N and M not exceeding 30 000 and 150 000 respectively on the first line, meaning that there were N team members living in N distinct rooms and M direct paths. On the next N lines there are N integers, one on each line, the i-th of which gives the comfort index of the words of the team member in the i-th room. Then follow M lines, each containing two integers i and j indicating a directed path from the i-th room to the j-th one. Process to end of file.

Output

For each test case, output one line with only the maximized sum of accumulated comfort indices.

Sample Input

2 2
14
21
0 1
1 0

Sample Output

35

Hint

32-bit signed integer type is capable of doing all arithmetic.

#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007 using namespace std; const int N=,maxn=,inf=0x3f3f3f3f; int n,m,index,num,cnt;
int dfn[N],low[N],value[N],a[N];
int in[N],out[N];
int ins[N],inans[N];
vector<int>v[N],kk[N],ans[N];
stack<int>s; void tarjan(int u)
{
ins[u]=;
dfn[u]=low[u]=++index;
s.push(u);
for(int i=;i<v[u].size();i++)
{
int t=v[u][i];
if(!dfn[t])
{
tarjan(t);
low[u]=min(low[u],low[t]);
}
else if(ins[t]==)low[u]=min(low[u],dfn[t]);
}
if(dfn[u]==low[u])
{
++num;
while(!s.empty()){
int k=s.top();
s.pop();
ins[k]=;
inans[k]=num;
a[num]+=value[k];
ans[num].push_back(k);
if(k==u)break;
}
}
}
int dfs(int u)
{
if(!dfn[u])
{
int res=;
dfn[u]=;
for(int i=;i<kk[u].size();i++)
res=max(res,dfs(kk[u][i]));
a[u]+=res;
}
return a[u];
}
int main()
{
while(cin>>n>>m){
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(ins,,sizeof(ins));
memset(inans,,sizeof(inans));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(a,,sizeof(a));
for(int i=;i<n;i++)
{
v[i].clear();
ans[i].clear();
kk[i].clear();
}
while(!s.empty())s.pop();
for(int i=;i<n;i++)
{
cin>>value[i];
if(value[i]<)value[i]=;
}
while(m--){
int a,b;
cin>>a>>b;
v[a].push_back(b);
}
for(int i=;i<n;i++)
if(!dfn[i])
tarjan(i);
for(int i=;i<n;i++)
{
for(int j=;j<v[i].size();j++)
{
int p=v[i][j];
if(inans[p]!=inans[i])
kk[inans[i]].push_back(inans[p]);
}
}
/* for(int i=1;i<=num;i++)
{
for(int j=0;j<kk[i].size();j++)
cout<<kk[i][j]<<" ";
cout<<endl;
}*/
int res=;
memset(dfn,,sizeof(dfn));
for(int i=;i<=num;i++)
{
res=max(res,dfs(i));
}
cout<<res<<endl;
}
return ;
}

此题也可以用spfa做,但是我感觉dfs更简便,只是时间复杂度有点高,spfa只需遍历一遍

先缩点然后dfs找最大的就行了

poj3160强连通分量加dfs的更多相关文章

  1. hdu 3836 Equivalent Sets(强连通分量--加边)

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

  2. [BZOJ1194][HNOI2006][强连通分量Tarjan+dfs]潘多拉的盒子

    [BZOJ1194][HNOI2006]潘多拉的盒子 Input 第一行是一个正整数S,表示宝盒上咒语机的个数,(1≤S≤50).文件以下分为S块,每一块描述一个咒语机,按照咒语机0,咒语机1„„咒语 ...

  3. Tarjan算法初探 (1):Tarjan如何求有向图的强连通分量

    在此大概讲一下初学Tarjan算法的领悟( QwQ) Tarjan算法 是图论的非常经典的算法 可以用来寻找有向图中的强连通分量 与此同时也可以通过寻找图中的强连通分量来进行缩点 首先给出强连通分量的 ...

  4. 求图的强连通分量--tarjan算法

    一:tarjan算法详解 ◦思想: ◦ ◦做一遍DFS,用dfn[i]表示编号为i的节点在DFS过程中的访问序号(也可以叫做开始时间)用low[i]表示i节点DFS过程中i的下方节点所能到达的开始时间 ...

  5. 寻找图的强连通分量:tarjan算法简单理解

    1.简介tarjan是一种使用深度优先遍历(DFS)来寻找有向图强连通分量的一种算法. 2.知识准备栈.有向图.强连通分量.DFS. 3.快速理解tarjan算法的运行机制提到DFS,能想到的是通过栈 ...

  6. 【有向图】强连通分量-Tarjan算法

    好久没写博客了(都怪作业太多,绝对不是我玩的太嗨了) 所以今天要写的是一个高大上的东西:强连通 首先,是一些强连通相关的定义 //来自度娘 1.强连通图(Strongly Connected Grap ...

  7. 【数据结构】DFS求有向图的强连通分量

    用十字链表结构写的,根据数据结构书上的描述和自己的理解实现.但理解的不透彻,所以不知道有没有错误.但实验了几个都ok. #include <iostream> #include <v ...

  8. 有向图 加最少的边 成为强连通分量的证明 poj 1236 hdu 2767

    poj 1236: 题目大意:给出一个有向图, 任务一: 求最少的点,使得从这些点出发可以遍历整张图  任务二: 求最少加多少边 使整个图变成一个强连通分量. 首先任务一很好做, 只要缩点 之后 求 ...

  9. DFS的运用(二分图判定、无向图的割顶和桥,双连通分量,有向图的强连通分量)

    一.dfs框架: vector<int>G[maxn]; //存图 int vis[maxn]; //节点访问标记 void dfs(int u) { vis[u] = ; PREVISI ...

随机推荐

  1. Java Reference 源码分析

    @(Java)[Reference] Java Reference 源码分析 Reference对象封装了其它对象的引用,可以和普通的对象一样操作,在一定的限制条件下,支持和垃圾收集器的交互.即可以使 ...

  2. Asp.Net 常用工具类---Config操作(7)

    近期工作比较忙,忙到忘记写博客(自己的借口,主要加班下班后不想动). 月初的时候,打算每两天写一篇博文,分享自己的一些心得和开发体验,无奈现在只写到第六篇,然而时间已经是20号,岁月不饶人! 总想写点 ...

  3. 【翻译】CSS水平和垂直居中的12种方法

    英语原文链接 在CSS中有许多不同的方法能够做到水平和垂直居中,但很难去选择合适的那个.我会向你展示我所看到的所有的方法,帮助你在所面对的情境下选择最棒的那一个. 方法1 此方法将只能垂直居中单行文本 ...

  4. https单向认证和双向认证区别

    关于证书 1.每个人都可以使用一些证书生成工具为自己的站点生成证书(比如jdk的keytool),大家称它为“自签名证书”,但是自己生成的证书是不被互联网承认的,所以浏览器会报安全提示,要求你手动安装 ...

  5. Http相关

    1.http请求 http请求分为三部分:请求行,请求头,请求正文 1. 请求行 请求方式  GET   POST 请求资源路径 协议版本 GET与POST请求区别? get只能传递1kb以下数据,P ...

  6. ajax使用及代码表示

    最近学习了ajax,记录一下学习写过的代码和一些问题 一.原生ajax var xhr = null; if(window.XMLHttpRequest) { xhr = new XMLHttpReq ...

  7. Excel图表-创意雷达图-原创图表

    p{ font-size: 15px; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid #aaa; width: 99%; ...

  8. 使用 SLF4J + LogBack 构建日志系统(转)

    转载自:http://www.cnblogs.com/mailingfeng/p/3499436.html 上次我们讨论了如何选择一个好的开源日志系统方案,其中的结论是:使用 SLF4J + LogB ...

  9. (iOS)关于UITableView设置contentsize(原创)

    由于UITableView是继承自UIScrollView的,所以他是可以设置contentsize的. 但是,我在试验的过程中,初始化UITableView实例后,直接设置它的contentsize ...

  10. html结合js实现简单的树状目录

    最近在学jsp,期末了要做项目,需要用到树状目录,百度了很多,都没有找到想要的答案,最后自己折腾了半天,才搞定. 下面我就来分享一下怎么实现一个简单的树状目录: 1. 下载jquery-treevie ...