poj2186强联通+缩点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37521566/article/details/80341877
题目链接点击打开链接
Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 37945 Accepted: 15456

Description

Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is 
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow. 

Input

* Line 1: Two space-separated integers, N and M 

* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular. 

Output

* Line 1: A single integer that is the number of cows who are considered popular by every other cow. 

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity. 

模板题目,结合度数判断一下就可以了

#include<iostream>
#include<string.h>
#include<stdio.h>
#define maxn 10005
using namespace std;
int n,m;
int a,b;
struct node
{
    int u;//保存每一条边的起始点
    int v;//保存每一条边的终止点
    int next;//保存每一条边的上一条边的编号
}edge[maxn*5];//保存每一条边,下标表示每一条边的编号
int head[maxn];//保存每一个顶点的起始边的编号
int dfn[maxn],low[maxn];
int stack[maxn];
int cnt,tot,top;
int num;
int sum[maxn];
int father[maxn];
int du[maxn];
bool check[maxn];
void add(int u,int v,int &k)
{
    edge[k].u=u;
    edge[k].v=v;
    edge[k].next=head[u];
    head[u]=k++;
}
void tarjan(int u)
{
    dfn[u]=low[u]=++tot;
    check[u]=true;
    stack[++top]=u;
    for(int k=head[u];k!=-1;k=edge[k].next)
    {
        int v=edge[k].v;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(check[v])
        {
            low[u]=min(low[u],low[v]);
        }
    }
    if(dfn[u]==low[u])
    {
        cnt++;
        int temp;
        do
        {
          temp=stack[top--];
          father[temp]=cnt;
          sum[cnt]++;
          check[temp]=false;
        }while(u!=temp);
    }
}
void solve()
{
    for(int i=1;i<=n;i++)
    {
        if(!dfn[i])
        {
            tarjan(i);
        }
    }
}
void init()
{
    num=0;
    tot=0;
    cnt=0;
    top=0;
    memset(head,-1,sizeof(head));
    memset(check,false,sizeof(check));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(father,0,sizeof(father));
    memset(sum,0,sizeof(sum));
    memset(du,0,sizeof(du));
}
int main()
{
    scanf("%d%d",&n,&m);
    init();
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&a,&b);
        add(a,b,num);
    }
    solve();
    for(int i=1;i<=n;i++)
    {
        for(int j=head[i];j!=-1;j=edge[j].next)
        {
            if(father[i]!=father[edge[j].v])
            {
                du[father[i]]++;
            }
        }
    }
    int t=0,ans;
    for(int i=1;i<=cnt;i++)
    {
        if(!du[i])
        {
            t++;
            ans=sum[i];
        }
    }
    if(t==1)
    {
        printf("%d\n",ans);
    }
    else
    {
        printf("0\n");
    }
}


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页