看样例然后发现只要求一个一笔画即可,用板子。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; const int maxn = 1e5 + 5;
int n, b[maxn], c[maxn], _b[maxn], _c[maxn];
int d[maxn << 1], tot;
vector<pair<int, int>> adj[maxn << 1];
bool vis[maxn];
int ans[maxn];
int cnt; void dfs(int cur) {
while (!adj[cur].empty()) {
auto tmp = adj[cur].back();
adj[cur].pop_back();
if (!vis[tmp.second]) {
vis[tmp.second] = 1;
dfs(tmp.first);
ans[cnt++] = tmp.first;
}
}
} int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++)
scanf("%d", &b[i]), d[++tot] = b[i];
for (int i = 1; i < n; i++)
scanf("%d", &c[i]), d[++tot] = c[i]; sort(d + 1, d + 1 + tot);
tot = unique(d + 1, d + 1 + tot) - d - 1;
for (int i = 1; i < n; i++) {
_b[i] = lower_bound(d + 1, d + 1 + tot, b[i]) - d;
_c[i] = lower_bound(d + 1, d + 1 + tot, c[i]) - d;
if (_b[i] > _c[i]) {
puts("-1");
return 0;
}
adj[_b[i]].push_back({_c[i], i});
adj[_c[i]].push_back({_b[i], i});
} vector<int> v;
for (int i = 1; i <= tot; i++) {
if (adj[i].size() % 2 == 1)
v.push_back(i);
}
if (v.size() == 2) {
dfs(v[0]);
ans[cnt++] = v[0];
} else if(v.size() == 0) {
dfs(1);
ans[cnt++] = 1;
}
if (cnt == n)
for (int i = cnt - 1; ~i; --i)
printf("%d ", d[ans[i]]);
else puts("-1");
return 0;
}

Codeforces 1152E(欧拉路径)的更多相关文章

  1. CodeForces 1152E Neko and Flashback

    题目链接:http://codeforces.com/problemset/problem/1152/E 题目大意 有一个 1~n-1 的排列p 和长度为 n 的数组 a,数组b,c定义如下: b:b ...

  2. Codeforces Round #508 (Div. 2) E. Maximum Matching(欧拉路径)

     E. Maximum Matching 题目链接:https://codeforces.com/contest/1038/problem/E 题意: 给出n个项链,每条项链左边和右边都有一种颜色(范 ...

  3. Codeforces Round #375 (Div. 2) E. One-Way Reform 欧拉路径

    E. One-Way Reform 题目连接: http://codeforces.com/contest/723/problem/E Description There are n cities a ...

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

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

  5. Codeforces Round 1152 (div. 2)

    奇差.ABC三题,排名400. 首先是策略问题. 由于第一眼看到D的时候感觉不太会做,于是去看E. 一看到E这不欧拉回路吗,可做可做, 于是--我不会欧拉回路! 手推.推了半天啥也没弄出来, 于是大概 ...

  6. 【题解】Weird journey Codeforces 788B 欧拉路

    传送门:http://codeforces.com/contest/788/problem/B 好题!好题! 首先图不连通的时候肯定答案是0,我们下面讨论图联通的情况 首先考虑,如果我们每条边都经过两 ...

  7. 【codeforces 723E】One-Way Reform

    [题目链接]:http://codeforces.com/contest/723/problem/E [题意] 给你一个无向图; 让你把这m条边改成有向图; 然后使得出度数目等于入度数目的点的数目最多 ...

  8. 【codeforces 508D】Tanya and Password

    [题目链接]:http://codeforces.com/problemset/problem/508/D [题意] 给你一个字符的所有连续3个的子串; 让你复原出原串; (包含小写.大写字母以及数字 ...

  9. Educational Codeforces Round 85 (Div. 2)

    题目链接:https://codeforces.com/contest/1334 A. Level Statistics 题意 一个关卡有玩家的尝试次数和通关次数,按时间顺序给出一个玩家 $n$ 个时 ...

随机推荐

  1. hdmap相关

    图片来源:https://vires.com/ 新闻摘抄: 对此,武汉光庭信息技术股份有限公司副总经理罗跃军告诉记者:“事实上,最初由自动驾驶车身控制系统协会所提出的地图精度要求很高,需要达到5cm甚 ...

  2. 虫草医药网站html模板

    虫草医药网站html模板是一款宝王虫草医药网站模板html源码整站下载. 模板地址:http://www.huiyi8.com/sc/8783.html

  3. BZOJ 1629 [Usaco2005 Nov]Cow Acrobats:贪心【局部证明】

    题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1332 题意: 有n头牛在“叠罗汉”. 第i头牛的体重为w[i],力量为s[i]. 一头 ...

  4. 分享知识-快乐自己:Struts2文件上传及文件下载

    1)Struts2单文件上传 action:类文件 package com.mlq.action; import com.opensymphony.xwork2.ActionSupport; impo ...

  5. listen 59

    Different Brain Regions Handle Different Music Types (Vivaldi) versus (the Beatles) . Both great. Bu ...

  6. storm源码剖析(2):storm的配置项

    storm的配置项,可以从backtype/storm/Config.java中找到所有配置项及其描述

  7. linux命令学习笔记:cut详解

    cut命令从文件的每一行剪切字节.字符和字段并将它们写至标准输出.它是以文件的每一行作为处理对象的. 命令格式:cut [选项] [范围] 文件.选项用来指定单位(字节.字符还是字段),范围指定选项的 ...

  8. ivew组件的使用

    iview的官网:https://www.iviewui.com/docs/guide/start 1.选择快速上手 2.安装 解压,cmd,cd进你解压后的文件,cnpm i 3.打包 npm ru ...

  9. UNR #1 火车管理

    很简单 用一个线段树维护 1.答案 2.当前栈顶是什么时候push进来的 然后用一棵以时间为版本的可持久化线段树维护每个操作之后第一个覆盖到他的操作是哪个 就可以了 询问直接在线段树上询问,修改在两棵 ...

  10. ACM学习历程—HDU 1059 Dividing(dp && 多重背包)

    Description Marsha and Bill own a collection of marbles. They want to split the collection among the ...