POJ-2186 (强连通分量)

这篇博客详细介绍了如何解决POJ-2186问题,主要探讨了图论中的强连通分量概念。通过实例分析和代码解释,帮助读者理解如何在实践中找到图的强连通分量。
摘要由CSDN通过智能技术生成

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 37824 Accepted: 15398

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. 

//Poj - 2186 强连通分量
//题意:
//总共有n头牛,m个有序对(a, b) 代表牛a崇拜牛b, 崇拜具有传递性,
//求出被其他所有牛崇拜的牛的总数。
//思路: 
//如果枚举每一个点, 看看能不能连通其他的所有顶点, 复杂度是O(nm) 会超时,
//用强连通分量分解, 将相互连通的点缩成一个点,最后在对强连通分解后的图求拓扑排序, 
//那么只需验证拓扑序最后一位的缩点是否能连通其他的点, 如果可以那么就输出最后一个缩点集合的个数。
//复杂度为 O((n + m) + n)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

#define v_int vector<int>
#define vv_int vector<v_int>
#define ll long long
#define INF (ll)0x3f3f3f3f3f3f3f
const int maxn = 1e5 + 10;
const int maxm = 5e5 + 10;
v_int G[maxn], rG[maxn], sc;
int n, m;
int vis[maxn], a[maxn], b[maxn], topo[maxn];
void addEdge(int from, int to) {
	G[from].push_back(to);
	rG[to].push_back(from);
}
void dfs(int v) {
	vis[v] = 1;
	for (int i = 0; i < G[v].size(); i++) {
		if (!vis[G[v][i]]) dfs(G[v][i]);
	}
	sc.push_back(v);
}
void rdfs(int v, int k) {
	vis[v] = 1;
	topo[v] = k;
	for (int i = 0; i < rG[v].size(); i++) {
		if (!vis[rG[v][i]]) rdfs(rG[v][i], k);
	}
}
int scc() {
	sc.clear();
	memset(vis, 0, sizeof(vis));
	for (int i = 0; i < n; i++) {
		if (!vis[i]) dfs(i);
	}
	memset(vis, 0, sizeof(vis));
	int k = 0;
	for (int i = sc.size() - 1; i >= 0; i--) {
		if (!vis[sc[i]]) rdfs(sc[i], k++);
	}
	return k;
}
void solve() {
	for (int i = 0; i < m; i++)
		addEdge(a[i] - 1, b[i] - 1);
	int cur_n = scc();
	int ans = 0, flag_u = 0;
	for (int i = 0; i < n; i++) {
		if (topo[i] == cur_n - 1) {
			flag_u = i;
			ans++;
		}
	}
	memset(vis, 0, sizeof(vis));
	rdfs(flag_u, 0);
	for (int i = 0; i < n; i++) {
		if (!vis[i]) {
			ans = 0; break;
		}
	}
	cout << ans;
}
int main() {
#ifdef ZTZTMS
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> a[i] >> b[i];
	}
	solve();
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值