ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

P3387 缩点

2021-08-10 22:01:14  阅读:190  来源: 互联网

标签:缩点 int top ++ P3387 vis dfn low


\(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());
}

标签:缩点,int,top,++,P3387,vis,dfn,low
来源: https://www.cnblogs.com/leiyuanze/p/15125974.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有