zoukankan      html  css  js  c++  java
  • CF459E Pashmak and Graph

    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
    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 .

    这题和之前广工某题题意一样,但是广工那题说明了每个权值都不同,所以好做很多,但是这个题有相同的权值。所以需要处理一些东西,先按权值从小到大排序

    然后dp[edge[k].e]=max(dp[edge[k].e],dp[edge[k].s]+1).但是由于权值相同的问题,所以我们需要分段处理,把权值相同的分成一块,然后处理的时候弄一个临时的dp增加边的时候只能用前面的dp增加,因为这一块相同权值的的线段都不能放在同一个上升序列里面。

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <stack>
    typedef long long ll;
    #define X first
    #define Y second
    #define mp(a,b) make_pair(a,b)
    #define pb push_back
    #define sd(x) scanf("%d",&(x))
    #define Pi acos(-1.0)
    #define sf(x) scanf("%lf",&(x))
    #define ss(x) scanf("%s",(x))
    #define maxn 10000000
    #include <ctime>
    const int inf=0x3f3f3f3f;
    const long long mod=1000000007;
    using namespace std;
    struct node
    {
        int s,e,w;
    }edge[300005];
    int dp[300005];
    int fdp[300005];
    bool cmp(node a,node b)
    {
        return a.w<b.w;
    }
    int main()
    {
        #ifdef local
        freopen("in","r",stdin);
        //freopen("data.txt","w",stdout);
        int _time=clock();
        #endif
        int n,m;
        cin>>m>>n;
        for(int i=0;i<n;i++)scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].w);
        sort(edge,edge+n,cmp);
        int ans=0;
        for(int i=0;i<n;i++)
        {
            int j;
            for(j=i;edge[j].w==edge[i].w&&j<n;j++);
            for(int k=i;k<j;k++)
                {
                    fdp[edge[k].e]=max(fdp[edge[k].e],dp[edge[k].s]+1);
                }
            for(int k=i;k<j;k++)
                {
                    dp[edge[k].e]=fdp[edge[k].e];
                    ans=max(dp[edge[k].e],ans);
                }
            i=j-1;
        }
        cout<<ans<<endl;
        #ifdef local
        printf("time: %d
    ",int(clock()-_time));
        #endif
    }
    View Code
  • 相关阅读:
    ajax传参后台接受遇到问题
    使用xUnit为.net core程序进行单元测试(上)
    流程图规范
    知乎上看到的一些生活道理
    Dos常用命令
    c# 操作word写入特殊字符
    vb程序安装时需要在客户端安装MSSOAP30.dll,但注册不上,请问怎么处理
    VB 调用 webservice 出现:WSDLReader:Loading of the WSDL file failed HRESULT=0×80040154: 没有注册类别 解决方案
    The type 'System.Object' is defined in an assembly that is not referenced
    数据库还原失败解决方法
  • 原文地址:https://www.cnblogs.com/scau-zk/p/5655008.html
Copyright © 2011-2022 走看看