题目;
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=72192#problem/C
题意:
判断项链的珠子是否首尾相连.
思路:
欧拉回路+打印路径.
无向图为欧拉图的充要条件是没有度为奇数的顶点.
将路线连起来之后只需要判断首尾是否相连即可.
路径输出要逆向.
AC.
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int MAX = 52;
int vis[1005][1005], cnt[MAX];
struct node {
int x, y;
}ans[1005];
int k;
void dfs(int s)
{
for(int i = 1; i < MAX; ++i) {
if(vis[s][i]) {
vis[s][i]--;
vis[i][s]--;
dfs(i);
ans[k].x = s;
ans[k++].y = i;
}
}
return ;
}
int main()
{
//freopen("in", "r", stdin);
int T, ca = 1;
scanf("%d", &T);
while(T--) {
int n, b, a;
memset(vis, 0, sizeof(vis));
memset(cnt, 0, sizeof(cnt));
if(ca != 1) printf("\n");
scanf("%d", &n);
int st;
for(int i = 0; i < n; ++i) {
scanf("%d %d", &a, &b);
st = a;
vis[a][b]++;
vis[b][a]++;
cnt[a]++; cnt[b]++;
}
int ok = 0;
for(int i = 1; i < MAX; ++i) {
if(cnt[i] & 1) {
ok = 1;
break;
}
}
k = 0;
if(!ok) {
dfs(st);
if(k < n && ans[k-1].y != ans[0].x) ok = 1;
}
printf("Case #%d\n", ca++);
if(ok) printf("some beads may be lost\n");
else {
for(int i = k-1; i >= 0; --i) {
printf("%d %d\n", ans[i].x, ans[i].y);
}
}
}
return 0;
}