POJ2186 Popular Cows ,有向图, Tarjan算法

题意:

给定一个有向图,求有多少个顶点是由任何顶点出发都可达的。

顶点数<= 10,000,边数 <= 50,000

定理:

有向无环图中唯一出度为0的点,一定可以由任何点出发均可达

(由于无环,所以从任何点出发往前走,必然终止于一个出度为0的点)

1. 求出所有强连通分量

2. 每个强连通分量缩成一点,则形成一个有向无环图DAG。

3. DAG上面如果有唯一的出度为0的点,则该点能被所有的点可达。那么该点所代表的连通分量上的所有的原图中的点,都能被原图中的所有点可达,则该连通分量的点数,就是答案。

4. DAG上面如果有不止一个出度为0的点,则这些点互相不可达,原问题无解,答案为0

#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;

const int maxn = 10000 + 100;
vector<int> g[maxn];
int dfn[maxn], low[maxn], belong[maxn], dfs_clock, scc_cnt, size[maxn];
stack<int> s;
int n, m;

void dfs(int u){
    dfn[u] = low[u] = ++dfs_clock;
    s.push(u);
    for(int i=0; i<g[u].size(); ++i){
        int v = g[u][i];
        if(!dfn[v]){
            dfs(v);
            low[u] = min(low[u], low[v]);
        }else if(!belong[v]){
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]){
        scc_cnt++;
        for(;;){
            int x = s.top(); s.pop();
            belong[x] = scc_cnt;
			size[scc_cnt]++;
            if(x == u) break;
        }
    }
}  

void find_scc(int n){
    dfs_clock = scc_cnt = 0;
    memset(belong, 0, sizeof belong );
    memset(dfn, 0, sizeof dfn );
    for(int i=1; i<=n; ++i)
        if(!dfn[i]) dfs(i);
}  

int main(){
	scanf("%d%d", &n, &m);
	int u, v;
	for(int i=0; i<m; ++i){
		scanf("%d%d", &u, &v);
		g[u].push_back(v);
	}
	find_scc(n);

	int out[maxn];
	memset(out, 0, sizeof out );
	for(int i=1; i<=n; ++i){
		for(int j=0; j<g[i].size(); ++j){
			int &v = g[i][j];
			if(belong[i] != belong[v]){
				out[belong[i]]++;
			}
		}
	}	

	int cnt = 0, ans;
	for(int i=1; i<=scc_cnt; ++i){
		if(out[i]==0){
			cnt++;
			ans = size[i];
		}
	}
	if(cnt==1){
		printf("%d\n", ans);
	}else {
		printf("0\n");
	}	

	return 0;
}
时间: 2024-10-06 08:42:31

POJ2186 Popular Cows ,有向图, Tarjan算法的相关文章

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

题目连接: http://poj.org/problem?id=2186 题目大意: 每头奶牛都希望自己成为最欢迎的那头牛.给你N头牛,M个崇拜关系(A,B).意思是牛A 崇拜牛B.特别是,如果牛A崇拜牛B,牛B崇拜牛C,那么牛A也崇拜牛C.那么问题来了: 请计算出被所有牛崇拜的牛的个数. 思路: 把崇拜关系(A,B)看做是一条有向边,并且,我们发现牛的崇拜关系具有传递性.那么只要 牛A有一条路径连向牛B,就可以判定牛A崇拜牛B.于是,被所有牛崇拜的牛就是所有的点 都存在一条路径连向它的有向路径

强连通分量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 --- 强连通

给一个有向图,问有多少结点是其他所有结点都可以到达的. 等价于,在一个有向无环图上,找出度为0 的结点,如果出度为0的结点只有一个,那么这个就是答案,如果大于1个,则答案是0. 这题有环,所以先缩点.求唯一出度为0的强连通分量. #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<iostream> #define inf 0x3f3f3f3f

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

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 [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

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 popula

POJ - 2186 - Popular Cows (tarjan)

Popular Cows 题目传送:Popular Cows 思路:tarjan算法求强连通分量 AC代码: #include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <strin

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

poj Popular Cows(tarjan +缩点)

Language: Default Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24384   Accepted: 10007 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