\(Tarjan\) 模板

#include<cstdio>
#include<queue>
#include<iostream>
#define re register
using namespace std; const int N = 1e4 + 5;
int n, m, a[N], h1[N], h2[N];
struct edge{int nxt, to;}e1[N * 10], e2[N * 10]; inline void add1(int u, int v)
{
static int tot1 = 0;
e1[++tot1] = edge{h1[u], v}, h1[u] = tot1;
}
inline void add2(int u, int v)
{
static int tot2 = 0;
e2[++tot2] = edge{h2[u], v}, h2[u] = tot2;
} int dfn[N], low[N], col[N], vis[N], st[N], top, dfc, color;
void tarjan(int x)
{
dfn[x] = low[x] = ++dfc, st[++top] = x, vis[x] = 1;
for(re int i = h1[x]; i; i = e1[i].nxt)
{
int v = e1[i].to;
if (!dfn[v]) tarjan(v), low[x] = min(low[x], low[v]);
else if (vis[v]) low[x] = min(low[x], dfn[v]);
}
if (dfn[x] == low[x])
{
col[x] = ++color, vis[x] = 0;
while (st[top] ^ x) col[st[top]] = color, vis[st[top]] = 0, --top;
--top;
}
} queue<int> Q;
int f[N], val[N], in[N];
int topu()
{
for(re int i = 1; i <= color; i++)
if (!in[i]) Q.push(i), f[i] = val[i], vis[i] = 1;
while (!Q.empty())
{
int now = Q.front(); Q.pop();
for(re int i = h2[now]; i; i = e2[i].nxt)
{
--in[e2[i].to], f[e2[i].to] = max(f[e2[i].to], f[now] + val[e2[i].to]);
if (!in[e2[i].to]) Q.push(e2[i].to);
}
}
int res = 0;
for(re int i = 1; i <= color; i++) res = max(res, f[i]);
return res;
} int main()
{
scanf("%d%d", &n, &m);
for(re int i = 1; i <= n; i++) scanf("%d", &a[i]);
for(re int i = 1, u, v; i <= m; i++) scanf("%d%d", &u, &v), add1(u, v);
for(re int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i);
for(re int i = 1; i <= n; i++)
{
val[col[i]] += a[i];
for(re int j = h1[i]; j; j = e1[j].nxt)
if (col[i] ^ col[e1[j].to]) add2(col[i], col[e1[j].to]), ++in[col[e1[j].to]];
}
printf("%d\n", topu());
}

最新文章

  1. js中数组连接concat()
  2. HDU 4465 数值计算,避免溢出
  3. Buy the Ticket
  4. DataFrame使用mysql数据
  5. Java基础—异常处理总结
  6. android 9Path图片的使用
  7. 总结:spring 的ioc 知识点
  8. java工程开发之图形化界面之(第三课)
  9. regress_partition.sql
  10. Unslider Web前端框架之图片轮播
  11. MySQL ID排序乱了的解决办法
  12. python书籍推荐:python编码推荐(高清完整pdf)
  13. 2018-2019-2 《Java程序设计》第9周学习总结
  14. centos7之NFS使用
  15. 把本人基于Dubbo的毕业设计分享粗来~
  16. 适用于 Windows 10 的触摸板手势
  17. 2014年西安区域赛的几道水题(A. F. K)
  18. [leetcode]38. Count and Say数数
  19. mysql 开发进阶篇系列 38 mysql日志之错误日志log-error
  20. 转载:磁盘目录(1.3.3)《深入理解Nginx》(陶辉)

热门文章

  1. Docker 工作原理分析
  2. MySQL进阶实战1,数据类型与三范式
  3. K8s安装乐维5.0应用部署文档
  4. Django的manytomany字段
  5. uniapp onLoad里面拿不到reject的参数
  6. 自定义RBAC(4)
  7. 玩 ChatGPT 的正确姿势「GitHub 热点速览 v.22.49」
  8. 史上最小 x86 Linux 模拟器「GitHub 热点速览 v.22.50」
  9. python文件的写入与读出
  10. python画社交网络图