题目链接:

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 nm (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: uiviwi (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.

Examples
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

题意:

一个有向图,找出一条最长的路径,这条路径上的每条边权重都严格递增;问最长的长度是多少;

思路:

按边的权重排序,排完后把一层一层的更新;

AC代码:
#include <bits/stdc++.h>
/*
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define For(i,j,n) for(int i=j;i<=n;i++)
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=3e5+;
const int maxn=;
const double eps=1e-; int dis[N],temp[N],le[N];
/*
struct Edge
{
int from,to,va,next;
}edge[N];
void add_edge(int s,int e,int w)
{
edge[cnt].next=head[s];
edge[cnt].from=s;
edge[cnt].to=e;
edge[cnt].va=w;
head[s]=cnt++;
}*/
struct PO
{
int u,v,w;
}po[N];
int cmp(PO x,PO y)
{
return x.w<y.w;
}
/*
void dfs(int cur,int fa,int wi)
{
cout<<cur<<" "<<fa<<" "<<wi<<" "<<dis[cur]<<"###"<<endl;
for(int i=head[cur];i!=-1;i=edge[i].next)
{
int y=edge[i].to,w=edge[i].va;
cout<<y<<" "<<w<<"$$$$$"<<endl;
if(w>wi)
{
if(dis[y]<dis[cur]+1)dis[y]=dis[cur]+1,dfs(y,cur,w);
}
}
}*/ int n,m; int main()
{ read(n);read(m);
For(i,,m)
read(po[i].u),read(po[i].v),read(po[i].w);//add_edge(po[i].u,po[i].v,po[i].w);//add_edge(v,u,w); sort(po+,po+m+,cmp);
int cnt=;
le[]=;
For(i,,m)
{
if(po[i].w!=po[i-].w)
{
le[++cnt]=i;
}
}
le[++cnt]=m+; for(int j=;j<=cnt;j++)
{
for(int i=le[j];i<le[j+];i++)
temp[po[i].v]=max(dis[po[i].u]+,temp[po[i].v]);
for(int i=le[j];i<le[j+];i++)
dis[po[i].v]=temp[po[i].v];
}
int ans=;
For(i,,n)
{
ans=max(ans,dis[i]);
}
cout<<ans<<"\n";
return ;
}

codeforces 459E E. Pashmak and Graph(dp+sort)的更多相关文章

  1. Codeforces 459E Pashmak and Graph(dp+贪婪)

    题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...

  2. Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP

    http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35  36组数据 ...

  3. codeforces 459 E. Pashmak and Graph(dp)

    题目链接:http://codeforces.com/contest/459/problem/E 题意:给出m条边n个点每条边都有权值问如果两边能够相连的条件是边权值是严格递增的话,最长能接几条边. ...

  4. codeforces 459E

    codeforces 459E E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabyte ...

  5. CodeForces - 459E Pashmak and Graph[贪心优化dp]

    E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. CF459E Pashmak and Graph (DP?

    Codeforces Round #261 (Div. 2) E - Pashmak and Graph E. Pashmak and Graph time limit per test 1 seco ...

  7. cf459E Pashmak and Graph

    E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. Codeforces Round #261 (Div. 2) E (DP)

    E. Pashmak and Graph Pashmak's homework is a problem about graphs. Although he always tries to do hi ...

  9. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

随机推荐

  1. Maven单元测试

    // SKU码:系列前3位+6位年月日+3位序号(自动生产,取数据库中当天最大的,没有就赋值位001) // 订单编号:BRD+6位年月日+5位序号 // // 退单号:BRT+6位年月日+3位序号 ...

  2. [JSOI2016]反质数序列

    我竟然半个小时切了一道JSOI2016,,,,不敢相信. 首先可以发现,如果N个数中1出现的次数<=1的话,我们按不能在一个集合连无向边的话,连出的一定是一个二分图. 接下来我来证明一下: 因为 ...

  3. Android SDK Manager 更新时的“https://dl-ssl.google.com refused”错误

    Android SDK Manager 消除SDK更新时的“https://dl-ssl.google.com refused”错误 消除SDK更新时,有可能会出现这样的错误:Download int ...

  4. 转 vs2010转vs2008 其他的一样

    如果你使用VS2010的任何版本写代码,那么在VS2008中就不能打开VS2010的解决方案了,为此,通过以下三步就可以解决了一.对于工程名.sln; 1.用你喜欢的编辑器打开sln文件,比如note ...

  5. Linux查看系统状态命令top

    用法 top 自动刷新系统状态,要结束使用[Ctrl]+[C] 效果图: 信息解释(转自百度经验http://jingyan.baidu.com/article/4d58d5412917cb9dd4e ...

  6. bootstrap -- col-sm-6 和 col-xs-6

  7. LVM创建

    LVM介绍 PV(Physical Volume) - 物理卷 物理卷在逻辑卷管理中处于最底层,它可以是实际物理硬盘上的分区,也可以是整个物理硬盘,也可以是raid设备 VG(Volume Group ...

  8. [转]java中的字符串相关知识整理

    字符串为什么这么重要 写了多年java的开发应该对String不陌生,但是我却越发觉得它陌生.每学一门编程语言就会与字符串这个关键词打不少交道.看来它真的很重要. 字符串就是一系列的字符组合的串,如果 ...

  9. docker下用keepalived+Haproxy实现高可用负载均衡集群

    启动keepalived后宿主机无法ping通用keepalived,报错: [root@localhost ~]# ping 172.18.0.15 PING () bytes of data. F ...

  10. Ubuntu安装教程--Win7系统中含100M保留分区

    1.检查 Win7 保留分区 1)进入 Win7 打开库目录.在左側栏找到"计算机",瞄准点右键选择"管理"菜单: 2)在出来的管理面板左边找到"磁盘 ...