BZOJ1051 [HAOI2006] Popular Cattle

Source: Internet
Author: User

Title Link: http://www.lydsy.com/JudgeOnline/problem.php?id=1051

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

The first line is two numbers n,m. Next m line, two numbers per line, A, B, meaning that A is considered to be welcome (the information given may be repeated, that is, there may be multiple A, b)

Output

A number, that is, how many cows are considered popular by all cows.

Review Tarjan template, Tarjan after the contraction point, if there is only a 0 of the strong Unicom component output its size, otherwise no solution

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <stack>6 #defineRep (i,l,r) for (int i=l; i<=r; i++)7 #defineCLR (x, y) memset (x,y,sizeof (×))8 #defineTravel (x) for (int i=last[x]; i; i=edge[i].pre)9 using namespacestd;Ten Const intINF =0x3f3f3f3f; One Const intMAXN =10010; A structedge{ -     intpre,to; -}edge[50010]; the intn,m,x,y,tot=0, ans,scc=0, dfsclock=0, LAST[MAXN],DFN[MAXN],LOW[MAXN],BELONG[MAXN],CNT[MAXN]; - BOOLISIN[MAXN],OUD[MAXN]; -Stack <int>s; -InlineintRead () { +     intAns =0, F =1; -     Charc =GetChar (); +      while(!IsDigit (c)) { A         if(c = ='-') F =-1; atc =GetChar (); -     } -      while(IsDigit (c)) { -Ans = ans *Ten+ C-'0'; -c =GetChar (); -     } in     returnAns *F; - } toInlinevoidAddedge (intXinty) { +Edge[++tot].pre =Last[x]; -Edge[tot].to =y; theLAST[X] =tot; * } $ voidTarjan (intx) {Panax NotoginsengDFN[X] = low[x] = + +Dfsclock; -ISIN[X] =1; S.push (x); the Travel (x) { +         if(!Dfn[edge[i].to]) { A Tarjan (edge[i].to); theLOW[X] =min (low[x],low[edge[i].to]); +         } -         Else if(Isin[edge[i].to]) low[x] =min (low[x],dfn[edge[i].to]); $     } $     if(Low[x] = =Dfn[x]) { -Scc++; -          while(S.top ()! =x) { theIsin[s.top ()] =0; -Belong[s.top ()] =SCC;Wuyi S.pop (); thecnt[scc]++; -         } WuISIN[X] =0; BELONG[X] = SCC; S.pop (); cnt[scc]++; -     } About } $ intMain () { -n = read (); m =read (); -Rep (I,1, m) x = Read (), y =read (), Addedge (x, y); -CLR (DFN,0); CLR (CNT,0); ARep (I,1, N)if(!Dfn[i]) Tarjan (i); +CLR (Oud,0); theRep (now,1, N) Travel (now)if(Belong[now]! = belong[edge[i].to]) Oud[belong[now]] =1; -Ans =0; $Rep (I,1, SCC)if(!Oud[i]) { the         if(ANS) {ans =0; Break;} theAns =Cnt[i]; the     } theprintf"%d\n", ans); -     return 0; in}
View Code

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.