POJ-3275:Ranking the Cows(Floyd、bitset)

Ranking the Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3301 Accepted: 1511

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?"
 

概译:农夫有N头牛,他要给牛排名,他已经知道M对牛的相对排名(比如X>Y),求出他还需要知道多少对,就能准确地将所有牛排名。

输入:输入N,M。接下来的M行每行输入X,Y,代表X>Y。

思路:当任意两头牛都明确知道相对rank时,即可以准确排名,此时共须知n*(n-1)/2对。已给M对,再求出这M对所隐藏的排名共ans对(例:2>1,1>5是M对里给出的,则2>5是隐藏的一对),则n*(n-1)/2 - M - ans就是最后的输出。

可视为有向图,2>1则画出一条2指向1的边,用Floyd就可以完成对隐藏路径的连通。O(N³)复杂度较高,此题M(边数)较少,可以用枚举边的方式,输入时记录每个节点的入边和出边,Floyd时枚举每个转折点的入边和出边。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<vector>
 5 using namespace std;
 6 
 7 int n,m,ans;
 8 bool mp[1005][1005];
 9 vector<int>ru[1005],chu[1005];
10 
11 int main()
12 {
13     scanf("%d%d",&n,&m);
14 
15     for (int i = 0; i < m; i++)
16     {
17         int a,b;
18         scanf("%d%d", &a, &b);
19         ru[b].push_back(a);
20         chu[a].push_back(b);
21         mp[a][b] = true;
22     }
23 
24     for (int k = 1; k <= n; k++)
25         for (int a = 0; a < ru[k].size(); a++)
26             for (int b = 0; b < chu[k].size(); b++)
27             {//C++11的“int i:ru[k]”POJ貌似编译不过去
28                 int i = ru[k][a];
29                 int j = chu[k][b];
30                 if(!mp[i][j])
31                 {
32                     ru[j].push_back(i);
33                     chu[i].push_back(j);
34                     mp[i][j] = true;
35                     ans++;
36                 }
37             }
38                 
39     printf("%d\n",(n-1)*n/2-m-ans);
40 
41     return 0;
42 }

 

也可以使用STL容器bitset,它使得mp数组以二进制01串形式进行位运算,通常可以将复杂度除以64.使用详见代码:

 1 #include<cstdio>
 2 #include<bitset>
 3 #include<iostream>
 4 using namespace std;
 5 
 6 const int maxn=1000+5;
 7 int n,m,ans;
 8 bitset<maxn>bit[maxn];//类似于上面那个方法的mp二维数组
 9 
10 int main()
11 {
12     scanf("%d%d",&n,&m);
13     
14     for (int i = 0; i < m; i++)
15     {
16         int a,b;
17         scanf("%d%d",&a,&b);
18         bit[a].set(b);//将bit[a][b]设为1
19     }
20     
21     for (int i = 1; i <= n; i++)
22         for (int j = 1; j <= n; j++)
23             if (bit[j][i])//这其实就是个暴力的Floyd
24                 bit[j] |= bit[i];//或运算使得i中为1的点(即有向图中i指向的点),j也指向它
25                 
26     for (int i = 1; i <= n; i++)
27         for (int j = 1; j <= n; j++)
28             if (bit[i][j])
29                 ans++;
30     //这里的ans是枚举之后得到的所有边,已经Floyd处理过了,所以包括隐藏的
31 
32     cout << n*(n-1)/2-ans << endl;
33 
34     return 0;
35 }

 

转载于:https://www.cnblogs.com/AlphaWA/p/9311772.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值