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

题意是给定n个点m条边,求一条最长路径,使得路径上的边的权值严格递增

这题原来是ccr给的代码,但是实际上还是挺水的……我觉得最难的是A和C啊QAQ

先把边按权值排序,然后令f[i]表示以i结尾的路径的最大长度

然后一条一条加进去就好了

以下ccr的代码

#include<bits/stdtr1c++.h>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef double ld;
typedef struct edge{
int f, t, v;
bool operator<(const edge& e) const{
return v < e.v;
}
}edge;
const int N = 3e5 + 50;
int ans[N];
vector< pi > w;
int main() {
int n, m, a, b, c;
vector< edge > v;
cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> a >> b >> c;
edge e = {a, b, c};
v.push_back(e);
}
sort(v.begin(), v.end()); int i = 0, j;
while( i < v.size()){
j = i;
while(j < v.size() && v[j].v == v[i].v){
if(ans[v[j].f] + 1 > ans[v[j].t]) w.push_back(pi(v[j].t, ans[v[j].f] + 1));
j++;
}
for(int q = 0; q < w.size(); q++){
ans[w[q].first] = max(ans[w[q].first], w[q].second);
}
i = j;
w.clear();
}
cout << *max_element(ans, ans + N) << endl;
}

cf459E Pashmak and Graph的更多相关文章

  1. CF459E Pashmak and Graph (DP?

    Codeforces Round #261 (Div. 2) E - Pashmak and Graph E. Pashmak and Graph time limit per test 1 seco ...

  2. CF459E Pashmak and Graph (Dag dp)

    传送门 解题思路 \(dag\)上\(dp\),首先要按照边权排序,然后图都不用建直接\(dp\)就行了.注意边权相等的要一起处理,具体来讲就是要开一个辅助数组\(g[i]\),来避免同层转移. 代码 ...

  3. Codeforces 459E Pashmak and Graph(dp+贪婪)

    题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...

  4. CodeForces - 459E Pashmak and Graph[贪心优化dp]

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

  5. codeforces 459E E. Pashmak and Graph(dp+sort)

    题目链接: E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input st ...

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

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

  7. Codeforces 459E Pashmak and Graph

    http://www.codeforces.com/problemset/problem/459/E 题意: 给出n个点,m条边的有向图,每个边有边权,求一条最长的边权上升的路径的长度. 思路:用f存 ...

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

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

  9. Codeforces 459E Pashmak and Graph:dp + 贪心

    题目链接:http://codeforces.com/problemset/problem/459/E 题意: 给你一个有向图,每条边有边权. 让你找出一条路径,使得这条路径上的边权严格递增. 问你这 ...

随机推荐

  1. USB-CSW之旅

    前面总结了usb mass storage的枚举过程,如果所有枚举正常,那么会进入bulkonly协议定义的数据交流,在这个数据包里面还包含了SCSI的协议. Bulkonly协议usbmassbul ...

  2. 狗屎的asp.net core 果然发生了重大的变更

    狗屎的asp.net core  果然发生了重大的变更 ....仔细看官方的文档,想骂娘....你发布版本的时候,能不能先想好先设计好再发布?朝三暮四...大撒笔....你老这样变来变去,谁还敢碰你啊 ...

  3. Android ListView实现圆角

    首先呢,我们还是看几个示图: 这种带有圆角的listview' 看起来很棒吧,确实是这样,其实也不能这么说,主要方形太多了,斯通见惯就不值钱了,“物以稀为贵嘛”. 就好比学java都搞androd,很 ...

  4. Unix/Linux环境C编程入门教程(37) shell常用命令演练

     cat命令 cat命令可以用来查看文件内容. cat [参数] 文件名. grep-指定文件中搜索指定字符内容. Linux的目录或文件. -path '字串' 查找路径名匹配所给字串的所有文件 ...

  5. Validate Binary Search Tree 解答

    Question Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is d ...

  6. Median of Two Sorted Arrays 解答

    Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...

  7. 剑指offer-面试题17.合并两个排序的链表

    题目:输入两个递增的排序的链表,合并这两个链表并使新链表中的节点仍然是 按照递增排序的.例如链表1链表2合并为链表3. List1:->->-> List2:->->-& ...

  8. Android专项面试训练题(一)

    1.下面不可以退出Activity的是?(D) A.finish() B.抛异常强制退出 C.System.exit(0) D.onStop() 解析: A, finish() 方法就是退出activ ...

  9. rsync常用参数详解

    rsync常用参数详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在linux中,一切皆是文件,包括你的终端,硬件设备信息,目录,内核文件等等.所以工作中我们难免会遇到拷贝文件 ...

  10. STL中的set集合容器进行集合运算:并、交、差实例

    集合容器的集合运算:并.交.差: #include "stdafx.h" #include <iostream> #include <set> #inclu ...