Partial order relation of discrete mathematics

Partial order relation
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description
Given the relation matrix of binary relation on finite set, determine whether the relation is partial order relation.
Input
multiple sets of test data, for each set of test data, enter a positive integer n (1 <= n <= 100) in the first row, and enter a relationship matrix of n rows and n columns from the second row to the n+1 row.
Output
For each set of test data, if it is a partial order relationship, 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
partial order relation formal definition: let R be A binary relation on a set A, if R satisfies reflexivity, anti-symmetry, and transitivity, is called a partial order relation on A.
Source
xry-fhf

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int a[101][101];
int main()
{
    memset(a,0,sizeof(a));
    int n,i,j,flag,k;
    while(~scanf("%d",&n))
    {  flag=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(i==j)
                {
                    if(a[i][j]!=1)
                    {
                        flag=1;
                    }
                }
                if(a[i][j]==1&&a[j][i]==1&&i!=j)
                {
                    flag=1;
                }
                if(a[i][j]==1)
                {
                    for(k=0;k<n;k++)
                    {
                        if(a[j][k]==1&&a[i][k]!=1)
                        {
                            flag=1;
                        }
                    }
                }
            }
        }
        if(flag==1)
        {
            printf("no\n");
        }
        else
        {
            printf("yes\n");
        }
    }
    return 0;
}

THINK:
For the partial order relationship, it is necessary to satisfy the reflexivity, anti-symmetry, and transitivity, which is represented in the relational matrix. The reflexivity is that the diagonal elements are all 0, and the anti-symmetry is a[i][j] and a[j] [i] is not 1 at the same time, (when i is not equal to j), if it is passed, it must satisfy a[i][j]=1 and a[j][k] is also 1, but this is a[i] [k] should also be 1; to write code, just use a layer of loop to represent it

if(a[i][j]==1)
                {
                    for(k=0;k<n;k++)
                    {
                        if(a[j][k]==1&&a[i][k]!=1)
                        {
                            flag=1;
                        }
                    }
                }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324650214&siteId=291194637