码迷,mamicode.com
首页 > 其他好文 > 详细

Popular Cows 强连通(kosaraju)

时间:2016-08-20 11:27:25      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 30652   Accepted: 12439

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

 

正向dfs 把顶点标号 反向dfs找出强连通分量。将强连通分量缩点构成DAG。只有一个强连通分量符合条件,只要DAG最后一个强连通分量反向dfs即可,只要全部到达既满足。否则,有多个出度为0的缩点。

 

#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

int n,k;
const int maxn=10010;
vector<int> g[maxn];
int V;//顶点
vector<int> rg[maxn],vs;
bool used[maxn];
int belong[maxn];

void add_edge(int from,int to){
    g[from].push_back(to);
    rg[to].push_back(from);
}

void dfs(int v){
    used[v]=1;
    for(int i=0;i<g[v].size();i++){
        if(!used[g[v][i]]) dfs(g[v][i]);
    }
    vs.push_back(v);
}

void rdfs(int v,int k){
    used[v]=1;
    belong[v]=k;
    for(int i=0;i<rg[v].size();i++)
        if(!used[rg[v][i]]) rdfs(rg[v][i],k);
}
int scc(){
    k=0;
    memset(used,0,sizeof(used));
    vs.clear();
    for(int v=1;v<=n;v++){
        if(!used[v]) dfs(v);
    }
    memset(used,0,sizeof(used));
    for(int i=vs.size()-1;i>=0;i--)
        if(!used[vs[i]])    rdfs(vs[i],k++);
    return k;
}
int main(){
    int m,a,b;
    //freopen("data.in","r",stdin);
    while(~scanf("%d%d",&n,&m)){
        for(int i=0;i<m;i++){
            scanf("%d%d",&a,&b);
            add_edge(a,b);
        }
        int nn= scc(),u=0,num=0;
        //统计备选解的个数
        for(int i=1;i<=n;i++){
            if(belong[i]==nn-1){
                u=i;num++;
            }
        }
        //检查是否从所有点可达
        memset(used,0,sizeof(used));
        rdfs(u,0);
        for(int v=1;v<=n;v++){
            if(!used[v]){
                num=0;break;
            }
        }
        printf("%d\n",num);
    }
}

 

Popular Cows 强连通(kosaraju)

标签:

原文地址:http://www.cnblogs.com/acmtime/p/5789742.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!