cychester

Luogu 1341 无序字母对 - 欧拉路径

Solution

找一条字典序最小的欧拉路径。

用 $multiset$ 存储领接表。

欧拉路径模板传送门


Code

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<set>
 5 using namespace std;
 6 
 7 const int N = 1e3 + 5;
 8 
 9 int n, up = 'z' - 'A' + 1;
10 int ans[N], tot, d[N], num;
11 
12 multiset<int> to[N];
13 
14 void dfs(int u) {
15     multiset<int> :: iterator i;
16     for (i = to[u].begin(); i != to[u].end(); i = to[u].begin()) {
17         int nt = *i;
18         to[nt].erase(to[nt].find(u));
19         to[u].erase(to[u].find(nt));
20         dfs(nt);
21     }
22     ans[++tot] = u;
23 }
24 
25 int main()
26 {
27     scanf("%d", &n);
28     for (int i = 1; i <= n; ++i) {
29         char s[3];
30         scanf("%s", s);
31         to[s[0] - 'A' + 1].insert(s[1] - 'A' + 1);
32         to[s[1] - 'A' + 1].insert(s[0] - 'A' + 1);
33         d[s[1] - 'A' + 1]++;
34         d[s[0] - 'A' + 1]++;
35     }
36     for (int i = 1; i <= up; ++i)
37         if (d[i] & 1)
38             num++;
39     if (num == 1 || num > 2)
40         return puts("No Solution"), 0;
41     if (num == 0) {
42         for (int i = 1; i <= up; ++i)
43             if (d[i]) {dfs(i); break;}
44     }
45     else 
46         for (int i = 1; i <= up; ++i)
47             if (d[i] && (d[i] & 1)) {dfs(i); break;}
48     for (int i = tot; i; i--) 
49         putchar(ans[i] + 'A' - 1);
50     puts("");
51 }
View Code

 

posted on 2018-09-30 12:44  cychester  阅读(165)  评论(0编辑  收藏  举报

导航