1. 程式人生 > >POJ-3275:Ranking the Cows(Floyd、bitset)

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

accep lines ... std between farmer 轉折點 using 多少

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(N3)復雜度較高,此題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 }

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