zoukankan      html  css  js  c++  java
  • [Codeforces Round #261 (Div. 2) E]Pashmak and Graph(Dp)

    Description

    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.

    Solution

    题意:给出一个无自环、重边的带权有向图,求边权严格递增的最长的路径(可以是非简单路径)

    思路题QAQ

    O(mlogm)以边权排序后再逐个处理,这样就保证了递增,然后可以O(m)求解,维护每个点作为结尾的最长的边权上升路径的长度

    注意边权相等的情况要放在一起处理

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #define MAXN 300010
    using namespace std;
    int n,m,f[MAXN],g[MAXN];
    struct Node
    {
        int u,v,w;
        bool operator < (const Node& x) const
        {return w<x.w;}
    }Edges[MAXN];
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            memset(f,0,sizeof(f));
            memset(g,0,sizeof(g));
            for(int i=1;i<=m;i++)
            scanf("%d%d%d",&Edges[i].u,&Edges[i].v,&Edges[i].w);
            int t=1,res=0;
            sort(Edges+1,Edges+1+m);
            for(int i=1;i<=m;i++)
            {
                f[i]=g[Edges[i].u];
                
                if(Edges[i].w!=Edges[i+1].w)
                {
                    for(int j=t;j<=i;j++)
                    g[Edges[j].v]=max(g[Edges[j].v],f[j]+1),res=max(res,g[Edges[j].v]);
                    t=i+1;
                }
            }
            printf("%d
    ",res);
        }
        return 0;
    } 
  • 相关阅读:
    Java中List集合去除重复数据的六种方法
    常见的Redis面试"刁难"问题,值得一读
    以Integer类型传参值不变来理解Java值传参
    Linux系统安装snmp服务
    直接取数据到RANGE
    SAP翔子_2019集结号
    销售订单BOM组件分配(CP_BD_DIRECT_INPUT_PLAN_EXT)
    SAP翔子_webservice篇索引
    函数篇3 EXCEL导入函数去除行数限制
    ABAP基础篇4 常用的字符串操作语法
  • 原文地址:https://www.cnblogs.com/Zars19/p/6917791.html
Copyright © 2011-2022 走看看