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. CSS 总结

    CSS 积累总结 1. ::Selection 选择器 使被选中的文本成为灰色: ::selection { color:#CCC; background:red; --- 选中背景颜色变成红色 } ...

  2. android 自定义AlertDialog(一段)

    java: final AlertDialog dialog = new AlertDialog.Builder(mContext) .create(); dialog.setCancelable(f ...

  3. OSCHina技术导向:Java开源QQ工具iQQ

    iQQ 使用Java语言跨平台开发,基于腾讯WebQQ 3.0网络协议.可以使用于Java所支持的各种平台上运行.作者基于Linux(Ubuntu 12.04)系统,使用IDE NetBeans开发, ...

  4. Tengine笔记2:通过IP、域名、端口实现虚拟主机

    一.通过端口创建虚拟主机 案例:通过端口访问两个不同的页面 将/usr/local/tengine-2.1.0/html/index.html内的内容改为 Welcom to port1 然后在/op ...

  5. fafu 1568 Matrix(二分匹配+二分)

    Description:   You are given a matrix which <= n <= m <= ). You are supposed to choose n el ...

  6. 启动Tomcat自动加载(运行)类

    其实这是紧跟着我上次写的java计时器Timer的,因为Timer的测试类写好后,不可能要通过什么东西去触发,对已经存在的时间点进行监控 所以,在启动项目是自动运行此类 方法如下: 一.在web.xm ...

  7. MIN (Transact-SQL)【转】

    MIN (Transact-SQL)   其他版本 SQL Server 2005   此主题尚未评级 - 评价此主题   返回表达式中的最小值. 后面可能跟随 OVER 子句. Transact-S ...

  8. ASP.NET MVC 学习之路-6

    本文在于巩固基础 上文中使用的Code First创建数据库 本文将使用数据库生成模型 这里使用ADO.NET实体数据模型来生成模型 下面按照指导完成操作 下面看看如何使用这个框架 数据访问修改主要是 ...

  9. HDU 1020 Encoding POJ 3438 Look and Say

    Encoding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  10. DataTable中执行DataTable.Select("条件"),

    我们在使用Sql ******这些数据库时,可以轻松的通过Sum.Aver.Count等统计出相关结果,那么,在已经把数据检索出来的DataSet(DataTable)中呢?特别是通过Web Serv ...