题目链接: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. 【hihocoder 1554】最短的 Nore0061

    [链接]http://hihocoder.com/problemset/problem/1554 [题意] 中文题 [题解] DP; 设f[i][j][k]表示前i个字符,第一个串已经得到了前j个字符 ...

  2. Delphi 文件操作(路径、目录)

    Delphi利用系统环境变量获取常用系统目录 //譬如 %WINDIR% 是表示系统目录的系统变量, 可以这样获取: var s: string; begin s := GetEnvironmentV ...

  3. NOIp2018集训test-10-21 (联考六day1)

    今天被高一狂踩,两个手抖,t1一个1写成2,t3一个+=写成=,所谓失之毫厘谬以千里,直接丢了50分. 完全背包 看到背包体积如此之大物品体积如此之小容易很想到贪心,肯定要先加很多很多的性价比最高的最 ...

  4. (转)Openfire 中SASL的认证方式之:PLAIN,DIGEST-MD5,anonymous

    转:http://blog.csdn.net/coding_me/article/details/39524137 SASL  的认证方式包括:     1. PLAIN:plain是最简单的机制,但 ...

  5. flask 的orm

    https://www.cnblogs.com/chichung/p/9794702.html

  6. AI应用在金融领域,如何能够在商业上有所突破

    AI应用在金融领域,如何能够在商业上有所突破 如今,随着社会不断发展,技术不断进步,国内外各大金融机构已经在大数据.人工智能.区块链等新技术上有很多尝试,智能客服.智能投顾等新金融形式也早已不新鲜.那 ...

  7. 正则化:L0 vs L1 vs L2

    原文地址:https://www.jianshu.com/p/e5c9a9fc84d4 为什么正则化可以缓解过拟合? 过拟合时,拟合函数的系数往往非常大.过大的权重会导致模型过多地学习到某些数据的个性 ...

  8. 通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷

    通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷.效果如下:     步骤1:通过MyEclipse中的window->show View->ot ...

  9. shell提取rpm的报名

    说明: rpm本身未安装,下面方法是基于rpm包文件实现提取rpm的包名 rpm -qpi ZXVEi-git0/CgsOS/abrt-2.1.11-48.el7.centos.x86_64.rpm ...

  10. sanic连接mongo

    方法一: #没有密码,就是没有用户和用户密码 settings={"MOTOR_URI":"mongodb://127.0.0.1:27017/zzy"} ap ...