題目大意:給一張無向圖,找一條字典序最小的歐拉路徑
題解:若圖不連通或有兩個以上的奇數點,則沒有歐拉路徑,可以$dfs$,在回溯時把這個節點加入答案
卡點:沒有在回溯時加入答案,導致出現了歐拉路徑沒走環(少走了一段)
?
C++ Code:
#include <cstdio>
#include <cctype>
#include <algorithm>
#define maxn 60
int m, start = 52, ind[maxn];
int v[maxn], n, ret[256];
bool e[maxn][maxn];
char ans[maxn * maxn];int f[maxn];
int find(int x) {return x == f[x] ? x : (f[x] = find(f[x]));}void dfs(int u) {for (int i = 1; i <= n; i++) if (e[u][i]) {e[u][i] = e[i][u] = false;dfs(i);}ans[m--] = v[u];
}
int main() {scanf("%d", &m);for (int i = 'A'; i <= 'Z'; i++) v[++n] = i, ret[i] = n;for (int i = 'a'; i <= 'z'; i++) v[++n] = i, ret[i] = n;for (int i = 1; i <= n; i++) f[i] = i;for (int i = 0; i < m; i++) {char ch = getchar();while (!isalpha(ch)) ch = getchar();int a = ret[static_cast<int> (ch)], b;ch = getchar();while (!isalpha(ch)) ch = getchar();b = ret[static_cast<int> (ch)];start = std::min(start, std::min(a, b));e[a][b] = e[b][a] = true;ind[a]++, ind[b]++;f[find(a)] = find(b);}int cnt = 0;for (int i = 1; i <= n; i++) if (ind[i] && f[i] == i) cnt++;if (cnt > 1) {puts("No Solution");return 0;}cnt = 0;for (int i = 1; i <= n; i++) if (ind[i] & 1) {if (!cnt) start = i;cnt++;}if (cnt > 2) {puts("No Solution");return 0;}dfs(start);puts(ans);return 0;
}