㊗️:☀️☃️☃️☃️☀️ • 3个月前
#include<iostream>
#include<cstring>
#include<set>
using namespace std;
int map[51][51];
int deg[51];
bool vis[51];
set<int> colorList;
int ans[1001];
int order = 0;
void dfs1(int node) {
vis[node] = true;
for (int color:colorList) if (map[node][color] && !vis[color]) {
dfs1(color);
}
}
void dfs2(int node) {
//不必回溯状态,如果路没走完,for循环会继续,达成插入的效果
for (int color : colorList) if (map[node][color]) {
map[node][color]--;
map[color][node]--;
dfs2(color);
}
ans[order++] = node; //已经走过的必然是最后的顺序
}
int main(){
int t,n,from ,to;
cin >> t;
for (int a = 1; a <= t; a++) {
memset(map, 0, sizeof map);
memset(deg, 0, sizeof deg);
memset(vis, 0, sizeof vis);
order = 0;
colorList.clear();
cin >> n;
for (int i = 0; i < n; i++) {
cin >> from >> to;
map[from][to]++;
map[to][from]++;
deg[from]++;
deg[to]++;
colorList.insert(from);
colorList.insert(to);
}
cout << "Case #" << a << endl;
bool able = true;
dfs1(*colorList.begin());
for (int color: colorList) {
if (!vis[color]) {
able = false;
break;
}
if (deg[color] % 2) {
able = false;
break;
}
}
if (able == false) {
cout << "some beads may be lost" << endl;
}
else {
dfs2(*colorList.begin());
for (int i = n-1; i >=0; i--) cout << ans[i+1] << " " << ans[i] << endl;
}
cout << endl;
}
return 0;
}
评论:
请先登录,才能进行评论