Luogu P2835 刻录光盘___强连通分量

题目大意:

给出N个人,每个人都给出能够允许过来刻印光盘的人,问最少要给几个人光盘能让将所有人都刻印到。

N≤200

题解:

直接做强连通分量,然后将所有相邻的不同的强连通分量给做标记,弄成同一个集合,然后最后统计各个强连通分量的能到达数,为0则无人帮助则ans++。

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 205
using namespace std;
int next[N*N],to[N*N],a[N],ls[N],dfn[N],low[N],c[N],rp[N],ru[N];
int n,x,ty,tot,num,cnt,ans;

void add(int u,int v)
{
    next[++num]=ls[u];
    to[num]=v;
    ls[u]=num;
}
void tarjan(int dep)
{
    dfn[dep]=low[dep]=++ty;
    a[++tot]=dep;
    c[dep]=1;
    int i=ls[dep];
    while (i) 
    {
          if (!dfn[to[i]]) 
             {
                   tarjan(to[i]);
                   low[dep]=min(low[dep],low[to[i]]);
             }
             else if (c[to[i]]) low[dep]=min(low[dep],dfn[to[i]]);
          i=next[i];
    }

    if (dfn[dep]==low[dep]) 
       {
              cnt++;  
              int x=0;
              while (x!=dep)
                    {
                        x=a[tot];
                        rp[x]=cnt;
                        c[x]=0;
                        tot--;
                    }
        }
}

int main()
{
    scanf("%d",&n);
    for (int i=1; i<=n; i++) 
        {
             scanf("%d",&x);
             while (x) 
                {
                        if(!x) break;
                        add(i,x);
                        scanf("%d",&x);
                 }
        }
    for (int i=1; i<=n; i++) 
         if (!dfn[i]) tarjan(i);

    for (int i=1; i<=n; i++)   
    {
         int j=ls[i];
         while (j)
        {
               if (rp[i]!=rp[to[j]]) ru[rp[to[j]]]++; 
               j=next[j];
        } 
    }
    for(int i=1; i<=cnt; i++) 
                 if (!ru[i]) ans++; 
    printf("%d\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值