1 import java
.io
.BufferedReader
;
2 import java
.io
.FileReader
;
3 import java
.io
.PrintWriter
;
4 import java
.io
.IOException
;
5 import java
.util
.StringTokenizer
;
7 public class exclusive_petr
implements Runnable
{
8 private void solve() throws IOException
{
10 int m
= 'Z' - 'L' + 1;
11 boolean[][] e
= new boolean[m
][m
];
12 int[][] edges
= new int[n
][];
13 for (int i
= 0; i
< n
; ++i
) {
14 int a
= nextToken().charAt(0) - 'L';
15 int b
= nextToken().charAt(0) - 'L';
16 edges
[i
] = new int[]{a
, b
};
20 boolean[] sane
= new boolean[1 << m
];
21 for (int i
= 0; i
< sane
.length
; ++i
) {
23 for (int j
= 0; j
< m
; ++j
)
24 if (((i
>> j
) & 1) != 0)
25 for (int k
= j
+ 1; k
< m
; ++k
)
26 if (((i
>> k
) & 1) != 0)
30 int[] cnt
= new int[1 << m
];
31 int[] prev
= new int[1 << m
];
33 for (int i
= 1; i
< cnt
.length
; ++i
) {
34 cnt
[i
] = Integer
.MAX_VALUE
;
36 for (int j
= i
; j
> 0; j
= (j
- 1) & i
) {
38 if (cnt
[i ^ j
] + 1 < cnt
[i
]) {
39 cnt
[i
] = cnt
[i ^ j
] + 1;
45 int[] step
= new int[m
];
46 int cur
= cnt
.length
- 1;
49 for (int j
= 0; j
< m
; ++j
)
50 if (((set
>> j
) & 1) != 0)
54 writer
.println(cnt
[cnt
.length
- 1] - 2);
55 for (int i
= 0; i
< n
; ++i
)
56 if (step
[edges
[i
][0]] < step
[edges
[i
][1]]) {
57 writer
.println((char) ('L' + edges
[i
][0]) + " " + (char) ('L' + edges
[i
][1]));
59 writer
.println((char) ('L' + edges
[i
][1]) + " " + (char) ('L' + edges
[i
][0]));
64 public static void main(String
[] args
) throws InterruptedException
{
65 Thread thread
= new Thread(new exclusive_petr());
70 static final String TASK_ID
= "exclusive";
71 static final String IN_FILE
= TASK_ID
+ ".in";
72 static final String OUT_FILE
= TASK_ID
+ ".out";
73 BufferedReader reader
;
74 StringTokenizer tokenizer
;
79 reader
= new BufferedReader(new FileReader(IN_FILE
));
81 writer
= new PrintWriter(OUT_FILE
);
85 } catch (Exception e
) {
91 int nextInt() throws IOException
{
92 return Integer
.parseInt(nextToken());
95 long nextLong() throws IOException
{
96 return Long
.parseLong(nextToken());
99 double nextDouble() throws IOException
{
100 return Double
.parseDouble(nextToken());
103 String
nextToken() throws IOException
{
104 while (tokenizer
== null || !tokenizer
.hasMoreTokens()) {
105 tokenizer
= new StringTokenizer(reader
.readLine());
107 return tokenizer
.nextToken();