Partial order relation
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
Given the relation matrix of binary relation on a finite set, it is determined whether the relation is a partial order relation.
Input
For multiple groups of test data, for each group of test data, enter the positive integer n (1 < = n < = 100) in the first row, and enter the relationship matrix of N rows and N columns in the second row to n+1 row.
Output
For each group of test data, if it is a partial order relationship, then output yes, otherwise, output no.
Sample Input
4 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 4 1 0 0 1 0 1 0 0 0 0 1 0 1 0 0 1
Sample Output
yes no
Hint
Formal definition of partial order relation: let R be A binary relation on set A, if R satisfies reflexivity, antisymmetry and transitivity, then R is called partial order relation on A.
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; int a[105][105]; int main() { int n, i, j, flag, k; while(~scanf("%d", &n)) { for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) { scanf("%d", &a[i][j]); } } flag = 1; for(i = 1; i <= n; i++) {///Reflexivity, 1 diagonal if(a[i][i] != 1) flag = 0; } for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) { if(a[i][j] == 1 && a[j][i] == 1 && i != j) {///Antisymmetry, if IRJ & & JRI, then i = j does not hold, then it does not have antisymmetry flag = 0; } if(a[i][j] == 1) {///Symmetry, if IRJ & & JRK is iRk, if it doesn't hold, it doesn't have antisymmetry for(k = 1; k <= n; k++) { if(a[j][k] == 1 && a[i][k] != 1) { flag = 0; } } } } } if(flag) printf("yes\n"); else printf("no\n"); } return 0; }