bzoj1051 [HAOI2006] Popular Cattle

Source: Internet
Author: User

1051: [HAOI2006] Popular cow time limit:10 Sec Memory limit:162 MB
submit:4773 solved:2541
[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 Output1HINT

100% of Data n<=10000,m<=50000Analysis: For this problem, it can be very natural to think of the strong connected components, why? Because if the answer is only 1 cows, then this cow must be composed of one point of the strong connected components, if the answer is greater than 1, then the requirements of the cattle must be in the same strong connected components, and only a strong connectivity components to meet the requirements, this can be easily proved that: the assumption that the required strong connected components have N, Then the nth strong connected component is bound to be connected with the first n-1 strong connected component, so that a strong connected component is formed, so that there are only 1 strong connected components that can meet the requirements by analogy.According to this, when we are in the statistical answer, if we find that the strong connected components that meet the requirements are more than 1, then there is no solution. How do you know if this strong connection meets the requirements? Very simple, out of the degree of 0 can, if the degree is not 0, then this "strong connectivity component" is another strong connected component part, as to how to find the degree, DFS built a mess to do it.
#include <cstdio>#include<stack>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intMAXN =10010, MAXM =50010;intN, M,head[maxn],to[maxm],nextt[maxm],tot,scc[maxn],dfsclock,low[maxn],pre[maxn],num,shuliang[maxn],ans;intHEAD2[MAXN], TO2[MAXN], NEXTT2[MAXN], Tot2;stack<int>s;voidAddintXinty) {Tot++; To[tot]=y; Nextt[tot]=Head[x]; HEAD[X]=tot;}voidADD2 (intXinty) {Tot2++; TO2[TOT2]=y; NEXTT2[TOT2]=Head2[x]; HEAD2[X]=Tot2;}voidTarjan (intu)    {s.push (U); Low[u]= Pre[u] = + +Dfsclock;  for(inti = Head[u];i;i =Nextt[i]) {        intv =To[i]; if(!Pre[v])            {Tarjan (v); Low[u]=min (Low[u], low[v]); }        Else            if(!Scc[v]) Low[u]=min (Low[u], pre[v]); }    if(Low[u] = =Pre[u]) {num++;  while(1)        {            intt =S.top ();            S.pop (); Scc[t]=num; Shuliang[num]++; if(T = =u) Break; }    }}voidLianbian (intu) {     for(inti = Head[u]; I i =Nextt[i]) {        intv =To[i]; if(Scc[u]! =Scc[v]) add2 (Scc[u], scc[v]); }}intMain () {scanf ("%d%d", &n, &m);  for(inti =1; I <= m; i++)    {        intA, B; scanf ("%d%d", &a, &b);    Add (A, b); }     for(inti =1; I <= N; i++)        if(!Scc[i]) Tarjan (i);  for(inti =1; I <= N; i++) Lianbian (i);  for(inti =1; I <= num; i++)        if(!Head2[i]) {            if(ans) {printf ("0\n"); return 0; } ans=Shuliang[i]; } printf ("%d\n", ans); return 0;}

bzoj1051 [HAOI2006] Popular Cattle

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.