Codeforces 459E. Pashmak and Graph (DP)

12 篇文章 0 订阅

Description
Pashmak’s homework is a problem about graphs. Although he always tries to do his homework completely, he can’t solve this problem. As you know, he’s really weak at graph theory; so try to help him in solving the problem.

You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.

Help Pashmak, print the number of edges in the required path.

Input
The first line contains two integers n , m ( 2   ≤   n   ≤   3 ⋅ 1 0 5 ; 1   ≤   m   ≤   m i n ( n ⋅ ( n   −   1 ) ,   3 ⋅ 1 0 5 ) ) n, m (2 ≤ n ≤ 3·10^5; 1 ≤ m ≤ min(n·(n - 1), 3·10^5)) n,m(2n3105;1mmin(n(n1),3105)). Then, m lines follows. The i-th line contains three space separated integers: u i , v i , w i ( 1   ≤   u i ,   v i   ≤   n ; 1   ≤   w i   ≤   1 0 5 ) u_i, v_i, w_i (1 ≤ u_i, v_i ≤ n; 1 ≤ w_i ≤ 10^5) ui,vi,wi(1ui,vin;1wi105) which indicates that there’s a directed edge with weight w i w_i wi from vertex u i u_i ui to vertex v i v_i vi.

It’s guaranteed that the graph doesn’t contain self-loops and multiple edges.

Output
Print a single integer — the answer to the problem.

Examples
Input
3 3
1 2 1
2 3 1
3 1 1
Output
1

Input
3 3
1 2 1
2 3 2
3 1 3
Output
3

Input
6 7
1 2 1
3 2 5
2 4 2
2 5 2
2 6 9
5 4 3
4 3 4
Output
6

Solution
定义 e d p [ i ] edp[i] edp[i]:以边i结尾的最长路径, n d p [ i ] ndp[i] ndp[i] :以点i结尾的最长路径
因为路径上的边权需要递增,所以需要按边权从小到大的顺序做DP转移

	//非严格递增的转移方程(按边权顺序)
    int u = e.u,v = e.v,id = e.id;// 分别为有向边的两端点和边的编号
	edp[id] = ndp[u] + 1;
	ndp[v] = max(ndp[v],edp[id]);

然而这里需要严格递增,所以对于边权相同的边,他们的“拓扑序”是相同的,不应该出现状态转移的先后
我的方法是先 做 N o d e u − > E d g e i d Node_u -> Edge_{id} Nodeu>Edgeid 转移,全部转移完后再做 E d g e i d − > N o d e v Edge_{id} -> Node_v Edgeid>Nodev 转移
这样就不会受到相同权值边的影响

Hint
对于一些问题,重点就是要找到其“拓扑序”,从而满足无后效性的特点,才能使用DP进行转移

Code

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int maxn = 3e5 + 5;
const int maxv = 1e5 + 7;
int lim;
int ndp[maxn],edp[maxn];
struct Edge{
	int u,v,id;
};
vector<Edge>val[maxv];
int main(){
	int n,m;scanf("%d%d",&n,&m);
	for(int i = 1;i <= m;++i){
		int u,v,w;scanf("%d%d%d",&u,&v,&w);
		val[w].pb((Edge){u,v,i});
		lim = max(lim,w);
	}
	int res = 0;
	for(int w = 1;w <= lim;++w){
		for(auto e : val[w]){
			int u = e.u,v = e.v,id = e.id;
			edp[id] = ndp[u] + 1;
		}
		for(auto e : val[w]){
			int u = e.u, v = e.v,id = e.id;
			ndp[v] = max(ndp[v],edp[id]);
		}
	}
	for(int i = 1;i <= n;++i) res = max(res,ndp[i]);
	printf("%d\n", res);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值