POJ 2186 Kosaraju算法 强连通分量

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 30842 Accepted: 12529
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

USACO 2003 Fall

//http://poj.org/problem?id=2186


//先对原图G进行dfs搜索,得到各个点搜索完成的顺序,之后对逆图H进行dfs搜索,从最晚完成搜索的点开始,删除记录能够遍历到的点,这些点构成一个强连通分量.得到所有强连通分量后,把他们各自抽象成一个点,形成一个有向无环图,在该图上寻找出度为0的点.如果仅有一个出度为0的点,则该点被所有点指向,答案为该强连通分量的大小.如果有多个出度为0的点,则这些点无法互相可达,无解,答案为0.
//有向无环图(DAG)保证至少存在一个出度为0的点.
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <algorithm>
#include <functional>
#include <vector>
#include <iomanip>
#include <math.h>
#include <iostream>
#include <sstream>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
const int MAX=10005;
int N,M,a,b,Cnt;
int Num[MAX],Tree[MAX],OutDgree[MAX];
vector<int> G[MAX],H[MAX],F;
bool Mark1[MAX],Mark2[MAX];
void dfs1(int x)
{
    Mark1[x]=true;
    for (int i=0; i<(int)G[x].size(); i++)
        if (!Mark1[G[x][i]])
            dfs1(G[x][i]);
    F.push_back(x);
}
void dfs2(int x)
{
    Mark2[x]=true;
    Num[x]=Cnt;         //点x的连通分量编号为Cnt
    Tree[Cnt]+=1;      //编号为Cnt的连通分量大小+1
    for (int i=0; i<(int)H[x].size(); i++)
        if (!Mark2[H[x][i]])
            dfs2(H[x][i]);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    while (cin>>N>>M)
    {
        memset(Mark1,false,sizeof(Mark1));
        memset(Mark2,false,sizeof(Mark2));
        memset(Num,0,sizeof(Num));
        memset(Tree,0,sizeof(Tree));
        memset(OutDgree,0,sizeof(OutDgree));
        Cnt=0;
        F.clear();
        for (int i=0; i<MAX; i++)
        {
            G[i].clear();
            H[i].clear();
        }
        for (int i=0; i<M; i++)
        {
            cin>>a>>b;
            G[a].push_back(b);
            H[b].push_back(a);
        }
        for (int i=1; i<=N; i++)
        {
            if (!Mark1[i])
                dfs1(i);
        }
        for (int i=(int)F.size()-1; i>=0; i--)
        {
            if (!Mark2[F[i]])
            {
                Cnt++;
                dfs2(F[i]);
            }
        }
        for (int i=1;i<=N;i++)
        {
            for (int j=0;j<(int)G[i].size();j++)
            {
                if (Num[i]!=Num[G[i][j]])
                    OutDgree[Num[i]]+=1;
            }
        }
        int temp=0,Ans=0;
        for (int i=1;i<=Cnt;i++)
        {
            if (!OutDgree[i])
                temp++,Ans=i;
        }
        cout<<(temp>1?0:Tree[Ans])<<endl;
    }
    return 0;
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值