Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. You are supposed to help the scientists to count the maximum number of trees in the forest, and for any pair of birds, tell if they are on the same tree.

## 1119. Pre- and Post-order Traversals (30)-PAT甲级真题（前序后序转中序）

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the[……]

## 1122. Hamiltonian Cycle (25)-PAT甲级真题

The “Hamilton cycle problem” is to find a simple cycle that contains every vertex in a graph. Such a cycle is called a “Hamiltonian cycle”.

In this problem, you are supposed to tell if a given cycle is a Hamiltonian cycle.

#### Input Specification:

Each input file contains one test case. For each case[……]

## 1124. Raffle for Weibo Followers (20)-PAT甲级真题

John got a full mark on PAT. He was so happy that he decided to hold a raffle（抽奖） for his followers on Weibo — that is, he would select winners from every N followers who forwarded his post, and give away gifts. Now you are supposed to help him generate the list of winners.

#### Input Specification:

Ea[……]

## PAT 1129. Recommendation System (25) 甲级

Recommendation system predicts the preference that a user would give to an item. Now you are asked to program a very simple recommendation system that rates the user’s preference by the number of times that an item has been accessed by this user.

#### Input Specification:

Each input file contains one t[……]

## PAT 1128. N Queens Puzzle (20)-甲级

The “eight queens puzzle” is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal. The eight queens puzzle is an example of the more general N queens problem of p[……]

## 1120. Friend Numbers (20)-PAT甲级真题

Two integers are called “friend numbers” if they share the same sum of their digits, and the sum is their “friend ID”. For example, 123 and 51 are friend numbers since 1+2+3 = 5+1 = 6, and 6 is their friend ID. Given some numbers, you are supposed to count the number of different friend ID’s among t[……]

## 1121. Damn Single (25)-PAT甲级真题

“Damn Single (单身狗)” is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of.

#### Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=50000)[……]

## 1108. Finding Average (20)-PAT甲级真题

The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A “legal” input is a real number in [-1000, 1000] and is accurate up to no more than 2 decimal places. When you calculate t[……]

## PAT 1125. Chain the Ropes (25)-甲级

Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fold two segments into loops and chain them into one piece, as shown by the figure. The resulting chain will be treated as another segment of rope and can be folded again. After each chaining, the lengt[……]