Codeforces Round #261 (Div. 2)E题(DP:DAG上的最长路)

E. Pashmak and Graph
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nm (2 ≤ n ≤ 3·105; 1 ≤ m ≤ min(n·(n - 1), 3·105)). Then, m lines follows. The i-th line contains three space separated integers: uiviwi (1 ≤ ui, vi ≤ n; 1 ≤ wi ≤ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex 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.

Sample test(s)
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

题意:n个点,m条边,每条边有权值,要求找一条边数最多的路径使得路径上的权值严格递增

思路:看到这题感觉跟DAG上找最长路问题差不多,但还是稍微有点不同,做法就是先将所有边按权值从小到大排序,然后从后往前dp,dp[u]表示以u节点开头的最长路径的长

度,然后转移方程为dp[u]=dp[v]+1,注意在一段长度一样的边的区间先不要更新dp组数,可以用一个辅助数组来保存这一段所更新的值,等这段区间完了之后再更新回去dp即


#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         using namespace std; #define maxn 300010 int n,m; struct node{ int w; int u; int v; bool operator < (const node&a)const{ return w 
        
          =0;i--) { u=edge[i].u; v=edge[i].v; maxii=dp[v]+1; if(vis[u] 
         
           vis[u])vis[u]=maxii; if(ans 
          
            edge[i-1].w){ for(j=i;j 
            
           
          
         
       
      
      
     
     
    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值