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;
}


标题基于SpringBoot的高校餐饮档口管理系统设计与实现AI更换标题第1章引言介绍高校餐饮档口管理系统的研究背景、意义、国内外现状及论文方法与创新点。1.1研究背景与意义阐述高校餐饮档口管理现状及系统开发的重要性。1.2国内外研究现状分析国内外高校餐饮管理系统的研究与应用进展。1.3研究方法及创新点概述本文采用的研究方法及系统设计的创新之处。第2章相关理论总结与高校餐饮档口管理系统相关的现有理论。2.1SpringBoot框架理论阐述SpringBoot框架的原理、优势及其在Web开发中的应用。2.2数据库设计理论介绍数据库设计的基本原则、方法和步骤。2.3系统安全理论讨论系统安全设计的重要性及常见安全措施。第3章系统需求分析对高校餐饮档口管理系统的功能需求、性能需求等进行详细分析。3.1功能需求分析列举系统需实现的主要功能,如档口管理、订单处理等。3.2性能需求分析分析系统对响应时间、并发处理能力等性能指标的要求。3.3非功能需求分析阐述系统对易用性、可维护性等非功能方面的需求。第4章系统设计详细描述高校餐饮档口管理系统的设计过程。4.1系统架构设计给出系统的整体架构,包括前端、后端和数据库的设计。4.2模块设计详细介绍各个功能模块的设计,如用户管理、档口信息管理等。4.3数据库设计阐述数据库表结构的设计、数据关系及索引优化等。第5章系统实现与测试介绍高校餐饮档口管理系统的实现过程及测试方法。5.1系统实现系统各模块的具体实现过程,包括代码编写和调试。5.2系统测试方法介绍系统测试的方法、测试用例设计及测试环境搭建。5.3系统测试结果与分析从功能、性能等方面对系统测试结果进行详细分析。第6章结论与展望总结本文的研究成果,并展望未来的研究方向。6.1研究结论概括高校餐饮档口管理系统的设计与实现成果。6.2展望指出系统存在的不足及未来改进和扩展的方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值