Language:DefaultPopular cows
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 24384 |
|
Accepted: 10007 |
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: N cows envy each other, can convey envy, ask the maximum number of cows envied by all cows
Ideas:
Tarjan point, the different points of the planning to a strong Unicom branch, and finally determine whether the strong connectivity sub-expenditure degree of 0 is unique, the only solution, the answer is the number of points of the strong connected branch, no one has no solution
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <queue> #include <stack> #include <vector> #include <set> #include <map > #define L (x) (x<<1) #define R (x) (x<<1|1) #define MID (x, y) ((x+y) >>1) #define EPS 1e-8typedef __ Int64 ll; #define FRE (i,a,b) for (i = A; I <= b; i++) #define MEM (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &C) #define PF printf#define Bug pf ("hi\n") using namespace std; #define N 10024int head[n],low[n],time [N],e_num,tim_num;int n,m,ans,instack[n],vis[n],type[n],out[n];struct stud{int to,next;} e[n*10];stack<int>q;inline void Add (int u,int v) {e[e_num].to=v; E[e_num].next=head[u]; head[u]=e_num++;} void Tarjan (int x) {int i,j; Q.push (x); Instack[x]=1; Time[x]=low[x]=++tim_num; For (i=head[x];i!=-1;i=e[i].next) {int to=e[i].to; if (time[to]==0) {Tarjan (to); if (Low[x]>low[to]) low[x]=low[to]; } else if (Instack[to]&&low[x]>time[to]) low[x]=time[to]; } if (Low[x]==time[x]) {ans++; do{J=q.top (); Q.pop (); instack[j]=0; Type[j]=ans; Mark J belongs to the first few Unicom branch vis[ans]++; This unicom branch of point +1//printf ("%d ans=%d\n", J,ans); }while (j!=x); }}void solve () {int i,j; MEM (time,0); MEM (instack,0); MEM (out,0); MEM (vis,0); ans=tim_num=0; for (i=1;i<=n;i++) if (time[i]==0) Tarjan (i), for (i=1;i<=n;i++) for (j=head[i];j!=-1;j=e[j].next) {int to= E[j].to;if (Type[i]!=type[to]) {out[type[i]]=1; }}int t=0,pos;for (i=1;i<=ans;i++)//has a total of ANS branch, take out if (out[i]==0) {t++;p os=i;if (t>1) break;} If there is only one branch of the Unicom with a degree of 0, then the number of this branch is the answer,//Otherwise, no solution if (t==1) printf("%d\n", Vis[pos]); elseprintf ("0\n");} int main () {int i,j; scanf ("%d%d", &n,&m); {int u,v; e_num=0; MEM (head,-1); while (m--) {SFF (u,v); Add (U,V); } solve (); } return 0;}
POJ Popular Cows (Tarjan + pinch point)