离散题目18

离散题目18
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description
给出一个集合A和A上的关系R,求关系R的传递闭包。
例如:
A={0,1,2} , R={<0,0>,<1,0>,<2,2>,<1,2>,<2,1>}
t(R) = {<0,0>,<1,0>,<2,2>,<2,1>,<1,2>,<1,1>,<2,0>};
Input
多组输入,输入n、m,集合A={0, 1, …, n-1 };m代表关系的数量,n、m不超过20.
Output
每组输入输出t(R),根据t(R)中序偶的第一个数字升序排序,如果第一个数字相同,根据第二个升序排序。
Example Input
3 5
0 0
1 0
2 2
1 2
2 1
Example Output
0 0
1 0
1 1
1 2
2 0
2 1
2 2

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node{
    int x,y;
}a[110];
bool cmp(struct node q,struct node w){
    if (q.x!=w.x)
        return q.x<w.x;
    return q.y<w.y;
}
int main(){
    int n,m,xx,yy;
    while(~scanf("%d%d",&n,&m)){
        for (int i=0;i<m;i++){
            scanf("%d%d",&a[i].x,&a[i].y);
        }
        int t=m;
        int flag;
        for (int i=0;i<t;i++){
            for (int j=0;j<t;j++){
                if (a[j].x==a[i].y){
                    flag=0;
                    for (int k=0;k<t;k++){
                        if (a[k].x==a[i].x&&a[k].y==a[j].y){
                            flag=1;
                            break;
                        }
                    }
                    if (flag==0){
                        a[t].x=a[i].x;
                        a[t++].y=a[j].y;
                    }
                }
            }
        }
        sort(a,a+t,cmp);
        for (int i=0;i<t;i++){
            printf("%d %d\n",a[i].x,a[i].y);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值