P2341 受欢迎的牛

8 篇文章 0 订阅
4 篇文章 0 订阅

luogu ——————

思路和爱在心中差不多 爱在心中

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string> 
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
int num[50009],head[10009],nxt[50009],dfn[10009],low[10009],color[10009],st[10009];
int dfs_num=0,color_num=0,top=0,cnt=0;
int n,m,ans=0,numc=0;
bool vis[10009],f[10009];
void Tarjan(int x)
{
    st[++top]=x;
    dfn[x]=++dfs_num;
    low[x]=dfs_num;
    vis[x]=true;

    for(int i=head[x];i;i=nxt[i])
    {
        if(!dfn[num[i]]) 
        {
            Tarjan(num[i]);
            low[x]=min(low[num[i]],low[x]);
        }
        else if(vis[num[i]]) low[x]=min(low[x],dfn[num[i]]);
    }

    if(low[x]==dfn[x])
    {
        vis[x]=false;
        color[x]=++color_num;
        int t=0;
        while(st[top]!=x)
        {
            vis[st[top]]=false;
            color[st[top]]=color_num;
            top--;t++;
        }
        top--;t++;
        if(t==1) f[color_num]=true;
    }

}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        cnt++;
        num[cnt]=b;
        nxt[cnt]=head[a];
        head[a]=cnt; 
    }
    for(int i=1;i<=n;i++)
     if(!dfn[i]) Tarjan(i);

    int cntc;
    for(int c=1;c<=color_num;c++)
    {
        cntc=0;
        for(int i=1;i<=n;i++)
        if(color[i]==c)
        {
            st[++cntc]=i;
        }
        bool flag=false;
        for(int x=1;x<=cntc;x++)
        {
            for(int i=head[st[x]];i;i=nxt[i])
            {
                if(color[num[i]]!=c)// 有指出去的点,就不是 
                {
                    flag=true;break;
                }
            }
            if(flag) break;
        }
        if(flag) continue;
        if(!flag)
        {
            numc++;ans=c;
            if(numc>=2)
            {
                printf("0\n");
                return 0;
            }   
        }

    }
    if(f[ans]) {printf("1\n");return 0;}
    int fig=0;
    for(int i=1;i<=n;i++)
    {
        if(color[i]==ans)
        fig++;
    } 

    printf("%d",fig);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值