id=2186 ">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 Field=source&key=usaco+2003+fall "style=" Text-decoration:none ">usaco 2003 Fall |
Test instructions
A herd of cows finds the number of cows that are popular among all the other cows. The popularity of this is transitive. For example, a thinks B is popular. B thinks c is popular, so a thinks C is also popular.
Analysis:
Suppose that a cow is popular. The cow can then be reached from all the other cows. Suppose it seems too complicated to do it with a search. First, a strong link to shrink points. This gets a DAG, assuming that the DAG has and has only one indent point with a scale of 0 (the maximum strong connectivity component), then the number of cows that this indentation contains 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)