题目链接:http://codeforces.com/problemset/problem/1152/E

题目大意

  有一个 1~n-1 的排列p 和长度为 n 的数组 a,数组b,c定义如下:

    b:bi = min(ai, ai + 1),1 <= i <= n-1。

    c:ci = max(ai, ai + 1),1 <= i <= n-1。

  数组b',c'定义如下:

    b':b'i = bpi,1 <= i <= n-1。

    c':c'i = cpi,1 <= i <= n-1。

  给定数组 b',c',求 a。

分析

  一笔画问题,求无向图欧拉通路。
  这里用了Hierholzer算法(DFS)。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< string, int > PSI;
typedef set< int > SI;
typedef vector< int > VI;
typedef map< int, int > MII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; struct Edge{
int id;
int from, to; Edge() {}
Edge(int id, int from, int to) : id(id), from(from), to(to) {}
}; struct Vertex{
int in = ;
vector< Edge > edges;
}; int n;
int b[maxN], c[maxN], a[maxN];
unordered_map< int, Vertex > mIV;
int vis[maxN];
int S;
int cnt; // Hierholzer算法求欧拉路
inline void hierholzer(int s) {
vector< Edge > &tmp = mIV[s].edges;
while(!tmp.empty()) {
Edge t = tmp.back();
tmp.pop_back(); if(vis[t.id]) continue;
vis[t.id] = ;
hierholzer(t.to);
a[cnt--] = t.to;
}
} int main(){
INIT();
cin >> n;
For(i, , n - ) cin >> b[i];
For(i, , n - ) {
cin >> c[i];
if(c[i] < b[i]) {
cout << - << endl;
return ;
}
}
For(i, , n - ) {
mIV[b[i]].edges.PB(Edge(i, b[i], c[i]));
++mIV[b[i]].in;
mIV[c[i]].edges.PB(Edge(i, c[i], b[i]));
++mIV[c[i]].in;
} // 找起点
// cnt记录奇度顶点个数
foreach(i, mIV) {
if(i->sd.in % == ) {
++cnt;
S = i->ft;
}
}
if(S == ) S = b[]; // 没有奇度顶点就随便选一个顶点
if(cnt > || cnt == ) { // 有两个或0个奇度顶点,才可能有欧拉通路
cout << - << endl;
return ;
} cnt = n;
hierholzer(S);
a[cnt--] = S; // 整个图可能不连通
if(cnt == )For(i, , n) cout << a[i] << " ";
else cout << -;
cout << endl;
return ;
}

CodeForces 1152E Neko and Flashback的更多相关文章

  1. [欧拉路]CF1152E Neko and Flashback

    1152E - Neko and Flashback 题意:对于长为n的序列c和长为n - 1的排列p,我们可以按照如下方法得到长为n - 1的序列a,b,a',b'. ai = min(ci, ci ...

  2. Codeforces Round #554 (Div. 2) E Neko and Flashback (欧拉路径 邻接表实现(当前弧优化..))

    就是一欧拉路径 贴出邻接表欧拉路径 CODE #include <bits/stdc++.h> using namespace std; const int MAXN = 100005; ...

  3. codeforces#1152D. Neko and Aki's Prank(dp)

    题目链接: https://codeforces.com/contest/1152/problem/D 题意: 给出一个$n$,然后在匹配树上染色边,每个结点的所有相邻边只能被染色一次. 问,这颗树上 ...

  4. codeforces#1152C. Neko does Maths(最小公倍数)

    题目链接: http://codeforces.com/contest/1152/problem/C 题意: 给出两个数$a$和$b$ 找一个$k(k\geq 0)$得到最小的$LCM(a+k,b+k ...

  5. Codeforces C.Neko does Maths

    题目描述: C. Neko does Maths time limit per test 1 second memory limit per test 256 megabytes input stan ...

  6. CodeForces 1152D Neko and Aki's Prank

    说明 Catalan(i) 表示卡特兰数的第 i 项. 题目链接:http://codeforces.com/problemset/problem/1152/C 题目大意 有 n 个左括号和 n 个右 ...

  7. CodeForces 1152F2 Neko Rules the Catniverse (Large Version)

    题目链接:http://codeforces.com/problemset/problem/1152/F2 题目大意 见http://codeforces.com/problemset/problem ...

  8. CodeForces 1152F1 Neko Rules the Catniverse (Small Version)

    题目链接:http://codeforces.com/problemset/problem/1152/F1 题目大意 有 n 个星球,给定限制 m,从 x 星球走到 y 星球的条件是,$1 \leq ...

  9. E. Neko and Flashback

    传送门: 题意:假定我们已知a[]={3,4,6,5,7},  那么b[]通过min(a[i],a[i+1])得到 那么b[]={3,4,5,5}, c[]通过max(a[i],a[i+1])得到 c ...

随机推荐

  1. Linux环境进程间通信----系统 V 消息队列(二)

    一.消息队列是一条由消息连接而成的链表,它保存在内核中,通过消息队列的引用标示符来访问. 二.消息队列不同于管道,通信的两个进程可以是完全无关的进程,它们之间不需要约定同步的方法.只要消息队列存在并且 ...

  2. CSS:CSS 盒子模型

    ylbtech-CSS:CSS 盒子模型 1.返回顶部 1. CSS 盒子模型 CSS 盒子模型(Box Model) 所有HTML元素可以看作盒子,在CSS中,"box model&quo ...

  3. python re 正則匹配規則

  4. lamp+nginx代理+discuz+wordpress+phpmyadmin搭建

    我们以模拟实际需求的形式来复习.需求如下:1. 准备两台centos 6,其中一台机器跑mysql,另外一台机器跑apache和nginx + php2. 同时安装apache和nginx,其中ngi ...

  5. 基于Netty的RPC架构学习笔记(十二):借助spring实现业务分离、聊天室小项目、netty3和4、5的不同、业务线程池以及消息串行化

    文章目录 借助spring实现业务分离(

  6. MySQL之explain命令解释

    explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 使用方法,在select语句前加上explain就可以了.如: explai ...

  7. Spring Boot整合Thymeleaf模板引擎

    什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框 ...

  8. 使ie6/7/8支持css3的方法

    使用PIE.htc让IE6\7\8支持CSS3部分属性 包括圆角,阴影,背景渐变等效果 下载地址 http://css3pie.com/download/ 需要注意几点的是 第一,pie是以相对页面h ...

  9. 6-MySQL-Ubuntu-操作数据表的基本操作(一)

    注: SQL语句的关键字不区分大小写,如select 和Select都可以 (1) 查看当前使用的数据库; select database(); (2) 使用某数据库或切换到某数据库 use 数据库名 ...

  10. vue中this.$refs可以拿到,但是里面的属性undefind的问题

    1.和vue的生命周期有关,必须要在从mounted开始拿,才能拿得到里面的Dom元素 2.想在element ui 对话框打开后取dom时,应该使用$nextTick,而不是直接使用this.$re ...