Poj 2168 popular cows

Source: Internet
Author: User

Description

Every cow's dream is to becomethe 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
Istransitive, if a thinks B is popular and B thinks C is popular, then a willalso think that C is
Popular, even if this is not explicitly specified by an Ordered Pair in theinput. Your task is to compute the number of cows that are considered popularby every other cow.

Input

* Line 1: two space-separatedintegers, N and m

* Lines 2 .. 1 + M: two space-separated numbers a and B, meaning that a thinks B ispopular.

Output

* Line 1: A single integerthat 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

Topic Introduction: (a, B) indicates that a thinks B is popular, and each ox thinks that he is popular. If (a, B) AND (B, c), then (a, c ). That is, a considers B to be popular, and B thinks C to be popular. Then a also thinks C is popular. The problem is, find out how many cows are welcomed by all the cows.

Method: kosaraju algorithm. Actually, I don't know much about it ..... It was evening to solve this problem. This algorithm is learned only when this question is done. Look at this algorithm to see more than two points ..... I understand the algorithm, but I still don't know the principle ..... What is to be learned?

 

#include<iostream>#include <cstdio>#include <cstring>#include <vector>using namespace std;vector<int> v1[10010], v2[10010];vector<int> s;int num[10010], sum[10010], mark[10010];int counter;void DFS1(int x){    mark[x] = 1;for (int i = 0; i < v1[x].size(); ++i)        {if (!mark[v1[x][i]])                {DFS1(v1[x][i]);                }}s.push_back(x);};void DFS2(int x){    num[x] = counter;  sum[counter]++; mark[x] = 1;for (int i = 0; i < v2[x].size(); ++i)           {if (!mark[v2[x][i]])                   {DFS2(v2[x][i]);}}};void SUM(int n){    memset(mark, 0, sizeof(mark)); for (int i = 1; i <= n; i++)    {for (int j = 0; j < v1[i].size();j++)        {int x = v1[i][j];            if (num[i] != num[x]){mark[num[i]] = 1;}}    }int flag = 0, ans;for (int i = 1; i <= counter; i++){     if (!mark[i])       {ans = i;           flag++;       }}if (flag == 1){printf("%d\n", sum[ans]);}else{printf("0\n");}};int main(){int n, m, a, b, i, j;scanf("%d%d",&n,&m);for (i = 0;i<m;i++){scanf("%d%d", &a, &b);v1[a].push_back(b);v2[b].push_back(a);}for (i = 1;i<=n;i++){if (!mark[i]){DFS1(i);}}memset(mark, 0, sizeof(mark));counter = 0;for (i = s.size() - 1;i>=0;i--){if (!mark[s[i]]){counter++;DFS2(s[i]);}}SUM(n);return 0;}

 

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.