SGU 101,郁闷,想出来算法,但是不知道是哪个地方的问题,wa在第四个test上。

#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 reverse() {
Edge rev;
rev.no = no;
rev.u = v;
rev.v = u;
rev.d = '-';
return rev;
}
}; class Graph {
private:
map<int, int> u2i;
vector<vector<Edge> > G;
int deg[], no, use[];
vector<Edge> solution;
public:
Graph(vector<Edge>& edges) {
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));
dfs(beg);
return ;
}
void dfs(int u) { for (int i = ; i < G[u].size(); i++) {
if (use[G[u][i].no] == ) {
use[G[u][i].no] = ;
solution.push_back(G[u][i]);
dfs(u2i[G[u][i].v]);
break;
}
}
}
void check(int n) {
if (solution.size() != n) {
while ();
}
for (int i = ; i < solution.size() - ; i++) {
/*
printf("%d %d, %d %d\n",
solution[i].getU(), solution[i].getV(),
solution[i + 1].getU(), solution[i + 1].getV());
*/ //if (solution[i].getV() != solution[i + 1].getU()) {
if (solution[i].v != solution[i + ].u) {
while ();
} }
}
void getSolution() {
for (int i = ; i < solution.size(); 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() == -) {
while ();
printf("No solution\n");
} else {
graph.check(n);
graph.getSolution();
} //system("pause");
}

SGU 101的更多相关文章

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

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

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

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

  3. SGU 101 Domino【欧拉路径】

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

  4. SGU 101 修改

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

  5. SGU 101.Domino (欧拉路)

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

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

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

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

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

  8. SGU 101 Domino 题解

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

  9. SGU 101 AC

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

随机推荐

  1. 百度云管家-V4.6.1-单文件版绿色版

    转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/baidu-is-clouds-butler-v4-6-1-single-file-green-edition ...

  2. ▲▲▲▲▲▲▲▲▲▲▲yum源的配置(本地和ftp)▲▲▲▲▲▲▲▲▲▲▲▲▲v

    ★★★★★★★★★★★★★★★本机yum源★★★★★★★★★★★★★★★★ 1. 首先把DVD里的OS镜像mount处理,如果插入光驱自动mount的话,一般在/media下面,比如RHEL_6.3 ...

  3. jQuery获取同级元素

    next()相邻下一个同级元素 prev()相邻上一个同级元素 siblings()所有同级元素 $("#id").next(); $("#id").prev( ...

  4. Spring多数据源的动态切换

    Spring多数据源的动态切换 目前很多项目中只能配置单个数据源,那么如果有多个数据源肿么办?Spring提供了一个抽象类AbstractRoutingDataSource,为我们很方便的解决了这个问 ...

  5. ArcGIS Server10.1授权文件

    3dengine,101,ecp.arcgis.server,01-jan-2020,UTE784S3EY83ZJKN0085 3dserver,101,ecp.arcgis.server,01-ja ...

  6. JSON C# Class Generator ---由json字符串生成C#实体类的工具

    json作为互联网上轻量便捷的数据传输格式,越来越受到重视.但在服务器端编程过程中,我们常常希望能通过智能提示来提高编码效率.JSON C# Class Generator 能将json格式所表示的J ...

  7. C#使用Zxing2.0生成二维码 带简单中心LOGO

    参考:http://www.open-open.com/lib/view/open1379214678162.html 代码:http://files.cnblogs.com/halo/%E4%BA% ...

  8. 有关linux日志分析的详细介绍

    linux的日志文件可以帮助我们了解系统所处的状态,比如查出哪些用户有登入,及其它安全相关的一些问题. linux下的日志分析. 以下内容,部分参考了:探讨 linux 日志分析 这篇文章. 1.了解 ...

  9. 跟着PHP100第一季学写一个CMS(1-10)

    笔记: 这次用的方法是先跟着视频做一遍,隔一天或半天后独立再做一遍,能发现真正不会的地方记录下来. CMS0.1界面布局1.问题:分两个css来实现时basic.css+index.php出现定位不正 ...

  10. 使用Java反射(Reflect)、自定义注解(Customer Annotation)生成简单SQL语句

    这次给大家介绍一下在Java开发过程中 使用自定义注解开发:主要知识点:            1.反射            主要用于提取注解信息            2.自定义异常  主要是为了 ...