Codeforces 1152E(欧拉路径)
看样例然后发现只要求一个一笔画即可,用板子。
#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(欧拉路径)的更多相关文章
- CodeForces 1152E Neko and Flashback
题目链接:http://codeforces.com/problemset/problem/1152/E 题目大意 有一个 1~n-1 的排列p 和长度为 n 的数组 a,数组b,c定义如下: b:b ...
- Codeforces Round #508 (Div. 2) E. Maximum Matching(欧拉路径)
E. Maximum Matching 题目链接:https://codeforces.com/contest/1038/problem/E 题意: 给出n个项链,每条项链左边和右边都有一种颜色(范 ...
- 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 ...
- Codeforces Round #554 (Div. 2) E Neko and Flashback (欧拉路径 邻接表实现(当前弧优化..))
就是一欧拉路径 贴出邻接表欧拉路径 CODE #include <bits/stdc++.h> using namespace std; const int MAXN = 100005; ...
- Codeforces Round 1152 (div. 2)
奇差.ABC三题,排名400. 首先是策略问题. 由于第一眼看到D的时候感觉不太会做,于是去看E. 一看到E这不欧拉回路吗,可做可做, 于是--我不会欧拉回路! 手推.推了半天啥也没弄出来, 于是大概 ...
- 【题解】Weird journey Codeforces 788B 欧拉路
传送门:http://codeforces.com/contest/788/problem/B 好题!好题! 首先图不连通的时候肯定答案是0,我们下面讨论图联通的情况 首先考虑,如果我们每条边都经过两 ...
- 【codeforces 723E】One-Way Reform
[题目链接]:http://codeforces.com/contest/723/problem/E [题意] 给你一个无向图; 让你把这m条边改成有向图; 然后使得出度数目等于入度数目的点的数目最多 ...
- 【codeforces 508D】Tanya and Password
[题目链接]:http://codeforces.com/problemset/problem/508/D [题意] 给你一个字符的所有连续3个的子串; 让你复原出原串; (包含小写.大写字母以及数字 ...
- Educational Codeforces Round 85 (Div. 2)
题目链接:https://codeforces.com/contest/1334 A. Level Statistics 题意 一个关卡有玩家的尝试次数和通关次数,按时间顺序给出一个玩家 $n$ 个时 ...
随机推荐
- html5--1.19 通用属性
html5--1.19 通用属性 学习要点: 1.通用属性的概念及几个常用的通用属性2.对属性值的若干点补充 通用属性 通用属性(全局属性)可以用于任何的HTML5元素:通用属性有十几种:这节课不会全 ...
- 如何改变cmd窗口大小
按下Win+R(windows徽标和R一起按),再输入cmd. 输入mode ,查看当前屏幕的大小,来为后面准备(大概估计数值大小). 再输入mode con:cols=400 lines=400 ...
- Java_正则_00_资源贴
二.参考资料 1.揭开正则表达式的神秘面纱
- 1076 Forwards on Weibo (30)(30 分)
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...
- 描述怎样通过flask+redis+sqlalchemy等工具,开发restful api
flask开发restful api系列(8)-再谈项目结构 摘要: 进一步介绍flask的项目结构,使整个项目结构一目了然.阅读全文 posted @ 2016-06-06 13:54 月儿弯弯02 ...
- 单机 Oracle 11g(11.2.0.4)手动打补丁PSU(11.2.0.4.8)
环境说明:database : 11.2.0.4 x64os: centos6.7 x64 准备内容:OPatch : p6880880_112000_Linux-x86-64.zipDB PSU : ...
- bzoj 3073 [Pa2011]Journeys ——线段树优化连边
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3073 建两棵线段树,一棵孩子向父亲连边,是走出去的:一棵父亲向孩子连边,是走进来的. 注意第 ...
- Grunt:GruntFile.js
ylbtech-Grunt:GruntFile.js 1.返回顶部 1. module.exports = function (grunt) { grunt.initConfig({ useminPr ...
- 五 Vue学习 首页学习 (上)
首页: http://localhost:8002/#/, 登录页面如下: index.js文件中如下的路由配置,转过去看login.vue是如何实现的. const routes = [ { ...
- Windows WMIC命令使用详解1
https://blog.csdn.net/enweitech/article/details/51982114 在CMD和Powershell中 使用WMIC 先决条件: a. 启动Windows ...