Codeforces Round #261 (Div. 2) E
Description
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]=max(dp[u],dp[v]+1)(v起点,u终点)
根据权值从小到大排序,分别处理相同权值片段的长度,最后求最大值
#include<stdio.h>
//#include<bits/stdc++.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<sstream>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<limits.h>
#define inf 0x7fffffff
#define INFL 0x7fffffffffffffff
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define ULL unsigned long long
using namespace std;
const int M = ;
int n,m;
struct P
{
int v,u,w;
}H[M];
bool solve(P x,P y)
{
return x.w<y.w;
}
int dp[M],ans[M];
int j;
int main()
{
std::ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=;i<m;i++)
{
cin>>H[i].v>>H[i].u>>H[i].w;
}
sort(H,H+m,solve);
for(int i=;i<m;i=j)
{
for(j=i;H[i].w==H[j].w&&j<m;j++)
{
ans[H[j].u]=dp[H[j].u];
}
for(j=i;H[i].w==H[j].w&&j<m;j++)
{
ans[H[j].u]=max(ans[H[j].u],dp[H[j].v]+);
}
for(j=i;H[i].w==H[j].w&&j<m;j++)
{
dp[H[j].u]=ans[H[j].u];
}
}
int Max=-;
for(int i=;i<=n;i++)
{
Max=max(dp[i],Max);
}
cout<<Max<<endl;
return ;
}
Codeforces Round #261 (Div. 2) E的更多相关文章
- Codeforces Round #261 (Div. 2)[ABCDE]
Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden ...
- Codeforces Round #261 (Div. 2) B
链接:http://codeforces.com/contest/459/problem/B B. Pashmak and Flowers time limit per test 1 second m ...
- 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 Round #261 (Div. 2)459D. Pashmak and Parmida's problem(求逆序数对)
题目链接:http://codeforces.com/contest/459/problem/D D. Pashmak and Parmida's problem time limit per tes ...
- Codeforces Round #261 (Div. 2) - E (459E)
题目连接:http://codeforces.com/contest/459/problem/E 题目大意:给定一张有向图,无自环无重边,每条边有一个边权,求最长严格上升路径长度.(1≤n,m≤3 * ...
- Codeforces Round #261 (Div. 2) B. Pashmak and Flowers 水题
题目链接:http://codeforces.com/problemset/problem/459/B 题意: 给出n支花,每支花都有一个漂亮值.挑选最大和最小漂亮值得两支花,问他们的差值为多少,并且 ...
- Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)
题目链接:http://codeforces.com/problemset/problem/459/A A. Pashmak and Garden time limit per test 1 seco ...
- Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
题意:n个点,m条边,每条边有一个权值,找一条边数最多的边权严格递增的路径,输出路径长度. 解法:先将边权从小到大排序,然后从大到小遍历,dp[u]表示从u出发能够构成的严格递增路径的最大长度. dp ...
- Codeforces Round 261 Div.2 D Pashmak and Parmida's problem --树状数组
题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求有多少对这样的(i,j). 解法:分别从左到右,由右到 ...
- Codeforces Round #261 (Div. 2)
第一场难得DIV2简单+AK人数多: E:给出一张图,求最多的边数,满足:在这个边的集合中后面的边的权值大于前面的边; 思路:我们将图按权值排列,以为只可能边权值小的跟新权值大的所以对于一条边我们只跟 ...
随机推荐
- openwrt: sysupgrade
sysupgrade 用法: sysupgrade [...] sysupgrade [-q] [-i] 升级选项: -d 重启前等待 delay 秒 -f 从 .tar.gz (文件或链接) 中恢复 ...
- 新产品为了效果,做的比較炫,用了非常多的图片和JS,所曾经端的性能是非常大的问题,分篇记录前端性能优化的一些小经验。
第一篇:HTTPserver 因tomcat处理静态资源的速度比較慢,所以首先想到的就是把全部静态资源(JS,CSS,image,swf) 提到单独的server,用更加高速的HTTPserver,这 ...
- 提升自身的iOS编程水平 (转载)
阅读博客 在现在这个碎片化阅读流行的年代,博客的风头早已被微博盖过.而我却坚持写作博客,并且大量地阅读同行的iOS开发博客.博客的文章长度通常在3000字左右,许多iOS开发知识都至少需要这样的篇幅才 ...
- POJ1077 Eight —— 正向BFS
主页面:http://www.cnblogs.com/DOLFAMINGO/p/7538588.html 代码一:以数组充当队列,利用结构体中的pre追溯上一个状态在数组(队列)中的下标: #incl ...
- WAS域名解析问题
1.如果dmgr和his在一台机器上,但web服务器用的是app服务器 如下图: 插件都处理完成. 这时候,通过外网域名访问时,出现如下情况 或者 说明:ihs服务器上webserver1的文件Plu ...
- codeforces 414A A. Mashmokh and Numbers(素数筛)
题目链接: A. Mashmokh and Numbers time limit per test 1 second memory limit per test 256 megabytes input ...
- hel软工网络16个人作业1
1Task1:注册个人博客账号 1Task2:注册码云账号 1Task3:提出问题 3.1问题一:软件工程是什么? 在第一章中我们可以从P8得到: 1.软件工程就是把系统的.有序的.可量化的方法应用到 ...
- 基于redis实现tomcat8的tomcat集群的session持久化实现(tomcat-redis-session-manager二次开发)
前言: 本项目是基于jcoleman的tomcat-redis-session-manager二次开发版本 1.修改了小部分实现逻辑 2.去除对juni.jar包的依赖 3.去除无效代码和老版本tom ...
- darwin Stream Server源码分析
摘要 所谓的流式媒体简单的讲就是指人们通过网络实时的收看多媒体信息:如音频流.视频流等.与流式媒体对应的传统工作方式是下载+播放模式,即用户首先下载多媒体文件,然后再在本地播放,这种方法的一个主要缺 ...
- 在 Ubuntu 系统中有三种设置环境变量 PATH 的方法。(ZT) repost
来源地址: http://blog.csdn.net/jernymy/article/details/6547671 第一种适用于为单一用户设置PATH.第二种是为全局设置 PATH.第三种方法适合于 ...