感谢这里

test4确实是个不连通的case,奇怪的是我用check函数跟if (check() == false)来判断这个case,当不连通时就死循环,得到的结果是不一样的,前者得到WA,后者得到TLE,难道SGU能自动在某个条件下终止死循环,然后返回false吗。

不连通的情况FIX了,修改了代码如下:

#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[], 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));
dfs(beg);
return ;
}
bool dfs(int u) {
for (int i = ; i < G[u].size(); i++) {
if (use[G[u][i].no] == ) {
use[G[u][i].no] = ; if (dfs(u2i[G[u][i].v])) {
solution.push_back(G[u][i]);
return true;
} else {
use[G[u][i].no] = ;
}
}
}
for (int i = ; i <= n; i++) {
if (use[i] == ) {
return false;
}
}
return true;
}
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 ();
} }
}
bool check2(int n) {
if (solution.size() != n) {
while ();
return false;
} else {
return true;
}
}
bool checkConnectity() {
memset(vUse, , sizeof(vUse));
vector<int> Q;
Q.push_back();
vUse[] = ; for (int i = ; i < Q.size(); i++) {
for (int j = ; j < G[Q[i]].size(); j++) {
if (vUse[u2i[G[Q[i]][j].v]] == ) {
vUse[u2i[G[Q[i]][j].v]] = ;
Q.push_back(u2i[G[Q[i]][j].v]);
}
}
} for (int i = ; i < no; i++) {
if (vUse[i] == ) {
return false;
}
}
return true;
}
void getSolution() {
//for (int i = 0; i < solution.size(); i++) {
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() == - || graph.checkConnectity() == false) {
printf("No solution\n");
} else {
graph.getSolution();
} //system("pause");
}

上面的代码能处理自环的情况,可面临一个链上套了很多环的情况就会TLE,test 13应该就是这样的一个case。

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

    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【欧拉路径】

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

  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. opencv 手写选择题阅卷 (三)训练分类器

    opencv 手写选择题阅卷 (三)训练分类器 1,分类器选择:SVM 本来一开始用的KNN分类器,但这个分类器目前没有实现保存训练数据的功能,所以选择了SVN分类器; 2,样本图像的预处理和特征提取 ...

  2. What is a First Chance Exception?

    Refrences: http://blogs.msdn.com/b/davidklinems/archive/2005/07/12/438061.aspx To be continued...

  3. JDK中工具类的使用

    JDK中内置了很多常用的工具类,且多以“s”结尾,如:集合工具类Collections,数组工具类Arrays,对象工具类Objects,文件工具类Files,路径工具类Paths,数学工具类Math ...

  4. MFC中快速应用OpenCV(转)

    转载链接:http://wiki.opencv.org.cn/index.php/MFC%E4%B8%AD%E5%BF%AB%E9%80%9F%E5%BA%94%E7%94%A8OpenCV 简介和缘 ...

  5. DEDECMS中,引入文件

    引入文件:dede:include 标签:{dede:include filename="foot.htm"/}

  6. mysql 主从同步 Last_SQL_Error

    参考文章: http://kerry.blog.51cto.com/172631/277414/ http://hancang2010.blog.163.com/blog/static/1824602 ...

  7. ubuntu 14.04 nagios4+ndoutils2.0+centreon2.5.4配置

    ubuntu 14.04 nagios4+ndoutils2.0+centreon2.5.4(原创) 开发应用centreon是开源的IT监控软件,由法国人于2003年开发,最初名为Oreon,并于2 ...

  8. C++ 对数组sizeof 和对数组元素sizeof

    这一段程序 下面这段程序很有看点://arr1 is an array of intsint *source=arr1;size_t sz=sizeof(arr1)/sizeof(*arr1);//n ...

  9. Web前端的35个jQuery小技巧

    1. 禁止右键点击 $(document).ready(function(){     $(document).bind("contextmenu",function(e){   ...

  10. php中封装的curl函数(抓取数据)

    介绍一个封闭好的函数,封闭了curl函数的常用步骤,方便抓取数据. 代码如下: <?php /** * 封闭好的 curl函数 * 用途:抓取数据 * edit by www.jbxue.com ...