Transfer closure SDUT discrete mathematics OJ4175

Transitive closure Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description It is known that there are n cattle, m times of c...
Transitive closure

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

It is known that there are n cattle, m times of combat relationship. Ask the number of cattle that can finally determine the ranking.

Input

For multiple groups of test data, for each group of test data, input two integers n (1 < = n < = 100) and m (0 < = m < = 4950) in the first row, respectively, to indicate that there are n cattle and m times of combat relationship, and then input two positive integers x and Y in each row of m to indicate that cattle with No. x can defeat cattle with No. y, the data is legal, and query can determine the number of cattle in the ranking.

Output

For each group of test data, the output integer ans indicates the number of cows that can determine the ranking.

Sample Input

5 5 4 3 4 2 3 2 1 2 2 5

Sample Output

2
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; int ralationship[105][105];///Array to determine if there is a relationship void f(int);///Calculate transfer relationship int main() { int n, m, i, j, x, y, sum1, sum2; while(~scanf("%d %d", &n, &m)) { memset(ralationship, 0, sizeof(ralationship)); while(m--) { scanf("%d %d", &x, &y); ralationship[x][y] = 1;///Enter the two cows that determine the relationship } f(n);///Call function to calculate transfer relationship sum2 = 0;///Number of cows that can be ranked for(i = 1; i <= n; i++) { sum1 = 0;///Number of relationships between cattle i and other n - 1 cattle for(j = 1; j <= n; j++) { if(i == j) continue;///If i = j points to a cow, there is no operation in this cycle if(ralationship[i][j] || ralationship[j][i]) sum1++;///If i is related to j-n, sum1++ } if(sum1 == n - 1) sum2++;///If sum1 = n - 1, that is, the relationship between this cow and other N - 1 cows is determined, then its ranking is determined } printf("%d\n", sum2); } return 0; } void f(int n) { int i, j, k; for(k = 1; k <= n; k++) { for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) {///When I beat J or (I beat k and k beat J) holds at least one, i.e. the relationship between I and j is determined and assigned as 1 ralationship[i][j] = ralationship[i][j] || (ralationship[i][k] && ralationship[k][j]); } } } }

7 November 2019, 13:55 | Views: 3272

Add new comment

For adding a comment, please log in
or create account

0 comments