Bzoj 1051: [haoi2006] popular Tarjan contraction point

Source: Internet
Author: User
1051: [haoi2006] popular ox time limit: 10 sec memory limit: 162 MB
Submit: 2092 solved: 1096
[Submit] [Status] Description

The desire of every ox is to become the most popular ox. Now there is a nheaded ox. Here, we will give you an integer (a, B) for M, which indicates that ox A thinks ox B is popular. This relationship is passed. If a thinks B is popular and B thinks C is popular, then a thinks that C is popular. Your task is to find out how many cows are considered popular by all the cows.

Input

The first row has two numbers N and M. In the next m row, there are two numbers A and B in each row, which means that a considers B to be popular (the given information may be duplicated, that is, multiple numbers A and B may appear)

Output

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

Sample input3 3
1 2
2 1
2 3
Sample output1

[Data Scope]
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 suddenly found that there were problems with the previous Tarjan, VIS mark should be played when the stack is out, rather than exit. It is very simple to judge the "root" after this question is reduced. However, due to the lack of in-depth thinking, I did not grasp this question. "When and only when the Dag degrades to a" Tree ", so it was wrong after half a day.
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<queue>#include<map>#include<set>using namespace std;#define MAXN 11000#define MAXV MAXN#define MAXE MAXN*20struct Edge{        int np;        Edge *next;}E[MAXE],*V[MAXV];int tope=-1;void addedge(int x,int y){        E[++tope].np=y;        E[tope].next=V[x];        V[x]=&E[tope];}int low[MAXN],dfn[MAXN];int dfstime=0;int stack[MAXN];int tops=-1;int color[MAXN],topc=0;int size_c[MAXN];int size_sub[MAXN];bool vis[MAXN];set<int> S[MAXN];void tarjan(int now){        low[now]=dfn[now]=++dfstime;        Edge *ne;        stack[++tops]=now;        for (ne=V[now];ne;ne=ne->next)        {                if (vis[ne->np])continue;                if (dfn[ne->np])                {                        low[now]=min(low[now],dfn[ne->np]);                }else                {                        tarjan(ne->np);                        low[now]=min(low[now],low[ne->np]);                }        }        if (low[now]==dfn[now])        {                ++topc;                while (stack[tops]!=now)                {                        vis[stack[tops]]=true;                        color[stack[tops--]]=topc;                        size_c[topc]++;                }                vis[stack[tops]]=true;                color[stack[tops--]]=topc;                size_c[topc]++;        }}pair<int,int> edge[MAXE];int topedge;int degree[MAXN];queue<int> Q;int main(){        freopen("input.txt","r",stdin);        int n,m;        scanf("%d%d",&n,&m);        int i,j,k,x,y,z;        for (i=0;i<m;i++)        {                scanf("%d%d",&x,&y);                addedge(x,y);                edge[i].first=x;                edge[i].second=y;        }        sort(edge,&edge[m]);        topedge=0;        for (i=1;i<m;i++)        {                if (edge[i]!=edge[topedge])                        edge[++topedge]=edge[i];        }        for (i=1;i<=n;i++)        {                if (!dfn[i])tarjan(i);        }    //    memset(V,0,sizeof(V));    //    tope=-1;        for (i=0;i<m;i++)        {                if (color[edge[i].first]==color[edge[i].second])continue;        //        if (S[color[edge[i].first]].find(color[edge[i].second])!=S[color[edge[i].first]].end())continue;    //            addedge(color[edge[i].first],color[edge[i].second]);        //        S[color[edge[i].first]].insert(color[edge[i].second]);                degree[color[edge[i].first]]++;        }        int ans;        ans=0;        for (i=1;i<=topc;i++)        {                if (degree[i]==0)ans++,x=i;        }        if (ans==1)        {                printf("%d\n",size_c[x]);        }else        {                printf("0\n");        }        return 0;}

 

Bzoj 1051: [haoi2006] popular Tarjan contraction point

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.