Pro.ID22717 TitleThe Lost Cows Title链接http://10.20.2.8/oj/exercise/problem?problem_id=22717 AC0 Submit7 Ratio0.00% 时间&空间限制描述One sunny day farmer John was kidnapped by evil farmer Marcus's cows. FJ wasn't too concerned about his forced holiday but wanted to make sure that his cows got home safely together. The cows are spread out in every one of FJ's N (3 ≤ N ≤ 200) pastures conveniently numbered 1..N. The barn is located at pasture 1. The farm has an interesting navigation system: at every pasture i there are M (1 ≤ M ≤ 200) signs Sij (1 ≤ Sij ≤ N) which one could reference as Si1..SiM; each sign points the way to a pasture. Sometimes a sign points to a path that leads back to the same pasture. Farmer Marcus's cows allow FJ to write a single message to all of his cows. FJ's plan is to write a list of sign numbers such that any cow who follows those instructions will all arrive at the barn when each cow has completed all the instructions. When a cow starts at a given pasture then she will first follow the path indicated by the first sign number on FJ's list. When she arrives at the second pasture, she looks at the second sign of FJ's list and follows the path marked by that sign. She continues until she exhausts the instruction list, at which point she should be at the barn. Find a list of instructions containing no more than 5,000,000 sign numbers that will guide every cow, from every pasture, to the barn after all instructions are followed. It is guaranteed that such a list exists. Consider a set of three signs in four pastures that direct the cows like these do: ** Pasture# ** The set of instructions below will direct cows to the barn from any of the four pastures: Instruction# Sign# Instruction# Sign# The cow in pasture 1 will read sign #1 at time 1 and be directed to pasture 4. At time 2, she is in pasture 4 and (per FJ's instructions) read sign #2 and then be directed to pasture 4. Below is a table that shows the cow's travels: * * * * Cow in pasture 1 * * * * Given a set of signs, create a set of instructions. 输入Line 1: Two space separated integers: N and M Lines 2..M+1: Line i+1 describes the contents of each pasture's N signs with N integers: S1i..SNi 输出Description One sunny day farmer John was kidnapped by evil farmer Marcus's cows. FJ wasn't too concerned about his forced holiday but wanted to make sure that his cows got home safely together. The cows are spread out in every one of FJ's N (3 ≤ N ≤ 200) pastures conveniently numbered 1..N. The barn is located at pasture 1. The farm has an interesting navigation system: at every pasture i there are M (1 ≤ M ≤ 200) signs Sij (1 ≤ Sij ≤ N) which one could reference as Si1..SiM; each sign points the way to a pasture. Sometimes a sign points to a path that leads back to the same pasture. Farmer Marcus's cows allow FJ to write a single message to all of his cows. FJ's plan is to write a list of sign numbers such that any cow who follows those instructions will all arrive at the barn when each cow has completed all the instructions. When a cow starts at a given pasture then she will first follow the path indicated by the first sign number on FJ's list. When she arrives at the second pasture, she looks at the second sign of FJ's list and follows the path marked by that sign. She continues until she exhausts the instruction list, at which point she should be at the barn. Find a list of instructions containing no more than 5,000,000 sign numbers that will guide every cow, from every pasture, to the barn after all instructions are followed. It is guaranteed that such a list exists. Consider a set of three signs in four pastures that direct the cows like these do: ** Pasture# ** The set of instructions below will direct cows to the barn from any of the four pastures: Instruction# Sign# Instruction# Sign# The cow in pasture 1 will read sign #1 at time 1 and be directed to pasture 4. At time 2, she is in pasture 4 and (per FJ's instructions) read sign #2 and then be directed to pasture 4. Below is a table that shows the cow's travels: * * * * Cow in pasture 1 * * * * Given a set of signs, create a set of instructions. Input Line 1: Two space separated integers: N and M Lines 2..M+1: Line i+1 describes the contents of each pasture's N signs with N integers: S1i..SNi Output Lines 1..?: The sign numbers the cows should follow, one per line. Sample Input 4 3 Sample Output 1 Source 样例输入4 3 样例输出1 作者 |