codeforces 459E
codeforces 459E
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 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.
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

.
题目类型:DP
思路:1. dfs铁定超时,因为一开始没想到办法如何两条边去更新一个点时存储哪一个
2.看到排序之后立马有思路,但是还是超时?!为什么?因为我不是更新的点,是去更新的边,这样一条边可能会被访问很多很多次,而如果访问点的话,一条边只需要被访问一次(当然后来修改为AC的之后一条边可能被访问2次),当然就算不超时,后面也会wa的为什么呢?下面解释、、
3.修改为更新点,wa一次。。因为当更新第一次后,后面相同的wei可能会被放弃,最后面的一组样例就是这样错的,使用vector存储要更新的点
4.改了之后还是wa,因为要更新的点覆盖了之前这个点的更新信息,所以用pair存储就可以了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<map>
#include<algorithm>
#define M(a,b) memset(a,b,sizeof(a))
using namespace std;
int n,m;
int res;
int p[];
int psave[];
struct ed
{
int from;
int to;
int wei;
bool operator < (const ed& rhs) const
{
return wei<rhs.wei;
}
}edge[];
vector<pair<int,int> > g;
void dp()
{
int pre = edge[].wei;
//cout<<u<<'!'<<endl;
for(int i = ;i<m;i++)
{
if(pre!=edge[i].wei)
{
pre = edge[i].wei;
for(int j = ;j<g.size();j++)
if(g[j].second>p[g[j].first]) p[g[j].first] = g[j].second;
g.clear();
}
//cout<<edge[i].from<<' '<<edge[i].to<<' '<<p[edge[i].from]<<' '<<pw[edge[i].from]<<endl;
if(p[edge[i].from]+>p[edge[i].to])
{
g.push_back(make_pair(edge[i].to,p[edge[i].from]+));
if(p[edge[i].from]+>res) res = p[edge[i].from]+;
}
}
for(int j = ;j<g.size();j++)
if(psave[g[j].second]>p[g[j].first]) p[g[j].first] = psave[g[j].second];
return;
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
int u,v,w;
res = ;
M(p,);
for(int i = ;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
edge[i].from = u;
edge[i].to = v;
edge[i].wei = w;
}
sort(edge,edge+m);
// cout<<temu<<endl;
p[edge[].from] = ;
dp();
for(int i = ;i<n;i++)
{
if(p[i]>res)
res = p[i];
}
printf("%d\n",res);
}
return ;
}
/*
9 8
1 2 1
2 3 2
4 5 1
5 6 2
6 3 3
3 7 4
7 8 5
8 9 6
*/
codeforces 459E的更多相关文章
- Codeforces 459E Pashmak and Graph(dp+贪婪)
题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...
- Codeforces 459E Pashmak and Graph
http://www.codeforces.com/problemset/problem/459/E 题意: 给出n个点,m条边的有向图,每个边有边权,求一条最长的边权上升的路径的长度. 思路:用f存 ...
- Codeforces 459E Pashmak and Graph:dp + 贪心
题目链接:http://codeforces.com/problemset/problem/459/E 题意: 给你一个有向图,每条边有边权. 让你找出一条路径,使得这条路径上的边权严格递增. 问你这 ...
- 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 459E Roland and Rose
本以为是个树形DP,按照树形DP的方法在那里dfs,结果WA到死,因为它存在有向环,不是树,凡是存在环的情况切记不要用树形的方法去做 题目的突破点在于将边排完序之后,用点表示以该点为边结尾的最大长度, ...
- LUXURY 7
A.Little Pony and Expected Maximum Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:% ...
- Codeforces Round #261 (Div. 2) - E (459E)
题目连接:http://codeforces.com/contest/459/problem/E 题目大意:给定一张有向图,无自环无重边,每条边有一个边权,求最长严格上升路径长度.(1≤n,m≤3 * ...
- codeforces的dp专题
1.(467C)http://codeforces.com/problemset/problem/467/C 题意:有一个长为n的序列,选取k个长度为m的子序列(子序列中不能有位置重复),求所取的k个 ...
随机推荐
- HOLOLENS 扫描特效 及得出扫描结果(SurfacePlane)
HOLOLENS 扫描特效 及得出扫描结果(SurfacePlane) 要求只扫出地板和墙, 由于地板和墙面积较大 扫描结果 HOLOTOOLKIT老版本点在参数调节PlaneFinding.Find ...
- Python mock
在测试过程中,为了更好地展开单元测试,mock一些数据跟对象在所难免,下面讲一下python的mock的简单用法. 关于python mock,网上有很多资料,这里不会讲的特别深,但一定会是实用为主, ...
- 创业日志N,一听到别人说创业我就怕
最近总有些朋友想创业,咨询我的意见. 一般是在公司干腻了,有个想法,想出来试试水.还有是觉得自己的idea太棒了,想试试. 相同的是两者都有点怕.听说最近创业环境不好,没有投资!有个朋友推荐的创业者, ...
- 使用SharpPCap在C#下进行网络抓包
在做大学最后的毕业设计了,无线局域网络远程安全监控策略那么抓包是这个系统设计的基础以前一直都是知道用winpcap的,现在网上搜了一下,有用C#封装好了的,很好用下面是其中的几个用法这个类库作者的主页 ...
- .Net Core Linux centos7行—发布程序到生产环境
实验demo现在需要发布到生产环境,发现在发布的时候要考虑到不一致的几个地方. 1.各类配置文件线下,线上不一致. 2.绑定的url不一致,可能是域名不一致,也可能是schema不一致(http,ht ...
- React JS的基本用法[ES5,纯前端写法]
1.配置webpack npm install -g webpack #webpack的cli npm install -g webpack-dev-server #webpack自带的服务器 npm ...
- windows中LNK文件打开方式恢复(每日一修(1))
相信有些用户曾试过错误地把LNK文件的打开方式更改其他文件,导致系统所有的快捷方式都失效.在vista与Windows7系统还不普遍使用的时候,相信大家会有点惊慌失措,不要紧,下面只要大家进行如下操作 ...
- 冰冻三尺非一日之寒--Django框架【进阶篇】
第十九章 Django进阶 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去 ...
- PHP 做文件校验,MD5,CRC32,SHA等
函数 hash_file(): 使用给定文件的内容生成哈希值 说明 string hash_file ( string $algo , string $filename [, bool $raw_ou ...
- UOJ58 【WC2013】糖果公园
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...