0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 03:31:59 +00:00

P1341 无序字母对

https://www.luogu.com.cn/record/77844194
This commit is contained in:
Baoshuo Ren 2022-06-24 21:25:18 +08:00
parent a36a2f6976
commit 83f81f49ee
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
3 changed files with 84 additions and 0 deletions

78
Luogu/P1341/P1341.cpp Normal file
View File

@ -0,0 +1,78 @@
#include <iostream>
#include <cstring>
#include <set>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1005;
int n, cnt, s, t, deg[N], ans[N];
int g[N][N];
std::set<char> nodes;
void dfs(char u) {
for (int i = 1; i <= 128; i++) {
if (g[u][i]) {
g[u][i]--;
g[i][u]--;
dfs(i);
}
}
ans[++cnt] = u;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
char u, v;
cin >> u >> v;
g[u][v]++, g[v][u]++;
deg[u]++, deg[v]++;
nodes.insert(u);
nodes.insert(v);
}
int x = 0;
t = *nodes.rbegin();
for (const char &node : nodes) {
if (deg[node] & 1) {
x++;
if (!s) s = node;
}
}
if (!s) s = *nodes.begin();
if (x && x != 2) {
cout << "No Solution" << endl;
exit(0);
}
dfs(s);
if (cnt <= n) {
cout << "No Solution" << endl;
} else {
for (int i = cnt; i; i--) {
cout << (char)(ans[i]);
}
cout << endl;
}
return 0;
}

BIN
Luogu/P1341/data/P1341_2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/P1341/data/P1341_2.out (Stored with Git LFS) Normal file

Binary file not shown.