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的更多相关文章

  1. CodeForces - 325E:The Red Button (哈密尔顿 转 欧拉回路)

    Piegirl found the red button. You have one last chance to change the inevitable end. The circuit und ...

  2. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  3. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  4. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  5. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  6. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  7. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  8. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  9. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. aspx和razor的区别

    两者几乎都不懂,现在要选择一种,只能百度,然后一条一条看,也不知道诸位大神哪个说的对. 两个引擎语法完全不一样,性能上Asp.Net略占优势,语法糖则是razor的强项. 开发MVC3首选razor ...

  2. 在Azure Cloud Service中部署Java Web App(2)

    接上文. 9.在进行发布之前,需要对我们的订阅做一些设置,因为默认情况下,Azure的service end指向的是Azure global的站点,如果我们要将服务发布在Azure的中国站点,需要做下 ...

  3. MEMS加速度计工作原理

    一般加速度计有两块芯片组成,一块是MEMS传感器,另一块是客户化的信号处理芯片. 加速度计也称惯性传感器,因为它的工作原理就是靠MEMS中可移动部分的惯性.由于中间电容板的质量很大,而且它是一种悬臂构 ...

  4. perl /m 当作多行处理

    高级用法 多行匹配: zjtest7-frontend:/root/0825# cat a2.pl print "1111111111111\n"; my $_="abc ...

  5. [Linux]shell编程基础/linux基础入门

    声明执行程序 #!/bin/bash 用来告诉系统使用/bin/bash 程序来执行该脚本.譬如python 脚本,可以这样写: #!/usr/bin/python   赋值和引用 赋值公式: 变量名 ...

  6. C指针

    1,每行最大长度,处理的最大列号; preprocessor directives,preprocessor,预处理器读入源代码,根据预处理指令对其进行修改,把修改后 的源代码递交给编译器; 预处理器 ...

  7. spring 定时任务(3)--配置多个定时任务

    <!-- 定义调用对象和调用对象的方法 --> <!-- 定时任务 A start --> <bean id="jobtask" class=&quo ...

  8. Windows Server 2012 R2超级虚拟化之七 远程桌面服务的增强

    Windows Server 2012 R2超级虚拟化之七  远程桌面服务的增强 在Windows Server 2012提供的远程桌面服务角色,使用户能够连接到虚拟桌面. RemoteApp程序.基 ...

  9. STL set接口中使用结构体类型

    需要在结构体中重载'<'运算符,下面是我写的一个例子: #include<iostream> #include<set> using namespace std; str ...

  10. vs2010编译live555源码

    最近加入了公司的C++视频小组,利用中秋这个假期将研究了一些live555的源码,现在先将如何编译使用vs2010编译live555,整理出来,对以后分析代码有很大帮助. 1.下载live555源码, ...