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 题意: 给你一个有向图,每条边有边权. 让你找出一条路径,使得这条路径上的边权严格递增. 问你这 ...
随机推荐
- jquery判断元素是否隐藏的多种方法
第一种:使用CSS属性 复制代码 代码如下: var display =$('#id').css('display'); if(display == 'none'){ alert("被 ...
- ./configure : /bin/sh^M : bad interpreter
用命令行来编译Qt的时候发生标题尚的错误. 原因是文件中带有DOS行结束符,必须把它转换成UNix结束符 references: http://stackoverflow.com/questions/ ...
- (?:pattern) (?=pattern) (?!pattern)
(pattern) 匹配 pattern 并获取这一匹配.所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0- ...
- Topological Sorting
Topological sorting/ ordering is a linear ordering of its vertices such that for every directed edge ...
- Repeated DNA Sequences 解答
Question All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: ...
- 左右HttpClient上传的方法来解决中国的乱码
二手HttpClient人们都知道通过addTextBody方法来加入要上传的文本信息,可是,假设要上传中文的话.或还有中文名称的文件会出现乱码的问题,解决的方法事实上非常easy: 第一步:设置Mu ...
- gcc和arm-linux-gcc区别
安装arm-linux-gcc的时候,查了不少资料,总算环境搭好了.于是,想写个程序员的经典程序---hello world. 语法都没错,生成test.c. 命令行运行:arm-linux-gcc ...
- 【类似N^N做法的斐波那契数列】【HDU1568】 Fibonacci
Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- FFmpeg详解
认识FFMPEG FFMPEG堪称自由软件中最完备的一套多媒体支持库,它几乎实现了所有当下常见的数据封装格式.多媒体传输协议以及音视频编解码器.因此,对于从事多媒体技术开发的工程师来说,深入研究FFM ...
- [转载]android中The connection to adb is down,问题和解决
原网址:http://blog.sina.com.cn/s/blog_5fc93373010164p3.html 今天我出现了第3个错误,于是百度了一下.感觉这篇博客对我挺有帮助,推荐给大家.以下是原 ...