poj 2186 Popular Cows//强连通分量(tarjan实现)

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

强连通分量(tarjan实现)

Popular Cows

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 41941 Accepted: 17026

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. 

 

题意:求ai的个数,其中ai是其他点都可以到达该点的点。

那么可以得到,满足题意点一定是强连通。

那么可以通过此想到解决方案。

求出所有强连通,缩点操作后,找出度为0的点xi,如果只有一个xi,那么直接输出,如果有多个xi表面没有满足题意的点,即图是分离的。

一开始为什么我要topu??明显假算法!

 

#include<iostream>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
#include<climits>
#include<cstdlib>
#include<queue>
using namespace std;
#define LL long long
const int max_n=10005;
int n,m;
vector<int>g[max_n];//原图
int dfn[max_n];//时间戳
int low[max_n];//同环指向前面的时间戳
int inst[max_n];//是否在栈
int tot;//顶点数
int color[max_n];  //染色
int col_num;    //连通数量
stack<int>s;

int ou[max_n];

void tarjan(int x){
    low[x]=dfn[x]=++tot;
    s.push(x);inst[x]=1;
    for(int i=0;i<(int)g[x].size();i++){
        int v=g[x][i];
        if(!dfn[v]){   //延展
            tarjan(v);
            low[x]=min(low[x],low[v]);
        }
        else if(inst[v]){ //在栈,更新
            low[x]=min(low[x],dfn[v]);
        }
    }
    if(low[x]==dfn[x]){  //找到联通分量
        int now;
        ++col_num ;//染色
        do{         //清空
            now=s.top();s.pop();
            color[now]=col_num ;
            inst[now]=0 ;
        }while(x!=now);
    }
}

int main(){
    ios::sync_with_stdio(false);  cin.tie(0);
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int a,b;cin>>a>>b;
        g[a].push_back(b);
    }
    for(int i=1;i<=n;i++) {
        if(!dfn[i])
            tarjan(i);
    }
    for(int i=1;i<=n;i++){
        for(int j=0;j<(int)g[i].size();j++){
            int u=i,v=g[i][j];
            if(color[u]==color[v]) continue;//因为缩点,自己集合不in
            ou[color[u]]++;
        }
    }
    int p=0,u;
    for(int i=1;i<=col_num;i++) if(ou[i]==0) {p++;u=i;}
    if(p>1) {cout<<0;return 0;}
    int ans=0;
    for(int i=1;i<=n;i++){
        if(color[i]==u )ans++;
    }
    cout<<ans;
    return 0;
}

如果要把不是强连通的图转化为强连通,需要连接的最小边数是max(cntin,cntou)。

例题http://poj.org/problem?id=1236

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值