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\n",ans);
}

int main()
{
    #ifdef LOCAL
    freopen("in","r",stdin);
    #endif
    ios::sync_with_stdio(0);
    while( ~scanf("%d%d",&n,&m))run();
}
View Code

 

转载于:https://www.cnblogs.com/hlmark/p/4025527.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值