#include<stdio.h>
#include<iostream>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<functional>
#include<string>
#include<algorithm>
#include<time.h>
#include<bitset>
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
typedef int Int;
template <class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template <class T> inline void gmin(T &a,T b){if(b<a)a=b;}
using namespace std;
const int N=205,M=0,L=0,Z=1e9+7,maxint=2147483647,ms31=522133279,ms63=1061109567,ms127=2139062143;
int n,m;
int a[N];
void fre()
{
freopen("embedding.in","r",stdin);
freopen("embedding.out","w",stdout);
}
void print()
{
for(int i=1;i<m;i++)printf("%d ",a[i]);
printf("%d\n",a[m]);
}
int main()
{
fre();
while(scanf("%d",&n),n)
{
if(n==3)
{
puts("1 2 3 4 5 6");
puts("4 5 6 1 3 2");
puts("5 1 4 6 3 2");
}
else if(n==4)
{
puts("1 2 3 4 5 6 7 8");
puts("5 6 7 8 1 3 4 2");
puts("6 1 3 5 7 4 8 2");
}
else if(n==5)
{
puts("1 2 3 4 5 6 7 8 9 10 ");
puts("6 7 8 9 10 1 3 4 5 2");
puts("9 1 7 10 6 8 3 5 4 2");
}
else
{
m=n<<1;
//第一张图
for(int i=1;i<=n;i++)a[i]=i;
a[n+1]=n+1;
for(int i=2;i<n;i++)a[n+i]=a[i]+n+1;
a[n+n]=n+2;
print();
//第二张图
for(int i=1;i<=n;i++)a[i]=n+i;
a[n+1]=1;
for(int i=2;i<n;i++)a[n+i]=a[i]-n+1;
a[n+n]=2;
print();
//第三张图
a[1]=1;a[n+1]=n+1;
int id=n+2;
for(int i=2;i<=n;i++)
{
a[i]=id;
a[n+i]=a[i]-n;
id+=2;
if(id>n+n)id=n+3;
}
print();
}
}
return 0;
}
/*
【题意】
我们定义毛毛虫图为2n个节点的图为类似于下面形状的图:
n=3时:
5 6
| |
1-2-3-4
n=4时:
5 7 8
| | |
1-2-3-4-5
n=5时:
7 8 9 10
| | | |
1-2-3-4-5-6
现在给你一个n(3<=n<=100),让你输出3个嵌套在2n点完全图内的毛毛虫图,
使得2n点完全图内的每条边都最多被一个毛毛虫图所覆盖。
输出规则:
输出三行,每行2n个数。数的先后顺序与毛毛虫图的编号相对应。
【类型】
构造
【分析】
做构造题。一般有两种思路:
1,所有数据都通用的构造法。
2,适用于一定数据的构造法。
对于1,看起来更优美。但是构造难度更大。
对于2,构造起来比较容易,但是容易出现问题。一定要保证精确覆盖所有情况
我们可以对于n=3,4,5特判
然后在n=6开始。
本来是这样的对应关系:
n+2 n+3 n+4 n+5 ... n+n
1 2 3 4 5 ... n n+1
2~n分别与n+2~n+n做对应。
我们可以左平移,右平移,或者不平移。
这样由2到n,再由n+2到n+n,都实现了3次边匹配。
至于下面一行的链接方式,
第一圈可以是1,2,3,...,n,n+1
第二圈可以是n+1,n+2,n+3,...n+n,1。
第三圈可以是传统的奇偶交替方式:
两边依然保持为1 n
中间则是2 4 6 3 5 7,或者2 4 6 3 5这两种。
于是我们发现n至少为6,因为哪怕是5,也会产生2 4 3 5这样的重边。
于是3,4,5特判,5以上构造,这题就可以AC啦!
【时间复杂度&&优化】
O(n)
【trick】
这道题我的特判n==5把自己坑掉了。还查错好久都没有找到错。
蠢蠢蠢!
构造题一定要注意边界条的件特判。
【数据】
puts("1 2 3 4 5 6 7 8 9 10 ");
puts("6 7 8 9 10 1 3 4 5 2");
puts("9 1 7 10 6 8 3 5 4 2");
7 8 9 10
1 2 3 4 5 6
3 4 5 2
6 7 8 9 10 1
3 5 4 2
9 1 7 10 6 8
*/
【ASC44E】【构造】Embedding Caterpillars 毛毛虫图向完全图的映射
最新推荐文章于 2020-02-11 09:23:16 发布