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
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<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=3e5+10;
const int maxn=1005;
const double eps=1e-10;

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,1,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+1,po+m+1,cmp);
        int cnt=1;
        le[1]=1;
        For(i,2,m)
        {
            if(po[i].w!=po[i-1].w)
            {
                le[++cnt]=i;
            }
        }
        le[++cnt]=m+1;


        for(int j=1;j<=cnt;j++)
        {
            for(int i=le[j];i<le[j+1];i++)
            temp[po[i].v]=max(dis[po[i].u]+1,temp[po[i].v]);
            for(int i=le[j];i<le[j+1];i++)
            dis[po[i].v]=temp[po[i].v];
        }
        int ans=0;
        For(i,1,n)
        {
        ans=max(ans,dis[i]);
        }
        cout<<ans<<"
";
        return 0;
}
原文地址:https://www.cnblogs.com/zhangchengc919/p/5657918.html