1051: [HAOI2006]受欢迎的牛

本文介绍了一道关于求解受欢迎的牛数量的问题。通过输入牛的数量及它们之间的受欢迎关系,利用图论中的强连通分量算法进行求解。文章详细展示了如何实现该算法,并给出了完整的代码示例。

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

1051: [HAOI2006]受欢迎的牛

Time Limit: 10 Sec   Memory Limit: 162 MB
Submit: 3167   Solved: 1659
[ Submit][ Status][ Discuss]

Description

每一头牛的愿望就是变成一头最受欢迎的牛。现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎。 这种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认为牛C受欢迎。你的任务是求出有多少头牛被所有的牛认为是受欢迎的。

Input

第一行两个数N,M。 接下来M行,每行两个数A,B,意思是A认为B是受欢迎的(给出的信息有可能重复,即有可能出现多个A,B)

Output

一个数,即有多少头牛被所有的牛认为是受欢迎的。

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

HINT

100%的数据N<=10000,M<=50000

Source



连通块缩点。。然后显然出度为0的点都有可能是超级屌的牛,但是如果点数>1就不存在了。。

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

const int maxn = 1e4 + 10;

int pre[maxn],low[maxn],sccno[maxn],out[maxn],p[maxn],Size[maxn],cur = 0,t = 0;
int n,m,i,j;

vector <int> v[maxn];
stack <int> s;

void dfs(int k)
{
	pre[k] = low[k] = ++t;
	s.push(k);
	for (int l = 0; l < v[k].size(); l++)
	{
		int to = v[k][l];
		if (pre[to] == -1) dfs(to),low[k] = min(low[to],low[k]);
		else
		{
			if (sccno[to] == -1) low[k] = min(low[k],low[to]);
		}
	}
	if (low[k] == pre[k])
	{
		int ss = 0; cur++;
		while (1)
		{
			int S = s.top(); ++ss;
			s.pop();
			sccno[S] = cur;
			if (S == k) break;
		}
		Size[cur] = ss;
	}
}

int main()
{
	#ifdef YZY
		freopen("yzy.txt","r",stdin);
	#endif
	
	cin >> n >> m;
	while (m--)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		v[x].push_back(y);
	}
	
	memset(pre,-1,sizeof(pre));
	memset(sccno,-1,sizeof(sccno));
	for (i = 1; i <= n; i++)
	if (pre[i] == -1) dfs(i);
	
	for (i = 1; i <= n; i++)
	    for (int l = 0; l < v[i].size(); l++)
	  	{
	  		int to = v[i][l];
	  		if (sccno[to] == sccno[i]) continue;
	  		++out[sccno[i]];
	  	}
	
	int tot = 0,ans = 0;
	for (i = 1; i <= cur; i++)
		if (!out[i])
		{
			++tot;
			ans += Size[i];
		}
	
	if (tot == 1) cout << ans;
	else cout << 0;
	return 0;
}


# P2341 [USACO03FALL / HAOI2006] 受欢迎 G ## 题目背景 本题测试数据已修复。 ## 题目描述 每头奶都梦想成为棚里的明星。被所有奶喜欢的奶就是一头明星奶。所有奶都是自恋狂,每头奶总是喜欢自己的。奶之间的“喜欢”是可以传递的——如果 $A$ 喜欢 $B$,$B$ 喜欢 $C$,那么 $A$ 也喜欢 $C$。栏里共有 $N$ 头奶,给定一些奶之间的爱慕关系,请你算出有多少头奶可以当明星。 ## 输入格式 第一行:两个用空格分开的整数:$N$ 和 $M$。 接下来 $M$ 行:每行两个用空格分开的整数:$A$ 和 $B$,表示 $A$ 喜欢 $B$。 ## 输出格式 一行单独一个整数,表示明星奶的数量。 ## 输入输出样例 #1 ### 输入 #1 ``` 3 3 1 2 2 1 2 3 ``` ### 输出 #1 ``` 1 ``` ## 说明/提示 只有 $3$ 号奶可以做明星。 【数据范围】 对于 $10\%$ 的数据,$N\le20$,$M\le50$。 对于 $30\%$ 的数据,$N\le10^3$,$M\le2\times 10^4$。 对于 $70\%$ 的数据,$N\le5\times 10^3$,$M\le5\times 10^4$。 对于 $100\%$ 的数据,$1\le N\le10^4$,$1\le M\le5\times 10^4$。 c++,不要vector,变量名小写5字符以内,需要函数:void Tarjan(int u) { dfn[u] = low[u] = ++num; //初始化结点u的dfn和low值 st[++top] = u; //将结点u压入栈中 vis[u] = 1; //标记u在栈中 for (int i = head[u]; i; i = e[i].nxt) { //枚举u的所有出边 int v = e[i].to; if (!dfn[v]) { //结点v未被访问过,说明是树枝边 Tarjan(v); low[u] = min(low[u], low[v]); } else if (vis[v]) //v在栈中,是返祖边 low[u] = min(low[u], dfn[v]); // } int tmp = 0; if (low[u] == dfn[u]) { //结点u是该强连通分量的根 ++cnt; //强连通分量数量加一 do { //将当前结点前所有还在栈空间内的结点都归为当前强连通分量 tmp = st[top--]; vis[tmp] = 0; color[tmp] = cnt; //将同一个强连通分量内的点均标记为相同编号,也可理解为染色 } while(tmp != u); } } set<pair<int, int> > mark;//记录是否连接过 void solution() { //通过tarjan算法将所有强连通分量分配编号 for (int i = 1; i <= n; i++) if (!dfn[i]) Tarjan(i); //遍历所有连边,判断相邻两个结点是否所属同一强连通分量 for (int u = 1, v; u <= n; u++) { for (int i = head[u]; i; i = e[i].nxt) { v = e[j].to; //当相邻两个结点不属于同一强连通分量,则以强连通分量编号为点建边 if (color[u] != color[v] && mark[{color[u], color[v]}].find != mark.end()) { link(color[u], color[v]); mark.insert({color[u], color[v]}); } } } }
最新发布
08-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值