The Necklace
来源:互联网 发布:linux systemd-logind 编辑:程序博客网 时间:2024/05/18 02:31
题目链接
- 题意:
给很多由两个颜色组成的珠子,看是否能连成一串,相邻珠子相邻部分颜色相同 - 分析:
裸地欧拉路判断+路径输出
const int MAXN = 1100;const int MAXV = 60;const int MAXE = 2020;struct Edge{ int from, to;};struct UnDirect_Euler{ int n, m; int deg[MAXV]; bool vis[MAXE]; vector<int> G[MAXV]; vector<Edge> edges; stack<int> sk; vector<int> ans; void init(int n) { this->n = n; edges.clear(); REP(i, n) { deg[i] = 0; G[i].clear(); } } void addEdge(int a, int b) { edges.push_back((Edge) { a, b }); edges.push_back((Edge) { b, a }); m = edges.size(); G[a].push_back(m - 2); G[b].push_back(m - 1); deg[a]++; deg[b]++; } void dfs(int u, int ind) { REP(i, G[u].size()) { int x = G[u][i]; Edge& e = edges[x]; if (!vis[x]) { vis[x] = vis[x ^ 1] = true; dfs(e.to, x); } } if (ind >= 0) sk.push(ind); } //返回1:欧拉回路 返回2:欧拉路经 int solve(int s) { ans.clear(); int cnt = 0; REP(i, n) { if (deg[i] == 1) { if (++cnt > 2) return 0; s = i; } else if (deg[i] % 2 != 0) return 0; } while (!sk.empty()) sk.pop(); REP(i, m) vis[i] = false; dfs(s, -1); REP(i, m) if (!vis[i]) return 0; while (!sk.empty()) { ans.push_back(sk.top()); sk.pop(); } return cnt != 0 ? 1 : 2; }} graph;int a[MAXN], b[MAXN];int main(){ int T, n; RI(T); FE(kase, 1, T) { graph.init(60); RI(n); REP(i, n) { RII(a[i], b[i]); graph.addEdge(a[i], b[i]); } printf("Case #%d\n", kase); if (graph.solve(a[0]) == 2) { vector<int>& ans = graph.ans; REP(i, ans.size()) { Edge& e = graph.edges[ans[i]]; int u = e.from, v = e.to; printf("%d %d\n", u, v); } } else puts("some beads may be lost"); puts(""); } return 0;}
9 0
- The Necklace
- uva 10054 - The Necklace
- 10054 - The Necklace(***)
- UVaOJ 10054 - The Necklace
- uva 10054 - The Necklace
- UVa 10054 - The Necklace
- 10054 - The Necklace
- 10054 - The Necklace
- uva10054-The Necklace(项链)
- The Necklace UVA10054
- UVA 10054 The Necklace
- Uva-10054-The Necklace
- UVaOJ10054---The Necklace
- uva 10054 - The Necklace
- uva 10054 - The Necklace
- uva 10054 - The Necklace
- UVA10054 The Necklace
- UVA 10054 The Necklace
- 贝尔曼福特算法
- Android去除系统自带动画的两种方法
- PL/SQL_使用复合数据类型2(PL/SQL集合)
- 项目经理修炼路
- 2003年清华大学计算机研究生机试真题之五
- The Necklace
- uva 10534 Wavio Sequence | dp
- iOS系统GCD学习(5):信号量机制
- UIGestureRecognizer学习笔记
- iOS系统GCD学习(6):Dispatch Sources
- 计算机书目分享
- C# DataTable 用法小结
- Fedora20问题解决集锦
- iOS手势UIGestureRecognizer