Codeforces Round #261 (Div. 2) E

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 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.

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
Note

In the first sample the maximum trail can be any of this trails: .

In the second sample the maximum trail is .

In the third sample the maximum trail is .

题意:按照权值递增循序走,能走出最长的路径是多少

解法:求出dp[u]=max(dp[u],dp[v]+1)(v起点,u终点)

根据权值从小到大排序,分别处理相同权值片段的长度,最后求最大值

 1 #include<stdio.h>
 2 //#include<bits/stdc++.h>
 3 #include<string.h>
 4 #include<iostream>
 5 #include<math.h>
 6 #include<sstream>
 7 #include<set>
 8 #include<queue>
 9 #include<map>
10 #include<vector>
11 #include<algorithm>
12 #include<limits.h>
13 #define inf 0x7fffffff
14 #define INFL 0x7fffffffffffffff
15 #define lson l,m,rt<<1
16 #define rson m+1,r,rt<<1|1
17 #define LL long long
18 #define ULL unsigned long long
19 using namespace std;
20 const int M = 2000000;
21 int n,m;
22 struct P
23 {
24     int v,u,w;
25 }H[M];
26 bool solve(P x,P y)
27 {
28     return x.w<y.w;
29 }
30 int dp[M],ans[M];
31 int j;
32 int main()
33 {
34     std::ios::sync_with_stdio(false);
35     cin>>n>>m;
36     for(int i=0;i<m;i++)
37     {
38         cin>>H[i].v>>H[i].u>>H[i].w;
39     }
40     sort(H,H+m,solve);
41     for(int i=0;i<m;i=j)
42     {
43         for(j=i;H[i].w==H[j].w&&j<m;j++)
44         {
45             ans[H[j].u]=dp[H[j].u];
46         }
47         for(j=i;H[i].w==H[j].w&&j<m;j++)
48         {
49             ans[H[j].u]=max(ans[H[j].u],dp[H[j].v]+1);
50         }
51         for(j=i;H[i].w==H[j].w&&j<m;j++)
52         {
53             dp[H[j].u]=ans[H[j].u];
54         }
55     }
56     int Max=-1;
57     for(int i=1;i<=n;i++)
58     {
59         Max=max(dp[i],Max);
60     }
61     cout<<Max<<endl;
62     return 0;
63 }

 

 

转载于:https://www.cnblogs.com/yinghualuowu/p/6582104.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值