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 题意: 给你一个有向图,每条边有边权. 让你找出一条路径,使得这条路径上的边权严格递增. 问你这 ...
随机推荐
- Wpf中MediaElement循环播放
原文:Wpf中MediaElement循环播放 前一段时间做了一个项目,里面牵涉到媒体文件的循环播放问题,在网上看了好多例子,都是在xaml中添加为MediaElement添加一个TimeLine,不 ...
- Windows 7下可以使用的各个命令语句+C#打开
Windows 7下可以使用的各个命令语句: control.exe /name microsoft.folderoptions 启动资源管理器的 文件夹属性 选项卡 control.exe /n ...
- 徐汉彬:亿级Web系统搭建—单机到分布式集群
当一个Web系统从日访问量10万逐步增长到1000万,甚至超过1亿的过程中,Web系统承受的压力会越来越大,在这个过程中,我们会遇到很多的问题.为了解决这些性能压力带来问题,我们需要在Web系统架构层 ...
- IOS 计算密码强度
+ (BOOL) judgeRange:(NSArray*)conditionArr Password:(NSString*)password { NSRange range; BOOL result ...
- easy ui 如何单个引用其中某个插件?
记录一下这个方法,前端时间一直在纠结这个问题,后来听一些前辈讲解后才恍然大悟,要单独引用某个插件,我们需要重视的是:easyloaer.js ,easy ui的下载包中也有easyloader的dem ...
- ViewPager的Adapter中视图重用
ViewPager的PagerAdapter不像ListView/GridView的BaseAdapter,它是没有内部视图重用机制的,也就是说我先inflate出来一个,然后调用destroyIte ...
- Android launcher3 开发初始篇
版本号:1.0 日期:2014.8.26 2014.8.27 2014.11.10 版权:© 2014 kince 转载注明出处 好久没有写博客,也是由于工作比較忙的关系.当然这不是理 ...
- mysql、sqlServer、hsql、oracle、db2各数据库支持的字段类型与最大精度
- UVA 12232 - Exclusive-OR(带权并查集)
UVA 12232 - Exclusive-OR 题目链接 题意:有n个数字.一開始值都不知道,每次给定一个操作,I a v表示确认a值为v,I a b v,表示确认a^b = v,Q k a1 a2 ...
- C# 数据的序列化存取
1,什么是序列化? 序列化 (Serialization)将对象的状态信息转换为可以存储或传输的形式的过程.在序列化期间,对象将其当前状态写入到临时或持久性存储区.以后,可以通过从存储区中读取或反序列 ...