CodeForces - 459E Pashmak and Graph[贪心优化dp]
1 second
256 megabytes
standard input
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.
The first line contains two integers n, m (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: ui, vi, wi (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.
Print a single integer — the answer to the problem.
3 3
1 2 1
2 3 1
3 1 1
1
3 3
1 2 1
2 3 2
3 1 3
3
6 7
1 2 1
3 2 5
2 4 2
2 5 2
2 6 9
5 4 3
4 3 4
6
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,尾端点为v,
裸地转移就是dp[v]=max(dp[v],dp[u]+1)
注意边相等要单独处理,然后就AC了……
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=3e5+;
struct edge{int x,y,z;}e[N<<];
int n,m;int f[N],g[N];
bool operator <(const edge &a,const edge &b){
return a.z<b.z;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].z);
sort(e+,e+m+);
for(int j,i=;i<=m;i++){
for(j=i;e[j].z==e[i].z;j++) g[e[j].y]=max(g[e[j].y],f[e[j].x]+);
for(j=i;e[j].z==e[i].z;j++) f[e[j].y]=g[e[j].y];
i=j-;
}
printf("%d\n",*max_element(f+,f+n+));
return ;
}
CodeForces - 459E Pashmak and Graph[贪心优化dp]的更多相关文章
- Codeforces 459E Pashmak and Graph(dp+贪婪)
题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...
- Codeforces 459E Pashmak and Graph:dp + 贪心
题目链接:http://codeforces.com/problemset/problem/459/E 题意: 给你一个有向图,每条边有边权. 让你找出一条路径,使得这条路径上的边权严格递增. 问你这 ...
- Codeforces 459E Pashmak and Graph
http://www.codeforces.com/problemset/problem/459/E 题意: 给出n个点,m条边的有向图,每个边有边权,求一条最长的边权上升的路径的长度. 思路:用f存 ...
- 【贪心优化dp决策】bzoj1571: [Usaco2009 Open]滑雪课Ski
还有贪心优化dp决策的操作…… Description Farmer John 想要带着 Bessie 一起在科罗拉多州一起滑雪.很不幸,Bessie滑雪技术并不精湛. Bessie了解到,在滑雪场里 ...
- CodeForces 311 B Cats Transport 斜率优化DP
题目传送门 题意:现在有n座山峰,现在 i-1 与 i 座山峰有 di长的路,现在有m个宠物, 分别在hi座山峰,第ti秒之后可以被带走,现在有p个人,每个人会从1号山峰走到n号山峰,速度1m/s.现 ...
- 『数组的最大代价 贪心优化DP』
数组的最大代价(51nod 1270) Description 数组A包含N个元素A1, A2......AN.数组B包含N个元素B1, B2......BN.并且数组A中的每一个元素Ai,都满足1 ...
- Codeforces 570E - Pig and Palindromes - [滚动优化DP]
题目链接:https://codeforces.com/problemset/problem/570/E 题意: 给出 $n \times m$ 的网格,每一格上有一个小写字母,现在从 $(1,1)$ ...
- Codeforces 643C Levels and Regions 斜率优化dp
Levels and Regions 把dp方程列出来, 把所有东西拆成前缀的形式, 就能看出可以斜率优化啦. #include<bits/stdc++.h> #define LL lon ...
- CodeForces 834D The Bakery(线段树优化DP)
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
随机推荐
- mysql 存储过程 invoker invoker
方法一:修改存储过程的definer update mysql.proc set definer='root@localhost' where db='db_name'; 方法二:修改sql secu ...
- redis4安装
第一步官网下载 https://redis.io/ rz 上传或者直接wget http://download.redis.io/releases/redis-4.0.2.tar.gz 2.将其下 ...
- golang 数组以及slice切片
老虞学GoLang笔记-数组和切片 数组 Arrays 数组是内置(build-in)类型,是一组同类型数据的集合,它是值类型,通过从0开始的下标索引访问元素值.在初始化后长度是固定的,无法修改其 ...
- 设计中最常用的CSS选择器
准确而简洁的运用CSS选择器会达到非常好的效果.我们不必通篇给每一个元素定义类(class)或ID,通过合适的组织,可以用最简单的方法实现同样的效果.在实际工作中,最常用的选择器有以下五类: 一.标签 ...
- 使用特殊构造的5GB文件测试Win2012Dedup功能
WinServer2012一个崭新的功能就是支持重复数据删除功能.为了测试这个功能的情况,特别构造了一个特殊的5GB文件用于此次测试.惯例,先说下测试环境的搭建1 使用ESXi5上的版本8的VM,安装 ...
- selenium测试(Java)(三)
控制浏览器: http://www.cnblogs.com/moonpool/p/5657752.html
- C++ 数据抽象
C++ 数据抽象数据抽象是指,只向外界提供关键信息,并隐藏其后台的实现细节,即只表现必要的信息而不呈现细节. 数据抽象是一种依赖于接口和实现分离的编程(设计)技术. 让我们举一个现实生活中的真实例子, ...
- Eclipse最经常使用快捷键总结
1. ctrl+shift+r:打开资源 这可能是全部快捷键组合中最省时间的了. 这组快捷键能够让你打开你的工作区中不论什么一个文件,而你仅仅须要按下文件名称或mask名中的前几个字母,比方appl ...
- MVC生成图片验证码,可指定位数
前台: <h2>mvc后台生成验证码,可指定位数</h2> <img id="gc" src="GetValidateCode" ...
- 什么是mysql的元数据呢?
需求描述: 什么是mysql的元数据?metadata? 概念解释: 简单来说:任何描述数据库的数据就是数据库的元数据库(与数据库中存在的内容相对) 比如,用于描述一个冰箱是如何构成,怎么使用的就是冰 ...