题目链接: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. java——String类,时间类,格式化

    日期类 格式化

  2. AcWing 286. 选课 (树形依赖分组背包)打卡

    有依赖的背包 首先依赖的概念,就是一个东西依附与一个东西之上,我们想买附品的话必须要把主品先买下来,这个可以先做下这道题 https://www.cnblogs.com/Lis-/p/11047466 ...

  3. Makefile中的函数

    Makefile 中的函数 Makefile 中自带了一些函数, 利用这些函数可以简化 Makefile 的编写. 函数调用语法如下: $(<function> <arguments ...

  4. SPSS聚类分析:K均值聚类分析

    SPSS聚类分析:K均值聚类分析 一.概念:(分析-分类-K均值聚类) 1.此过程使用可以处理大量个案的算法,根据选定的特征尝试对相对均一的个案组进行标识.不过,该算法要求您指定聚类的个数.如果知道, ...

  5. PHP对象在内存中的分配(转载)

    http://www.cnblogs.com/hongfei/archive/2012/06/12/2547120.html 对像在PHP 里面和整型.浮点型一样,也是一种数据类,都是存储不同类型数据 ...

  6. Java 序列化和反序列化(一)Serializable 使用场景

    目录 Java 序列化和反序列化(一)Serializable 使用场景 1. 最简单的使用:Serializable 接口 2. 序列化 ID 的问题 3. 静态字段不会序列化 4. 屏蔽字段:tr ...

  7. 最小生成树--Prim及Kruskal

    //prim算法#include<cstdio> #include<cmath> #include<cstring> #include<iostream> ...

  8. 随时更新web html 项目页面,查看手机等其他移动设备的几种方法?

    想一想自己一边写着代码,一边随时看着手机更新页面,对于前端开发者来说是不是爽歪歪: 实现以上效果只需要几个方法: 1) 安装虚拟机 Oracle VM VirtualBox (安装过程省略) 2) 安 ...

  9. 三.Python数据类型详述

    Python第三节数据类型详述 一.多变量赋值 python允许多变量赋值 多变量赋相同的值a = b = c = 1 多变量赋不同的值a, b, c = 1, 2, "fuckyou&qu ...

  10. 初识微服务框架ServiceComb

    https://blog.csdn.net/zengdongwen/article/details/93486257 后续跟进学习.