You are on page 1of 21

2005 ACM Regional Programming Contest Asia Taipei

2005/10/30
There are 9 problems, 21 pages, in this problem set. The team who solves the most number of problems wins the contest. If more than one team solve the same number of problems, then the team who takes less time to solve the problems wins. Each submitted program will be judged to be either correct or incorrect. A correct program must solve all the test cases of the problem correctly. The time to solve a problem is measured from the beginning of the contest to the time the submission of the program which solves the problem correctly, plus a penalty of 20 minutes for each incorrect submission for the problem. There are no penalties for the submitions of the problems which cannot be solved correctly at the end of the contest. Sample input data and the output for the sample input are provided in each problem. You may create these sample input data to test your program. However, test cases used by the Judges may not be the same as the sample input. It is not necessary to include comments in your programs. Any algorithm can be used to solve the problems. However, a program whose running time exceeds 30 seconds will be considered to be incorrect. Each input le may contain more than one test case. The description of the input format is based on records. A record usually contains a list of data. Unless stated otherwise, these data may span more than one line. The number of characters in a line is no more than 255. A special record, usually one or more 0s, indicates the end of an input data. Anything after this special record should be ignored. The name of the input le for problem i is pi.in. Error checking of the input data is not required. You may assume that the input data are always correct. The output le must be the standard output, unless it is specied in the problem.

Problem 1 Numbering Cities


Input le: p1.in Scientists and engineers have found a way for human to settle in a far-away planet. The new immigrants have started building small towns and communication links connecting nearby towns. In building the communication network, they rst chose a town as a center. Then they connected each nearby town to the center by a communication link. As more and more people migrate to the new planet, a number of such networks have been built. These networks need to be connected to make it possible for people in every town in the planet to communicate. They connected these networks by connecting the centers using a simple path. The rst group of immigrants are mostly scientists and engineers. They prefer numbering their towns rather than naming them. Consider n towns connected by a network with only one center. If we number the center 0 and the nearby towns by integers in {1, 2, . . . , n 1}. There is an interesting property of this numbering method. Suppose that we label of each link by the absolute value of the dierence of the numbers of the two towns it connects. All links will get a distinct label. Furthermore, the labels of the links are exactly {1, 2, . . . , n 1}. Thus, links are numbered automatically. This is called perfect numbering of the towns. The immigrants love the numbering method very much, and wants to know if the numbering can be extended to the networks with many centers. Therefore, you are invited to write a program to solve the problem. Let us study more about perfect numbering of the towns. Assume that all towns are properly numbered. It is easy to see that if the numbers of any two towns connected to the same center are switched, then the new numbering is also perfect. Therefore, perfect numbering of the towns is not unique. It is convenient to assign a name to each town. Assume that there are at most 26 centers, and they are labeled by a0, b0 . . . , z0. The towns connected to center a0 will be called a1, a2, . . ., the towns connected to center b0 will be called b1, b2, . . ., and so on. To make the problem simple, we want a perfect numbering such that the numbers assign to the towns connected to the same center are ordered by their names. For example, ais label will be smaller than ajs label whenever i < j. Finally, it is required to label a0 with 0 whenever possible.

Input Format
The input le contains several test cases. Each test case is described in 2 lines. The rst line is an integer n, which is the number of centers. The second line contains n integers. Each integer indicates the number of nearby towns connected to the corresponding center. For example, 4 3 2 2 4 means that there are 4 centers a0, b0, c0, and d0. The center a0 connects 3 nearby towns, the center b0 connects 2 nearby towns, etc. Assume that each center can connect at most 60 towns. The last test case is followed by a 0, which signals the end of input le.

Output Format
For each test case print out the labels of each town. The output should be ordered by labels, not by the names of the towns. Print 8 (town, label) pairs in a line, except the last line. Print 1 blank line in front of each test case. If there are no solutions, print no solutions.

Sample Input
4 3 2 2 4 5 0 0 0 0 0 0

Output for the Sample Input


(a0, 0) (b1, 1) (b2, 2) (c0, 3) (d1, 4) (d2, 5) (d3, 6) (d4, 7) (d0, 8) (c1, 9) (c2, 10) (b0, 11) (a1, 12) (a2, 13) (a3, 14) (a0, 0) (c0, 1) (e0, 2) (d0, 3) (b0, 4)

Problem 2 Perfect Domination on Trees


Input le: p2.in The concept of domination in graph theory arises naturally from the facility location problem in operations research. Consider a geographical area that is partitioned into regions. Facilities are going to be placed in some of the regions. The problem is to choose a minimum number of regions at which to place these service facilities, so that each region is served by a facility in it or at least one facility adjacent to it when there is no facility in this region. This location problem can be modeled by a graph problem to nd a minimum-cardinality vertex subset of an input graph such that every vertex not in the subset is adjacent to at least one vertex in the subset. Depending on dierent requirements in various location problems, domination has many variants. Here we consider one of the variants. A perfect dominating set of a graph G = (V, E) is a subset D V such that every vertex v in V D (that is, v V and v D) is adjacent to exactly one vertex in D. The perfect / domination problem is to nd a minimum-cardinality perfect dominating set D of G, i.e., |D| is smallest among the cardinalities of all perfect dominating sets of G. The perfect domination number of a graph G, denoted by perfect(G), is the cardinality of a minimum-cardinality perfect dominating set of G. In this problem, you need to write a program to nd the perfect domination number of an input graph which is a tree. For example, consider a 6-vertex tree T shown in Figure 1. In (a), vertices 3 and 5 do not form a perfect dominating set of T , because vertex 6 is not in D = {3, 5} and it is not adjacent to any vertex in D. Furthermore, vertices 4 is adjacent to both vertex 3 and 5. In (b), vertices 3 and 4 form a perfect dominating set. It is also easy to check that {3, 4} is a minimum-cardinality perfect dominating set of T . Therefore, the perfect domination number of this example equals 2.

Figure 1: An illustration of a none perfect dominating set and a perfect dominating set In this problem, the input graph is a tree T with n vertices {1, 2, . . . , n}, where 1 n 1000. Write a program to compute the perfect domination number of T .

Input Format
The input consists of a number of test cases. Each test case consists of one tree, which has the following format: The rst line contains one positive integer m = n 1, which is the number of edges of the tree. The next m lines contain m edges such that one line contains one edge. Each edge is represented by two positive integers; the rst integer represents one end-vertex of the edge and the second one represents the other end-vertex. Finally, a 0 at the last line indicates the end of the input le.

Output Format
The output contains one line for each test case. Each line contains an integer, which is the perfect domination number of the corresponding input tree.

Sample Input
5 1 2 3 4 4 1 1 0 3 3 4 5 6 2

Output for the Sample Input


2 1

Problem 3 Dome of Tuxville


Input le: p3.in It has been many years since the MacroSolved Cybercomputer has taken control of earth and enslaved most of the humanity to power its operation through some biological process with an enormous matrix of cells each containing a paralyzed human prison. Now, even the city of Tuxville, the only know remaining habitat where humans can walk freely, is constantly raided and bombarded by eets of giant ying robotic bugs sent by MacroSolved. The engineers in Tuxville has recently developed a new technology called shield projector for protecting the city. The projector is placed on the ground and is fed enough energy so that it creates a semi-sphere dome of radius R, within which no robotic bugs can enter. Of course the required energy is a strictly increasing function of R. The city council has requested the engineers to nd the best spot to place the shield projector, so that it requires the minimal amount of energy while generating a dome of shield large enough to protect every building in the city. As a junior member of the engineering team, you are to compute the desired position (the x-y coordinates) for the projector given the list of positions and heights of all buildings in Tuxville.

Input Format
The input le consists of several test cases. Each test case begins with an integer N, 1 N 30, the number of buildings in Tuxville. Each of the next N lines contains 3 real numbers x, y, and h which gives the position and the height of one building. The last test case is followed by a line consisting of a single 0. Every real number t in the input le has at most 3 digits after the decimal point and 999.999 t 999.999. Note that there may be two or more buildings of equal or dierent heights occupying the same position, such as buildings 3 and 5 in the second test case. There may also be three or more buildings lying on the same straight line as exemplied by buildings 1, 2, and 4 in the second test case.

Output Format
Print the x-y coordinates of the desired position for each test case on one line. Round all numbers to 3 decimal places, printing the trailing 0s after the decimal place as necessary.

Sample Input
4 2.0 1.0 1.0 -2.0 -0.2 1.3 -2.0 1.0 5 1.0 8.0 8.0 -4.0 -4.0 1.0 4.5 2.0 -4.0 1.0 0 0.2 0.3 7.9 0.1 4.0 1.0 8.0 2.0 3.0

Output for the Sample Input


-0.200 0.000 1.300 0.000

Problem 4 Forest Trek


Input le: p4.in Captain Picard and the Enterprise crew is under a mission to study an interesting culture in a distant planet called Baku. They decide to build an invisible observation station where they can watch and record how Baku people live, without revealing the crews presence or in any way disturbing the Baku people. The Baku people live on a continent almost fully covered by the trees and completely surrounded by the ocean. There are populated villages, connected by trails. The trail system covers the entire continent and divides the continent into many forests. It is one of these forests that Picard and his crew will choose to build the station in. According to past experience and some correlation study, the level of exploration by the inhabitants is directly proportional to the collective population of the surrounding villages, and inversely proportional to the area of the forest. Please help Picard nd the forest which is least explored by the Baku inhabitants.

Input Format
The input le consists of several test cases. Each test case begins with a integer V , 3 V 100, the number of villages on the continent. Each of the next V lines contains 4 entries separated by spacesthe village name, the village population, and its position in x-y coordinates. The next line contains an integer E, 3 E 4950, the number of trails connecting villages. Each of the next E lines contains 2 village names separated by spaces. The last test case is followed by a line consisting of a single 0. Every real number t in the input le has at most 3 digits after the decimal point and 999.999 t 999.999. The names of the villages are in all capitals, and the length of a name is between 1 and 8, inclusive. The villages and the trails do not appear in any particular order in the input le. A trail between two villages, say ALF and BET, will appear only once, either as ALF BET or as BET ALF. Trails do not meet each other except at the villages. Every village is directly or indirectly connected to every other village. In computing the area of the forests, the trails are considered as straight line segments, and neither trails nor villages occupy signicant areas. Assume that the outer rim of the trail system is built along the coast line.

Output Format
For each test case, print on one line the smallest population density rounded to 3 decimal places, along with the desired forest denoted by its surrounding villages. For the population, print the trailing 0s after the decimal place as necessary. For the village names, print them in counter-clockwise order. Suppose the desired forest is surrounded by k villages. Of all k possible output sequences, print the one beginning with the alphabetically rst village. If there are more than one such sequences, print the one whose second city has the smallest alphabetical ordering. Thus we choose ALF BET ... over ALF EPS ... in the sample output.
3

500:ALF

400:BET

500:CALM

400:DEL

600:EPS

500:FIE

500:GO

400:HUA

-1

-2

500:ING

-3

-4

-3

-2

-1

In the sample input there are three forests. We nd their surrounding populations, areas, and densities to be the following, and therefore output information describing the second forest. Note that each of ALF and BET only contributes once to the collective population of the second forest. population area density 2000 2.000 1000.000 3400 5.500 618.182 1400 0.500 2800.000

Sample Input
9 ALF BET CALM DEL EPS FIE GO HUA ING 11 DEL ING ING ING ALF ALF ALF ALF BET BET FIE 0 500 400 500 400 600 500 500 400 500 CALM DEL HUA EPS DEL HUA EPS BET FIE GO GO 0.000 2.000 0.000 1.000 -3.000 0.000 -2.000 0.000 -1.000 0.000 0.000 0.000 1.000 0.000 2.000 0.000 0.000 -2.000

Output for the Sample Input


618.182 ALF BET GO FIE BET ALF EPS ING HUA

10

Problem 5 Real-Time Disk Scheduling


Input le: p5.in A magnetic disk is made up of the rotating disks, the heads to access disks, and an assembly arm that moves the heads into the specied cylinder. Generally, a disk task would specify the track location of data we want to retrieve. When serving the disk task, a seek-time is taken to move the disk arm to the appropriate cylinder. The seek-time to move the disk arm from track location x to track location y is b|x y|. Here b is a given constant, representing the speed of the disk, |x y| is the distance (absolute value) between x and y. Conventionally, the goal of disk scheduling is to maximize the disk-throughput for serving a given set of tasks. That is, all tasks must be scheduled and executed as soon as possible to minimize the execution time. However, in a time-critical application, the rst goal of disk scheduling is to meet the specied real-time constraints. That is, all tasks must be nished before its deadlines. Maximizing the disk throughput is also important, but the schedule is meaningless if the specied real-time constraints are not satised. Therefore, the formal denition of the real-time disk scheduling is as follows: Given a set of real-time disk tasks T = {T1 , T2 , . . . , Tn }, where n is the number of input disk tasks. Each task has a deadline and a track location. We say that a schedule is feasible if all tasks meet their deadlines in the schedule. Our goal is to nd a feasible schedule with minimum execution time. For example, assume that we have four tasks, {T1 , T2 , T3 , T4 } whose deadlines are {5, 10, 12, 24} and track locations at {100, 40, 600, 70}. Assume that the current position of disk head is 20 and b = 0.01. The optimal solution is (2, 4, 1, 3). That is, the tasks are scheduled in the order: T2 , T4 , T1 , T3 . The minimum execution time for the schedule of this four tasks are 0.01(40 20) + 0.01(70 40) + 0.01(100 70) + 0.01(600 100) = 5.8. In this schedule, all tasks are scheduled before their deadlines. Note that the disk head can be at any position before the schedule starts, and can be at any position when the schedule ends.

11

Input Format
The input le may contain several test cases. Each test case consists of a set of real-time disk tasks. The rst line of test case is a single integer n, representing the number of tasks. The second line of the test case has two numbers. The rst number is a real number, representing the disk speed b. The second number is an integer p, representing the current position of disk head. Starting from the third line, each line species a task. There are two integers in each line. The rst integer is the track location of the task. The second integer is the deadline of the task. All integers have the maximum value 1000000. The last test case is followed by a line containing 0. This signals the end of the input le.

Output Format
The output is a number t, the minimum execution time of all tasks. If no feasible schedules exist, output -1.

Sample Input
4 0.01 20 100 5 40 10 600 12 70 24 0

Output for the Sample Input


5.8

12

Problem 6 Verifying Experimental Data


Input le: p6.in Professor Abscentmind collects n, 1 n 1000, rocks during a eld trip. His mission is to analyze the relations between the weights of the rocks. Unfortunately, the car carries Professor Abscentmind had an accident. In the accident, all of his scales were broken. The rocks are heavy and cannot be carried back. He can only hand design a balance that can only tell whether one rock is heavier than the other or not, and performs the experiments on the eld. Professor Abscentmind can only put one rock on one end of the balance and one other rock on the other end of the balance. There is no way to put multiple rocks or no rocks on one end of the balance. Because the balance is hand made without ne tuning, it cannot tell whether two rocks are equal weight or not even if they are. Furthermore, when the weights of the two rocks are close, the balance may have a measurement error. That is, the balance may say rock 1 is heavier than rock 2 while actually rock 1 is lighter than rock 2. Professor Abscentmind marked the rocks from 1 to n and recorded the results of the balancing experiments. After he had used the balance m, 1 m 10000, times, the balance broke down and could not be repaired. We call each time the balance is used a measurement. A measurement can be recorded as an ordered pair < i, j >, where i and j are the indices of the two rocks being balanced, and the result is rock i is heavier than rock j. It is impossible to have a measurement < i, i >. Note that it is possible that two rocks are balanced more than once with same or dierent results. Each measurement counts no matter it is a duplicate or not. After Professor Abscentmind returns to the laboratory, he wants to verify the correctness of his experimental data. He knows that if rock a1 is heavier than rock a2 , and rock a2 is heavier than rock a3 , then rock a1 must be heavier than rock a3 . A measurement < i, j > is suspicious if there exists a sequence of measurements H =< j, b1 >, < b1 , b2 >, < b2 , b3 >, . . ., < bk1 , bk >, < bk , i > where k > 0. That is, the balance says i is heavier than j, but you can deduce the contradictory result of j is heavier than i by a sequence of measurements H or there is another measurement H =< j, i >. Your task is to write a program to determine the number of suspicious measurements, counting all possible duplicates, giving a set of experimental records.

Input Format
The input le may contain more than one set of experimental records. The rst two line of each experimental record contains n and m respectively. Then follows by m lines, each line has two numbers i and j separated by a blank indicating the measurement result < i, j >. The last set of experimental records is followed by a line containing 0, indicating the end of test le.

Output Format
For each set of experimental records, output the number of suspicious measurements.

13

Sample Input
3 3 1 2 3 2 1 1 0

2 3 1

Output for the Sample Input


3 0

14

Problem 7 Easy and Not Easy Sudoku Puzzles


Input le: p7.in Sudoku is popular puzzle demanding logic and patience. The aim of the puzzle is to enter a numeral from 1 through 9 in each cell of a 9 9 grid made up of 3 3 sub-grids, starting with various numerals given in some cells. Each row, column and sub-grid must contain only one instance of each numeral. See Figure 2 and Figure 3 for a sample puzzle and its solution. 5 6 8 4 7 6 4 1 9 8 3 1 9 8 6 8 2 2 8 7 5 9 3 7 9 5 6 3 1 6

Figure 2: An example of a Sudoku puzzle 5 6 1 8 4 7 9 2 3 3 7 9 5 2 1 6 8 4 4 2 8 9 6 3 1 7 5 6 1 3 7 8 9 5 4 2 7 9 4 6 5 2 3 1 8 8 5 2 1 3 4 7 9 6 9 3 5 4 7 8 2 6 1 1 4 6 2 9 5 8 3 7 2 8 7 3 1 6 4 5 9

Figure 3: A solution to the Sudoku puzzle Some Sudoku puzzles may have unique solution. Dr. Susie thinks it is a good tool to enhance childrens patience. However, he wants to avoid dicult puzzles so that the children will be able to solve it in a reasonable amount of time. After some research, Dr. Susie noticed that the following two heuristics are easily understood by children: 1. Each numeral can appear in any row, column, and sub-grid only once. Therefore, if the union of the numeral appeared in the same row, column and sub-grid of some free cell contains 8 numerals, and leaves only one available numeral to be lled into that cell, then it should be lled with that numeral. For example, in Figure 4, the cell marked x can only be lled with 2 since all the other 8 numerals have appeared in the row, column and sub-grid of it. 15

2. Each sub-grid has to contain all the numerals. Therefore, if a numeral has not appeared in some sub-grid, say numeral 1, if there is only one free cell which can be lled by 1, due to the fact that all the other free cells in that sub-grid have 1 in the same row or column, then ll that cell with numeral 1. For example, in Figure 5 only the cell marked x can be lled with 1, all the other free cells are ruled out for the possibility due to 1s appeared in other sub-grid. 1 6 4 3 5 8 x 1 9 7

Figure 4: Heuristic Rule 1

1 1

7 6 x 1

Figure 5: Heuristic Rule 2 He decided that if a puzzle can be solved by only applying the two heuristics above, then it is an easy puzzle, otherwise it is not an easy puzzle. You are to write a program to tell whether puzzles are easy or not easy.

Input Format
The rst line contains an integer n, n 15, indicating the number of puzzles. The following 9n rows contains n puzzles. Each puzzle consists of 9 rows. Each row contains 9 numerals0 indicates that the cell has to be lled, and other numerals indicate that the cell is given that numeral.

16

Output Format
The output are n bits. The i-th bit equals to 1 if the i-th puzzle is easy, and 0 if the puzzle is not easy.

Sample Input
2 0 0 3 1 5 4 9 6 8 7 0 3 1 5 4 9 6 8 8 0 0 9 2 7 3 5 1 8 0 0 9 2 7 3 5 1 0 0 0 8 3 6 4 2 7 0 0 0 8 3 6 4 2 7 3 0 5 4 9 2 8 0 6 3 0 5 4 9 2 8 0 6 0 8 0 5 6 3 2 0 9 0 8 0 5 6 3 2 0 9 0 0 0 7 1 8 5 4 3 0 0 0 7 1 8 5 4 3 0 0 0 3 8 1 7 9 2 0 0 0 3 8 1 7 9 2 0 0 8 2 4 9 6 3 5 0 0 8 2 4 9 6 3 5 9 3 2 6 7 5 1 8 4 9 3 2 6 7 5 1 8 4

Sample output: 01

17

Problem 8 The Rank of a Tree


Input le: p8.in Let Bn denote the set of binary trees with n internal nodes (and thus with n + 1 leaves) and T Bn . We assume that all internal nodes of T are numbered from 1 to n, according to the in-order traversal of T . For each internal node i T , the left sub-tree of i is the sub-tree of T rooted at the left child of i. Then the left-weight of i, denoted by wl (T, i), is the number of leaves in the left sub-tree of i. The integer sequence wl (T ) = (wl (T, 1), wl (T, 2), . . . , wl (T, n)) is dened as the left-weight sequence of T and every binary tree can be characterized by its left-weight sequence. For example, Figure 6 shows a tree T B6 with wl (T ) = (1, 2, 1, 4, 1, 2).
T

2 1 5

6 3

wl (T ) = (1, 2, 1, 4, 1, 2)

Figure 6: The left-weight sequence of a tree T . The number of binary trees in Bn is given by the Catalan number Cn = 1 2n . n+1 n

For example, all the left-weight sequences of T B4 are listed in Table 1 in lexicographic order. The rank of a left-weight sequence is its ordinal number in the lexicographic order list of all left-weight sequences in Bn . For example, the rank of the tree T B4 with wl (T ) = (1, 1, 2, 4) is 7. Design a program for computing the rank of a specic binary tree T Bn .

Input Format
The input le consists of several test cases. Each test case starts with a positive integer n, 3 n 10. Then followed by a line which contains the left-weight sequence of a specic binary tree. The line containing a 0 indicates the end of test cases.

Output Format
For each test case, output the rank of the given left-weight sequence in one line.

18

Rank weight sequence 1 1111 2 1112 3 1113 4 1114 5 1121 6 1123 7 1124 8 1131 9 1134 10 1211 11 1212 12 1214 13 1231 14 1234 Table 1: Left-weight sequences of T B4

Sample Input
5 1 1 1 1 2 8 1 2 3 1 1 2 1 8 10 1 1 2 1 5 1 2 1 4 1 0

Output for the Sample Input


2 1323 9000

19

Problem 9 Optimal Bus Route Design


Input le: p9.in A big city wants to improve its bus transportation system. One of the improvement is to add scenic routes which goes through attractive places. Your task is to construct a bus-route-plan for sight-seeing buses in a city. You are given a set of scenic locations. For each of these given locations, there should be only one bus route that passes this location, and that bus route should pass this location exactly once. The number of bus routes is unlimited. However, each route should contain at least two scenic locations. From location i to location j, there may or may not be a connecting street. If there is a street from location i to location j, then we say j is an out-neighbor of i. The length of the street from i to j is d(i, j). The streets might be one way. So it may happen that there is a street from i to j, but no street from j to i. In case there is a street from i to j and also a street from j to i, the lengths d(i, j) and d(j, i) might be dierent. The route of each bus must follow the connecting streets and must be a cycle. For example, the route of Bus A might be from location 1 to location 2, from location 2 to location 3, and then from location 3 to location 1. The route of Bus B might be from location 4 to location 5, then from location 5 to location 4. The length of a bus route is the sum of the lengths of the streets in this bus route. The total length of the bus-route-plan is the sum of the lengths of all the bus routes used in the plan. A bus-route-plan is optimal if it has the minimum total length. You are required to compute the total length of an optimal bus-route-plan.

Input Format
The input le consists of a number of test cases. The rst line of each test case is a positive integer n, which is the number of locations. These n locations are denoted by positive integers 1, 2, , n. The next n lines are information about connecting streets between these locations. The i-th line of these n lines consists of an even number of positive integers and a 0 at the end. The rst integer is a location j which is an out-neighbor of location i, and the second integer is d(i, j). The third integer is another location j which is an out-neighbor of i, and the fourth integer is d(i, j ), and so on. In general, the (2k 1)th integer is a location t which is an out-neighbor of location i, and the 2kth integer is d(i, t). The next case starts immediately after these n lines. A line consisting of a single 0 indicates the end of the input le. Each test case has at most 99 locations. The length of each street is a positive integer less than 100.

Output Format
The output contains one line for each test case. If the required bus-route-plan exists, then the output is a positive number, which is the total length of an optimal bus-route-plan. Otherwise, the output is a letter N.

20

Sample Input
3 2 1 1 8 2 3 1 5 4 7 6 6 3 2 3 2 0 2 3 1 0 1 3 2 0 3 2 7 0 3 3 2 4 4 4 2 6 3 1 2 6 3 8 5 7 1 1 7 7 9 5 8 2 0 4 4 0 0 0 0 0 8 1 0 0

1 0 1 0 1 0

Sample Output
7 25 N

21

You might also like