• [洛谷P1341]无序字母对


    题目大意:给一张无向图,找一条字典序最小的欧拉路径

    题解:若图不连通或有两个以上的奇数点,则没有欧拉路径,可以$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;
    }
    

      

  • 相关阅读:
    updatepanel,linkbutton一点问题
    URL编码处理
    习惯
    style.display,有点问题
    [转]Atlas goes M2.2 or June CTP
    贴代码——从泛型数组中递归删除不符合要求的项
    做好项目,思想要不得
    关于MemoryStream类
    DateTimePicker控件的使用
    Console“自服务”读取文件
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/10014705.html
Copyright © 2020-2023  润新知