hdu 4635 Strongly connected (tarjan)

把所有scc找出来,缩点,然后在出度为0或入度为0的的块中选择点数最小的块作为独立块,使得这一块和其他的块不能强连通,其余的点都实现强连通即可!

#include <cstdio>
#include <vector>
#include <stack>
#include <cstring>
using namespace std;

const int N = 100010;
vector <int> G[N];
int pre[N], lowlink[N], sccno[N], num[N], dfs_clock, scc_cnt;
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( pre[v], lowlink[u] );
    }
    if ( lowlink[u] == pre[u] ) {
        scc_cnt++;
        for (;;) {
            int x = S.top(); S.pop();
            sccno[x] = scc_cnt;
            num[scc_cnt]++;
            if ( x == u ) break;
        }
    }
}

void find_scc( int n )
{
    dfs_clock = scc_cnt = 0;
    memset( num, 0, sizeof(num));
    memset( pre, 0, sizeof(pre));
    memset( sccno, 0, sizeof(sccno));
    for ( int i = 1; i <= n; ++i ) if ( !pre[i] ) dfs(i);
}
int T, n, m;
int in[N], out[N];
int main()
{
    while ( scanf("%d", &T) != EOF ) {
        int icase = 1;
        while (T--) {
            scanf("%d%d", &n, &m);
            for ( int i = 0; i <= n; ++i ) G[i].clear();
            int tmp = m, maxx = 0x3fffffff;
            while ( tmp-- ) {
                int a, b;
                scanf("%d%d", &a, &b);
                G[a].push_back(b);
            }
            find_scc( n );
            //printf("%d\n", scc_cnt);
            if ( scc_cnt == 1 ) {
                printf("Case %d: -1\n", icase++);
                continue;
            }
            memset( in, 0, sizeof(in));
            memset( out, 0, sizeof(out));
            for ( int i = 1; i <= n; ++i ) for ( int j = 0; j < G[i].size(); ++j ) {
                int v = G[i][j];
                if ( sccno[i] != sccno[v] ) out[sccno[i]]++, in[sccno[v]]++;
            }
            for ( int i = 1; i <= scc_cnt; ++i ) if ( num[i] < maxx && ( in[i] == 0 || out[i] == 0 )) maxx = num[i];
            printf("Case %d: ", icase++ );
            printf("%d\n", n*(n-1) - m - maxx*(n-maxx));
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值