CF 459E && 市赛I题

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 .

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=3e5+100;
struct node
{
    int u;
    int v;
    int next;
    int w;
}edge[maxn];
int pre[maxn];
long long d[maxn];
long long dp[maxn];
int ct;
void add(int u,int v,int w)
{
    edge[ct].u=u;
    edge[ct].v=v;
    edge[ct].w=w;
    edge[ct].next=pre[u];
    pre[u]=ct++;
}
bool cmp(node a,node b)
{
    return a.w<b.w;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ct=0;
        memset(pre,-1,sizeof(pre));
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
        }
        sort(edge,edge+m,cmp);
        memset(dp,0,sizeof(dp));
        memset(d,0,sizeof(d));
        long long ans=-1;
        int j=0;
        for(int i=0;i<m;i++)
        {
            int j;
           for( j=i;edge[j].w==edge[i].w&&j<m;j++);//权值相同的边一起加

           for(int k=i;k<j;k++)
              d[edge[k].v]=max(d[edge[k].v],dp[edge[k].u]+1);//先保留原来节点值,后更新,不然会更新出错。
           for(int k=i;k<j;k++)
            dp[edge[k].v]=d[edge[k].v];
           i=j-1;
        }
        for(int i=1;i<=n;i++)
            ans=max(ans,dp[i]);
         printf("%I64d\n",ans);
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值