poj2186 Popular Cows

#include<iostream>
#include<cstdlib>
using namespace std;
#define N 10000
#define M 50000
struct
{
    int s,e;
}edge[M];
typedef struct Arc
{
    int v;
    struct Arc*next;
}arc;
arc *cow[N],*rcow[N];
int n,m;
int cnt,ts[N],deg[N],sc[N];
char v[N];
void dfsr(int t)
{
    v[t]=1;
    arc*p;
    for(p=rcow[t];p;p=p->next)
        if(!v[p->v])
            dfsr(p->v);
    ts[cnt++]=t;
}
void dfs(int t)
{
    v[t]=1;
    sc[t]=cnt;
    arc*p;
    for(p=cow[t];p;p=p->next)
        if(!v[p->v])
            dfs(p->v);
}
int main()
{
    int s,e,i,k;
    arc*p;
 memset(cow,NULL,sizeof(cow));
 memset(rcow,NULL,sizeof(rcow));
 memset(deg,0,sizeof(deg));
    scanf("%d%d",&n,&m);
    for(i=0;i<m;i++)
    {
        scanf("%d%d",&s,&e);
        s--,e--;
        edge[i].s=s;
        edge[i].e=e;
        p=(arc*)malloc(sizeof(arc));
        p->v=e;
        p->next=cow[s];
        cow[s]=p;
        p=(arc*)malloc(sizeof(arc));
        p->v=s;
        p->next=rcow[e];
        rcow[e]=p;
    }
 memset(v,0,sizeof(v));
    cnt=0;
    for(i=0;i<n;i++)
        if(!v[i])
            dfsr(i);
    memset(v,0,sizeof(v));
    cnt=0;
    for(i=n-1;i>=0;i--)
        if(!v[ts[i]])
        {
            dfs(ts[i]);
            cnt++;
        }
    k=cnt;
    for(i=0;i<m;i++)
    {
        s=edge[i].s;
        e=edge[i].e;
        if(sc[s]!=sc[e])
            deg[sc[s]]++;
    }
    cnt=0;
    for(i=0;i<k;i++)
 {
        if(!deg[i])
        {
            cnt++;
            s=i;
        }
 }
    if(cnt>1)
        printf("0/n");
    else
    {
        for(cnt=0,i=0;i<n;i++)
            if(sc[i]==s)
                cnt++;
        printf("%d/n",cnt);
    }
// system("pause");
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值