码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 3795 Grouping

时间:2014-10-15 05:38:19      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   sp   div   

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\n",ans); } int main() { #ifdef LOCAL freopen("in","r",stdin); #endif ios::sync_with_stdio(0); while( ~scanf("%d%d",&n,&m))run(); }

 

ZOJ 3795 Grouping

标签:style   blog   color   io   os   ar   for   sp   div   

原文地址:http://www.cnblogs.com/hlmark/p/4025527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!