poj 3275 Ranking the Cows 图+BFS

解决Farmer John的N头牛根据产奶速率排序的问题。已知M组比较结果,需确定最少额外比较次数C,以完全确定所有牛的排序。涉及DFS算法及关系链建立。

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++ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值