• ZOJ 3795 Grouping(scc+最长路)


    Grouping

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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}

    题目给出N个点 , M条边, 然后问一些相关联的人(存在一条路u -> v)不能分在同一组 , 最少分的组的数目 。

    这样肯定要求一个  scc ..  那么在同一个scc里面的人(假设有w个人)都要拆分成w组 , 那么我们以这个w作为

    scc图的点权 。 目的是所有 scc图(DAG) 的最长路所构成的权 ,取最大。

    最长路用一个记忆化搜索即可 , 训练的时候不会这么用 , 卡蒙了~~ 。  

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 100010;
    
    int pre[N] , lowlink[N] , sccno[N] , dfs1_clock , scc_cnt ;
    stack<int>st;
    int n , m ;
    int w[N] , dep_w[N];
    bool vis[N];
    int in[N];
    int dp[N];
    
    
    int eh[N] , et[N<<2] ,nxt[N<<2] , tot ;
    
    void addedge( int u , int v )
    {
        et[tot] = v , nxt[tot] = eh[u] , eh[u] = tot ++ ;
    }
    
    struct node
    {
        int u , v ;
    }e[N*3];
    
    
    void dfs1( int u )
    {
        pre[u] = lowlink[u] = ++dfs1_clock;
        st.push(u);
        for( int i = eh[u]; ~i ; i = nxt[i] ){
            int v = et[i] ;
            if(!pre[v]){
                dfs1(v);
                lowlink[u] = min(lowlink[u],lowlink[v]);
            }
            else if( !sccno[v] ){
                lowlink[u] = min( lowlink[u] , pre[v] );
            }
        }
        if( lowlink[u] == pre[u] )
        {
            scc_cnt++;
            for(;;){
                int  x = st.top(); st.pop();
                sccno[x] = scc_cnt ;
                if( x == u ) break;
            }
        }
    }
    
    void find_scc()
    {
        dfs1_clock = scc_cnt = 0 ;
        memset ( pre , 0 , sizeof pre ) ;
        memset ( sccno , 0 , sizeof sccno ) ;
        while( !st.empty() ) st.pop();
    
        for( int i = 1 ; i <= n ; ++i ){
            if( !pre[i] )dfs1(i);
        }
    }
    
    void init()
    {
    
        tot= 0 ;
        memset( eh , -1 ,sizeof eh );
        memset( dp , -1 ,sizeof dp );
        memset( w , 0 ,sizeof w );
        memset( vis ,false , sizeof vis );
        memset( in , 0, sizeof in );
    }
    
    void dfs( int u )
    {
        if( dp[u] == -1 ){
            int sum_now = w[u] ;
            for( int i = eh[u] ; ~i ; i = nxt[i] ){
                int v = et[i];
                dfs(v);
                sum_now = max( sum_now , w[u] + dp[v] );
            }
            dp[u] = sum_now;
        }
    }
    
    void run()
    {
        int u , v ;
        init();
        for( int i = 0 ; i < m ;++i ){
            scanf("%d%d",&e[i].u,&e[i].v);
            addedge( e[i].u , e[i].v );
        }
        find_scc();
        tot = 0 ;
        memset(eh , -1 , sizeof eh );
    
        for( int i = 0 ;i < m ;++i ){
            if( sccno[e[i].u] != sccno[e[i].v] ){
                addedge(  sccno[e[i].u] , sccno[e[i].v] );
                in[ sccno[e[i].v] ] ++;
            }
        }
    
        for( int i = 1 ; i <= n ; ++i  )w[ sccno[i] ] ++ ;
    
        int ans = 0 ;
        for( int i = 1 ; i <= scc_cnt ; ++i ) if( !in[i] ) addedge(0,i);
        dfs(0);
        for( int i = 1; i <= scc_cnt ; ++i ) ans = max( ans , dp[i] );
        printf("%d
    ",ans);
    }
    
    int main()
    {
        #ifdef LOCAL
        freopen("in","r",stdin);
        #endif
        ios::sync_with_stdio(0);
        while( ~scanf("%d%d",&n,&m))run();
    }
    View Code
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    nginx查看配置文件nginx.conf路径
    http://127.0.0.1/thinkphp5/public/index/teacher/delete/id/1.html 这样的URL下,页面收不到get参数
    thinkphp5访问报错 ...with message 'mkdir(): Permission denied' in... 或...failed to open stream: Permission denied' in...
    thinkphp报错No input file specified. (对于隐藏url中的index.php 是如何做到的?)
    收藏网址
    jquery 和 原生js 获取<select><option></option></select>标签的值和文本
    Macbook下MySQL卸载方法
    提示错误 Call to undefined function imagepng() in …
    Mac 安装phpmyadmin提示无法登陆 mysql服务器解决办法
    php把一个字符串分割成字符数组(可以用参数指定数组每个元素字符的长度)
  • 原文地址:https://www.cnblogs.com/hlmark/p/4025527.html
Copyright © 2020-2023  润新知