POJ2186 强连通分量+缩点

Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 40234   Accepted: 16388

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

 

题意:强连通分量缩点图求出度为0的点。

思路:首先图要连通,其次出度为零的强连通分量个数只能为1.

代码:

 

  1 #include"bits/stdc++.h"
  2 
  3 #define db double
  4 #define ll long long
  5 #define vl vector<ll>
  6 #define ci(x) scanf("%d",&x)
  7 #define cd(x) scanf("%lf",&x)
  8 #define cl(x) scanf("%lld",&x)
  9 #define pi(x) printf("%d\n",x)
 10 #define pd(x) printf("%f\n",x)
 11 #define pl(x) printf("%lld\n",x)
 12 #define rep(i, a, n) for (int i=a;i<n;i++)
 13 #define per(i, a, n) for (int i=n-1;i>=a;i--)
 14 #define fi first
 15 #define se second
 16 using namespace std;
 17 typedef pair<int, int> pii;
 18 const int N = 1e6 + 5;
 19 const int mod = 1e9 + 7;
 20 const int MOD = 998244353;
 21 const db PI = acos(-1.0);
 22 const db eps = 1e-10;
 23 const ll INF = 0x3fffffffffffffff;
 24 int n, m;
 25 int cnt, num, id;
 26 int head[N];
 27 bool ins[N];
 28 int out[N];
 29 int dfn[N], low[N];
 30 int beg[N];
 31 stack<int> s;
 32 struct P {int to, nxt;} e[N];
 33 
 34 void add(int u, int v) {
 35     e[cnt].to = v;
 36     e[cnt].nxt = head[u];
 37     head[u] = cnt++;
 38 }
 39 
 40 void tarjan(int u) {
 41     low[u] = dfn[u] = ++id;
 42     ins[u] = 1;
 43     s.push(u);
 44     for (int i = head[u]; ~i; i = e[i].nxt) {
 45         int v = e[i].to;
 46         if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]);
 47         else if (ins[v]) low[u] = min(low[u], dfn[v]);
 48     }
 49     if (low[u] == dfn[u]) {
 50         int v;
 51         do {
 52             v = s.top();
 53             s.pop();
 54             ins[v] = 0;
 55             beg[v] = num;//缩点
 56         } while (u != v);
 57         num++;
 58     }
 59 }
 60 
 61 int fa[N];
 62 bool vis[N];
 63 
 64 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
 65 void unio(int x, int y) {
 66     int xx = find(x), yy = find(y);
 67     if (xx != yy) fa[xx] = yy;
 68 }
 69 void init() {
 70     memset(head, -1, sizeof(head));
 71     memset(low, 0, sizeof(low));
 72     memset(dfn, 0, sizeof(dfn));
 73     memset(ins, 0, sizeof(ins));
 74     memset(out, 0, sizeof(out));
 75     memset(beg, 0, sizeof(beg));
 76     memset(vis,0, sizeof(vis));
 77     for (int i = 1; i <= n; i++) fa[i] = i;
 78     cnt = num = id = 0;
 79 }
 80 int main() {
 81     while (scanf("%d%d", &n, &m) == 2) {
 82         init();
 83         for (int i = 0; i < m; i++) {
 84             int x, y;
 85             ci(x), ci(y);
 86             add(x, y);
 87             unio(x, y);
 88         }
 89         for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i);
 90         for (int i = 1; i <= n; i++) {
 91             for (int j = head[i]; ~j; j = e[j].nxt) {
 92                 int v = e[j].to;
 93                 if (beg[i] != beg[v]) out[beg[i]]++;
 94             }
 95         }
 96         int ok = 1;
 97         int x = find(1);
 98         for (int i = 1; i <= n; i++)//联通
 99             if (find(i) != x) {
100                 ok = 0;
101                 break;
102             }
103         int tmp = 0, cnt = 0;
104         for (int i = 1; i <=n; i++) {//强连通分量个数
105             if (!out[beg[i]]){
106                 if(!vis[beg[i]]) vis[beg[i]]=1,cnt++;
107                 tmp++;
108             }
109         }
110         if (cnt==1&&ok==1) pi(tmp);
111         else puts("0");
112     }
113     return 0;
114 }

 

 

 

猜你喜欢

转载自www.cnblogs.com/mj-liylho/p/9548298.html
今日推荐