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;
    }
  • 相关阅读:
    Spring MVC返回多重的Json数据
    Eclipse Maven项目中修改JDK版本
    Maven的使用笔记
    Windows下Redis主从配置出现Writing to master:Unknow error
    Java开发必会的Linux命令(转)
    使用maven引入slf4j、logback时发生冲突
    使用SSM框架搭建JavaWeb,使用Junit测试时遇到CannotGetJdbcConnetionException
    HTTP基础
    express 热启动 静态文件部署 跨域解决 调试
    github+git提交 基础用法
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9118168.html
Copyright © 2011-2022 走看看