Popular Cows POJ - 2186(强连通分量)

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. 
 
解析:
  如果只有一个连通分量的出度为0  则输出这个连通分量里的点数
  其它则为0
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e5 + 10, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int n, m;
vector<int> G[maxn];
int pre[maxn], lowlink[maxn], sccno[maxn], dfs_clock, scc_cnt, out[maxn];
stack<int> S;
int vis[maxn];

void dfs(int u)
{
    pre[u] = lowlink[u] = ++dfs_clock;
    S.push(u);
    for(int i=0; i<G[u].size(); i++)
    {
        int v = G[u][i];
        if(!pre[v])
        {
            dfs(v);
            lowlink[u] = min(lowlink[u], lowlink[v]);
        }
        else if(!sccno[v])
            lowlink[u] = min(pre[v], lowlink[u]);
    }
    if(lowlink[u] == pre[u])
    {
        scc_cnt++;
        for(;;)
        {
            int x = S.top(); S.pop();
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}

int main()
{
    int u, v;
    rd(n), rd(m);
    for(int i=0; i<m; i++)
    {
        rd(u), rd(v);
        G[u].push_back(v);
    }
    for(int i=1; i<=n; i++)
        if(!pre[i]) dfs(i);
 //   cout << 111 << endl;
    if(scc_cnt == 1)
    {
        pd(n);
        return 0;
    }
    for(int i=1; i<=n; i++)
        for(int j=0; j<G[i].size(); j++)
        {
            int v = G[i][j];
            if(sccno[i] != sccno[v])
                out[sccno[i]]++;
        }
    int res = 0;
  //  cout << scc_cnt << endl;
//    for(int i=1; i<=scc_cnt; i++)
//        cout << in[i] << endl;

    int cnt = 0;
    for(int i=1; i<=scc_cnt; i++)
        if(out[i] == 0)
            cnt++;
    if(cnt != 1)
        return puts("0"), 0;
    for(int i=1; i<=n; i++)
        if(out[sccno[i]] == 0) res++;

    pd(res);

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/WTSRUVF/p/9700385.html
今日推荐