URAL 1614. National Project “Trams” [ 构造 欧拉回路 ]

传送门

1614. National Project “Trams”

Time limit: 0.5 second
Memory limit: 64 MB

President has declared the development of tram service a priority national project. As a part of this project, Yekaterinburg will receive enough budget funds to carry out a complete reconstruction of the city‘s tram network.

There are 2n tram stops in Yekaterinburg. In the course of reconstruction, the stops will be left in their places and no new stops will be built, but new tram railways will be laid so that it will be possible to go by tram from every tram stop to any other tram stop without any intermediate stops.

Having studied messages at the tram forum, the city authorities found out that citizens would be satisfied with the reconstruction only if for every pair of tram stops there would be a tram route connecting these stops without any intermediate stops. It is clear that the network of n(2n − 1) routes consisting of only two stops each satisfies this requirement. However, Yekaterinburg Mayor wants exactly n routes to be left after the reconstruction, and each tram must make exactly 2n stops (including the final ones) on its route. Trams must go along these routes in both directions. Suggest a plan of reconstruction that will satisfy both citizens and Mayor.

Input

The only input line contains the integer n, 1 ≤ n ≤ 100.

Output

Output n lines describing tram routes. Each route is a sequence of integers in the range from 1 to 2n separated by a space. A route may go through a stop several times. If the problem has several solutions, you may output any of them. If there is no solution, output one line containing the number −1.

Sample

input output
3
1 6 2 1 3 4
2 3 6 5 4 6
5 1 4 2 5 3

Problem Author: Alexander Ipatov (idea — Magaz Asanov)
Problem Source: The 12th Urals Collegiate Programing Championship, March 29, 2008

Tags: tricky problem  (hide tags for unsolved problems)

Difficulty: 732    Printable version    Submit solution    Discussion (4)
My submissions    All submissions (1013)    All accepted submissions (480)    Solutions rating (382)

题意:

1-2*n个车站,两两之间要连一条边,即  2*n*(2*n-1)/2条边。

现在让你设计n条路线,每个路线包括2*n个车站,即每条路线你能连出2*n-1条边。

求一种满足题意的设计方案。

题解:

一开始试图设计如:

1 2 3 4 5 6

2 4 6 1 3 5

3 6 2 5 1 4

的设计,不过,在n=4的时候就蹦了。

找了好久的规律,设计出如:

1 6 2 5 3 4

2 1 3 6 4 5

3 2 4 1 5 6

的设计,就能满足所有条件了。  (第一次从 1 2 3 4 5 6取首尾首尾,第二次 从 2 3 4 5 6 1 取首尾首尾。。。)

6160974 16:53:31
16 Mar 2015
njczy2010 1614. National Project “Trams” G++ 4.9 Accepted   0.046 414 KB
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <stack>
 4 #include <vector>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <map>
 8 #include <string>
 9
10 #define ll long long
11 int const N = 105;
12 int const M = 205;
13 int const inf = 1000000000;
14 ll const mod = 1000000007;
15
16 using namespace std;
17
18 int n;
19 int ans[N][2*N];
20 int b[N][2*N];
21
22 void ini()
23 {
24     int i,j;
25     int p=2*n;
26     for(i=1;i<=n;i++){
27         for(j=1;j<=2*n;j++){
28             b[i][j]=(i+j-1)%p;
29             if(b[i][j]==0){
30                 b[i][j]=p;
31             }
32         }
33     }
34     /*
35     printf("  b\n");
36     for(i=1;i<=n;i++){
37         printf("%d",b[i][1]);
38         for(j=2;j<=2*n;j++){
39             printf(" %d",b[i][j]);
40         }
41         printf("\n");
42     }*/
43 }
44
45 void solve()
46 {
47     int i,j;
48     for(i=1;i<=n;i++){
49         for(j=1;j<=2*n;j++){
50             ans[i][j]=b[i][j/2+1];
51             j++;
52             ans[i][j]=b[i][2*n-j/2+1];
53         }
54     }
55
56 }
57
58 void out()
59 {
60     int i,j;
61     for(i=1;i<=n;i++){
62         printf("%d",ans[i][1]);
63         for(j=2;j<=2*n;j++){
64             printf(" %d",ans[i][j]);
65         }
66         printf("\n");
67     }
68 }
69
70 int main()
71 {
72    // freopen("data.in","r",stdin);
73     //freopen("data.out","w",stdout);
74     //scanf("%d",&T);
75     //for(cnt=1;cnt<=T;cnt++)
76     while(scanf("%d",&n)!=EOF)
77     {
78         ini();
79         solve();
80         out();
81     }
82 }
时间: 2024-10-03 14:56:12

URAL 1614. National Project “Trams” [ 构造 欧拉回路 ]的相关文章

URAL 1614. National Project “Trams” (图论大YY)

1614. National Project "Trams" Time limit: 0.5 second Memory limit: 64 MB President has declared the development of tram service a priority national project. As a part of this project, Yekaterinburg will receive enough budget funds to carry out

hdu 4850 字符串构造---欧拉回路构造序列 递归+非递归实现

http://acm.hdu.edu.cn/showproblem.php?pid=4850 题意:构造长度为n的字符序列,使得>=4的子串只出现一次 其实最长只能构造出来26^4+4-1= 456979 的序列,大于该数的都是不可能的.构造方法,就是那种欧拉回路的序列,此题DFS会爆栈,手动扩展栈也可以AC...... 递归形式的开始WA了,没有细调就换非递归了,后来又想了想,虽然自己电脑上运行不了,但是先把长度按小的来,然后调试代码,然后在扩大,AC了,当时错在MOD,递归的MOD应该是26

hdu 4850 Wow! Such String! 构造 欧拉回路

Wow! Such String! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 934    Accepted Submission(s): 318 Special Judge Problem Description Recently, doge starts to get interested in a strange probl

Uva10129(欧拉回路)

Play on Words UVA - 10129 Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us. There is a

Google JavaScript规范

前言 此文档为翻译google的标准javascript规范 请与以下原文对照阅读 有异议的地方以原文为准 原文地址google javascript规范 http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml 文中推荐使用的工具是google推荐使用的,请根据实际情况选用 文中定义的很多格式主要是为了考虑js文件编译(js最小化)的问题,如果在项目中考虑使用 js文件编译 请尽量遵守 此文档不仅仅是一份编码风格

The 2014 ACMICPC Asia Invitational Xian

上半年邀请赛的时候真是险而又险地2题拿了个铜,确实其实跟没拿一样......现场前复盘一下,长长记性 [A]签到题 [B]最短路+DFS [C]最短路 [D]构造+欧拉回路 [E]数论,最佳平方逼近 [F]望而却步... [G]望而却步... [H]望而却步... [I] [J]最短路+TSP ------ [A] 看到的第一眼考虑KMP,最后才知道原来签到题直接模拟即可. [B] 第一步完成点对点之间的最短路这点肯定没有问题,后悔当时没有尝试直接暴搜...... [启发] 还是能做的一定努力出

03.Msbuild

MSBuild的深入认识 分类: 专题开发 自动化 2009-01-20 11:56 5711人阅读 评论(1) 收藏 举报 任务引擎脚本工作扩展build 最近在从事自动构造工作的过程中,对MSBuild本身有了一些更加深入的认识.MSBuild不仅仅是一个构造工具,应该称之为拥有相当强大扩展能力的自动化平台. 按照笔者现在的理解,MSBuild平台的主要涉及到三部分:执行引擎.构造工程.任务.其中最核心的就是执行引擎,它包括定义构造工程的规范,解释构造工程,执行“构造动作”:构造工程是用来描

[C4] Andrew Ng - Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization

About this Course This course will teach you the "magic" of getting deep learning to work well. Rather than the deep learning process being a black box, you will understand what drives performance, and be able to more systematically get good res

codeforces 1303 题解(更新中)

codeforces 1303 题解 A. Erasing Zeroes 想让字符串中的 \(1\) 连续,而我们能做的只有删 \(0\) ,则需要删去除开头以及结尾外的 所有 \(0\) 块.所以从头扫一遍统计开头 \(0\) 块,从尾扫一遍统计结尾 \(0\) 块,再用 \(0\) 的数量减去这两部分即可,可能为负所以跟 \(0\) 取最大值. 时间复杂度 \(O(n)\) #include <bits/stdc++.h> using namespace std; typedef long