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< ...
随机推荐
- python16_day27【crm 内嵌、删除、action】
一.内嵌 二.删除及关联关联显示 三.action
- fatal error C1010: unexpected end of file while looking for precompiled header directive
在编译VS时候,出现fatal error C1010: unexpected end of file while looking for precompiled head. 问题详细解释:致命错误C ...
- KEYENCE Programming Contest 2019 Solution
A - Beginning 签到. #include <bits/stdc++.h> using namespace std; int main() { ]; while (scanf(& ...
- centos远程访问mssql数据库
http://blog.path8.net/archives/5921.html http://www.jaggerwang.net/2013/03/18/centos%E4%B8%8B%E5%AE% ...
- uva1025 dp
这题说的是给了n个车站 从1号 车站到 n号车站,有m1辆车从1 开往n 有m2 辆车从n 开往1 一个人从1 车站 到达n 车站在T 时刻 要求再 车站呆的时间尽量少 dp[i][j] 表示 在 第 ...
- AVAudioFoundation(5):音视频导出
本文转自:AVAudioFoundation(5):音视频导出 | www.samirchen.com 本文主要内容来自 AVFoundation Programming Guide. 要读写音视频数 ...
- 【转载】Java动态代理之JDK实现和CGlib实现(简单易懂)
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6542259.html 一:代理模式(静态代理) 代理模式是常用设计模式的一种,我们在软件设计时常用的代理一般是 ...
- Python笔记 #13# Pandas: Viewing Data
感觉很详细:数据分析:pandas 基础 import pandas as pd import numpy as np import matplotlib.pyplot as plt dates = ...
- Cooperation.GTST团队项目总结
Cooperation.GTST团队项目总结 项目实现情况 目前对于基本UI界面的设计已经实现,对博客园接口XML的解析也已经完成,但是还暂时无法动态获取对应数据. 几张静态预览图展示(侧滑栏设计加入 ...
- 更改idea快捷键方式为eclipse风格
打开配置窗口 菜单栏中的File-settings 或者快捷键 ctrl+alt+s 设置keymap 在弹出的setting页面中左侧导航中选择Keymap: 在keymaps下拉列表中选择Ecli ...