Think:
用数组来存储输入的情况 标记0 1, 然后遍历数组, 根据自反定义求解。。
因为是自反的所以 每个元素都有自回路 也就是MAP[i][i] = 1;
Problem Description
Given a set A and a relation R. Write a function to determine whether a relation is reflexive.
For example:
A={1,2,3,4,5} , R={<1,1>,<1,2>,<2,2>,<3,3>,<2,3>,<4,4>, <4,5>,<5,5>,<5,1>}
Evidently, relation R is reflexive.
Input
The first line is a number T indicating that there are T test cases. Each test case contains several lines. The first for each line is two number M and N specifying that there are N ordered pairs in R and A = { 1, 2, … ,N } .All numbers is no more than 100.
Output
For each test case, output a string ‘true’ occupying one line if the given relation is reflexive, or else ‘false’.
Example Input
2
5 9
1 1
1 2
2 2
3 3
2 3
4 4
4 5
5 5
5 1
5 8
1 1
1 2
2 2
3 3
2 3
4 4
4 5
5 1
Example Output
true
false
PS:中文翻译
Problem Description
DaYu平时只顾着看电影,没有学习离散,学期末快考试的时候才慌了神,因为时间不够,因此他决定只复习一个知识点,但是他发现他一个知识点都不会,因此他跑过来请你帮他解决一个问题。求一个集合是否是自反的。
Input
第一行输入组数T(T<10),每组的第一行输入集合元素个数m(m < 100)和对应关系个数n(n < 100),集合中元素为1,2,…,m,接下来n行每行输入两个数x,y(0 < x, y < 100)
Output
如果满足自反关系,则输出true,否则输出false。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
using namespace std;
bool MAP[1050][1050];
int main()
{
int T;
int i;
scanf("%d",&T);
while(T --)
{
memset(MAP, 0, sizeof(MAP));
int n, m;
bool flag = true;
scanf("%d %d",&m,&n);
int a, b;
for (i = 0;i <= n - 1;i ++)
{
scanf("%d %d",&a,&b);
MAP[a][b] = 1;
}
for (i = 1;i <= m;i ++)
{
if (MAP[i][i] != 1)
{
flag = false;
break;
}
}
if (flag == false)
printf("false\n");
else
printf("true\n");
}
return 0;
}