cf459E Pashmak and Graph
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
.
题意是给定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的更多相关文章
- CF459E Pashmak and Graph (DP?
Codeforces Round #261 (Div. 2) E - Pashmak and Graph E. Pashmak and Graph time limit per test 1 seco ...
- CF459E Pashmak and Graph (Dag dp)
传送门 解题思路 \(dag\)上\(dp\),首先要按照边权排序,然后图都不用建直接\(dp\)就行了.注意边权相等的要一起处理,具体来讲就是要开一个辅助数组\(g[i]\),来避免同层转移. 代码 ...
- Codeforces 459E Pashmak and Graph(dp+贪婪)
题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...
- CodeForces - 459E Pashmak and Graph[贪心优化dp]
E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 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 ...
- Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
题意:n个点,m条边,每条边有一个权值,找一条边数最多的边权严格递增的路径,输出路径长度. 解法:先将边权从小到大排序,然后从大到小遍历,dp[u]表示从u出发能够构成的严格递增路径的最大长度. dp ...
- Codeforces 459E Pashmak and Graph
http://www.codeforces.com/problemset/problem/459/E 题意: 给出n个点,m条边的有向图,每个边有边权,求一条最长的边权上升的路径的长度. 思路:用f存 ...
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP
http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35 36组数据 ...
- Codeforces 459E Pashmak and Graph:dp + 贪心
题目链接:http://codeforces.com/problemset/problem/459/E 题意: 给你一个有向图,每条边有边权. 让你找出一条路径,使得这条路径上的边权严格递增. 问你这 ...
随机推荐
- Linux下的命令行上网
对于网页浏览器现在大多数人用links/elinks,对了,还有个老牌一点的文本浏览器Lynx,links/elinks也是从Lynx中fork出来的. 以上所说的虽然能字符界面来浏览网页,但是不能显 ...
- easyui 表单验证validatetype——支持自定义验证
easyui 的validatebox()提供了自定义验证的方法,为此我把一些常用的数据验证汇总了一下,代码如下: 代码 Code highlighting produced by Actipro C ...
- openssl 生成CSR
openssl 生成CSR 2013-12-27 15:05 3699人阅读 评论(1) 收藏 举报 分类: Security(38) C/C++(105) 版权声明:本文为博主原创文章,未经博 ...
- ArcSDE for Oracle表空间管理——暂时(TEMP)表空间
Oracle暂时表空间主要用来做查询和存放一些缓冲区数据.暂时表空间消耗的主要原因是须要对查询的中间结果进行排序. 重新启动数据库能够释放暂时表空间,假设不能重新启动实例,而一直保持问题sql语句的运 ...
- MVC4 jquery 样式 主题 用法(案例)
MVC4已经自带了jquery,新建的项目,基本上什么都不用添加就可以运行,跑项目.(集成了那么多东西,jquery,modernizr,自带的默认权限,生成的模板,但是缺没有一个统一的文档或者什么去 ...
- Unity 3D 调用摄像头捕获照片 录像
1,要想调用摄像头首先要打开摄像头驱动,如果用户允许则可以使用. 2,定义WebCamTexture的变量用于捕获单张照片. 3,连续捕获须启用线程. 实现代码: using UnityEngine; ...
- visual studio 2010 C语言声明异常
如下这段程序,是C_Primer_plus_第五版内的一个复习题答案(感觉声明i的值有问题),在GCC上面可以运行,但是移植到VS2010就一堆错误, #include<stdio.h> ...
- [WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored (webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')
WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored (webxml attribu ...
- java学习之部分笔记
1.枚举类型 2.String的方法Index的用法.StringBuffer 的用法.Math的用法.Date类 3.用abstract修饰的类就是抽象类.抽象方法不能有主体.抽象类中,可以有抽象方 ...
- 如何安装chrome扩展,以json-handle
读取本地json文件 chrome插件安装 方式一,在线安装 直接插到json-handle地址,添加即可 https://chrome.google.com/webstore/detail/json ...