代码如下

#include <bits/stdc++.h>

using namespace std;

int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(n + 1);
vector<vector<int>> adj(n + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}
vector<int> dfn(n + 1), low(n + 1), scc(n + 1);
vector<vector<int>> has(1);
int dfn_cnt = 0, scc_cnt = 0;
stack<int> st;
function<void(int)> tarjan = [&](int u) {
dfn[u] = low[u] = ++dfn_cnt;
st.push(u);
for (int v : adj[u]) {
if (dfn[v] == 0) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (scc[v] == 0) {
low[u] = min(low[u], dfn[v]);
}
}
if (low[u] == dfn[u]) {
++scc_cnt;
has.push_back(vector<int>());
while (1) {
int v = st.top();
st.pop();
scc[v] = scc_cnt;
has[scc_cnt].push_back(v);
if (v == u) {
break;
}
}
}
};
for (int i = 1; i <= n; i++) {
if (dfn[i] == 0) {
tarjan(i);
}
}
vector<vector<int>> g(scc_cnt + 1);
vector<int> degree(scc_cnt + 1), b(scc_cnt + 1);
for (int i = 1; i <= scc_cnt; i++) {
for (auto x : has[i]) {
b[i] += a[x];
}
}
for (int u = 1; u <= n; u++) {
for (auto v : adj[u]) {
if (scc[u] != scc[v]) {
g[scc[u]].push_back(scc[v]);
++degree[scc[v]];
}
}
}
queue<int> q;
vector<int> f(scc_cnt + 1);
for (int i = 1; i <= scc_cnt; i++) {
if (degree[i] == 0) {
q.push(i);
}
}
while (!q.empty()) {
int u = q.front();
q.pop();
f[u] += b[u];
for (auto v : g[u]) {
f[v] = max(f[v], f[u]);
if (--degree[v] == 0) {
q.push(v);
}
}
}
cout << *max_element(f.begin(), f.end()) << endl;
return 0;
}

【模板】Tarjan scc缩点的更多相关文章

  1. HDU 5934 Bomb(tarjan/SCC缩点)题解

    思路:建一个有向图,指向能引爆对象,把强连通分量缩成一点,只要点燃图中入度为0的点即可.因为入度为0没人能引爆,不为0可以由别人引爆. 思路很简单,但是早上写的一直错,改了半天了,推倒重来才过了... ...

  2. 洛谷P2341 [HAOI2006]受欢迎的牛 (Tarjan,SCC缩点)

    P2341 [HAOI2006]受欢迎的牛|[模板]强连通分量 https://www.luogu.org/problem/P2341 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就 ...

  3. tarjan算法求scc & 缩点

    前置知识 图的遍历(dfs) 强连通&强连通分量 对于有向图G中的任意两个顶点u和v存在u->v的一条路径,同时也存在v->u的路径,我们则称这两个顶点强连通.以此类推,强连通分量 ...

  4. bzoj1093: [ZJOI2007]最大半连通子图 scc缩点+dag上dp

    一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径.若G'=(V ...

  5. Tarjan的缩点&&割点概述

    What is Tarjan? Tarjan,是一种用来解决图的联通性的一种有效途径,它的一般俗称叫做:缩点.我们首先来设想一下: 如果我们有一个图,其中A,B,C构成一个环,那么我们在某种条件下,如 ...

  6. HDU 3072--Intelligence System【SCC缩点新构图 &amp;&amp; 求连通全部SCC的最小费用】

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  7. UVA11324 The Lagest Lique(SCC缩点+DP)

    Given a directed graph G, con- sider the following transformation. First, create a new graph T(G) to ...

  8. POJ 2186 Popular cows(SCC 缩点)

    Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...

  9. P2746 P2812 [USACO5.3]校园网Network of Schools[SCC缩点]

    题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作"接受学校").注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学 ...

随机推荐

  1. poj3126 Prime Path(c语言)

    Prime Path   Description The ministers of the cabinet were quite upset by the message from the Chief ...

  2. 访谈:BugPhobia’s Brief Communication

    0x01 :采访的学长简介 If you weeped for the missing sunset, you would miss all the shining stars 梁野,北京航空航天大学 ...

  3. 结对项目 https://github.com/quchengyu/jiedui/tree/quchengyu-patch-1

    所选项目名称:文本替换      结对人:傅艺伟 github地址 : https://github.com/quchengyu/jiedui/tree/quchengyu-patch-1 用一个新字 ...

  4. 剑指offer:数组中重复的数字

    题目描述: 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度 ...

  5. 伪GZCC官网

    <html class="no-js"><head> <meta charset="utf-8"> <meta htt ...

  6. HDOJ4548_美素数

    简单的素数问题 HDOJ4548_美素数 #include<stdio.h> #include<stdlib.h> #include<math.h> #includ ...

  7. IIS7开启gZip动态压缩

    1.安装动态压缩模块: 安装过程可能的报错:This application has requested the Runtime to terminate it in an unusual way. ...

  8. [读书笔记]SQLSERVER企业级平台管理实践读书笔记02

    记录一下 这一块 join的理解了 再完善过来. 1. Statistics的用法: 清空执行计划用的命令 dbcc freeproccache 清空buffer pool 里面的缓存命令 dbcc ...

  9. Linux 文件系统概览

    本文导航 -定义07% -文件系统的基本功能12% -目录结构26% -Linux 统一目录结构50% -文件系统类型74% -挂载81% -结论90% -下个月92%   本文旨在高屋建瓴地来讨论 ...

  10. SAP字体调节大小

    登陆SAP 之后,菜单下面一行,最右边的那个彩色按钮(SAP GUI),点击“选项”-可视设计-字体设计-固定狂赌字体设计,点击:选择字体 即可.