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< ...
随机推荐
- (5)调度器(scheduler)
继承关系 原理介绍 Cocos2d-x调度器为游戏提供定时事件和定时调用服务.所有Node对象都知道如何调度和取消调度事件,使用调度器有几个好处: 每当Node不再可见或已从场景中移除时,调度器会停止 ...
- centos6 pip install python-ldap报错
error: Setup script exited with error: command 'gcc' failed with exit status 1 解决方法: 原因是版本不兼容,centos ...
- 爬取乌云上所有人民币和乌云符号的漏洞(python脚本)
import httplib from HTMLParser import HTMLParser import urlparse import urllib from bs4 import Beaut ...
- python学习之【16】网络编程
主题 客户端/服务器架构 套接字:通信终点 套接字地址 面向连接与无连接套接字 Python中的网络编程 SOCKET模块 套接字对象方法 TCP/IP客户端和服务器 UDP/IP客户端和服务器 So ...
- url的正则表达式
http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
- Vue-cli proxyTable 解决开发环境的跨域问题
Vue-cli proxyTable 解决开发环境的跨域问题 proxyTable: { '/list': { target: 'http://api.xxxxxxxx.com', pathRewri ...
- LINUX实践--ELF分析
一.ELF文件头(定义在/usr/include/elf.h)中 二.实践部分 第一行 对应e_ident[EI_NIDENT]:实际表示内容为7f45 4c46 0101 0100 0000 000 ...
- TypeScript 照猫画虎
定义变量类型 const num: number = 1 定义函数参数类型 const init: (p: str) => void = function(param) { alert(para ...
- 【找不到符号】Maven打包找不到符号的问题排查
当碰到maven错误:找不到符号问题时,通常第一反应应该是执行eclipse的Project -> Clean … -> Clean all projects,然后再执行maven cle ...
- FreeSouth的学习osg小贴士
http://www.osgchina.org/index.php?option=com_content&view=article&id=150&catid=91&It ...