http://acm.pku.edu.cn/JudgeOnline/problem?id=3275
poj 3275 Ranking the Cows
Time Limit:2000MS Memory Limit:65536K
Total Submit:526 Accepted:184
Description
Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a different positive rate, and FJ would like to
order his cows according to these rates from the fastest milk producer to the slowest.
FJ has already compared the milk output rate for M (1 ≤ M ≤ 10,000) pairs of cows. He wants to make a list of C
additional pairs of cows such that, if he now compares those C pairs, he will definitely be able to deduce the
correct ordering of all N cows. Please help him determine the minimum value of C for which such a list is
possible.
Input
Line 1: Two space-separated integers: N and M
Lines 2..M+1: Two space-separated integers, respectively: X and Y. Both X and Y are in the range 1...N and
describe a comparison where cow X was ranked higher than cow Y.
Output
Line 1: A single integer that is the minimum value of C.
Sample Input
5 5
2 1
1 5
2 3
1 4
3 4
Sample Output
3
Hint
From the information in the 5 test results, Farmer John knows that since cow 2 > cow 1 > cow 5 and cow 2 > cow 3
> cow 4, cow 2 has the highest rank. However, he needs to know whether cow 1 > cow 3 to determine the cow with
the second highest rank. Also, he will need one more question to determine the ordering between cow 4 and cow 5.
After that, he will need to know if cow 5 > cow 3 if cow 1 has higher rank than cow 3. He will have to ask three
questions in order to be sure he has the rankings: "Is cow 1 > cow 3? Is cow 4 > cow 5? Is cow 5 > cow 3?"
Source
USACO 2007 March Gold
Description:
有N个数字,已经比较了M对(x,y),其中x>y, 问至少再比较多少对数,就能把N个数按大小有序的排列起来
Solution:
这题挺好的,基础题。DFS
如果一列数是大小有序的,那么它们任意两个数之间都有大小关系,所以就在DFS之后去找任意两个数之间没有关系的那一对数,
然后ans++,本题的核心思想
1.一个二维的标记数组n×n,赋初值为false,[i][j]=false,[j][i]=false表示(i,j) (j,i)没有大小关系
2.记录下m对数,同时记录n个数的所有比自己小的那些数,vector,list都行
3.根据2记录的那些关系链,把所有i和j有关系的数组值赋为true
4.数1到n中哪两个数之间彼此没有关系,就ans++
解决Farmer John的N头牛根据产奶速率排序的问题。已知M组比较结果,需确定最少额外比较次数C,以完全确定所有牛的排序。涉及DFS算法及关系链建立。

203

被折叠的 条评论
为什么被折叠?



