强连通分量/tarjan (洛谷 2746 校园网)

题目链接
tarjan求强连通分量,然后缩点,
对于任务a

是入度为零的点的个数

对于任务b

是max(入度为零的个数, 出度为零的个数)

所以可知。
ps.不知在任务b卡了多久。
下面是ac代码:

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <queue>
#include <stack>
#define ll long long
using namespace std;
const int N = 1e6+10;
int ver[N], ne[N], he[N];
int dfn[N], low[N], dir[N], dic[N], s[N], ins[N], part[N];
int root;
int tot, top, pcnt;
int ind, n, m;
void init()
{
    memset(he, 0, sizeof(he));
    memset(ne, 0, sizeof(ne));
    memset(dfn, 0, sizeof(dfn));
    memset(low, 0, sizeof(low));
    memset(ins, 0, sizeof(ins));
    memset(dir, 0, sizeof(dir));
    memset(dic, 0, sizeof(dic));
    ind = 0;
    pcnt = 0;
    top = 0;
    tot = 0;
}
void add(int x, int y)
{
    ver[++tot] = y;
    ne[tot] = he[x];
    he[x] = tot;
}
void tarjan(int x)
{
    dfn[x] = low[x] = ++ind;
    s[top++] = x;
    ins[x] = 1;
    for (int i = he[x]; i; i = ne[i])
    {
        int y = ver[i];
        if (!dfn[y])
        {
            tarjan(y);
            low[x] = min(low[x], low[y]);
        }
        else if (ins[y]) low[x] = min(low[x], dfn[y]);
    }
    int y;
    if (low[x] == dfn[x])
    {
        pcnt++;
        do
        {
            y = s[--top];
            part[y] = pcnt;
            ins[y] = 0;
        } while(x != y);
    }
}
int main()
{
    while(cin >> n)
    {
        init();
        for (int i = 1; i <= n; i++)
        {
            int v;
            while(scanf("%d", &v), v)
            {
                add(i, v);
            }
        }
        for (int i = 1; i <= n; i++)
        {
            if (!dfn[i]) tarjan(i);
        }
        for (int x = 1; x <= n; x++)
        {
            for (int i = he[x]; i; i = ne[i])
            {
                int y = ver[i];
                if (part[y] != part[x])
                {
                    dir[part[y]]++;
                    dic[part[x]]++;
                }
            }
        }
/*
        for (int i = 1; i <= n; i++)
        {
            cout << i << ":" << part[i] << endl;
        }
        for (int i = 1; i <= pcnt; i++)
        {
            cout << dir[i] << " ";
        }
        cout << endl;
        for (int j = 1; j <= pcnt; j++)
        {
            cout << dic[j] << " ";
        }
        cout << endl;
*/
        if (pcnt == 1)
        {
            printf("1\n0\n");
            continue;
        }
        int cntx = 0;
        int cnty = 0;
        for (int i = 1; i <= pcnt; i++)
        {
            if (dic[i] == 0)
                cntx++;
            if (dir[i] == 0)
                cnty++;
        }
        printf("%d\n%d\n", cnty, max(cntx, cnty));
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值