二元关系性质----离散数学编程实训(Educoder)

这篇文章描述了一个C++程序,用于分析给定集合A上二元关系R的自反性、反自反性、对称性、反对称性和传递性,通过输入关系的序偶数量和元素,输出相应的性质判断。
摘要由CSDN通过智能技术生成

判断一个二元关系的性质(是否具有自反性、反自反性、对称性、反对称性和传递性)

R是A上的关系,判断该关系是否具有自反性、反自反性、对称性、反对称性和传递性。输入的第一行为A的元素的个数n和关系R的序偶的个数m,后面的m行对应m个序偶,输出是否具有各性质。1≤n≤5000, 0≤m≤100000。 样例: 输入: 5 6 1 5 2 2 3 3 3 4 4 3 5 1 输出: 无自反性 无反自反性 有对称性 无反对称性 无传递性 输入说明:第一行的5表示A的元素为1到5,6表示R有6个序偶,后面6行对应R的6个序偶,R={<1,5>,<2,2>,<3,3>,<3,4>,<4,3>,<5,1>} 输出说明:缺少<1,1>,所以无自反性;有<3,3>,所以无反自反性;有<1,5>和<5,1>、<3,4>和<4,3>,所以有对称性;有<1,5>和<5,1>,所以无反对称性;有<1,5>和<5,1>,但没有<1,1>所以无传递性。

#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;

    
    vector<vector<bool>> relation(n + 1, vector<bool>(n + 1, false));

    
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        relation[u][v] = true;
    }

    bool reflexive = true; 
    bool irreflexive = true; 
    bool symmetric = true; 
    bool antisymmetric = true; 
    bool transitive = true; 

    for (int i = 1; i <= n; i++) {
        if (!relation[i][i]) {
            reflexive = false; 
        } else {
            irreflexive = false; 
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (relation[i][j] && !relation[j][i]) {
                symmetric = false; 
            }
            if (relation[i][j] && relation[j][i] && i != j) {
                antisymmetric = false; 
            }
            for (int k = 1; k <= n; k++) {
                if (relation[i][j] && relation[j][k] && !relation[i][k]) {
                    transitive = false; 
                }
            }
        }
    }

    if (reflexive) {
        cout << "有自反性" << endl;
    } else {
        cout << "无自反性" << endl;
    }

    if (irreflexive) {
        cout << "有反自反性" << endl;
    } else {
        cout << "无反自反性" << endl;
    }

    if (symmetric) {
        cout << "有对称性" << endl;
    } else {
        cout << "无对称性" << endl;
    }

    if (antisymmetric) {
        cout << "有反对称性" << endl;
    } else {
        cout << "无反对称性" << endl;
    }

    if (transitive) {
        cout << "有传递性" << endl;
    } else {
        cout << "无传递性" << endl;
    }

    return 0;
}

具体情况具体判断,上述代码仅供参考。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值