zoukankan      html  css  js  c++  java
  • CodeForces960F:Pathwalks (主席树+DP)

    You are given a directed graph with n nodes and m edges, with all edges having a certain weight.

    There might be multiple edges and self loops, and the graph can also be disconnected.

    You need to choose a path (possibly passing through same vertices multiple times) in the graph such that the weights of the edges are in strictly increasing order, and these edges come in the order of input. Among all such paths, you need to find the the path that has the maximum possible number of edges, and report this value.

    Please note that the edges picked don't have to be consecutive in the input.

    Input

    The first line contains two integers n and m (1 ≤ n ≤ 100000,1 ≤ m ≤ 100000) — the number of vertices and edges in the graph, respectively.

    m lines follows.

    The i-th of these lines contains three space separated integers aibi and wi (1 ≤ ai, bi ≤ n0 ≤ wi ≤ 100000), denoting an edge from vertex ai to vertex bi having weight wi

    Output

    Print one integer in a single line — the maximum number of edges in the path.

    Examples

    Input
    3 3
    3 1 3
    1 2 1
    2 3 2
    Output
    2
    Input
    5 5
    1 3 2
    3 2 3
    3 4 5
    5 4 0
    4 5 8
    Output
    3

    Note

    The answer for the first sample input is 2: . Note that you cannot traverse  because edge  appears earlier in the input than the other two edges and hence cannot be picked/traversed after either of the other two edges.

    In the second sample, it's optimal to pick 1-st, 3-rd and 5-th edges to get the optimal answer: .

     题意:问树上最长链大小,这条链满足从起点向终点,是按照给出的顺序,而且长度严格递增。

    思路:忽略空间时间问题,先假设线段树可以实现。

             N棵线段树,每棵开100000个叶子节点,第i棵树的j叶子表示以i节点为尾,最后一条边长度是j的最大链。

            合并为主席树。然后DP乱搞。

            怎么合并呢? 假设现在新加了一条有向边,(u->v,len),那么把第v棵线段树,长度[1,len-1]的节点都更新node[v][i]=max(node[v][i],node[u][i]+1);

     AC in one go!cheers。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=100010;
    int rt[maxn],cnt;
    struct node
    {
        int l,r,val;
        node(){ l=r=val=0;}
        node(int L,int R,int V):l(L),r(R),val(V){}
    }s[maxn*20];
    int query(int Now,int L,int R,int l,int r)
    {
        if(l<=L&&r>=R) return s[Now].val;
        int res=0,Mid=(L+R)>>1;
        if(l<=Mid) res=max(res,query(s[Now].l,L,Mid,l,r));
        if(r>Mid) res=max(res,query(s[Now].r,Mid+1,R,l,r));
        return res;
    }
    void update(int &Now,int pre,int L,int R,int pos,int val)
    {
        Now=++cnt; s[Now]=node(s[pre].l,s[pre].r,max(s[pre].val,val));
        if(L==R) return ; int Mid=(L+R)>>1;
        if(pos<=Mid) update(s[Now].l,s[pre].l,L,Mid,pos,val);
        else update(s[Now].r,s[pre].r,Mid+1,R,pos,val);
    }
    int main()
    {
        int N,M,i,u,v,c,ans=0;
        scanf("%d%d",&N,&M);
        for(i=1;i<=M;i++){
            scanf("%d%d%d",&u,&v,&c); c+=2; //防止线段树越界 
            int tmp=query(rt[u],1,100002,1,c-1);
            update(rt[v],rt[v],1,100002,c,tmp+1);
            ans=max(tmp+1,ans);
        }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    19.1.30 [LeetCode 24] Swap Nodes in Pairs
    19.1.29 [LeetCode 23] Merge k Sorted Lists
    06_Python异常处理机制
    05_Python的文件操作
    04_Python中的35个关键字
    03_Python基础语法
    02_Python开发环境使用和PDB调试
    01_Python基础知识梳理
    socket post
    python_socket_cmd
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9118168.html
Copyright © 2011-2022 走看看