The title is right here!
1051: [HAOI2006] Popular cow time limit:10 Sec Memory limit:162 MB
submit:4438 solved:2353
[Submit] [Status] [Discuss] Description the desire of every cow is to become one of the most popular cows. Now there are n cows, give you m-pairs of integers (a, a, b), indicating that cow A is considered to be popular. This relationship is transitive, and if a thinks B is popular, B thinks C is popular, then bull a also thinks that Ox C is popular. Your task is to find out how many cows are considered popular by all cows. Input first row two number n,m. Next m line, two numbers per line, A, B, meaning that A is considered a welcome (the information given may be repeated, that is, there may be multiple, b) Output
A number, that is, how many cows are considered popular by all cows.
Sample Input3 3
1 2
2 1
2 3Sample Output1hint100% Data N<=10000,m<=50000source
[Submit] [Status] [Discuss]
This problem and codevs2822 love in the heart similar, is also a tarjan practiced hand problem, so specific Tarjan board I will no longer post it out.
The idea of the problem can also be the same as codevs2822, using TARJAN+SPFA, but because I am too lazy, do not want to play 140 lines of code, it is another way of thinking.
Positive solution: Tarjan Reduction Point
Specific practices:
The first step is to read the data to build the edge first;
The second step, start running Tarjan, do not forget in the Tarjan process to shrink points, the next process to use. (the so-called contraction point, that is, each point is grouped into a strong unicom block, the strong Unicom block operation)
The third step: according to M-side relations, statistics of each strong unicom block out of the degree.
The fourth step: the number of strong unicom blocks with a statistical degree of 0, if 1, the output of the number of times strong unicom block elements, otherwise output 0. (Here is the core idea of the topic, think about why, in fact, very simple).
First put on the code (we recommend that you do not use vector this thing, data structure as far as possible handwriting)
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <cstdlib>6#include <algorithm>7#include <stack>8 using namespacestd;9 intGet_num () {Ten intnum =0; One CharC; A BOOLFlag =false; - while((c = GetChar ()) = =' '|| c = ='\ r'|| c = ='\ n'); - if(c = ='-') theFlag =true; - Elsenum = C-'0'; - while(IsDigit (c =GetChar ())) -num = num *Ten+ C-'0'; + return(Flag?-1:1) *num; - } + Const intMAXN = 1e4 +5; A Const intMAXM = 5e4 +5; at intN,m,h[maxn],sccno[maxn],scc_cnt,id[maxn],sum,r[maxn],dfn[maxn],low[maxn],dfs_clock,ans,pos; -stack<int>s; - structedge{ - intFr,to,next; -}EDGES[MAXM <<1]; - voidAddedge (intUintv) { inedges[sum].fr =u; -Edges[sum].to =v; toEdges[sum].next =H[u]; +H[u] = sum++; - } the voidinit () { *memset (ID,0,sizeof(ID)); $memset (Sccno,0,sizeof(SCCNO));Panax NotoginsengMemset (R,0,sizeof(R)); -memset (h,-1,sizeof(h)); thememset (Edges,0,sizeof(edges)); +sum =0; ADfs_clock =0; thememset (DFN,0,sizeof(DFN)); +memset (Low,0,sizeof(Low)); - } $ voidTarjan (intu) { $Dfn[u] = Low[u] = + +Dfs_clock; - s.push (u); - for(inti = h[u];i! =-1; i =Edges[i].next) { theEdge E =Edges[i]; - if(!Dfn[e.to]) {Wuyi Tarjan (e.to); theLow[u] =min (low[u],low[e.to]); - } Wu Else if(!id[e.to]) -Low[u] =min (low[u],dfn[e.to]); About } $ if(Low[u] = =Dfn[u]) { -scc_cnt++; - while(true){ - intx =s.top (); A S.pop (); +SCCNO[SCC_CNT] + =1; theID[X] =scc_cnt; - if(x = = u) Break; $ } the } the return; the } the voidFind_tarjan () { - for(inti =1; I <= n;++i) { in if(!Dfn[i]) the Tarjan (i); the } About return; the } the intMain () { the intb; + init (); -n =get_num (); them =get_num ();Bayi for(inti =1; I <= m;++i) { theA =get_num (); theb =get_num (); - Addedge (A, b); - } the Find_tarjan (); the for(inti =0; I < sum;++i) { the intx =edges[i].fr; the inty =edges[i].to; - if(Id[x]! =Id[y]) ther[id[x]]++; the } theAns =0;94 for(inti =1; I <= scc_cnt;++i) { the if(R[i] = =0){ the if(!ans) theAns =Sccno[i];98 Else{ AboutAns =0; - Break;101 }102 }103 }104printf"%d\n", ans); the return 0;106}
This problem-solving report is written here, Noip countdown two months, I wish you can test a good result!
Bzoj 1051 Most Popular cow problem solving report