2019暑假个人排位集训补题--图相关

2019暑期集训图相关补题集

ZOJ 3795 Grouping

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3795
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<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std ;

const int maxn = 111111 ;
vector<int> g[maxn] ;
vector<int> vec[maxn] ;
int pre[maxn] , lowlink[maxn] , sccno[maxn] , dfs_clock , scc_cnt , val[maxn] , du[maxn] ;
stack<int> S;

void dfs ( int u ) {
    pre[u] = lowlink[u] = ++ dfs_clock ;
    S.push ( u ) ;
    for ( int i = 0 ; i < g[u].size () ; i ++ ) {
        int v = g[u][i] ;
        if ( !pre[v] ) {
            dfs ( 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 = S.top () ; S.pop () ;
            sccno[x] = scc_cnt ;
            if ( x == u ) break ;
        }
    }
}

void find_scc ( int n ) {
    dfs_clock = scc_cnt = 0 ;
    while ( !S.empty() ) S.pop () ;
    memset ( sccno , 0 , sizeof ( sccno ) ) ;
    memset ( pre , 0 , sizeof ( pre ) ) ;
    for ( int i = 1 ; i <= n ; i ++ )
        if ( !pre[i] ) dfs ( i ) ;
//    for ( int i = 1 ; i <= n ; i ++ )
//        printf ( "%d " , sccno[i] ) ;
//    puts ( "" ) ;
}

void new_graph ( int n ) {
    memset ( du , 0 , sizeof ( du ) ) ;
    memset ( val , 0 , sizeof ( val ) ) ;
    for ( int i = 1 ; i <= n ; i ++ ) {
        val[sccno[i]] ++ ;
    }
    for ( int i = 1 ; i <= n ; i ++ ) vec[i].clear () ;
    for ( int i = 1 ; i <= n ; i ++ ) {
        for ( int j = 0 ; j < g[i].size () ; j ++ ) {
            int u = sccno[i] ;
            int v = sccno[g[i][j]] ;
           // printf ( "i = %d , g = %d\n" , i , g[i][j] ) ;
           // printf ( "u = %d , v = %d\n" , u , v ) ;
            if ( u != v ) {
                vec[u].push_back ( v ) , du[v] ++ ;
         //       printf () ;
            }
        }
    }
//    printf ( " scccnt %d\n" , scc_cnt ) ;
//    for ( int i = 1 ; i <= scc_cnt ; i ++ ) {
 //       for ( int j = 0 ; j < vec[i].size () ; j ++ )
 //           printf ( "fuck %d %d\n" , i , vec[i][j] ) ;
 //   }
}

int dp[maxn] ;
int get_ans ( int u ) {
    if ( dp[u] ) return dp[u] ;
    int mx = 0 ;
    for ( int i = 0 ; i < vec[u].size () ; i ++ ) {
        int v = vec[u][i] ;
        mx = max ( mx , get_ans ( v ) ) ;
    }
    dp[u] = mx + val[u] ;
    return dp[u] ;
}

int main () {
    int n , m ;
    while ( scanf ( "%d%d" , &n , &m ) != EOF ) {
        memset ( dp , 0 , sizeof ( dp ) ) ;
        for ( int i = 1 ; i <= n ; i ++ ) g[i].clear () ;
        while ( m -- ) {
            int a , b ;
            scanf ( "%d%d" , &a , &b ) ;
            g[a].push_back ( b ) ;
        }
        find_scc ( n ) ;
        new_graph ( n ) ;
        int ans = 0 ;
        for ( int i = 1 ; i <= scc_cnt ; i ++ ) {
            if ( du[i] == 0 ) {
                //    printf ( "i = %d , fuck = %d\n" , i , get_ans (i) );
                    ans = max ( ans , get_ans ( i ) ) ;
            }
        }
        printf ( "%d\n" , ans ) ;
    }
    return 0 ;
}
/*
3 3
1 2
2 3
3 1
16 17
14 16
16 15
15 14
5 1
1 2
2 6
6 13
6 3
13 3
3 2
3 4
4 1
5 7
7 8
8 9
9 10
10 11
*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值