Codeforces 459e Roland and Rose

Source: Internet
Author: User

I thought it was a tree-like DP, where DFS is located according to the tree-like DP method. The result is wa, because it has a directed ring, not a tree, do not use the tree-like method in the case of loops.

The breakthrough point of the question is to use vertices to represent the maximum length of the end of an edge with this vertex after the edge is sorted, because the edge is sorted from small to large, therefore, the side appended must be smaller than the front side.

When the same side is used, a buffer is required, because operations on the same side may not affect the operation.

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n,m;const int N = 100010*3;struct edge{    int u,v,w;    bool operator < (const edge& rhs) const{        return w<rhs.w;    }}E[N];int dp[N];int last[N];int main(){    while (scanf("%d%d",&n,&m)!=EOF)    {        for (int i=0;i<m;i++){            scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].w);        }        sort(E,E+m);        memset(dp,0,sizeof dp);        memset(last,0,sizeof last);        for (int i=0;i<m;){            int j=i;            while (E[i].w==E[j].w && j<m) j++;            for (int k=i;k<j;k++){                last[E[k].v]=max(last[E[k].v],dp[E[k].u]+1);            }            for (int k=i;k<j;k++){                dp[E[k].v]=max(dp[E[k].v],last[E[k].v]);            }            i=j;        }        int ans=0;        for (int i=1;i<=n;i++){            ans=max(ans,dp[i]);        }        printf("%d\n",ans);    }    return 0;}

 

Codeforces 459e Roland and Rose

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.