Document directory
Timus 1080. Map colouring discusses the problem of map coloring.
1080. Map colouringtime limit: 1.0 second
Memory limit: 16 mbwe consider a geographical map
NCountries numbered from 1
N(0 <
N<99 ). for every country we know the numbers of other countries which are connected with its border. from every country we can reach to any other one, eventually crossing some borders. write a program which determines whether it is possible to color the map only in two colours-red and blue in such a way that if two countries are connected their colours are different. the color of the first country is red. your program must output one possible coloring for the other countries, or show, that such coloring is impossible. inputon the first line is written the number
N. On the following
NLines,
I-Th line contains the countries to which
I-Th country is connected. Every integer on this line is bigger
I, Counter t the last one which is 0 and marks that no more countries are listed for country
I. If a line contains 0, that means that
I-Th country is not connected to any other country, which number is larger
I. Outputthe output contains exactly one line. If the coloring is possible, this lines must contain a list of zeros and ones, without any separators between them.
I-Th digit in this sequence is the color of
I-Th country. 0 corresponds to red color, and one-to blue color. If a coloring is not possible, output the integer −1. Sample
Input |
Output |
3 2 0 3 0 0 |
010 |
Problem Author:Emil kelevedzhiev
Problem Source:Winter mathematical festival Varna '2001 ICS tournament
This question shows n countries on the map. This map has only one continent and no islands. It is required to write a program that colors all countries on the map in red and blue, and the neighboring countries in different colors.
The first line of the given input is the number n of countries on the map. Followed by N rows, each row indicates the number list of neighboring countries in country I. This list only lists countries with numbers greater than I and ends with 0.
The program is required to color each country in sequence. 0 indicates red, 1 indicates blue, and the first country is red. If no suitable coloring scheme is available, output-1.
1 using system;
2 using system. IO;
3 using system. Collections. Generic;
4
5 namespace skyiv. Ben. timus
6 {
7 // http://acm.timus.ru/problem.aspx? Space = 1 & num = 1080
8 sealed class t1080
9 {
10 static void main ()
11 {
12 dictionary <int, Object> [] map = read (console. In );
13 bool? [] Color = new bool? [Map. Length];
14 color [0] = false;
15 if (! Search (MAP, color, 0) console. Write (-1 );
16 else foreach (bool C in color) console. Write (C? 1: 0 );
17}
18
19 static dictionary <int, Object> [] Read (textreader reader)
20 {
21 dictionary <int, Object> [] map = new dictionary <int, Object> [Int. parse (reader. Readline ()];
22 For (INT I = 0; I <map. length; I ++) map [I] = new dictionary <int, Object> ();
23 For (INT I = 0; I <map. length; I ++)
24 {
25 string [] Ss = reader. Readline (). Split ();
26 For (int K = 0; k <ss. Length-1; k ++)
27 {
28 Int J = int. parse (ss [k])-1;
29 map [I] [J] = map [J] [I] = NULL;
30}
31}
32 return map;
33}
34
35 static bool search (Dictionary <int, Object> [] map, bool? [] Color, int K)
36 {
37 foreach (int v in map [K]. Keys)
38 If (color [v] = NULL)
39 {
40 color [v] =! Color [k];
41 if (! Search (MAP, color, V) return false;
42}
43 else if (color [v] = color [k]) return false;
44 return true;
45}
46}
47}
In the above program, lines 10th to 17 are the main method. First, call the read method to read the input to the map array (lines 12th), and then assign the color array color (lines 13th ), the data type of this array is a Boolean value that can be empty. null indicates that the country has not been colored, false indicates red, and true indicates blue. Then the first country is marked in red (14th rows ). In row 3, the search method is called to find the coloring scheme. If no suitable coloring scheme is found, the result is-1. Otherwise, the color of each country is output in sequence (Row 3 ).
Read the input using the read method from rows 1 to 33. Row 21st reads the number of countries and assigns a map array. The data type of the array is dictionary <int, Object>, and its index key is the number of all neighboring countries. The input is read cyclically at the beginning of Line 1. Note that all neighboring countries are set in row 29th, instead of only neighboring countries with a larger number.
The search method of rows 35th to 45 uses the deep priority search algorithm to find the coloring scheme. The parameter k indicates the country number. The loop starting from line 1 traverses all neighboring countries in the K countries. Row 3 determines that if the neighboring country has not been colored, another color (Row 3) will be taken, and the number V of the neighboring country will be used to recursively call itself to continue searching (Row 3 ). Row 3 determines that if the neighboring country already has the same color, the search will fail. Otherwise, the search continues until all neighboring countries are traversed.
The famous four-color theorem means that only four colors can be used to color the above map. This is also the first theorem mainly proved by computers. If some modifications are made to the preceding procedure, the four-color theorem can be used for verification (not proof.
Returned directory