"bzoj1051" [HAOI2006] Popular cattle Tarjan point of calculation points

Source: Internet
Author: User

"bzoj1051" [HAOI2006] Popular CattleJanuary 8, 2014 7450 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.

Sample INPUT3 3
1 2
2 1
2 3Sample Output1 "data range"
10% of data n<=20, m<=50
30% of Data n<=1000,m<=20000
70% of Data n<=5000,m<=50000
100% of data n<=10000,m<=50000 code
#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<queue>#include<typeinfo>#include<map>#include<bits/stdc++.h>typedefLong Longll;using namespacestd;#defineINF 10000000inline ll read () {ll x=0, f=1; CharCh=GetChar ();  while(ch<'0'|| Ch>'9')    {        if(ch=='-') f=-1; CH=GetChar (); }     while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; CH=GetChar (); }    returnx*F;}//***************************************************************inthead[10001],dfn[10001];structss{intTo,next; intHea;} e[10001*5];intt=1;intbcnt;intCNT;intbelong[10001];inthav[10001];int  out[10001];intn,m,top,low[10001],stacks[10001];intvis[10001];intinstack[10001];voidAddintUintv) {E[t].hea=u; E[t].to=v; E[t].next=Head[u]; Head[u]=t++;}voidDfsintu) {Dfn[u]=low[u]=++CNT; stacks[++top]=u; Instack[u]=1; Vis[u]=1;  for(intI=head[u];i;i=E[i].next) {        if(!Vis[e[i].to])            {DFS (e[i].to); Low[u]=min (low[u],low[e[i].to]); }        Else if(Instack[u]) {low[u]=min (low[u],dfn[e[i].to]);} }    intv=-1; if(low[u]==Dfn[u]) {bcnt++;  while(u!=v) {v=stacks[top--]; INSTACK[V]=0; BELONG[V]=CNT; HAV[BCNT]++; //vis[v]=0;        }    }}voidrebuild () { for(intI=1; i<=m;i++){        if(belong[e[i].hea]!=belong[e[i].to]) out[belong[e[i].hea]]++; }}voidTarjan () { for(intI=1; i<=n;i++){        if(!Vis[i]) DFS (i); } rebuild ();}voidWork () {intres=0, ans; //cout<<bcnt<<endl;     for(intI=1; i<=bcnt;i++){        if( out[i]==0) {res++; Ans=i; }       ///printf ("%d\n", Hav[i]);    }    if(res==1) {printf ("%d\n", Hav[ans]); }    Elseprintf"0\n");}intMain () {scanf ("%d%d",&n,&m); intb;  for(intI=1; i<=m; i++) {scanf ("%d%d",&a,&b);    Add (A, b);    } Tarjan ();    Work (); return 0;}

"bzoj1051" [HAOI2006] Popular cattle Tarjan point of calculation points

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.