zoukankan      html  css  js  c++  java
  • 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 ?
  • 相关阅读:
    单例 全局变量
    平安亲人 测试数据
    UIButton
    apple id
    背景图的按钮
    大头针飘移问题
    图文并茂Windows系统使用XAMPP搭建本地mysql数据库导入数据库并使用node.js访问数据库
    JS原生上传文件,读取文件格式,控制文件只可以上传某些格式,并使用fileReader转换格式
    今天学到的新知识使用localtunnel实现内网穿透,感觉很神奇哇~~
    在Mac OS上将Node.js连接到XAMPP MySQL服务器一直报错error connecting: Error: connect ECONNREFUSED
  • 原文地址:https://www.cnblogs.com/hlmark/p/4025527.html
Copyright © 2011-2022 走看看