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< ...
随机推荐
- 图:无向图(Graph)基本方法及Dijkstra算法的实现 [Python]
一般来讲,实现图的过程中需要有两个自定义的类进行支撑:顶点(Vertex)类,和图(Graph)类.按照这一架构,Vertex类至少需要包含名称(或者某个代号.数据)和邻接顶点两个参数,前者作为顶点的 ...
- forEach方法的实现
var arr = [1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5]; Array.prototype.forEach = Array.prototype.fo ...
- SpringMVC—概述
mvc容器的实例化: http://blog.csdn.net/lin_shi_cheng/article/details/50686876 Spring的启动过程: 1: 对于一个web应用,其部署 ...
- linux 服务器部署的web项目存入数据库的时间不正确
在linux获取当前时间 date 获取的时间是正常的 ----- java写了个测试类 public class TestDate { public static void main(String[ ...
- Python3.x获取网页源码
Python3.x获取网页源码 1,获取网页的头部信息以确定网页的编码方式: import urllib.request res = urllib.request.urlopen('http://ww ...
- monit拉起服务
check process hive_metastore matching "HiveMetaStore" start program = "/usr/bin/nohup ...
- maven和gradle中,dependency和plugin的区别
dependency引入的东西 作用:代码编译/运行时所需要的东西 打包:项目打包后这些东西基本都在(一般都在). 例如:JSON工具包GSON(com.google.code.gson),不仅开发时 ...
- 《Java程序设计》第四章-认识对象
20145221<Java程序设计>第四章-认识对象 总结 教材学习内容总结 类与对象 定义:对象是Java语言中重要的组成部分,之前学过的C语言是面向过程的,而Java主要是面向对象的. ...
- UVa 10491 奶牛和轿车(全概率公式)
https://vjudge.net/problem/UVA-10491 题意: 假设有a头牛,b辆车,在最终选择前主持人会替你打开c个有牛的门,输出"总是换门"的策略下,赢得车的 ...
- BZOJ 3123 【SDOI2013】 森林
题目链接:森林 这道题想法很显然.既然只有加边而没有删边,那么每次启发式合并就可以了.查询路径\(k\)小似乎需要主席树,那么把主席树和倍增表一起暴力重构就好了. 然后发现这样的空间复杂度是\(O(n ...