UVA10054 The Necklace
来源:互联网 发布:at89c52中文数据手册 编辑:程序博客网 时间:2024/05/20 13:15
深不可测的黑洞
呼~~终于把它A掉了
这道题是判断是否存在欧拉回路的,把颜色看成点,珍珠看成边,然后统计每一个点的度,如果有奇度就表明欧拉回路不存在,如果存在的话直接dfs出欧拉回路,然后扔到栈里最后直接输出就行了。(仰慕佳哥)
#include <cstdio>#include <cstring>#include <algorithm>#include <stack>#include <iostream>using namespace std;const int N = 55;int g[N][N], dg[N];stack<pair<int, int> >st;int is_euler(int mn){int s = -1;for (int i = 1; i <= mn; i++){if (dg[i]){if (s == -1) s = i;if (dg[i] % 2) return -1;}}return s;}void dfs(int cur, int mn){for (int i = 1; i <= mn; i++){if (g[cur][i]){g[cur][i] -= 1; g[i][cur] -= 1;dfs(i, mn);st.push(make_pair(cur, i));}}}int main(){ //freopen("out.txt", "w", stdout);int cs, n;scanf("%d", &cs);for (int p = 1; p <= cs; p++){ memset(g, 0, sizeof(g)); memset(dg, 0, sizeof(dg));if (p > 1) printf("\n");scanf("%d", &n);int x, y, mn = 0;for (int i = 0; i < n; i++){scanf("%d%d", &x, &y);g[x][y] += 1; g[y][x] += 1;dg[x] += 1; dg[y] += 1;mn = max(mn, max(x, y));}int f = is_euler(mn);printf("Case #%d\n", p);if (f == -1){puts("some beads may be lost");}else{dfs(f, mn);while(!st.empty()){printf("%d %d\n", st.top().first, st.top().second);st.pop();}}}return 0;}
- uva10054-The Necklace(项链)
- The Necklace UVA10054
- UVA10054 The Necklace
- UVa10054 The Necklace
- The Necklace UVA10054
- uva10054 The Necklace
- UVA10054 The Necklace
- UVA10054-The Necklace
- The Necklace UVA10054
- UVa10054 The Necklace,无向图求欧拉回路
- UVa10054 The Necklace (欧拉回路)
- 【UVa10054】The Necklace【欧拉回路】
- UVA10054 The Necklace (输出欧拉回路)
- UVA10054 The Necklace 无向图欧拉回路+连通性
- uva10054 The Necklace (欧拉回路路径输出 (并查集 + DFS) || (DFS + stack))
- The Necklace
- uva10054
- UVA10054
- android invalidate 执行流程详解
- Flex Tree 如何在不选定节点的情况下进行添加(Demo)
- Resource is out of sync with the file system错误解决
- 找新朋友
- cJson 创建 读取
- UVA10054 The Necklace
- uvaoj10474
- Fibonacci
- 最短路
- 关于css设置层透明
- Issue 14523: layout_span and layout_column is missing
- visio 2010 给线加箭头
- A strange lift
- 畅通工程续