poj2186强联通+缩点

题目链接 点击打开链接
Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 37945 Accepted: 15456

Description

Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is 
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow. 

Input

* Line 1: Two space-separated integers, N and M 

* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular. 

Output

* Line 1: A single integer that is the number of cows who are considered popular by every other cow. 

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity. 

模板题目,结合度数判断一下就可以了

#include<iostream>
#include<string.h>
#include<stdio.h>
#define maxn 10005
using namespace std;
int n,m;
int a,b;
struct node
{
    int u;//保存每一条边的起始点
    int v;//保存每一条边的终止点
    int next;//保存每一条边的上一条边的编号
}edge[maxn*5];//保存每一条边,下标表示每一条边的编号
int head[maxn];//保存每一个顶点的起始边的编号
int dfn[maxn],low[maxn];
int stack[maxn];
int cnt,tot,top;
int num;
int sum[maxn];
int father[maxn];
int du[maxn];
bool check[maxn];
void add(int u,int v,int &k)
{
    edge[k].u=u;
    edge[k].v=v;
    edge[k].next=head[u];
    head[u]=k++;
}
void tarjan(int u)
{
    dfn[u]=low[u]=++tot;
    check[u]=true;
    stack[++top]=u;
    for(int k=head[u];k!=-1;k=edge[k].next)
    {
        int v=edge[k].v;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(check[v])
        {
            low[u]=min(low[u],low[v]);
        }
    }
    if(dfn[u]==low[u])
    {
        cnt++;
        int temp;
        do
        {
          temp=stack[top--];
          father[temp]=cnt;
          sum[cnt]++;
          check[temp]=false;
        }while(u!=temp);
    }
}
void solve()
{
    for(int i=1;i<=n;i++)
    {
        if(!dfn[i])
        {
            tarjan(i);
        }
    }
}
void init()
{
    num=0;
    tot=0;
    cnt=0;
    top=0;
    memset(head,-1,sizeof(head));
    memset(check,false,sizeof(check));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(father,0,sizeof(father));
    memset(sum,0,sizeof(sum));
    memset(du,0,sizeof(du));
}
int main()
{
    scanf("%d%d",&n,&m);
    init();
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&a,&b);
        add(a,b,num);
    }
    solve();
    for(int i=1;i<=n;i++)
    {
        for(int j=head[i];j!=-1;j=edge[j].next)
        {
            if(father[i]!=father[edge[j].v])
            {
                du[father[i]]++;
            }
        }
    }
    int t=0,ans;
    for(int i=1;i<=cnt;i++)
    {
        if(!du[i])
        {
            t++;
            ans=sum[i];
        }
    }
    if(t==1)
    {
        printf("%d\n",ans);
    }
    else
    {
        printf("0\n");
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值