Discrete Problem 13

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;
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值