poj 2186 Popular Cows 【有向图求SCC +缩点】【求图中有多少个点 可以由其余所有点通过存在路径到达】

探讨了在一个牛群中确定哪些牛被认为是最受欢迎的问题。通过构建图论模型并运用Tarjan算法寻找强连通分量,最终统计出被视为最受欢迎的牛的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 25512 Accepted: 10461

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. 

题意:有n头牛,已知m个关系即牛a认为牛b是受欢迎,现在问你在n头牛里有多少头牛被 除它外所有的牛认为是受欢迎的。


思路:求出所有SCC,和SCC里面的点以及对应的出度。建议对缩点后的图要深刻了解!!!

处理有三

一:出度为0的SCC有0个,显然易见,该图就是一个强连通图,所有牛都满足条件;
二:出度为0的SCC有1个,满足条件的只有这一个SCC里面的牛;
三:出度为0的SCC超过1个,无论如何,出度超过0的SCC之间是无法连通的,所以没有牛满足。

AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 10000+10
#define MAXM 60000+10
#define INF 10000000
using namespace std;
vector<int> G[MAXN];
int low[MAXN], dfn[MAXN];
int dfs_clock;
int sccno[MAXN], scc_cnt;
vector<int> scc[MAXN];
stack<int> S;
bool Instack[MAXN];
int in[MAXN], out[MAXN];//SCC入度 出度 
int n, m;//n头牛 m个关系 
void getMap()
{
	int s, t;
	for(int i = 1; i <= n; i++) G[i].clear();
	while(m--)
	{
		scanf("%d%d", &s, &t);
		G[s].push_back(t);
	}
}
void tarjan(int u, int fa)
{
	int v;
	low[u] = dfn[u] = ++dfs_clock;
	S.push(u);
	Instack[u] = true;
	for(int i = 0; i < G[u].size(); i++)
	{
		v = G[u][i];
		if(!dfn[v])
		{
			tarjan(v, u);
			low[u] = min(low[u], low[v]);
		}
		else if(Instack[v])
		low[u] = min(low[u], dfn[v]);
	} 
	if(low[u] == dfn[u])
	{
		scc_cnt++;
		scc[scc_cnt].clear();
		for(;;)
		{
			v = S.top(); S.pop();
			Instack[v] = false;
			sccno[v] = scc_cnt;
			scc[scc_cnt].push_back(v);
			if(v == u) break; 
		}
	}
}
void find_cut(int l, int r)
{
	memset(low, 0, sizeof(low));
	memset(dfn, 0, sizeof(dfn));
	memset(sccno, 0, sizeof(sccno));
	memset(Instack, false, sizeof(Instack));
	dfs_clock = scc_cnt = 0;
	for(int i = l; i <= r; i++)
	if(!dfn[i]) tarjan(i, -1); 
}
void suodian()
{
	for(int i = 1; i <= scc_cnt; i++) in[i] = out[i] = 0;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 0; j < G[i].size(); j++)
		{
			int u = sccno[i];
			int v = sccno[G[i][j]];
			if(u != v)
			in[v]++, out[u]++; 
		}
	}
}
void solve()
{
	int sum = 0;//统计出度为0的SCC数目 
	int ans = 0;//统计最受欢迎的牛 
	for(int i = 1; i <= scc_cnt; i++)
	{
		if(out[i] == 0)//出度为0 
		{
			sum++;
			ans += scc[i].size();//累加 
		}
		if(sum > 1)//出度为0的SCC超过1个 不会有最受欢迎的牛 
		{
			printf("0\n");
			return ; 
		} 
	}
	if(sum == 0)//不存在出度为0的SCC 全部都是最受欢迎的牛 
	printf("%d\n", n);
	else if(sum == 1)//出度为0的SCC只有一个  该SCC里面的牛全是最受欢迎的 
	printf("%d\n", ans);
}
int main()
{
	while(scanf("%d%d", &n, &m) != EOF)
	{
		getMap();
		find_cut(1, n);
		suodian();
		solve();
	} 
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值