Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Задачи-Графы.doc
Скачиваний:
27
Добавлен:
23.03.2016
Размер:
116.22 Кб
Скачать

Input file: kingdom.In

Output file: kingdom.out

Time limit: 2 seconds

Memory limit: 64 megabytes

The Kingdom consists of n cities connected by n 1 roads in such a way that there is exactly one way to travel from one city to another. The King is a busy man and he constantly travels from city to city. Unfortunately, during one of his travels one of the roads got damaged and needed serious repairs. As a result, the King was unable to reach his destination in time.

After the incident the King decided to improve reliability of the road system. It was decided that the improved road system shall be able to withstand one damaged road, i.e. there shall always be a path from one city to another even when one road in the Kingdom is damaged. As always, the budget is limited so you need to minimize the number of new roads.

Your task is to build as few new roads as possible so that there is always a path between any two cities even if one of the roads gets unusable for any reason. There may be multiple optimal solutions. Any one can be used.

Input

The first line of the input file contains integer n — the number of cities in the kingdom (2 n 100 000). The following n 1 lines contain two integers ui, vi each — the cities connected by i-th road

(1 ui, vi n).

Output

The first line of the output file shall contain one integer k — the number of roads to be built. The following k lines shall contain two integers ai, bi each — the cities which should be connected by i-th new road (1 ai, bi n).

Sample input and output

kingdom.in

kingdom.out

5

1 2

2 3

3 4

3 5

2

1 4

4 5

4

1 2

1 3

1 4

2

3 2

1 4

Задача 10. Caterpillar (VIP-турнир 2007. Е)

Вход: caterpil.in

Выход: caterpil.out

Ограничение по времени 1 секунда на тест

Ограничение по памяти 64 M байт

An undirected graph is called a caterpillar if it is connected, has no cycles, and there is a path in the graph where every node is either on this path or a neighbor of a node on the path. This path is called the spine of the caterpillar and the spine may not be unique. You are simply going to check graph to see if it is caterpillar.

Input specifications

Test case starts with a line containing n indicating the number of nodes, numbered 1 through n. Starting on the following line will be some pairs u v indicating an undirected edge between nodes u and v. This information may span multiple lines. You may assume that n ≤ 1000. Do not assume that the graph in the test case is connected or acyclic.

Output specifications

Generate one line of output. This line should either be Graph is a caterpillar.or Graph is not a caterpillar.

Sample Input and Output

caterpil.in

caterpil.out

22

1 2 2 3 2 4 2 5 2 6 6 7 6 10 10 8 9 10 10 12 11 12 12 13 12 17 18 17 15 17

15 14 16 15 17 20 20 21 20 22 20 19

Graph is not a caterpillar.

16

1 2 2 3 5 2 4 2 2 6 6 7 6 8 6 9 9 10 10 12 10 11 10 14 10 13 13 16 13 15

Graph is a caterpillar.