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 }
复制代码

 

 

posted @   樱花落舞  阅读(182)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示