离散题目15

本文介绍了一个通过编程解决离散数学中关系传递性的判断问题的方法。具体包括读取输入数据,如集合X及其关系R,并通过两层循环检查这些关系是否满足传递性条件。如果满足,则输出true,否则输出yes。

离散题目15
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description

给出集合X、X上的关系R,判断关系R是不是传递的。

例如: A={1,2,3} , R={<1,1>,<1,2>,<2,1>,<3,3>,<2,3>,<3,2>, <2,2>,<1,3>,<3,1>} 显然,R具有传递性。
Input

多组输入,每组输入第一行为集合X的元素;第二行为一个整数n ( n > 0 ),代表X上的关系R中序偶的个数;接下来n行用来描述X上的关系R,每行两个数字,表示关系R中的一个序偶。细节参考示例输入。

非空集合X的元素个数不大于500,每个元素的绝对值不大于2^32 - 1。
Output

每组输入对应一行输出,如果关系R具有传递性输出 ”true”,否则输出 ”yes”。
Example Input

1 2 3
9
1 1
2 2
3 3
1 2
2 1
1 3
3 1
2 3
3 2
1 2 3
6
1 1
1 2
3 3
2 3
3 2
2 2

Example Output

true
yes

#include <bits/stdc++.h>
using namespace std;
set<int> a;
struct node
{
  int x;
  int y;
}t[23223];
int main()
{
    string ss, buf;
    while(getline(cin, ss))
    {
        int o;
        stringstream cs(ss);
        while(cs>>buf)
        {
           sscanf(buf.c_str(), "%d", &o);
           a.insert(o);
        }
        int n;
        int f = 0;
        cin>>n;
        for(int i=0;i<n;i++)
        {
           cin>>t[i].x>>t[i].y;
           if((!a.count(t[i].x))||(!a.count(t[i].y)))
           f = 1;
        }
        int f1;
        if(f==0)
        {
            for(int i=0;i<n;i++)
            {
               for(int j=0;j<n;j++)
               {
                  if(t[i].x==t[j].y)
                  {
                     f1 = 0;
                     for(int k=0;k<n;k++)
                     {
                        if(t[k].x==t[i].y&&t[k].y==t[j].x)
                        f1 = 1;
                     }
                  if(f1==0)
                  {
                  f = 1;
                  break;
                  }
                  }
               }
               if(f==1)
               {
               break;
               }
            }
        }
        if(f==0)
        cout<<"true"<<endl;
        else
        cout<<"yes"<<endl;
        a.clear();
        getline(cin, ss, '\n');
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值