[POJ2186]Popular Cows(Tarjan)

【原题】

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.

【思路】Tarjan缩点 输出缩点后出度为0的点缩点前的点的个数 如果有两个 则不完全连通 输出0

  1 #include <algorithm>
  2 #include <cmath>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <list>
  6 #include <map>
  7 #include <iostream>
  8 #include <queue>
  9 #include <set>
 10 #include <stack>
 11 #include <string>
 12 #include <vector>
 13 #include <iomanip>
 14 #define LL long long
 15 #define inf 0x3f3f3f3f
 16 #define INF 0x3f3f3f3f3f3f
 17 #define F first
 18 #define S second
 19 #define lson  rt << 1
 20 #define rson  rt << 1 | 1
 21 using namespace std;
 22 
 23 const int maxn = 1e4 + 7;
 24 const int maxm = 5e4 + 7;
 25 int n, m, x, y, out[maxn];
 26 vector<int> G[maxn];
 27 
 28 int indx = 0, sum = 0;
 29 int in[maxn], dfn[maxn], low[maxn], bel[maxn], cnt[maxn];
 30 stack<int> S;
 31 void tarjan(int u)
 32 {
 33     dfn[u] = low[u] = ++indx;
 34     S.push(u);
 35     in[u] = 1;
 36     for (int i = 0; i < G[u].size(); i++)
 37     {
 38         int v = G[u][i];
 39         if (!dfn[v])
 40         {
 41             tarjan(v);
 42             low[u] = min(low[u], low[v]);
 43         }
 44         else if (in[v]) low[u] = min(low[u], dfn[v]);
 45     }
 46     if (dfn[u] == low[u])
 47     {
 48         sum++;
 49         int v;
 50         bel[u] = sum;
 51         do
 52         {
 53             v = S.top();
 54             bel[v] = sum;
 55             cnt[sum]++;
 56             in[v] = 0;
 57             S.pop();
 58         } while (u != v);
 59     }
 60 }
 61 
 62 int main()
 63 {
 64     ios::sync_with_stdio(false);
 65     cin.tie(0);
 66     cin >> n >> m;
 67     int ta, tb;
 68     for (int i = 1; i <= m; i++)
 69     {
 70         cin >> ta >> tb;
 71         G[ta].push_back(tb);
 72     }
 73     for (int i = 1; i <= n; i++)
 74     {
 75         if (!dfn[i]) tarjan(i);
 76     }
 77     for (int i = 1; i <= n; i++)
 78     {
 79         for (int j = 0; j < G[i].size(); j++)
 80         {
 81             int nw = G[i][j];
 82             if (bel[i] != bel[nw])
 83             {
 84                 out[bel[i]]++;
 85             }
 86         }
 87     }
 88     int flag = 1, ans = 0;
 89     for (int i = 1; i <= sum; i++)
 90     {
 91         if (!out[i])
 92         {
 93             if (ans)
 94             {
 95                 flag = 0;
 96                 break;
 97             }
 98             else ans = cnt[i];
 99         }
100     }
101     if (!flag) cout << "0" << endl;
102     else cout << ans << endl;
103 }
原文地址:https://www.cnblogs.com/hfcdyp/p/13323060.html