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终点)

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

 #include<stdio.h>
//#include<bits/stdc++.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<sstream>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<limits.h>
#define inf 0x7fffffff
#define INFL 0x7fffffffffffffff
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define ULL unsigned long long
using namespace std;
const int M = ;
int n,m;
struct P
{
int v,u,w;
}H[M];
bool solve(P x,P y)
{
return x.w<y.w;
}
int dp[M],ans[M];
int j;
int main()
{
std::ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=;i<m;i++)
{
cin>>H[i].v>>H[i].u>>H[i].w;
}
sort(H,H+m,solve);
for(int i=;i<m;i=j)
{
for(j=i;H[i].w==H[j].w&&j<m;j++)
{
ans[H[j].u]=dp[H[j].u];
}
for(j=i;H[i].w==H[j].w&&j<m;j++)
{
ans[H[j].u]=max(ans[H[j].u],dp[H[j].v]+);
}
for(j=i;H[i].w==H[j].w&&j<m;j++)
{
dp[H[j].u]=ans[H[j].u];
}
}
int Max=-;
for(int i=;i<=n;i++)
{
Max=max(dp[i],Max);
}
cout<<Max<<endl;
return ;
}

Codeforces Round #261 (Div. 2) E的更多相关文章

  1. Codeforces Round #261 (Div. 2)[ABCDE]

    Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden ...

  2. Codeforces Round #261 (Div. 2) B

    链接:http://codeforces.com/contest/459/problem/B B. Pashmak and Flowers time limit per test 1 second m ...

  3. Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP

    http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35  36组数据 ...

  4. Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida&#39;s problem(求逆序数对)

    题目链接:http://codeforces.com/contest/459/problem/D D. Pashmak and Parmida's problem time limit per tes ...

  5. Codeforces Round #261 (Div. 2) - E (459E)

    题目连接:http://codeforces.com/contest/459/problem/E 题目大意:给定一张有向图,无自环无重边,每条边有一个边权,求最长严格上升路径长度.(1≤n,m≤3 * ...

  6. Codeforces Round #261 (Div. 2) B. Pashmak and Flowers 水题

    题目链接:http://codeforces.com/problemset/problem/459/B 题意: 给出n支花,每支花都有一个漂亮值.挑选最大和最小漂亮值得两支花,问他们的差值为多少,并且 ...

  7. Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)

    题目链接:http://codeforces.com/problemset/problem/459/A A. Pashmak and Garden time limit per test 1 seco ...

  8. Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP

    题意:n个点,m条边,每条边有一个权值,找一条边数最多的边权严格递增的路径,输出路径长度. 解法:先将边权从小到大排序,然后从大到小遍历,dp[u]表示从u出发能够构成的严格递增路径的最大长度. dp ...

  9. Codeforces Round 261 Div.2 D Pashmak and Parmida's problem --树状数组

    题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求有多少对这样的(i,j). 解法:分别从左到右,由右到 ...

  10. Codeforces Round #261 (Div. 2)

    第一场难得DIV2简单+AK人数多: E:给出一张图,求最多的边数,满足:在这个边的集合中后面的边的权值大于前面的边; 思路:我们将图按权值排列,以为只可能边权值小的跟新权值大的所以对于一条边我们只跟 ...

随机推荐

  1. scala快速学习笔记(三):Collections,包

    VI.Collections 1.Array 一些常用方法:println,  map( _ * 2), filter(_ % 2 == 0),  sum,   reserve Array是不可变的, ...

  2. 20170225 ABAP获取字符串长度/字节长度

    函数YGET_CHAR_LONG: FUNCTION YGET_CHAR_LONG. *"-------------------------------------------------- ...

  3. redis13-----配置文件

    ==配置文件全解=== ==基本配置 daemonize no 是否以后台进程启动 databases 创建database的数量(默认选中的是database ) #刷新快照到硬盘中,必须满足两者要 ...

  4. POJ3252 Round Numbers —— 数位DP

    题目链接:http://poj.org/problem?id=3252 Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Su ...

  5. POJ1077 Eight —— 正向BFS

    主页面:http://www.cnblogs.com/DOLFAMINGO/p/7538588.html 代码一:以数组充当队列,利用结构体中的pre追溯上一个状态在数组(队列)中的下标: #incl ...

  6. Python中的sort() key含义

    sorted(iterable[, cmp[, key[, reverse]]]) iterable.sort(cmp[, key[, reverse]]) 参数解释: (1)iterable指定要排 ...

  7. 简易五子棋 V1.1.0

    main.cpp #include "fivechess.cpp" int main() { fivechess a; a.RunGame(); getchar(); return ...

  8. C/C++ 编译器优化

    0. gcc -o gcc -o 的优化仍然是机械的,想当然的.只有做到深入理解计算机系统,加深对编程语言的理解,才能写出最优化的代码. Linux下gcc 优化等级的介绍 gcc -o0 ⇒ 不提供 ...

  9. iphone 在设置了initial-scale=1 之后,在设置滚动条之后,没有滑动效果的解决办法

    iphone在设置了initial-scale=1 之后,我们终于可以以1:1 的比例进行页面设计了. 关于viewport,还有一个很重要的概念是:iphone 的safari 浏览器完全没有滚动条 ...

  10. 文本质量巧设置,一招让Visio 2007字体从模糊到清晰

    微软的Visio是一款很好用的画图工具,不过,它有一个地方不太好,就是中文字体比较模糊. 如下图: 矩形框内是宋体,9pt,字体很不清晰.无奈我就只好用雅黑字体,略微好一些. 今天发现一个设置,只有修 ...