不懂就问
查看原帖
不懂就问
705570
lzj001楼主2023/6/21 20:32
#include<bits/stdc++.h>
using namespace std;

const int N=1e5+5;
int n,m;
int head[N];
int in[N],ot[N];
int st=1;

bool ol() {
	int c=0,p=0;
	for (int i=1; i<=n; i++) {
		if (in[i]==0&&ot[i]==0) return false;
		if(in[i]==ot[i]) c++;
		if(in[i]-ot[i]==1) p++;
		if(in[i]-ot[i]==-1) st=i,p++;
	}
	if(c==n) return true;
	if(c==n-2&&p==2) return true;
	return false;
}

struct Edge {
	int to,next,num;
} edge[N*2];
struct E {
	int v;
	int u;
} e[N*2];
void addedge(int b,int e) {
    static int cnt=0;
    cnt++;
	edge[cnt].to=e;
	edge[cnt].next=head[b];
	head[b]=cnt;
}
bool cmp(E a,E b) {

	if(a.u==b.u)return a.v>b.v;
	else return a.u<b.u;
}
int vis[N];
stack<int> s;

void dfs(int x) {
	s.push(x);
	int w=head[x];
	if(w)
	{
		head[x]=edge[w].next;
		dfs(edge[w].to);
	}
}
int path[N*2];int cn;
void fleury(int st) {
		while (!s.empty()) s.pop();
	s.push(st);
	while(cn<m+1) {
		int k=s.top();s.pop();
		if(head[k]) {
			dfs(k);
		} 
		else path[++cn]=k;
		if(s.empty())break;
	}
}
int d[N];
int main() {
	cin>>n>>m;
	for(int i=1; i<=m; i++) {
		int u,v;
		cin>>u>>v;
		ot[u]++;
		in[v]++;
		e[i].u=u,e[i].v=v;
	}
	sort(e+1,e+1+m,cmp);
	for(int i=1; i<=m; i++) {
		addedge(e[i].u,e[i].v);
	}
	if(!ol()) {
		cout<<"No";
		return 0;
	} 
	else fleury(st);
    for(int i=cn;i>=1;i--)
    {
    	cout<<path[i]<<" ";
	}
	return 0;
}

为什么这段代码删掉addedge()中的static就会出错

2023/6/21 20:32
加载中...