Codeforces 325E
Codeforces 325E
原题
题目描述:给出\(n\)个点, 编号为\(0 \text ~ n-1\),每个点连出两条边分别为\(2i\)和\(2i+1\)(\(mod n\)),现从\(0\)出发,最后回到\(0\),且每个点必须且只到达一次(\(0\)为两次),输出一条可行路径。
solution
能发现\(2i=2(i+n/2)(mod n), 2i+1=2(i+n/2)+1(mod n)\),那就可以把\(i\)与\((i+n/2)\)看做一个点,原图变成一个\(n/2\)点\(n\)边图,每条边都要经过一次,也就是欧拉回路,然后搜索求解就好了。
code
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
using namespace std;
const int maxn=int(1e5)+100;
int n, sum;
int route[maxn];
bool vis[maxn];
void dfs(int cur)
{
if (vis[cur]) return;
vis[cur]=true;
dfs((cur*2)%n);
dfs((cur*2+1)%n);
route[++sum]=cur;
}
void solve()
{
dfs(0);
for (int i=sum; i>0; --i) printf("%d ", route[i]);
printf("0\n");
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &n);
if (n & 1) printf("-1\n");
else solve();
return 0;
}
Codeforces 325E的更多相关文章
- CodeForces - 325E:The Red Button (哈密尔顿 转 欧拉回路)
Piegirl found the red button. You have one last chance to change the inevitable end. The circuit und ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- Centos6.5下安装php
安装php: yum -y install php 重启httpd服务激活php: /etc/init.d/httpd restart 测试php是否安装完成 vim /var/www/html/in ...
- Joomla JEvents 组件
JEvents http://extensions.joomla.org/extensions/extension/calendars-a-events/events/jevents Getting ...
- JavaBean之属性必须遵循命名规范
JavaBean中存在一些属性,今天写了一个Jsp的小例子.把Bean中的属性命名规范搞错了,具体的说属性的首字母大写了. 于是乎Jsp中读取属性时总是报错. javax.el.PropertyNot ...
- ReactNative
基于ReactNative实现的博客园手机客户端 去年九月,facebook发布了react-native,将web端的javaScript和react技术扩展到了IOS和Android的原生应用 ...
- Thread 线程简单例子
//这个方法是 静态的 public static void ThreadFunc() {//计数器 ; while(true) { //休眠1秒 Thread.Sleep(); //计数器递增 co ...
- javascript对象继承的实现
现在有两个对象,需要实现Chinese类型对象对Person类型对象的继承. 这里分两部分,属性和方法. 属性可以直接用构造函数的方法实现继承,而方法则要通过原型链来实现继承. 先解释什么是原型链,每 ...
- 【C++第二课】---C到C++的函数升级
C++中对C语言在函数使用方面做了很大的升级 一﹑内联函数 1.C++中推荐使用内联函数来替代宏片段代码 2.C++中使用关键字inline声明内联函数 例如: inline int func(int ...
- c# excel sheep 导出
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...
- wordpress All in one Seo
原文地址:http://www.7adesign.com/155.html WordPress插件All-in-one-seo-pack详细设置: I enjoy this plugin and ha ...
- 关于Makefile.am中与Build相关的变量设置 AM_CPPFLAGS
http://tonybai.com/2010/10/26/about-variables-related-to-building-in-makefile-am/ 关于Makefile.am中与Bui ...