题目链接: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. 【LeetCode 24】两两交换链表中的节点

    题目链接 [题解] 简单的链表操作 [代码] /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...

  2. Centos操作命令

    查看已经开放的端口:firewall-cmd --list-ports 开启端口:firewall-cmd --zone=public --add-port=80/tcp --permanent 重新 ...

  3. Ubuntu下安装Samba服务器

    闲来无聊尝试自己安装下Samba服务器,使本机和虚拟机可以无障碍传输文件(虽然用VMwaretools可传,但总感觉麻烦,而且速度欠佳) 首先,同安装qemu一样,在安装之前要确定你的系统apt列表已 ...

  4. 2019杭电多校第一场hdu6579 Operation(线性基)

    Operation 题目传送门 解题思路 把右边的数尽量往高位放,构造线性基的时候同时记录其在原序列中的位置,在可以插入的时候如果那个位置上存在的数字的位置比新放入的要小,就把旧的往后挤.用这种发现构 ...

  5. Django框架(五)—— 虚拟环境搭建

    目录 Django虚拟环境搭建 Django虚拟环境搭建 一.为什么要用虚拟环境 公司以前开发的项目是在Django1.5的基础上开发的,先要需要基于Django2.0开发一套项目.这样不能卸载原有版 ...

  6. java 生成随机数字+字母组合 和字母组合

    生成随机数包含数字,字母 /** * 生成随机数当作getItemID * n : 需要的长度 * @return */ private static String getItemID( int n ...

  7. DNF抽奖活动

    活动内容: DNF用户在注册页面注册获得抽奖资格(或分享好友注册)参与抽奖,产生奖品后,活动参与用户,在活动领奖页面领取奖品,金币及点券需填写相应游戏区服.qq号等信息,并且为防止活动刷子,在领取页提 ...

  8. 线性回归——Python代码实现

    import numpy as np def computer_error_for_give_point(w, b, points): # 计算出 观测值与计算值 之间的误差, 并累加,最后返回 平均 ...

  9. 【洛谷】P1288 取数游戏II

    题目链接:https://www.luogu.org/problemnew/show/P1288 题意:中文题面不赘述啦. 题解:代码很好写,其实就是判断边数是否为偶数.先手确定方向其实都是一样的,但 ...

  10. winfrom创建转圈等待窗体

    第一步:创建一个WaitForm public partial class WaitForm : Form { ; private ArrayList images = new ArrayList() ...