题目大意,给m个顶点,n条边的无向图,问是否存在欧拉回路。
细节1:起点是第一行数据中两个端点较小的那个,不一定是1号顶点。
细节2:要求输出的欧拉回路的边序列字典序最小。
细节处理:起点不要搞错了,注意输入格式。输出字典序可以简单排序处理,点数非常少,可以瞎搞。。。
/*
* 欧拉回路
*/
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
const int MAX = 2048;
const int MAX_V = 64;
struct Edge {
int to;
int eid;
bool operator<(const Edge& B)const {
return eid < B.eid;
}
};
vector<Edge> G[MAX_V];
bool vis[MAX];
stack<int> S;
int deg[MAX_V];
void dfs(int u) {
for (vector<Edge>::iterator it = G[u].begin();
it != G[u].end(); ++it) {
if (vis[it->eid]) continue;
vis[it->eid] = true;
dfs(it->to);
S.push(it->eid);
}
}
bool eular(int start) {
for (int i = 0; i < MAX_V; ++i) {
if (deg[i] & 1) {
//printf("node %d has deg %d\n", i, deg[i]);
return false;
}
}
memset(vis, false, sizeof(vis));
while (!S.empty()) S.pop(); //any case
for (int i = 1; i < MAX_V; ++i) {
sort(G[i].begin(), G[i].end());
}
dfs(start);
return true;
}
int main() {
int u, v, e;
while (~scanf(" %d %d", &u, &v) && u) {
for (int i = 0; i < MAX_V; ++i) G[i].clear();
memset(deg, 0, sizeof(deg));
int start = min(u, v);
while (~scanf(" %d", &e)) {
G[u].push_back((struct Edge){v, e});
G[v].push_back((struct Edge){u, e});
++deg[u];
++deg[v];
scanf(" %d %d", &u, &v);
if (u == 0) break;
}
if (eular(start)) {
printf("%d", S.top());
S.pop();
while (!S.empty()) {
printf(" %d", S.top());
S.pop();
}
puts("\n");
} else {
puts("Round trip does not exist.\n");
}
}
return 0;
}
不水了不水了,窝要开始按目录学习了...话说我想装好linux再专心码代码的啊啊啊啊啊...