UOJ117. 欧拉回路【欧拉回路模板题】
题目大意
就是让你对有向图和无向图分别求欧拉回路
非常的模板,但是由于UOJ上毒瘤群众太多了
所以你必须加上一个小优化
就是每次访问过一个边就把它删掉
有点像Dinic的当前弧优化的感觉
注意是在dfs完一个节点把当前的边加入到栈里面
然后输出的时候为了保证原来的顺序就直接弹栈就好了
//Author: dream_maker
#include<bits/stdc++.h>
using namespace std;
//----------------------------------------------
typedef pair<int, int> pi;
typedef long long ll;
typedef double db;
#define fi first
#define se second
#define fu(a, b, c) for (int a = b; a <= c; ++a)
#define fd(a, b, c) for (int a = b; a >= c; --a)
#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)
const int INF_of_int = 1e9;
const ll INF_of_ll = 1e18;
template <typename T>
void Read(T &x) {
bool w = 1;x = 0;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') w = 0, c = getchar();
while (isdigit(c)) {
x = (x<<1) + (x<<3) + c -'0';
c = getchar();
}
if (!w) x = -x;
}
template <typename T>
void Write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) Write(x / 10);
putchar(x % 10 + '0');
}
//----------------------------------------------
const int N = 4e5 + 10;
struct Edge {
int v, id, nxt;
} E[N];
int head[N], tot = 0;
int fro[N], to[N], n, m;
void addedge(int u, int v, int id) {
E[++tot] = (Edge) {v, id, head[u]};
head[u] = tot;
}
namespace Solve1 {
int du[N], vis[N];
stack<int> st;
void dfs(int u) {
for (int &i = head[u]; i; i = E[i].nxt) {
int cur = i;
if (vis[abs(E[cur].id)]) continue;
vis[abs(E[cur].id)] = 1;
dfs(E[cur].v);
st.push(E[cur].id);
}
}
void solve() {
fu(i, 1, m) ++du[fro[i]], ++du[to[i]];
fu(i, 1, n) if (du[i] & 1) {
printf("NO");
return;
}
fu(i, 1, m) {
addedge(fro[i], to[i], i);
addedge(to[i], fro[i], -i);
}
fd(i, n, 1) {
if (head[i]) {
dfs(i);
break;
}
}
if ((signed) st.size() != m) {
printf("NO");
} else {
printf("YES\n");
while (st.size()) {
Write(st.top()), putchar(' ');
st.pop();
}
}
}
}
namespace Solve2 {
int in[N], out[N], vis[N];
stack<int> st;
void dfs(int u) {
for (int &i = head[u]; i; i = E[i].nxt) {
int cur = i;
if (vis[E[cur].id]) continue;
vis[E[cur].id] = 1;
dfs(E[cur].v);
st.push(E[cur].id);
}
}
void solve() {
fu(i, 1, m) ++out[fro[i]], ++in[to[i]];
fu(i, 1, n) if (out[i] ^ in[i]) {
printf("NO");
return;
}
fu(i, 1, m) addedge(fro[i], to[i], i);
fu(i, 1, n) {
if (head[i]) {
dfs(i);
break;
}
}
if ((signed) st.size() != m) {
printf("NO");
} else {
printf("YES\n");
while (st.size()) {
Write(st.top()), putchar(' ');
st.pop();
}
}
}
}
int main() {
#ifdef dream_maker
freopen("input.txt", "r", stdin);
#endif
int op; Read(op);
Read(n), Read(m);
fu(i, 1, m) Read(fro[i]), Read(to[i]);
if (op == 1) Solve1::solve();
else Solve2::solve();
return 0;
}
UOJ117. 欧拉回路【欧拉回路模板题】的更多相关文章
- UOJ117 欧拉回路[欧拉回路]
找欧拉回路的模板题. 知识点详见图连通性学习笔记. 注意一些写法上的问题. line37&line61:因为引用,所以j和head值是同步更新的,类似于网络流的当前弧优化,除了优化枚举外,这样 ...
- hdu--1878--欧拉回路(并查集判断连通,欧拉回路模板题)
题目链接 /* 模板题-------判断欧拉回路 欧拉路径,无向图 1判断是否为连通图, 2判断奇点的个数为0 */ #include <iostream> #include <c ...
- UOJ117:欧拉回路——题解
http://uoj.ac/problem/117 (作为一道欧拉回路的板子题,他成功的令我学会了欧拉回路) (然而我不会背……) 就两件事: 1.无向图为欧拉图,当且仅当为连通图且所有顶点的度为偶数 ...
- [AHOI 2009] 维护序列(线段树模板题)
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- POJ2774 & 后缀数组模板题
题意: 求两个字符串的LCP SOL: 模板题.连一起搞一搞就好了...主要是记录一下做(sha)题(bi)过程心(cao)得(dan)体(xin)会(qing) 后缀数组概念...还算是简单的,过程 ...
- HDU 1251 Trie树模板题
1.HDU 1251 统计难题 Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...
- HDU-3549 最大流模板题
1.HDU-3549 Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...
- HDU 4280:Island Transport(ISAP模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...
- HDU-2222 Keywords Search(AC自动机--模板题)
题目大意:统计一共出现了多少次模板串. 题目分析:AC自动机的模板题.不过这题有坑,相同的模板串不能只算一次. 代码如下: # include<iostream> # include< ...
随机推荐
- (24)如何使用HttpClient
介绍 HttpClient是HTTP客户端的接口.HttpClient封装了各种对象,处理cookies,身份认证,连接管理等. 概念 HttpClient的使用一般包含下面6个步骤: 创建 Http ...
- SQL学习笔记六之MySQL数据备份和pymysql模块
mysql六:数据备份.pymysql模块 阅读目录 一 IDE工具介绍 二 MySQL数据备份 三 pymysql模块 一 IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测 ...
- 2017-2018-1 JaWorld 团队作业--冲刺3
2017-2018-1 JaWorld 团队作业--冲刺3 (20162306) 总体架构 我们本次团队项目设定为基于Android系统Java架构下的打飞机小游戏 游戏中所有模型的原型设定是精灵,因 ...
- scss使用指南--每天一点
我们平时都称之为 Sass,其实可分成sass和scss, 其中Sass 是以严格的缩进式语法规则来书写,不带大括号({})和分号(;),以".sass"后缀为扩展名:而 SCSS ...
- double保存小数点后两位
double getRound(double a){ return (int(a * 100 + 0.5)) / 100.0; };//利用的是强制转换
- C++ 自定义错误类
#include <iostream> #include <exception> using namespace std; struct MyException : publi ...
- [转] sql server 跨数据库调用存储过程
A库存储过程: create PROCEDURE [dbo].[spAAAForTest] ( ) =null , ) =null ) AS BEGIN select N'A' AS a , N'B' ...
- [转]Python的getattr(),setattr(),delattr(),hasattr()
getattr()函数是Python自省的核心函数,具体使用大体如下: 获取对象引用getattrGetattr用于返回一个对象属性,或者方法 class A: def __init__(self): ...
- 雷林鹏分享:Ruby 多线程
Ruby 多线程 每个正在系统上运行的程序都是一个进程.每个进程包含一到多个线程. 线程是程序中一个单一的顺序控制流程,在单个程序中同时运行多个线程完成不同的工作,称为多线程. Ruby 中我们可以通 ...
- 将数据提取到CSV文件中保存
这个方法可以实现,登录获取的token放入CSV文件,供后续调用,这里没有用登录举例 FileWriter fstream = new FileWriter("E:\\apache-jmet ...