题目链接:

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. Day 2 操作系统基础

    课前复习新知识 RAID(Redundant Arrays of Independent Disks)独立冗余磁盘阵列 定义:加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵 ...

  2. R必学包之dplyr

    http://www.360doc.com/content/17/1204/10/50223086_709726679.shtml

  3. IntelliJ IDEA 使用的问题总结

     第一个问题:idea 无法创建springboot的项目     1. 点击IDEA setting之后,找到Http Proxy 选择Atuo-detect proxy settings 之后点击 ...

  4. Ubuntu 16.04安装微信

    微信没有出Linux的版本,但是可以通过以下方式解决: 1.使用网页版,除了没有公众号之后,一切都没问题,包括传文件等. 网页登录地址:https://wx.qq.com/ 2.使用第三方版本,只不过 ...

  5. install Python 2.7 and Python 3.3 on CentOS 6

    来自:http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/ In this guide I will show you ...

  6. Java8期间及持续时间

    原文:http://www.yiibai.com/java8/java8_periodduration.html 使用Java8,两个专门类引入来处理时间差. Period - 处理有关基于时间的日期 ...

  7. virtualenv 配置python3环境

    virtualenv -p /usr/bin/python3 py3env source py3env/bin/activate pip install package-name

  8. BUPT复试专题—最小距离查询(2013)

    题目描述 给定一个由小写字母a到z组成的字符串S,其中第i个字符为S[i](下标从0开始).你需要完成下面两个操作:INSERT c  其中c是一个待输入的字符.你需要在字符串的末尾添加这个字符.保证 ...

  9. salt.states.file试用

    从master往linux上的minion复制文件参考http://netkiller.sourceforge.net/linux/management/saltstack.html后半部分,他已经写 ...

  10. Redis实现消息的发布/订阅

    利用spring-boot结合redis进行消息的发布与订阅: 发布: class Publish { private static String topicName = “Topic:chat”; ...