Ski Cows
InterviewTime limit1sMemory limit1024 MB
Count the downhill ski routes from the highest landmark to the lowest, where each ski path between two landmarks counts as a separate option.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Graph, Sorting
- Solved
- No attempts yet
Problem
Farmer John's cows behaved well, so he sends them on a winter vacation to visit USACO head coach Rob K. Rob lives in Colorado and likes to take the cows skiing. He needs your help to find the best ski location to visit. He wants a mountain with many routes from top to bottom, so the cows do not get bored.
The input file describes a mountain and its interesting landmarks. It gives each landmark's elevation and the ski paths between landmarks.
The landmark with the largest elevation is the top of the mountain, and the landmark with the smallest elevation is the bottom. Every ski route starts at the top and follows a sequence of ski paths, moving from landmark to landmark downhill, until it reaches the bottom.
Compute the number of different possible ski routes.
Input
The first line contains two integers: , , the number of landmarks on the mountain; and , the number of ski paths, where . A ski path connects two landmarks.
Next come space-separated positive integers smaller than 1,000,000, giving the elevation of each landmark in order (landmark 1, landmark 2, ..., landmark ). All elevations are unique. The integers are given 10 per line, except possibly the last line.
Finally come space-separated pairs of integers, each in the range , listing the two landmarks each ski path connects. Each pair connects two different landmarks, and each pair is unordered. Several ski paths may connect the same pair of landmarks. The pairs are given 10 pairs per line, except possibly the last line.
Output
A single integer: the total number of possible paths down the mountain.