请问一下,为什么会段错误?
查看原帖
请问一下,为什么会段错误?
69862
Sky_valley楼主2018/2/21 09:42
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#define il inline
using namespace std;
const int N=500000;
int  n,que[N],top,h;
char a,b;
bool pd[N],judge[N],f1,f2;
int  head[N],cnt,road[N];
struct node{
	int to,ne;
}edge[N]; 
il void add(int u,int v)
{
	++cnt;
	edge[cnt].to=v;
	edge[cnt].ne=head[u];
	head[u]=cnt;
}
il void pd_dfs(int wz)
{
	for(int i=head[wz]; i ; i=edge[i].ne)
	{
		judge[wz]=true;
		pd_dfs(edge[i].to);
	}
}
il void dfs(int wz,int lar)
{
	if(lar > n+1)
	{
		f1=true;
		return;
	}
	else
	{
		road[lar]=wz; judge[wz]=true;
		for(int j=head[wz]; j ; j=edge[j].ne)
		{
			if((!judge[edge[j].to]||(lar==n+1))&&!f1)
			{
				dfs(edge[j].to,lar+1);
			}
		}
	}
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	{
		a=getchar(), b=getchar();
		if(!pd[a-64]){
			que[++top]=a-64;
			h=top;
			while(que[h-1]>que[h]){
				swap(que[h-1],que[h]);
				--h;
			}
			pd[b-64]=true;
		}
		if(!pd[b-64])
		{
			que[++top]=b-64;
			h=top;
			while(que[h-1]>que[h]){
				swap(que[h-1],que[h]);
				--h;
			}
			pd[b-64]=true;
		}
		add(a-64,b-64);
		add(b-64,a-64);
	}
	pd_dfs(que[1]);
	for(int i=1;i<=top;++i)
	{
		if(!judge[que[i]])
		{ printf("No Solution");
			return 0;
		}
	}
	for(int i=1;i<=top;++i)
	{
		f1=false; f2=false;
		memset(judge,0,sizeof(judge));
		memset(road,0,sizeof(road));
		dfs(que[i],1);
		for(int i=1;i<=top;++i)
		{
			if(!judge[que[i]])
			{   f2=true;
				break;
			}
		}
		if(!f2) {
			for(int cp=1;cp<=n+1;++cp)
				printf("%c",road[cp]+64);
			return 0;
		}
	}
	return 0;
}

2018/2/21 09:42
加载中...