POJ 2186--Popular cows "strong connected branch && Tarjan indent"

Source: Internet
Author: User

Popular cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 27198 Accepted: 10963

Description

Every cow's dream is to become the very popular cow in the herd. In a herd of n (1 <= n <=) cows, you is given up to M (1 <= m <= 50,000) ordered pairs of the form (A, b) That's cow A thinks that cow B is popular. Since popularity is transitive, if a thinks B was popular and b thinks C is popular, then a would also think that C is
Popular, even if this is a ordered pair in the input, explicitly specified. Your task is to compute the number of cows that was 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 was considered popular by every and other cow.

Sample Input

3 31 22) 12 3

Sample Output

1

Hint

Cow 3 is the only Cow of high popularity.

Test instructions: Every cow has a dream to be the cow most admired by cows in the herd, and in the herd there are n cows given m pairs of ordered pairs (a, B), indicating a admiration for B. Because the admiration relationship is transitive, that is, if a admires b,b and admires C, a also admires C, counting the number of cows admired by each cow in the herd.

Analytical:

If a cow a in a strong connected component is admired by a strong connected component and the other end of the cow B, each cow in the strong connected component is admired by B;

If a cow a in the strong connected component admires the other end of the strong connected component of the cow B, then each cow in the strong connected component admires B.


So the "each strong connected branch" is shrunk to a point, recording the degree of each "indent", and constructing a new diagram. The number of "indents" with a statistic of 0, if exactly 1, indicates that the "indent" can be reached by all other "indents". The "indent" contains the number of points that we ask for the answer.


For simplicity:

Give a graph of how many points there are to meet the conditions: All other points can reach this point.

Strongly connected branches + indents, and then statistics of each "indentation" of the degree, if only one is 0, then the number of internal points output, if there are more than 0, indicating no answer, output 0.

#include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm > #define MAXN 10000 + 1000#define MAXM 50000 + 5000using namespace Std;int N, m;struct node {int u, V, next;}; Node Edge[maxm];int HEAD[MAXN], Cnt;int LOW[MAXN], dfn[maxn];int dfs_clock;int stack[maxn];bool Instack[maxn];int top; int BELONG[MAXN], scc_clock;int out[maxn];void init () {cnt = 0;memset (head,-1, sizeof (head));} void Addedge (int u, int v) {edge[cnt] = {u, V, head[u]};head[u] = cnt++;}        void Getmap () {while (m--) {int A, B;        scanf ("%d%d", &a, &b);    Addedge (A, b);    }}void Tarjan (int u, int per) {int V;    Low[u] = dfn[u] = ++dfs_clock;    stack[top++] = u;    Instack[u] = true;        for (int i = head[u]; I! =-1; i = edge[i].next) {v = edge[i].v;            if (!dfn[v]) {Tarjan (V, u);        Low[u] = min (Low[v], low[u]);        } else if (Instack[v]) {Low[u] = min (Low[u], dfn[v]); }} if (DFN[u] = = Low[u]) {scc_clock++;            do{v = stack[--top];            INSTACK[V] = false;        BELONG[V] = Scc_clock;    }while (U! = v);    }}void find () {memset (low, 0, sizeof (low));    memset (DFN, 0, sizeof (DFN));    Memset (Instack, False, sizeof (Instack));    memset (Belong, 0, sizeof (Belong));    Dfs_clock = Scc_clock = top = 0;    for (int i = 1; I <= n; ++i) {if (!dfn[i]) Tarjan (i, I);    }}void Suodian () {for (int i = 1; I <= scc_clock; ++i) out[i] = 0;        for (int i = 0; i < cnt; ++i) {int u = belong[edge[i].u];        int v = belong[edge[i].v];    if (U! = v) out[u]++;    }}void solve () {int ans = 0;int temp = 0;int i;        for (int i = 1; I <= scc_clock; ++i) {//printf ("%d\n", Out[i]);        if (out[i] = = 0) {temp = i;        ans++;        if (ans > 1) {printf ("0\n");    return;}}    } int num = 0; for (int i = 1; I <= n; ++i) {if (belong[i] = = temp) num++; } printf ("%d\n", num);}        int main () {while (scanf ("%d%d", &n, &m)! = EOF) {init ();        Getmap ();        Find ();        Suodian (); Solve ();} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2186--Popular cows "strong connected branch && Tarjan indent"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.