zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    错误
    分页查询
    异步请求jquery
    深入理解C/C++ [Deep C (and C++)]
    C语言经典算法100例(三)
    《Python》 计算机基础
    Python程序员的进化史
    以前没有写笔记的习惯,现在慢慢的发现及时总结是多么的重要。 这一篇文章主要关于java多线程一些常见的疑惑点。因为讲解多线程的书籍和文章已经很多了,所以我也不好意思多说,嘻嘻嘻、大家可以去参考一些那些书籍。我这个文章主要关于实际的一些问题。同时也算是我以后复习的资料吧,。还请大家多多指教。 同时希望多结交一些技术上的朋友。谢谢。
    快速读入函数
    一元二次方程公式
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5657918.html
Copyright © 2011-2022 走看看