zoukankan      html  css  js  c++  java
  • ZOJ 3795 Grouping(Tarjan收缩点+DAG)

    Suppose there are N people in ZJU, whose ages are unknown. We have some messages about them. The i-th message shows that the age of person si is not smaller than the age of person ti. Now we need to divide all these N people into several groups. One's age shouldn't be compared with each other in the same group, directly or indirectly. And everyone should be assigned to one and only one group. The task is to calculate the minimum number of groups that meet the requirement.

    Input

    There are multiple test cases. For each test case: The first line contains two integers N(1≤ N≤ 100000), M(1≤ M≤ 300000), N is the number of people, and M is is the number of messages. Then followed by M lines, each line contain two integers si and ti. There is a blank line between every two cases. Process to the end of input.

    Output

    For each the case, print the minimum number of groups that meet the requirement one line.

    Sample Input

    4 4
    1 2
    1 3
    2 4
    3 4
    

    Sample Output

    3
    

    Hint

    set1= {1}, set2= {2, 3}, set3= {4}



    记忆化搜索最长路径:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<limits.h>
    typedef long long LL;
    using namespace std;
    #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
    #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
    #define CLEAR( a , x ) memset ( a , x , sizeof a )
    
    const int maxn=100100;
    const int maxm=300100;
    struct node{
        int u,v;
        int next;
    }e[maxm],e2[maxm];
    int head[maxn],cntE,cntF;
    int DFN[maxn],low[maxn],h[maxn];
    int s[maxm],top,dex,cnt;
    int belong[maxn],instack[maxn];
    int dp[maxn],num[maxn];
    int n,m;
    void init()
    {
        top=cntE=cntF=0;
        dex=cnt=0;
        CLEAR(DFN,0);
        CLEAR(head,-1);
        CLEAR(instack,0);
        CLEAR(num,0);//fuck num没清0wa了2小时
    }
    void addedge(int u,int v)
    {
        e[cntE].u=u;e[cntE].v=v;
        e[cntE].next=head[u];
        head[u]=cntE++;
    }
    void Tarjan(int u)
    {
        DFN[u]=low[u]=++dex;
        instack[u]=1;
        s[top++]=u;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].v;
            if(!DFN[v])
            {
                Tarjan(v);
                low[u]=min(low[u],low[v]);
            }
            else if(instack[v])
                low[u]=min(low[u],DFN[v]);
        }
        int v;
        if(DFN[u]==low[u])
        {
            cnt++;
            do{
                v=s[--top];
                belong[v]=cnt;
                instack[v]=0;
            }while(u!=v);
        }
    }
    int dfs(int x)
    {
        if(dp[x]) return dp[x];
        dp[x]=num[x];
        for(int i=h[x];i!=-1;i=e2[i].next)
            dp[x]=max(dp[x],dfs(e2[i].v)+num[x]);
        return dp[x];
    }
    void work()
    {
        REPF(i,1,n)
          if(!DFN[i])  Tarjan(i);
        REPF(i,1,n)
           num[belong[i]]++;
        CLEAR(h,-1);
        CLEAR(dp,0);
        REPF(k,1,n)
        {
            for(int i=head[k];i!=-1;i=e[i].next)
            {
                int v=e[i].v;
                if(belong[k]!=belong[v])
                {
                    e2[cntF].u=belong[k];
                    e2[cntF].v=belong[v];
                    e2[cntF].next=h[belong[k]];
                    h[belong[k]]=cntF++;
                }
            }
        }
        int ans=0;
    //    cout<<"2333  "<<cnt<<endl;
        REPF(i,1,cnt)
            ans=max(ans,dfs(i));
        printf("%d
    ",ans);
    }
    int main()
    {
        int u,v;
        while(~scanf("%d%d",&n,&m))
        {
            init();
            for(int i=0;i<m;i++)
            {
                scanf("%d%d",&u,&v);
                addedge(u,v);
            }
            work();
        }
        return 0;
    }
    


    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    EasyUI--messager
    EasyUI--初学
    框架错误汇总
    OGNL调用静态方法和属性
    查询内容在网页里面分页显示+跳页查看
    struts2——通配符
    JavaScript 输出
    JavaScript语法(一)
    Struts+Hibernate+jsp页面 实现分页
    elasticsearch-5.x JAVA API(001)
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4753150.html
Copyright © 2011-2022 走看看