判断一个二元关系的性质(是否具有自反性、反自反性、对称性、反对称性和传递性)
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;
}
具体情况具体判断,上述代码仅供参考。