2186 Popular Cows 图的连通性

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9234 Accepted: 3690

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.

Source

缩点判断出度为0的点的数目,当为1时即为那个联通分支里的数目
当大于1时无解,输出0
如果只有一个联通分支,嘿嘿,很简单
下面为关键代码

 

void Tarjan(int u) //Tarjan

{

    int v;

    node[u].DFN=node[u].LOW=(++idx);

    instack[u]=true;

    stack[++top]=u;

    for(Edge*p=node[u].first; p; p=p->next)

    {

        v=p->adj;

        if(!node[v].DFN)

        {

            Tarjan(v);

            if(node[v].LOW<node[u].LOW)

                node[u].LOW=node[v].LOW;

        }

        else if(instack[v]&&node[v].DFN<node[u].LOW)

            node[u].LOW=node[v].DFN;

    }

    if(node[u].DFN==node[u].LOW)

    {

        b_cnt++;

        do

        {

            v=stack[top--];

            instack[v]=false;

            node[v].belongs=b_cnt;

        }

        while(u!=v);

    }

}

 

 

 

        for(i=1; i<=N; i++)

        {

            flag[i]=true;

            for(Edge*p=node[i].first; p; p=p->next)

            if(node[i].belongs!=node[p->adj].belongs

               &&hash[node[i].belongs]!=node[p->adj].belongs)

            {

                    hash[node[i].belongs]=node[p->adj].belongs;

                    InsertEdge2(node[i].belongs,node[p->adj].belongs);

            }

            else

            if(node[i].belongs==node[p->adj].belongs&&!flag[p->adj])

            {

                    now[node[i].belongs].sum++;

                    flag[p->adj]=true;

            }

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值