[Bzoj 1051] [Haoi 2006] Popular cattle (Tarjan)

Source: Internet
Author: User

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

Well... This question seems to have been seen on POJ?

Compare the water problem, very good to come up with ideas. The relationship between cows and cows is like a graph, the cow a likes the Cow B is equivalent to the establishment of the A->b, and then in this direction graph, each strong connected component of the cows are like each other, the image is shrunk into dag,dag if there is only one point with a degree of 0, This point is a popular bull for all the cows in the Chingqiang connected component, if there is no 0 point, of course, there is no popular cattle, if the number of points of 0 is greater than 1, then each of the strong connected components out of 0 The number of fans will be less than n-1, there is no popular cattle.

A bit of a hole in the title, seemingly on the subject of the data range is small, was the pit WA once.

#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include < stdlib.h> #define Maxe 100050#define maxv 500050using namespace std;struct edge{int u,v,next;} Edges[maxe];int n,m;int head[maxv],ncount=0;int low[maxv],dfn[maxv],belong[maxv],stack[maxv],top=0;int cnt=0,tot=0 ; tot= number of strong connected components int NUM[MAXV],OUTDEGREE[MAXV];    Num[i]= the number of points in the strong connected component of I, outdegree= the degree of bool visit[maxv];void addedge (int u,int V) {edges[++ncount].u=u;    Edges[ncount].v=v;    Edges[ncount].next=head[u]; Head[u]=ncount;}    void Tarjan (int u)//tarjan indent {low[u]=dfn[u]=++cnt;    Stack[++top]=u;    Visit[u]=true;        for (int p=head[u];p!=-1;p=edges[p].next) {int v=edges[p].v;            if (!dfn[v]) {Tarjan (v);        Low[u]=min (Low[u],low[v]);    } else if (Visit[v]) low[u]=min (Low[u],dfn[v]);        } if (Dfn[u]==low[u]) {tot++;        int v=-1;         while (U!=V) {v=stack[top--];   Belong[v]=tot;            num[tot]++;        Visit[v]=false;    }}}int Main () {memset (head,-1,sizeof (head));    scanf ("%d%d", &n,&m);        for (int i=1;i<=m;i++) {int u,v;        scanf ("%d%d", &u,&v);    Addedge (U,V);    } for (int i=1;i<=n;i++) if (!dfn[i]) Tarjan (i);    for (int i=1;i<=m;i++) if (belong[edges[i].u]!=belong[edges[i].v]) outdegree[belong[edges[i].u]]++; int Res=0,ans;            Res= the number of points with a degree of 0, ans= a strong connected component number of 0 for (int i=1;i<=tot;i++) if (outdegree[i]==0) {res++;        Ans=i;    } if (res==1) printf ("%d\n", Num[ans]);    else printf ("0\n"); return 0;}



[Bzoj 1051] [Haoi 2006] Popular cattle (Tarjan)

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.