2833 奇怪的梦境 【解题报告】

这是一个关于拓扑排序的解题报告。通过建立图并利用拓扑排序算法,程序判断是否存在一种顺序使得所有节点都被访问到。如果存在,则输出特定表情;否则,输出未访问节点数量。

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

#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
struct edge{
	int w,next;
}e[50000];
stack <int> s;
int n,m,cnt=1,ans;
int head[50000],d[50000];
void adde(int u,int w)
{
	e[cnt].w=w;
	e[cnt].next=head[u];
	head[u]=cnt++;
	d[w]++;
}
bool topsort()
{
	for(int i=1;i<=n;i++)
		if(!d[i]) s.push(i);
	while(!s.empty())
	{
		int k=s.top();s.pop();
		ans++;
		for(int u=head[k];u!=-1;u=e[u].next)
		{
			d[e[u].w]--;
			if(!d[e[u].w]) s.push(e[u].w);
		}
	}
	if(ans==n) return 1;
	else return 0;
}
int main()
{
	int u,w,v;
	memset(head,-1,sizeof(head));
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)//建图 
	{
		scanf("%d%d",&u,&w);
		adde(u,w);
	}	
	if(topsort())
	{
		printf("o(∩_∩)o");
	}
	else 
	{
		printf("T_T\n");
		printf("%d",n - ans);
	}
	return 0;
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

真·skysys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值