POJ 2186 Popular Cows (strong unicom)

Source: Internet
Author: User

http://poj.org/problem?id=2186

Popular cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 23819 Accepted: 9767

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.

Source

Usaco 2003 Fall


Test instructions

A herd of cows looking for the number of cows that are considered to be popular among all the other cows, which are popular for transmission, such as a think B is popular, B thinks C is popular, then a thinks C is also popular.

Analysis:

If a cow is popular, it can be reached from all the other cows, if it seems too complicated to use a search. First, the strong link shrinks, so that a dag, if the DAG has and only a 0 of the indentation point (the maximum strong unicom component), then this indentation includes the number of cattle is the answer.


/* * * author:fcbruce <[email protected]> * * time:tue Oct 03:00:16 PM CST * */#include <cstdio& GT, #include <iostream> #include <sstream> #include <cstdlib> #include <algorithm> #include < ctime> #include <cctype> #include <cmath> #include <string> #include <cstring> #include < stack> #include <queue> #include <list> #include <vector> #include <map> #include <set> #define SQR (x) ((x) * (x)) #define LL long long#define ITN int#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626#defi NE eps 1e-10#ifdef _win32 #define LLD "%i64d" #else #define LLD "%lld" #endif # define MAXM 50007#define maxn 10007using Nam Espace std;int n,m;int fir[maxn];int u[maxm],v[maxm],nex[maxm];int e_max;int PRE[MAXN],LOW[MAXN],SCCNO[MAXN],W[MAXN]  ; int st[maxn],top;int scc_cnt,dfs_clock;int deg[maxn];inline void Add_edge (int s,int t) {int e=e_max++;  u[e]=s;v[e]=t; Nex[e]=fir[u[e]];fir[u[e]]=e;} void Tarjan_dfs (int s) {  Pre[s]=low[s]=++dfs_clock;  St[++top]=s;    for (int e=fir[s];~e;e=nex[e]) {int t=v[e];      if (pre[t]==0) {Tarjan_dfs (t);    Low[s]=min (Low[s],low[t]);    } else {if (sccno[t]==0) low[s]=min (low[s],pre[t]);    }} if (Pre[s]==low[s]) {scc_cnt++;    for (;;)      {int x=st[top--];      sccno[x]=scc_cnt;      w[scc_cnt]++;    if (x==s) break;  }}}void FIND_SCC () {top=-1;  scc_cnt=dfs_clock=0;  memset (pre,0,sizeof Pre);  memset (low,0,sizeof low);  memset (w,0,sizeof W); for (int i=1;i<=n;i++) if (pre[i]==0) Tarjan_dfs (i);}  int main () {#ifdef fcbruce freopen ("/home/fcbruce/code/t", "R", stdin), #endif//Fcbruce scanf ("%d%d", &n,&m);  e_max=0;  memset (fir,-1,sizeof fir);    for (int e=0,u,v;e<m;e++) {scanf ("%d%d", &u,&v);  Add_edge (U,V);  } FIND_SCC ();  memset (deg,0,sizeof deg);    for (int e=0;e<e_max;e++) {if (Sccno[u[e]]==sccno[v[e]]) continue;  deg[sccno[u[e]]]++;  } int cnt=0,the_one; for (int i=1; i<=scc_cnt;i++) {if (deg[i]==0) {cnt++;    The_one=i;  }} if (cnt==1) printf ("%d\n", W[the_one]);  Else puts ("0"); return 0;}


POJ 2186 Popular Cows (strong unicom)

Related Article

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.