CF269E.String Theory

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Emuskald is an innovative musician and always tries to push the boundaries of music production. Now he has come up with an idea for a revolutionary musical instrument — a rectangular harp.

A rectangular harp is a rectangle n × m consisting of n rows and m columns. The rows are numbered 1 to n from top to bottom. Similarly the columns are numbered 1 to m from left to right. String pins are spaced evenly across every side, one per unit. Thus there are n pins on the left and right sides of the harp and m pins on its top and bottom. The harp has exactly n + m different strings, each string connecting two different pins, each on a different side of the harp.

Emuskald has ordered his apprentice to construct the first ever rectangular harp. However, he didn't mention that no two strings can cross, otherwise it would be impossible to play the harp. Two strings cross if the segments connecting their pins intersect. To fix the harp, Emuskald can perform operations of two types:

  1. pick two different columns and swap their pins on each side of the harp, not changing the pins that connect each string;
  2. pick two different rows and swap their pins on each side of the harp, not changing the pins that connect each string;

In the following example, he can fix the harp by swapping two columns:

Help Emuskald complete his creation and find the permutations how the rows and columns of the harp need to be rearranged, or tell that it is impossible to do so. He can detach and reattach each string to its pins, so the physical layout of the strings doesn't matter.

埃穆斯卡尔德是一位富有创新精神的音乐家,总是致力于突破音乐制作的边界。如今,他提出了一个革命性乐器的构想——矩形竖琴。

矩形竖琴是一个 n×mn \times m 的矩形,包含 nn 行和 mm 列。行从上到下编号为 11 至 nn;列从左到右编号为 11 至 mm。弦钉均匀分布在矩形竖琴的每一条边上,每单位长度一个钉。因此,竖琴的左侧和右侧各有 nn 个弦钉,顶部和底部各有 mm 个弦钉。该竖琴恰好有 n+mn + m 根互不相同的弦,每根弦连接两个位于竖琴不同边上的弦钉。

埃穆斯卡尔德已吩咐他的学徒建造史上第一架矩形竖琴。然而,他并未说明:任意两根弦均不可相交,否则竖琴将无法演奏。当连接两对弦钉的线段相交时,即称这两根弦相交。为修复竖琴,埃穆斯卡尔德可执行以下两类操作:

  1. 选取两列,并交换它们在竖琴每一边上的弦钉位置(不改变每根弦所连接的弦钉);
  2. 选取两行,并交换它们在竖琴每一边上的弦钉位置(不改变每根弦所连接的弦钉);

如下例所示,他可通过交换两列来修复竖琴:

请帮助埃穆斯卡尔德完成他的创作:找出需对竖琴的行与列进行的排列(即重排方式),或判定该问题无解。注意:他可以将每根弦从其弦钉上拆下并重新连接,因此弦的物理布局并不重要。

输入格式

The first line of input contains two space-separated integers numbers n and m (1 ≤ n, m ≤ 105), the height and width of the harp in units. Each of the following n + m lines contains 4 space-separated tokens, describing a single string: two symbols a__i, b__i and two integer numbers p__i, q__i. The pair a__i, p__i describes the first pin, and the pair b__i, q__i describes the second pin of the string;

A pair s, x describes the position of a single pin in a following way:

  1. s is equal to one of the symbols "L", "T", "R" or "B" (without quotes), which means that the pin is positioned on the left, top, right or bottom side of the harp accordingly;
  2. x is equal to the number of the row, if the pin is on the left or right border of the harp, and to the number of the column, if the pin is on the top or bottom border of the harp.

It is guaranteed that no two different strings are connected to the same pin.

输入的第一行包含两个以空格分隔的整数 nn 和 mm(1≤n,m≤1051 \leq n, m \leq 10^5),分别表示竖琴的高度和宽度(单位)。

接下来的 n+mn + m 行,每行包含四个以空格分隔的标记,描述一根琴弦:两个符号 aia_i、bib_i 和两个整数 pip_i、qiq_i。其中,对 (ai,pi)(a_i, p_i) 描述该琴弦的第一个琴钉,对 (bi,qi)(b_i, q_i) 描述该琴弦的第二个琴钉。

对 (s,x)(s, x) 描述一个琴钉的位置,方式如下:

  1. ss 等于符号 "L"、"T"、"R" 或 "B"(不含引号)之一,分别表示该琴钉位于竖琴的左侧、顶部、右侧或底部边界上;
  2. 若琴钉位于竖琴的左边界或右边界上,则 xx 表示其所在行号;若琴钉位于竖琴的上边界或下边界上,则 xx 表示其所在列号。

保证不存在两根不同的琴弦连接到同一个琴钉。

输出格式

If it is possible to rearrange the rows and columns to fix the harp, on the first line output n space-separated integers — the old numbers of rows now placed from top to bottom in the fixed harp. On the second line, output m space-separated integers — the old numbers of columns now placed from left to right in the fixed harp.

If it is impossible to rearrange the rows and columns to fix the harp, output "No solution" (without quotes).

如果可以通过重新排列行和列来修复竖琴,则在第一行输出 n 个以空格分隔的整数——这些整数表示修复后的竖琴中从上到下排列的各行的原始编号。在第二行输出 m 个以空格分隔的整数——这些整数表示修复后的竖琴中从左到右排列的各列的原始编号。

如果无法通过重新排列行和列来修复竖琴,则输出 "No solution"(不带引号)。

输入输出样例

  • 输入#1

    3 4
    L T 1 3
    L B 2 2
    L B 3 3
    T R 1 2
    T B 2 1
    T R 4 1
    B R 4 3

    输出#1

    1 2 3 
    3 2 1 4
  • 输入#2

    3 3
    L T 1 1
    T R 3 1
    R B 3 3
    B L 1 3
    L R 2 2
    T B 2 2

    输出#2

    No solution

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

首页