题目传送门
解题思路:
一道欧拉回路的模板题,详细定理见大佬博客,任意门
AC代码:
1 #include<cstdio> 2 #include<iostream> 3 4 using namespace std; 5 6 int ans[100000],du[106],n,k = 0x7f7f7f,b[103][103],cnt = 0x3f,bj,tot; 7 string l; 8 9 inline void dfs(int x) { 10 for(int i = 0;i < 58; i++) 11 if(b[x][i]) { 12 b[x][i] = b[i][x] = 0; 13 dfs(i); 14 } 15 ans[++tot] = x; 16 } 17 18 int main() 19 { 20 scanf("%d",&n); 21 for(int i = 1;i <= n; i++) { 22 int x,y; 23 cin >> l; 24 x = l[0] - 'A'; 25 y = l[1] - 'A'; 26 k = min(k,min(x,y)); 27 b[x][y] = b[y][x] = 1; 28 du[x]++; 29 du[y]++; 30 } 31 for(int i = 0;i < 58; i++) 32 if(du[i] % 2 == 1) { 33 bj++; 34 cnt = min(cnt,i); 35 } 36 if(bj == 0) dfs(k); 37 else if(bj == 2) dfs(cnt); 38 else { 39 printf("No Solution"); 40 return 0; 41 } 42 for(int i = tot;i >= 1; i--) 43 printf("%c",ans[i] + 'A'); 44 return 0; 45 }