SGU 101 AC
总算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的更多相关文章
- ACM: SGU 101 Domino- 欧拉回路-并查集
sgu 101 - Domino Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u Desc ...
- SGU 101
SGU 101,郁闷,想出来算法,但是不知道是哪个地方的问题,wa在第四个test上. #include <iostream> #include <vector> #inclu ...
- SGU 101 Domino (输出欧拉路径)
101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB Dominoes – game played wit ...
- SGU 101 Domino 题解
鉴于SGU题目难度较大,AC后便给出算法并发布博文,代码则写得较满意后再补上.——icedream61 题目简述:暂略 AC人数:3609(2015年7月20日) 算法: 这题就是一笔画,最多只有7个 ...
- SGU 101 Domino【欧拉路径】
题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=101 题意: N个多米诺骨牌,每个骨牌左右两侧分别有一个0~6的整数(骨牌可以旋转 ...
- SGU 101 修改
感谢这里. test4确实是个不连通的case,奇怪的是我用check函数跟if (check() == false)来判断这个case,当不连通时就死循环,得到的结果是不一样的,前者得到WA,后者得 ...
- SGU 101.Domino (欧拉路)
时间限制: 0.5 sec 空间限制: 4096 KB 描述 多米诺骨牌,一种用小的方的木块或其他材料,每个都被一些点在面上标记,这些木块通常被称为骨牌.每个骨牌的面都被一条线分成两个 方形,两边 ...
- SGU 101.Domino( 欧拉路径 )
求欧拉路径...直接dfs即可,时间复杂度O(N) -------------------------------------------------------------------------- ...
- sgu 101 Domino 解题报告及测试数据
101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB 题解: 求多米诺骨牌按照一定方式放置能否使相邻的位置 ...
随机推荐
- 对.Net WebSocket 和Socket的原理的思考
今早上班路上接到了一个朋友的微信信息,问我对WebSocket 是否熟悉,一楞,印象之中没有用过这个类....来到公司后,得空问了一下度娘,原来这是一个随着HTML5推出的一种新协议,意义在于能实现浏 ...
- 解决FPDF报错:FPDF error: Not a JPEG file / FPDF error: Not a PNG file
最近有个项目需要用到FPDF,但是输出的时候报错: FPDF error: Not a JPEG file: http://***/data/attachment/forum/201603/19/10 ...
- 添加打印机的时候怎样说windows没法连接到打印机毛病为0x00000002
把PrinterExtensionsandNotifications这个服务启动1下试试 PrintSpooler服务停止然后再启用试试
- Java单例模式--------懒汉式和饿汉式
单件模式用途:单件模式属于工厂模式的特例,只是它不需要输入参数并且始终返回同一对象的引用.单件模式能够保证某一类型对象在系统中的唯一性,即某类在系统中只有一个实例.它的用途十分广泛,打个比方,我们开发 ...
- Python 信号量
信号的概念 信号(signal)-- 进程之间通讯的方式,是一种软件中断.一个进程一旦接收到信号就会打断原来的程序执行流程来处理信号. 几个常用信号: SIGINT 终止进程 中断进 ...
- Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化
链接:http://codeforces.com/contest/665/problem/E 题意:求规模为1e6数组中,连续子串xor值大于等于k值的子串数: 思路:xor为和模2的性质,所以先预处 ...
- fedora 20 PIL
今天安装PIL花了我好多的时间. 刚开始,我手动下载PIL原码,编译安装. 启动我的django项目,报下面的错误,完全不懂是么意思. CommandError: One or more models ...
- easy ui datagrid 数据绑定
1.前台页面 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...
- Linux:crontab命令学习
一.crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动cro ...
- 实现WebApp直接调起NativeApp
原文http://www.baidufe.com/item/3444ee051f8edb361d12.html 试了一个小Demo,从WebApp上直接调起Android Native App,包括应 ...