1. 程式人生 > 其它 >求矩陣傳遞閉包_演算法學習筆記(57): 傳遞閉包

求矩陣傳遞閉包_演算法學習筆記(57): 傳遞閉包

技術標籤:求矩陣傳遞閉包

之前在題解裡看到“傳遞閉包”,一直以為是一種很高階的演算法,後來上離散數學的時候學到了,發現其實蠻簡單的。

從數學上來說,傳遞閉包是在集合

上求包含關係 的最小傳遞關係。從關係圖的角度來說,就是如果原關係圖上有 路徑,則其傳遞閉包的關係圖上就應有從 。通俗地講,就是 確定每個點是否能到達其他每個點

而這,把Floyd最短路演算法稍微改一下即可。設E是原來的關係矩陣,則可以這樣寫:

for (int k = 1; k <= n; ++k)
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            if (E[i][k] && E[k][j])
                E[i][j] = 1;

就是依次判斷:僅經由1號點能不能從i到達j,僅經由1、2號點能不能從i到達j……最後得到的E是傳遞閉包的關係矩陣。 E[i][j]如果等於1,則表示存在從ij的路徑。時間複雜度是


(POJ1975 Median Weight Bead)

Description
There are N beads which of the same shape and size, but with different weights. N is an odd number and the beads are labeled as 1, 2, ..., N. Your task is to find the bead whose weight is median (the ((N+1)/2)th among all beads). The following comparison has been performed on some pairs of beads:
A scale is given to compare the weights of beads. We can determine which one is heavier than the other between two beads. As the result, we now know that some beads are heavier than others. We are going to remove some beads which cannot have the medium weight.
For example, the following results show which bead is heavier after M comparisons where M=4 and N=5.
1. Bead 2 is heavier than Bead 1.
2. Bead 4 is heavier than Bead 3.
3. Bead 5 is heavier than Bead 1.
4. Bead 4 is heavier than Bead 2.
From the above results, though we cannot determine exactly which is the median bead, we know that Bead 1 and Bead 4 can never have the median weight: Beads 2, 4, 5 are heavier than Bead 1, and Beads 1, 2, 3 are lighter than Bead 4. Therefore, we can remove these two beads.
Write a program to count the number of beads which cannot have the median weight. Input
The first line of the input file contains a single integer t (1 <= t <= 11), the number of test cases, followed by the input data for each test case. The input for each test case will be as follows:
The first line of input data contains an integer N (1 <= N <= 99) denoting the number of beads, and M denoting the number of pairs of beads compared. In each of the next M lines, two numbers are given where the first bead is heavier than the second bead. Output
There should be one line per test case. Print the number of beads which can never have the medium weight. Sample Input
1
5 4
2 1
4 3
5 1
4 2 Sample Output
2

大意是給出一些水滴之間的的重量大小關係,求有多少滴水滴的重量不可能是這些水滴重量的中位數。(水滴的數量保證為奇數)

這就可以建個圖(設

代表 號水滴的重量,則 有邊代表 )求傳遞閉包,對每個點都統計 能到達多少個點、有多少個點能到達(相當於統計有多少水滴確定比它重、多少水滴確定比它輕)。如果這兩個數中有一個大於 n/2,那它就不可能成為中位數。
#include <cstring>
#include <iostream>
using namespace std;
bool E[105][105];
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        cin >> n >> m;
        memset(E, 0, sizeof(E));
        for (int i = 0; i < m; ++i)
        {
            int x, y;
            cin >> x >> y;
            E[x][y] = 1;
        }
        for (int k = 1; k <= n; ++k)
            for (int i = 1; i <= n; ++i)
                for (int j = 1; j <= n; ++j)
                    if (E[i][k] && E[k][j])
                        E[i][j] = 1;
        int cnt = 0;
        for (int i = 1; i <= n; ++i)
        {
            int from = 0, to = 0;
            for (int j = 1; j <= n; ++j)
            {
                from += E[j][i];
                to += E[i][j];
            }
            cnt += from > n / 2 || to > n / 2;
        }
        cout << cnt << endl;
    }
    return 0;
}

POJ3660 Cow Contest

Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1.. N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors.
The contest is conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ AN; 1 ≤ BN; AB), then cow A will always beat cow B.
Farmer John is trying to rank the cows by skill level. Given a list the results of M (1 ≤ M ≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory. Input
* Line 1: Two space-separated integers: N and M
* Lines 2.. M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and B Output
* Line 1: A single integer representing the number of cows whose ranks can be determined Sample Input
5 5
4 3
4 2
3 2
1 2
2 5 Sample Output
2

有若干只牛,給出它們之間的一些勝負關係,試求有多少隻牛的排名無法確定。

這個題我總感覺可以用並查集或拓撲排序之類的方法,但資料範圍這麼小,用傳遞閉包應該是最簡單的了。求出傳遞閉包後,對於每個點,如果存在另一個點,既不能到達它也不能被它到達,說明這個點對應牛的排名是無法確定的。

#include <iostream>
using namespace std;
bool E[105][105];
int main()
{
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i)
    {
        int x, y;
        cin >> x >> y;
        E[x][y] = 1;
    }
    for (int k = 1; k <= n; ++k)
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                if (E[i][k] && E[k][j])
                    E[i][j] = 1;
    int cnt = 0;
    for (int i = 1; i <= n; ++i)
    {
        bool ok = true;
        for (int j = 1; j <= n; ++j)
            if (i != j && !E[i][j] && !E[j][i])
                ok = false;
        cnt += ok;
    }
    cout << cnt << endl;
    return 0;
}

Pecco:演算法學習筆記(目錄)​zhuanlan.zhihu.com