40分求助
查看原帖
40分求助
905133
sssscy_free_stdio楼主2023/6/21 11:53
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
bool is_scanf[N],g[N][N],vis[N][N];
int e,now=0,tot=0,q[N];
inline void dfs(int x){
	if(now==e){
		for(int i=1;i<=tot;i++){
			printf("%c",q[i]);
		}
		exit(0);
	}
	for(int i='A';i<='z';i++){
		if(g[x][i]&&!vis[x][i]){
			vis[x][i]=vis[i][x]=1;
			++now,++tot;
			q[tot]=i;
			dfs(i);
			q[tot]=0;
			--now,--tot;
			vis[x][i]=vis[i][x]=0;
		}
	}
}
int main(){
	scanf("%d",&e);
	char a,b;
	char s[10];
	for(int i=1;i<=e;i++){
		scanf("%s",s);
		a=s[0],b=s[1];
		is_scanf[(int)a]=is_scanf[(int)b]=1;
		g[(int)a][(int)b]=g[(int)b][(int)a]=1;
	}	
	for(int i='A';i<='z';i++){
		if(is_scanf[i]){
			tot=0;
			tot++;
			q[tot]=i;
			dfs(i);
		}
	}
	printf("No Solution");
    return 0;
}

#5到#10都TLE,查了一个上午QAQ

2023/6/21 11:53
加载中...