CF1543E.The Final Pursuit

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Finally, you have defeated Razor and now, you are the Most Wanted street racer. Sergeant Cross has sent the full police force after you in a deadly pursuit. Fortunately, you have found a hiding spot but you fear that Cross and his force will eventually find you. To increase your chances of survival, you want to tune and repaint your BMW M3 GTR.

The car can be imagined as a permuted nn-dimensional hypercube. A simple nn-dimensional hypercube is an undirected unweighted graph built recursively as follows:

  • Take two simple (n−1)(n-1)-dimensional hypercubes one having vertices numbered from 00 to 2n−1−12^{n-1}-1 and the other having vertices numbered from 2n−12^{n-1} to 2n−12^{n}-1. A simple 00-dimensional Hypercube is just a single vertex.
  • Add an edge between the vertices ii and i+2n−1i+2^{n-1} for each 0≤i<2n−10\leq i \lt 2^{n-1}.

A permuted nn-dimensional hypercube is formed by permuting the vertex numbers of a simple nn-dimensional hypercube in any arbitrary manner.

Examples of a simple and permuted 33-dimensional hypercubes are given below:

Note that a permuted nn-dimensional hypercube has the following properties:

  • There are exactly 2n2^n vertices.
  • There are exactly n⋅2n−1n\cdot 2^{n-1} edges.
  • Each vertex is connected to exactly nn other vertices.
  • There are no self-loops or duplicate edges.

Let's denote the permutation used to generate the permuted nn-dimensional hypercube, representing your car, from a simple nn-dimensional hypercube by PP. Before messing up the functionalities of the car, you want to find this permutation so that you can restore the car if anything goes wrong. But the job isn't done yet.

You have nn different colours numbered from 00 to n−1n-1. You want to colour the vertices of this permuted nn-dimensional hypercube in such a way that for each and every vertex uu satisfying 0≤u<2n0\leq u \lt 2^n and for each and every colour cc satisfying 0≤c<n0\leq c \lt n, there is at least one vertex vv adjacent to uu having a colour cc. In other words, from each and every vertex, it must be possible to reach a vertex of any colour by just moving to an adjacent vertex.

Given the permuted nn-dimensional hypercube, find any valid permutation PP and colouring.

最后,你击败了Razor,如今已成为最臭名昭著的街头赛车手。克罗斯警长已派出全部警力对你展开致命追捕。幸运的是,你找到了一处藏身之所,但你担心克罗斯及其部下终将找到你。为了提高生存几率,你打算对你的宝马M3 GTR进行调校与重新喷漆。

这辆汽车可被建模为一个置换后的 nn 维超立方体(permuted nn-dimensional hypercube)。一个标准的 nn 维超立方体(simple nn-dimensional hypercube) 是一个无向无权图,其递归构造方式如下:

  • 取两个标准的 (n−1)(n-1) 维超立方体:其中一个的顶点编号为 00 到 2n−1−12^{n-1}-1,另一个的顶点编号为 2n−12^{n-1} 到 2n−12^{n}-1;而一个标准的 00 维超立方体仅包含单个顶点。
  • 对每个满足 0≤i<2n−10\leq i < 2^{n-1} 的 ii,在顶点 ii 与顶点 i+2n−1i+2^{n-1} 之间添加一条边。

一个置换后的 nn 维超立方体,则是通过对标准 nn 维超立方体的所有顶点编号施加任意一个置换(permutation)得到的。

下面给出了一个标准的和一个置换后的 33 维超立方体的示例:

注意:一个置换后的 nn 维超立方体具有以下性质:

  • 恰好含有 2n2^n 个顶点;
  • 恰好含有 n⋅2n−1n\cdot 2^{n-1} 条边;
  • 每个顶点恰好与 nn 个其他顶点相邻;
  • 图中不含自环(self-loops)或重边(duplicate edges)。

记将标准 nn 维超立方体变换为表示你座驾的该置换后 nn 维超立方体所用的顶点置换为 PP。在贸然改动车辆功能之前,你希望先找出这一置换 PP,以便万一发生故障时能及时恢复车辆原状。但任务尚未完成。

你手头有 nn 种不同的颜色,编号为 00 至 n−1n-1。你需要对这个置换后的 nn 维超立方体的顶点进行染色,使得:对每个顶点 uu(满足 0≤u<2n0\leq u < 2^n)以及每种颜色 cc(满足 0≤c<n0\leq c < n),均存在至少一个与 uu 相邻的顶点 vv,其颜色恰为 cc。换言之,从任意顶点出发,仅通过移动至一个相邻顶点,就必须能够到达任意一种颜色的顶点。

给定该置换后的 nn 维超立方体,请找出任意一组有效的置换 PP 和顶点染色方案。

输入格式

The first line of input contains a single integer tt (1≤t≤40961\leq t\leq 4096) — the number of test cases.

For each test case, the first line contains a single integer nn (1≤n≤161\leq n\leq 16).

Each of the next n⋅2n−1n\cdot 2^{n-1} lines contain two integers uu and vv (0≤u,v<2n0\leq u, v \lt 2^n) denoting that there is an edge between the vertices numbered uu and vv.

It is guaranteed that the graph described in the input is a permuted nn-dimensional hypercube.

Additionally, it is guaranteed that the sum of 2n2^n over all test cases does not exceed 216=65 5362^{16}=65\,536.

输入的第一行包含一个整数 tt(1≤t≤40961\leq t\leq 4096),表示测试用例的数量。

对于每个测试用例,第一行包含一个整数 nn(1≤n≤161\leq n\leq 16)。

接下来的 n⋅2n−1n\cdot 2^{n-1} 行中,每行包含两个整数 uu 和 vv(0≤u,v<2n0\leq u, v \lt 2^n),表示编号为 uu 和 vv 的顶点之间存在一条边。

保证输入所描述的图是一个经过顶点重标号(permutation)的 nn 维超立方体(hypercube)。

此外,保证所有测试用例中 2n2^n 的总和不超过 216=65 5362^{16}=65\,536。

输出格式

For each test case, print two lines.

In the first line, output any permutation PP of length 2n2^n that can be used to transform a simple nn-dimensional hypercube to the permuted nn-dimensional hypercube given in the input. Two permuted hypercubes are considered the same if they have the same set of edges. If there are multiple answers, output any of them.

In the second line, print the colouring. If there is no way to colour the vertices satisfying the conditions, output −1-1. Otherwise, output a single line containing 2n2^n space separated integers. The ii-th integer must be the colour of the vertex numbered (i−1)(i-1) in the permuted nn-dimensional hypercube. If there are multiple answers, output any of them.

对每个测试用例,输出两行。

第一行,输出任意一个长度为 2n2^n 的排列 PP,该排列可用于将一个简单的 nn 维超立方体变换为输入中给定的置换后的 nn 维超立方体。若两个置换后的超立方体具有相同的边集,则认为它们是相同的。若存在多个答案,输出其中任意一个即可。

第二行,输出顶点的着色方案。若不存在满足条件的顶点着色方案,则输出 −1-1;否则,输出一行包含 2n2^n 个以空格分隔的整数。其中第 ii 个整数表示在置换后的 nn 维超立方体中编号为 (i−1)(i-1) 的顶点的颜色。若存在多个答案,输出其中任意一个即可。

输入输出样例

  • 输入#1

    3
    1
    0 1
    2
    0 1
    1 2
    2 3
    3 0
    3
    0 1
    0 5
    0 7
    1 2
    1 4
    2 5
    2 6
    3 5
    3 6
    3 7
    4 6
    4 7

    输出#1

    0 1
    0 0
    0 1 3 2
    0 0 1 1
    5 3 0 7 2 6 1 4
    -1

说明/提示

The colouring and the permuted hypercube for the first test case is shown below:

The colouring and the permuted hypercube for the second test case is shown below:

The permuted hypercube for the third test case is given in the problem statement. However, it can be shown that there exists no way to colour that cube satifying all the conditions. Note that some other permutations like [0,5,7,3,1,2,4,6][0, 5, 7, 3, 1, 2, 4, 6] and [0,1,5,2,7,4,3,6][0, 1, 5, 2, 7, 4, 3, 6] will also give the same permuted hypercube.

第一个测试用例的着色方案及置换超立方体如下所示:

第二个测试用例的着色方案及置换超立方体如下所示:

第三个测试用例的置换超立方体已在题目陈述中给出。然而,可以证明:不存在满足所有条件的对该立方体的着色方案。注意,其他一些置换(例如 [0,5,7,3,1,2,4,6][0, 5, 7, 3, 1, 2, 4, 6] 和 [0,1,5,2,7,4,3,6][0, 1, 5, 2, 7, 4, 3, 6])也会生成相同的置换超立方体。

输入解题思路,AI测评打分。不知道怎么写?

首页