zoukankan      html  css  js  c++  java
  • Codeforces Round #261 (Div. 2) E

    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.

    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[u]=max(dp[u],dp[v]+1)(v起点,u终点)

    根据权值从小到大排序,分别处理相同权值片段的长度,最后求最大值

     1 #include<stdio.h>
     2 //#include<bits/stdc++.h>
     3 #include<string.h>
     4 #include<iostream>
     5 #include<math.h>
     6 #include<sstream>
     7 #include<set>
     8 #include<queue>
     9 #include<map>
    10 #include<vector>
    11 #include<algorithm>
    12 #include<limits.h>
    13 #define inf 0x7fffffff
    14 #define INFL 0x7fffffffffffffff
    15 #define lson l,m,rt<<1
    16 #define rson m+1,r,rt<<1|1
    17 #define LL long long
    18 #define ULL unsigned long long
    19 using namespace std;
    20 const int M = 2000000;
    21 int n,m;
    22 struct P
    23 {
    24     int v,u,w;
    25 }H[M];
    26 bool solve(P x,P y)
    27 {
    28     return x.w<y.w;
    29 }
    30 int dp[M],ans[M];
    31 int j;
    32 int main()
    33 {
    34     std::ios::sync_with_stdio(false);
    35     cin>>n>>m;
    36     for(int i=0;i<m;i++)
    37     {
    38         cin>>H[i].v>>H[i].u>>H[i].w;
    39     }
    40     sort(H,H+m,solve);
    41     for(int i=0;i<m;i=j)
    42     {
    43         for(j=i;H[i].w==H[j].w&&j<m;j++)
    44         {
    45             ans[H[j].u]=dp[H[j].u];
    46         }
    47         for(j=i;H[i].w==H[j].w&&j<m;j++)
    48         {
    49             ans[H[j].u]=max(ans[H[j].u],dp[H[j].v]+1);
    50         }
    51         for(j=i;H[i].w==H[j].w&&j<m;j++)
    52         {
    53             dp[H[j].u]=ans[H[j].u];
    54         }
    55     }
    56     int Max=-1;
    57     for(int i=1;i<=n;i++)
    58     {
    59         Max=max(dp[i],Max);
    60     }
    61     cout<<Max<<endl;
    62     return 0;
    63 }
  • 相关阅读:
    LPTSTR、LPCSTR、LPCTSTR、LPSTR的意义
    字符,字节和编码
    堆和栈的区别
    js正则表达式限制文本框只能输入数字,小数点,英文字母
    WPF 中的 LinkButton
    WPF中DataGrid的应用
    C#获取web.config配置文件内容
    js中格式化时间字符串
    WPF中的用户控件(UserControl)
    VS2010安装其他版本framework的问题解决方案
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/6582104.html
Copyright © 2011-2022 走看看