总算AC了,好帅气的算法,同时解决了自环跟非连通,一种自下向上找环的算法过程,这里把欧拉回路讲得很清楚,赞。

#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <string.h>
using namespace std; class Edge {
public:
int no, u, v;
char d;
Edge() {
no = u = v = d = -;
}
Edge reverse() {
Edge rev;
rev.no = no;
rev.u = v;
rev.v = u;
rev.d = d == '+' ? '-' : '+';
return rev;
}
}; class Graph {
private:
map<int, int> u2i;
vector<vector<Edge> > G;
int deg[], n, no, use[], vUse[];
vector<Edge> solution;
public:
Graph(vector<Edge>& edges) {
n = edges.size();
makeU2I(edges);
memset(deg, , sizeof(deg));
G.clear();
G.resize(no);
for (int i = ; i < edges.size(); i++) {
G[u2i[edges[i].u]].push_back(edges[i]);
G[u2i[edges[i].v]].push_back(edges[i].reverse());
deg[u2i[edges[i].u]]++;
deg[u2i[edges[i].v]]++;
}
}
void makeU2I(vector<Edge>& edges) {
u2i.clear();
for (int i = ; i < edges.size(); i++) {
u2i[edges[i].u] = u2i[edges[i].v] = ;
}
no = ;
for (map<int, int>::iterator it = u2i.begin(); it != u2i.end(); it++) {
it->second = no++;
}
}
int solve() {
int beg = -, end = -;
for (int i = ; i < no; i++) {
if (deg[i] & ) {
if (beg == -) {
beg = i;
} else if (end == -) {
end = i;
} else {
return -;
}
}
}
if (beg == -) {
beg = ;
}
memset(use, , sizeof(use));
Edge begEdge;
dfs(beg, -, begEdge);
if (solution.size() < n) {
// 判连通
return -;
}
return ;
}
void dfs(int u, int sign, Edge lastEdge) {
for (int i = ; i < G[u].size(); i++) {
if (use[G[u][i].no] == ) {
use[G[u][i].no] = ;
dfs(u2i[G[u][i].v], , G[u][i]);
}
} if (sign > ) {
solution.push_back(lastEdge);
}
}
void getSolution() {
for (int i = solution.size() - ; i >= ; i--) {
printf("%d %c\n", solution[i].no, solution[i].d);
}
}
}; int main()
{
int n;
scanf("%d", &n); vector<Edge> edges;
for (int i = ; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b); Edge e;
e.no = i + ;
e.u = a; e.v = b;
e.d = '+'; edges.push_back(e);
} Graph graph(edges);
if (graph.solve() == -) {
printf("No solution\n");
} else {
graph.getSolution();
} //system("pause");
}

SGU 101 AC的更多相关文章

  1. ACM: SGU 101 Domino- 欧拉回路-并查集

    sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Desc ...

  2. SGU 101

    SGU 101,郁闷,想出来算法,但是不知道是哪个地方的问题,wa在第四个test上. #include <iostream> #include <vector> #inclu ...

  3. SGU 101 Domino (输出欧拉路径)

    101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB Dominoes – game played wit ...

  4. SGU 101 Domino 题解

    鉴于SGU题目难度较大,AC后便给出算法并发布博文,代码则写得较满意后再补上.——icedream61 题目简述:暂略 AC人数:3609(2015年7月20日) 算法: 这题就是一笔画,最多只有7个 ...

  5. SGU 101 Domino【欧拉路径】

    题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=101 题意: N个多米诺骨牌,每个骨牌左右两侧分别有一个0~6的整数(骨牌可以旋转 ...

  6. SGU 101 修改

    感谢这里. test4确实是个不连通的case,奇怪的是我用check函数跟if (check() == false)来判断这个case,当不连通时就死循环,得到的结果是不一样的,前者得到WA,后者得 ...

  7. SGU 101.Domino (欧拉路)

    时间限制: 0.5 sec 空间限制: 4096 KB 描述 多米诺骨牌,一种用小的方的木块或其他材料,每个都被一些点在面上标记,这些木块通常被称为骨牌.每个骨牌的面都被一条线分成两个   方形,两边 ...

  8. SGU 101.Domino( 欧拉路径 )

    求欧拉路径...直接dfs即可,时间复杂度O(N) -------------------------------------------------------------------------- ...

  9. sgu 101 Domino 解题报告及测试数据

    101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB 题解: 求多米诺骨牌按照一定方式放置能否使相邻的位置 ...

随机推荐

  1. (UVALive 7261)Xiongnu's Land 二分

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  2. poj 3616 Milking Time

                                                                                                 Milking ...

  3. 小黑的镇魂曲(HDU2155:贪心+dfs+奇葩解法)

    题目:点这里 题目的意思跟所谓的是英雄就下100层一个意思……在T秒内能够下到地面,就可以了(还有一个板与板之间不能超过H高). 接触这题目是在昨晚的训练赛,当时拍拍地打了个贪心+dfs,果断跟我想的 ...

  4. Adobe Illustrator CS6 绿色简体中文版下载地址

    一.Adobe Illustrator CS6 简体中文精简绿色优化版:1.由官方简体中文正式版制作而成,只需要执行一次快速安装即可使用.已经注册,非tryout版,支持x64位系统.2.精简了Ext ...

  5. DWZ 刷新 dialog

    DWZ刷新dialog: 1,在删除按钮上添加callback属性;如:(callback="dialogAjax") <a class="delImg" ...

  6. 一段画对角线的canvas代码,之前没有写过canvas代码,现在记录下来

    <canvas id="other" style="width:320px;height:320px;"></canvas> var o ...

  7. Java Thread and runnable

    java中可有两种方式实现多线程, 一种是继承Thread类,(Thread本身实现了Runnable接口,就是说需要写void run 方法,来执行相关操作) 一种是实现Runnable接口 sta ...

  8. Implement Custom Cache Dependencies in ASP.NET 1.x

    Code download available at:CuttingEdge0407.exe(128 KB)   Contents What's a Cache Dependency, Anyway? ...

  9. SQL Server是如何让定时作业

    如果在SQL Server 里需要定时或者每隔一段时间执行某个存储过程或3200字符以内的SQL语句时,可以用管理->SQL Server代理->作业来实现. 1.管理->SQL S ...

  10. hdu 5343 MZL's Circle Zhou SAM

    MZL's Circle Zhou 题意:给定两个长度不超过a,b(1 <= |a|,|b| <= 90000),x为a的连续子串,b为y的连续子串(x和y均可以是空串):问x+y形成的不 ...