poj2186 Popular Cows --- 强连通

给一个有向图,问有多少结点是其他所有结点都可以到达的。

等价于,在一个有向无环图上,找出度为0 的结点,如果出度为0的结点只有一个,那么这个就是答案,如果大于1个,则答案是0。

这题有环,所以先缩点。求唯一出度为0的强连通分量。

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<iostream>
#define inf 0x3f3f3f3f
using namespace std;
#define M 10010//图中点数
int sta[M],top;
bool vis[M];
int dfn[M];
int low[M];
int ccnt;        //有向图强连通分量个数
int id;
vector<int> e[M];
vector<int> part[M];//每个联通块的组成
int inpart[M];//每个原图上的点在哪个联通块里
int n,m,out[M];

void tarjan(int x)
{
    int i,j;
    dfn[x]=low[x]=id++;
    vis[x]=1;
    sta[++top]=x;
    for(i=0;i<e[x].size();i++)
    {
        j=e[x][i];
        if(dfn[j]==-1)
        {
            tarjan(j);
            low[x]=min(low[x],low[j]);
        }
        else if(vis[j])
            low[x]=min(low[x],dfn[j]);
    }
    if(dfn[x]==low[x])
    {
        do
        {
            j=sta[top--];
            vis[j]=0;
            part[ccnt].push_back(j);
            inpart[j]=ccnt;
        }while(j!=x);
        ccnt++;
    }
}

void solve(int n)
{
    memset(sta,-1,sizeof sta);
    memset(vis,0,sizeof vis);
    memset(dfn,-1,sizeof(dfn));
    memset(low,-1,sizeof(low));
    top=ccnt=id=0;
    for(int i=1;i<=n;i++)
        if(dfn[i]==-1)
            tarjan(i);
}

int main()
{
    int i,j,a,b,c;
    while(~scanf("%d%d",&n,&m))
    {
        for(i=0;i<=n;i++)
        {
            part[i].clear();
            e[i].clear();
        }
        while(m--)
        {
            scanf("%d%d",&a,&b);
            e[a].push_back(b);
        }
        solve(n);
        memset(out,0,sizeof out);
        for(i=1;i<=n;i++)
        {
            for(j=0;j<e[i].size();j++)
            {
                a=e[i][j];
                if(inpart[a]!=inpart[i])
                    out[inpart[i]]++;
            }
        }
        int ans=0,flag=0;
        for(i=0;i<ccnt;i++)
        {
            if(out[i]==0)
            {
                flag++;
                ans+=part[inpart[i]].size();
            }
            if(flag>1)//大于一个出度为0的点 则没有符合条件的答案
            {
                ans=0;
                break;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

poj2186 Popular Cows --- 强连通,布布扣,bubuko.com

时间: 2024-12-25 00:06:25

poj2186 Popular Cows --- 强连通的相关文章

强连通分量tarjan缩点——POJ2186 Popular Cows

这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定意味着v可达u.    相互可达则属于同一个强连通分量    (Strongly Connected Component, SCC) §有向图和它的转置的强连通分量相同 §所有SCC构成一个DAG(有向无环图) dfn[u]为节点u搜索的次序编号(时间戳),即首次访问u的时间 low[u]为u或u的

POJ2186 Popular Cows 【强连通分量Kosaraju】

Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23445   Accepted: 9605 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 &l

POJ 2186 Popular Cows --强连通分量

题意:给定一个有向图,问有多少个点由任意顶点出发都能达到. 分析:首先,在一个有向无环图中,能被所有点达到点,出度一定是0. 先求出所有的强连通分支,然后把每个强连通分支收缩成一个点,重新建图,这样,这个有向图就变成了一个有向无环图. 在这个新的图中,只需知道出度为0的点有几个即可. 如果出度为0的点超过1个,则输出0:否则输出出度为0的点所代表的那个强连通分支的分量数即可. 用Tarjan求强连通分量 代码: #include <iostream> #include <cstdio&g

POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】

Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23445   Accepted: 9605 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 &l

POJ2186 Popular Cows【Kosaraju】【强连通分量】

Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24266Accepted: 9954 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 <= 5

Popular Cows 强连通(kosaraju)

Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 30652   Accepted: 12439 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 &

POJ2186 Popular Cows [tarjan 缩点]

Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31241   Accepted: 12691 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 &

poj2186 Popular Cows 题解——S.B.S.

Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29642   Accepted: 11996 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 &

[POJ2186]Popular Cows(强连通分量)

题目链接:http://poj.org/problem?id=2186 给定n个点m条边,求某点使得其他点都有通向它的一条路径,计算这个点集的大小. 强连通分解后求出度为0的连通分量的个数,如果有且仅有一个连通分量出度为1,则统计这个连通分量中点的数目. 遍历所有点的出边指向的点,判断这两个点是否属于同一个连通分量,记录每个连通分量中的点的数目. 1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip&