【模板】缩点 tarjan+dp

题目背景

缩点+DP

题目描述

给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大。你只需要求出这个权值和。

允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次。

输入输出格式

输入格式:

第一行,n,m

第二行,n个整数,依次代表点权

第三至m+2行,每行两个整数u,v,表示u->v有一条有向边

输出格式:

共一行,最大的点权之和。

输入输出样例

输入样例#1: 复制
2 2
1 1
1 2
2 1
输出样例#1: 复制
2

说明

n<=10^4,m<=10^5,点权<=1000

算法:Tarjan缩点+DAGdp

 

Tarjan+记忆化搜索;

缩点以后,重新建图,然后dp;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 400005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
	ll x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

int n, m;
int idx;
int col[maxn], dp[maxn], sum[maxn];
int head[maxn];
int sk[maxn], top;
int dfn[maxn], low[maxn];
int tot;
int vis[maxn];
int val[maxn];

struct node {
	int u, v, nxt;
}edge[maxn];

int cnt;
void addedge(int x, int y) {
	edge[++cnt].v = y; edge[cnt].nxt = head[x]; head[x] = cnt;
}

void tarjan(int x) {
	sk[++top] = x; vis[x] = 1;
	low[x] = dfn[x] = ++idx;
	for (int i = head[x]; i; i = edge[i].nxt) {
		int v = edge[i].v;
		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]) {
		tot++;
		while (sk[top + 1] != x) {
			col[sk[top]] = tot; sum[tot] += val[sk[top]]; vis[sk[top--]] = 0;
		}
	}
}

void DP(int x) {
	int maxx = 0;
	if (dp[x])return;
	dp[x] = sum[x];
	for (int i = head[x]; i; i = edge[i].nxt) {
		int v = edge[i].v;
		if (!dp[v])DP(v);
		maxx = max(maxx, dp[v]);
	}
	dp[x] += maxx;
}
int x[maxn], y[maxn];

int main()
{
	//ios::sync_with_stdio(0);
	rdint(n); rdint(m);
	for (int i = 1; i <= n; i++)rdint(val[i]);
	for (int i = 1; i <= m; i++) {
		rdint(x[i]); rdint(y[i]); addedge(x[i], y[i]);
	}
	for (int i = 1; i <= n; i++)if (!dfn[i])tarjan(i);
	ms(edge); cnt = 0; ms(head);
	for (int i = 1; i <= m; i++) {
		if (col[x[i]] != col[y[i]]) {
			addedge(col[x[i]], col[y[i]]);
		}
	}
	int ans = 0;
	for (int i = 1; i <= tot; i++) {
		if (!dp[i]) {
			DP(i); ans = max(ans, dp[i]);
		}
	}
	cout << ans << endl;
	return 0;
}

 

posted @ 2018-12-23 20:03  NKDEWSM  阅读(254)  评论(0编辑  收藏  举报