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

Source

USACO 2003 Fall


看完这题,我的想法就是tarjan然后缩点,但是最后一个地方想错了,导致了WA,只能说自己想的不够仔细

我的想法是,缩点以后,找出入度为强连通分量数-1的点,然后加起来,其实这是错误的

比如:1->2 ,3->2,2->4,这样4是符合的,但是4的入度并不是3

正解是寻找出度为0的点,因为这个点既然有边指向它,那么它一定没有边指出去,否则就与其他点连通了,当然如果这样的点超过1个,那么是无解的,因为他们自身不能指向对方。

#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>

using namespace std;

const int maxn=10006;
int DFN[maxn];
int low[maxn];
int sccnum[maxn];
int in[maxn];
int out[maxn];
int num[maxn];
int stack[maxn];
bool instack[maxn];
struct node
{
	int next;
	int to;
}edge[maxn*5];
int head[maxn];
int tot,top,index,sccNum,n;

void addedge(int from,int to)
{
	edge[tot].to=to;
	edge[tot].next=head[from];
	head[from]=tot++;
}

void tarjan(int u)
{
	DFN[u]=low[u]=++index;
	stack[top++]=u;
	instack[u]=1;
	for(int i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].to;
		if(DFN[v]==0)
		{
			tarjan(v);
			if(low[u]>low[v])
			   low[u]=low[v];
		}
		else if(instack[v])
		    if(low[u]>DFN[v])
		       low[u]=DFN[v];
	}
	if(DFN[u]==low[u])
	{
		sccNum++;
		do
		{
			top--;
			sccnum[stack[top]]=sccNum;
			num[sccNum]++;
			instack[stack[top]]=0;
		}while(stack[top]!=u);
	}
}

void solve()
{
	memset(instack,0,sizeof(instack));
	memset(num,0,sizeof(num));
	memset(DFN,0,sizeof(DFN));
	memset(in,0,sizeof(in));
	memset(out,0,sizeof(out));
	index=0;
	top=0;
	sccNum=0;
	for(int i=1;i<=n;i++)
	  if(DFN[i]==0)
	     tarjan(i);
}

int main()
{
	int m;
	while(~scanf("%d%d",&n,&m))
	{
		int x,y;
		tot=0;
		memset(head,-1,sizeof(head));
		for(int i=0;i<m;i++)
		{
			scanf("%d%d",&x,&y);
			addedge(x,y);
		}
		solve();
		for(int i=1;i<=n;i++)
		{
			for(int j=head[i];j!=-1;j=edge[j].next)
			{
				if(sccnum[i]!=sccnum[edge[j].to])
				{
					out[sccnum[i]]++;
					in[sccnum[edge[j].to]]++;
				}
			}
		}
		int ans=0,j,i,cnt=0;
		for(i=1;i<=sccNum;i++)
		{
			if(out[i]==0)
   			{
   				cnt++;
   			}
		}
		if(cnt>1 || cnt==0)
			printf("0\n");
		else
		    printf("%d\n",num[cnt]);
	}
	return 0;
}


代码转载自:https://pan.quark.cn/s/f87b8041184b Language: 中文 欢迎来到戈戈圈! 当你点开这个存储库的时候,你会看到戈戈圈的图标↓ 本图片均在知识共享 署名-相同方式共享 3.0(CC BY-SA 3.0)许可协议下提供,如有授权遵照授权协议使用。 那么恭喜你,当你看到这个图标的时候,就代表着你已经正式成为了一名戈团子啦! 欢迎你来到这个充满爱与希望的大家庭! 「与大家创造更多快乐,与人们一起改变世界。 」 戈戈圈是一个在中国海南省诞生的创作企划,由王戈wg的妹妹于2018年7月14日正式公开。 戈戈圈的创作类型广泛,囊括插画、小说、音乐等各种作品类型。 戈戈圈的目前成员: Contributors 此外,支持戈戈圈及本企划的成员被称为“戈团子”。 “戈团子”一词最初来源于2015年出生的名叫“团子”的大熊猫,也因为一种由糯米包裹着馅料蒸熟而成的食品也名为“团子”,不仅有团圆之意,也蕴涵着团结友爱的象征意义和大家的美好期盼,因此我们最终于2021年初决定命名戈戈圈的粉丝为“戈团子”。 如果你对戈戈圈有兴趣的话,欢迎加入我们吧(σ≧︎▽︎≦︎)σ! 由于王戈wg此前投稿的相关视频并未详细说明本企划的信息,且相关视频的表述极其模糊,我们特此创建这个存储库,以文字的形式向大家介绍戈戈圈。 戈戈圈自2018年7月14日成立至今,一直以来都秉持着包容开放、和谐友善的原则。 我们深知自己的责任和使命,始终尊重社会道德习俗,严格遵循国家法律法规,为维护社会稳定和公共利益做出了积极的贡献。 因此,我们不允许任何人或组织以“戈戈圈”的名义在网络平台或现实中发布不当言论,同时我们也坚决反对过度宣传戈戈圈的行为,包括但不限于与戈戈圈无关的任何...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值