uva10054 The Necklace (欧拉回路路径输出 (并查集 + DFS) || (DFS + stack))
2013-07-16 19:09
411 查看
题意: 用50种珠子, 每种珠子两头颜色不同, 当两个珠子的有一头颜色相同时, 这一头可以连起来。 给点一些珠子, 看能否连成项链。
思路:其实就是欧拉回路(项链)。 用并查集判断图的连通性 + 判断度数合法性, 图就存在。 然后用DFS输出路径。
又想到一种。二维数组G不再表示G[u][v] u v之间边的存在性, 而是表示边数。 这样只要走一条减一条, 统计走过的条数,最后等于edge, 则图就连通了。走的时候把路径u v压入path栈。 代码在最下面。
算法复杂度:
代码:
思路:其实就是欧拉回路(项链)。 用并查集判断图的连通性 + 判断度数合法性, 图就存在。 然后用DFS输出路径。
又想到一种。二维数组G不再表示G[u][v] u v之间边的存在性, 而是表示边数。 这样只要走一条减一条, 统计走过的条数,最后等于edge, 则图就连通了。走的时候把路径u v压入path栈。 代码在最下面。
算法复杂度:
代码:
#include <cstdio> #include <cstring> using namespace std; #define MAX_N 55 int deg[MAX_N]; int father[MAX_N]; int G[MAX_N][MAX_N]; int edge, vertex; int findFather(int); void unite(int, int); bool okDeg(); void DFS(int); int main() { int cases; scanf("%d", &cases); for(int css = 1; css <= cases; css++) { //init memset(deg, 0, sizeof(deg)); memset(G, 0, sizeof(G)); vertex = 0; for (int i = 0; i < MAX_N; i++) { father[i] = i; } //enter scanf("%d", &edge); for (int i = 0; i < edge; i++) { int u, v; scanf("%d%d", &u, &v); G[u][v]++; G[v][u]++; deg[u]++; deg[v]++; unite(u, v); int max = u > v ? u : v; vertex = max > vertex ? max : vertex; } //judge int set = 0; for (int i = 1; i <= vertex; i++) if (deg[i]){ if (father[i] == i) { set++; } } //output if (css != 1) { printf("\n"); } printf("Case #%d\n", css); if (!okDeg() || set > 1) { printf("some beads may be lost\n"); }else { DFS(vertex); } } return 0; } int findFather(int x) { int tp; if (father[x] != x) { father[x] = findFather(father[x]); return father[x]; }else { return x; } } void unite(int x, int y) { int pX = findFather(x); int pY = findFather(y); if (pX != pY) { father[pX] = pY; } } bool okDeg() { for (int i = 1; i <= vertex; i++) { if (deg[i] % 2 != 0) { return false; } } return true; } void DFS(int u) { for (int v = 1; v <= vertex; v++) { if (G[u][v]) { G[u][v]--; G[v][u]--; DFS(v); printf("%d %d\n", v, u); } } }
#include <cstdio> #include <cstring> #include <stack> using namespace std; #define MAX_N 55 #define EXIST 1 int edge, vistedEdge; int star; int G[MAX_N][MAX_N]; int deg[MAX_N]; stack<int> path; bool okDeg(); void DFS(int); void printPath(); int main() { int cases; scanf("%d", &cases); for (int css = 1; css <= cases; css++) { //init memset(G, 0, sizeof(G)); memset(deg, 0, sizeof(deg)); scanf("%d", &edge); vistedEdge = 0; //enter for (int i = 0; i < edge; i++) { int u, v; scanf("%d%d", &u, &v); deg[u]++; deg[v]++; G[u][v]++; G[v][u]++; star = u; } //judge && output DFS(star); if (css != 1) { printf("\n"); } printf("Case #%d\n", css); if (okDeg() && vistedEdge == edge) { printPath(); } else { printf("some beads may be lost\n"); } } return 0; } bool okDeg() { for (int i = 0; i < MAX_N; i++) { if (deg[i] % 2 != 0) { return false; } } return true; } void DFS(int u) { for (int v = 0; v < MAX_N; v++) { if (G[u][v] > 0) { G[u][v]--; G[v][u]--; DFS(v); vistedEdge++; path.push(u); path.push(v); } } } void printPath() { for (int i = 0; i < edge; i++) { int u = path.top(); path.pop(); int v = path.top(); path.pop(); printf("%d %d\n", v, u); } }
相关文章推荐
- The Necklace UVA - 10054 题解(欧拉回路,路径输出)
- UVA 10054 The Necklace(欧拉回路+输出路径)
- (UVa 10054)The Necklace --欧拉回路的判断和输出,DFS
- UVA10054 The Necklace (输出欧拉回路)
- UVa10054 - The Necklace(欧拉回路【输出带来的麻烦)
- [UVA 10054]The Necklace[欧拉回路][打印路径]
- The Necklace+uva+求欧拉回路并输出路径
- UVA 10054 The Necklace(欧拉回路,打印路径)
- uva_10054_The Necklace(欧拉回路+打印路径)
- UVA 10054 The Necklace (dfs欧拉回路)
- UVA 10054 - The Necklace 欧拉回路
- UVA 10054 The Necklace 转化成欧拉回路
- UVA 10054 - The Necklace 欧拉回路
- UVa10054 The Necklace,无向图求欧拉回路
- UVa 10054 : The Necklace 【欧拉回路】
- uva 10054 The Necklace(欧拉回路)
- UVA - 10054 - The Necklace (欧拉回路!!)
- UVA 10054 无向图的欧拉回路输出路径
- uva 10054 The Necklace 拼项链 欧拉回路基础应用
- UVa 10054 The Necklace(欧拉回路)