UVA 10054 The Necklace
2014-12-08 22:13
483 查看
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18806
无向图的欧拉回路
1.连通图:并查集判断
2.所有点的度数均为偶数:邻接表存储即可
AC代码:
无向图的欧拉回路
1.连通图:并查集判断
2.所有点的度数均为偶数:邻接表存储即可
AC代码:
#include <vector> #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; const int MAX = 1010; typedef pair <int, int> Pii; int a[MAX],b[MAX],vis[MAX]; int v[55],father[55]; vector <Pii> vt[55]; vector <Pii> path; void Init(){///初始化 path.clear(); memset(vis, 0, sizeof(vis)); for(int i=0; i<=50; i++){ father[i] = i; vt[i].clear(); v[i] = 0; } } int Find(int x){ if(x != father[x]) father[x] = Find(father[x]); return father[x]; } bool check(){///判断是否存在欧拉回路 for(int i=0; i<=50; i++) if(v[i]) if(Find(i) != Find(a[0])) return false; for(int i=0; i<=50; i++) if(vt[i].size() % 2) return false; return true; } void DFS(int u){///深搜路径 for(int i=0; i<vt[u].size(); i++){ if(vis[vt[u][i].first]) continue; vis[vt[u][i].first] = 1; DFS(vt[u][i].second); path.push_back(Pii(vt[u][i].first, b[vt[u][i].first] == u)); } } int main(){ //freopen("in.txt", "r", stdin); int cas,t=0; scanf("%d",&cas); while(cas--){ int n; Init(); scanf("%d",&n); for(int i=0; i<n; i++){ scanf("%d%d",&a[i],&b[i]); v[a[i]] = v[b[i]] = 1; father[Find(a[i])] = Find(b[i]); vt[a[i]].push_back(Pii(i, b[i])); vt[b[i]].push_back(Pii(i, a[i])); } if(t) cout << endl; printf("Case #%d\n",++t); if(check() == false){ cout << "some beads may be lost" << endl; continue; } for(int i=0; i<=50; i++){ if(vt[i].size()){ DFS(i); for(int j=path.size(); j>=1; j--){ int id = path[j-1].first; if(path[j-1].second) cout << b[id] << " " << a[id] << endl; else cout << a[id] << " " << b[id] << endl; } break; } } } //system("pause"); return 0; }
相关文章推荐
- uva 10054 - The Necklace
- UVA 10054 The Necklace(欧拉回路,打印路径)
- The Necklace UVA10054
- UVA - 10054 - The Necklace (欧拉回路!!)
- UVA10054 The Necklace(欧拉回路)
- The Necklace UVA10054
- uva 10054 - The Necklace
- UVA-10054 The Necklace 欧拉回路
- UVA10054 The Necklace
- uva10054-The Necklace(项链)
- UVA 10054 - The Necklace
- UVA - 10054 The Necklace
- uva 10054 The Necklace(欧拉回路)
- UVA 10054 The Necklace
- Uva 10054 - The Necklace(欧拉回路)
- uva 10054 - The Necklace
- UVA 10054 The Necklace(欧拉回路+输出路径)
- UVA 10054 The Necklace (无向图的欧拉回路)
- UVa 10054 : The Necklace 【欧拉回路】
- Uva-10054-The Necklace