SDUT 3806 离散题目12

Problem Description

给出两个集合,以及两个集合上的关系。判断该关系能不能构成函数

Input

多组输入。第一行数字表示集合A;第二行数字表示集合B;第三行一个数字N,表示关系的个数。以下N行,每行两个数字a b,用来描述关系a→b。0 < n < = 20000,集合A、B的大小不超过10000.

Output

每组数据输出一行,所给关系属于函数,输出’yes’ ,否则输出‘no’。

Example Input

1 2 3
4 5 6
3
1 4
2 5
3 6
1 2 3
4 5 6
3
1 4
1 5
1 6

Example Output

yes
no

代码:

#include<bits/stdc++.h>
using namespace std;
char s[25000];
int main()
{
    set<int> a, b;
    set<int>::iterator it;
    int n, len, i;
    while(gets(s))
    {
        len = strlen(s);
        for(i = 0; i < len; i++)
        {
            if(s[i] >= '0' && s[i] <= '9')
            {
                a.insert(s[i] - '0');//集合a
            }
        }
        gets(s);
        len = strlen(s);
        for(i = 0; i < len; i++)
        {
            if(s[i] >= '0' && s[i] <= '9')
            {
                b.insert(s[i] - '0');//集合b
            }
        }
        scanf("%d", &n);
        int num1, num2;
        map<int, int> c;
        int flag = 0;
        for(i = 0; i < n; i++)
        {
            scanf("%d %d", &num1, &num2);
            if(!c.count(num1))//num1 如果没有映射 num2
            {
                c[num1] = num2;//映射
            }
            else {
                if(c[num1] != num2)//已经映射了就判断相不相等
                    flag = 1;
            }
        }
        gets(s);//数据后面有很多的空格,坑点。。。
        if(!flag) printf("yes\n");
        else printf("no\n");
        a.clear(), b.clear(); c.clear();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值