60pts求助
查看原帖
60pts求助
1065071
Atserckcn楼主2024/7/18 18:17
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,M=2e5+5;
int n,m;
int start,inn,outn;
bool vis[M];
struct EDGE{
	int from,to,pre;
}e[M],edge[M];
int head[N],u,v,cnt_edge;
int in[N],out[N];
void add(int from,int to)
{
	edge[++cnt_edge].from=from;
	edge[cnt_edge].to=to;
	edge[cnt_edge].pre=head[from];
	head[from]=cnt_edge;
	return;
}
stack<int> st;
void dfs(int u)
{
	for(int i=head[u];i;i=head[u])
	{
		head[u]=edge[i].pre;
		dfs(edge[i].to);
	}
	st.push(u);
	return;
}
bool cmp(EDGE a,EDGE b)
{
	if(a.from!=b.from)
		return a.from<b.from;
	return a.to>b.to;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&e[i].from,&e[i].to);
		in[e[i].to]++;out[e[i].from]++;
	}
	for(int i=1;i<=n;i++)
	{
		if(abs(in[i]-out[i])>1)
		{
			printf("No\n");
			return 0;
		}
		if(in[i]-out[i]==1)
			inn++;
		if(out[i]-in[i]==1)
		{
			outn++;
			start=i;
		}
	}
	if((inn==0&&outn==0)||(inn==1&&outn==1))
	{
		sort(e+1,e+m+1,cmp);
		for(int i=1;i<=m;i++)
			add(e[i].from,e[i].to);
		dfs(start);
		while(!st.empty())
		{
			printf("%d ",st.top());
			st.pop();
		}
	}
	else
		printf("No");
	putchar('\n');
	return 0;
}

各位大佬帮帮这个小学蒟蒻吧。。

https://www.luogu.com.cn/record/166949416

2024/7/18 18:17
加载中...