POJ2186 solution report strongly connected branch + point reduction

Source: Internet
Author: User
Popular Cows
Time limit:2000 ms   Memory limit:65536 K
Total submissions:11739   Accepted:4641

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 (, 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 31 22 12 3

Sample output

1

Hint

Cow 3 is the only cow of high popularity. at the beginning, I used the common DFS for direct search. Unfortunately, the memory is too large, because I opened an array of 0.1 billion characters (PS: in the future, you must calculate the required memory before opening the large array.) After being MLE, you are unwilling to modify the memory. After downgrading the memory, you can continue to submit it, TLE, change it, and then TLE .. Several times later, I realized that common algorithms were hard to get through .. I have no idea how to change it... After reading the discussion, I learned that the Tarjan algorithm that requires strong-connected components is used. Baidu found Tarjan and it took a long time to understand it .. After reading Tarjan, I thought for more than an hour, wrote the code, and finally got this question AC .. It is really not easy, but this question has made me learn new things. A has a great sense of accomplishment. I would like to thank this question ~, Thank you for helping me ~! Idea: Use the Tarjan algorithm to calculate the number of strongly connected components C of a graph, regard each strongly connected component as a point (contraction point), and then find the degree of exit of each contraction point, determine whether the degree of exit of these points is equal to the C-1, if equal, then the number of points included in the connected branch of the degree of 0 is the demand, otherwise the output 0. Proof: assuming that the connected components of graph G have been obtained, any point in the connected branch can reach this point. Scale each connected component into a vertex. For a vertex, as long as other points exist in the one-way path to this point, the points contained in this point are supported by the points included in other connected branches. Therefore, the question is to find the degree of exit for each connected branch of the relational graph. If only one connected branch with an outbound degree of 0 exists, the number of points contained in the connected branch is required, otherwise, no cattle are supported by all cattle. # Include <iostream> <br/> using namespace std; <br/> int dfn [10010], low [10010], time, C, s [10010], S [10010], top; bool in [10010]; <br/> struct LIST <br/>{< br/> int v; LIST * next; <br/>}; </p> <p> LIST * head [10010], * rear [10010]; </p> <p> int chudu [10010]; </p> <p> void tarjan (int v) /* tarjan calculate the graph's strongly connected component */<br/> {<br/> dfn [v] = low [v] = ++ time; <br/> S [top ++] = v; <br/> in [v] = true; <br/> for (LIST * p = head [v]; p! = NULL; p = p-> next) <br/> if (! Dfn [p-> v]) <br/> {<br/> tarjan (p-> v ); <br/> if (low [p-> v] <low [v]) <br/> low [v] = low [p-> v]; <br/>}< br/> else if (in [p-> v] & low [p-> v] <low [v]) <br/> low [v] = low [p-> v]; </p> <p> if (low [v] = dfn [v]) <br/>{< br/> C ++; <br/> do <br/>{< br/> v = S [-- top]; <br/> in [v] = false; <br/> s [v] = C;/* thumbnail, mark Points belonging to the same strongly connected component */<br/>} while (low [v]! = Dfn [v]); <br/>}</p> <p> int main () <br/>{< br/> int n, m, I, a, B; <br/> while (cin> n> m) <br/> {<br/> memset (head, 0, sizeof (int) * 10010); <br/> memset (rear, 0, sizeof (int) * 10010); <br/> memset (s, 0, sizeof (int) * 10010); <br/> top = 0; <br/> for (I = 0; I <m; I ++) /* storage relationship diagram of the adjacent table */<br/> {<br/> scanf ("% d", & a, & B ); <br/> if (rear [a]! = NULL) <br/>{< br/> rear [a]-> next = new LIST; <br/> rear [a] = rear [a]-> next; <br/>}< br/> else <br/> head [a] = rear [a] = new LIST; <br/> rear [a]-> next = NULL; <br/> rear [a]-> v = B; <br/>}< br/> time = 0; C = 0; <br/> memset (dfn, 0, sizeof (int) * 10010 ); <br/> memset (low, 0, sizeof (int) * 10010); <br/> memset (in, 0, sizeof (bool )); <br/> for (I = 1; I <= n; I ++) <br/> if (! Dfn [I]) <br/> tarjan (I); <br/> if (C = 1)/* if there is only one strongly connected component, output n */<br/>{< br/> cout <n <endl; <br/> continue; <br/>}</p> <p> memset (chudu, 0, sizeof (chudu); </p> <p> for (I = 1; I <= n; I ++)/* calculate the degree of exit of each vertex after the thumbnail */<br/> for (LIST * p = head [I]; p! = NULL; p = p-> next) <br/> if (s [I]! = S [p-> v]) <br/> chudu [s [I] = 1; <br/> a = B = 0; <br/> for (I = 1; I <= C; I ++) <br/> if (chudu [I]) <br/> a ++; <br/> else <br/> B = I; <br/> if (a = C-1) /* count the total number of degrees is C-1 */<br/> {<br/> a = 0;/* if the total number of degrees is C-1, the number of points in the connected component with a degree of 0 is calculated */<br/> for (I = 1; I <= n; I ++) <br/> if (s [I] = B) <br/> a ++; <br/> cout <a <endl; <br/>}< br/> else/* if the total number of outbound degrees is not C-1, no solution is available, output 0 */<br/> cout <'0' <endl; </p> <p >}< br/> return 0; </p> <p>}

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.