Problem Description
给定一个数学函数F和两个集合A,B,写一个程序来确定函数是单射。
即A中的任意一个元素唯一的对应一个函数值,并且该值为B集合中的某个元素。
Input
多组输入。
首先输入集合的元素数n<=100000。
接下来的一行输入n 个整数0<=ai<=n。
接下来的一行输入n个整数 0<=bi<=n。
接下来的一行输入2n个整数ci,并且当ci的下标为奇数时表示A集合中的元素,当ci的下标为偶数时表示A集合中元素对应的函数值(即B集合的元素)。
Output
(一组答案占一行)
当满足单射关系时输出yes
不满足关系时输出no
Example Input
4 1 3 5 7 2 5 6 8 1 2 3 2 5 8 7 6 2 1 4 3 5 1 3 1 5
Example Output
yes no
code:
#include <stdio.h>
#include <string.h>
int main()
{
int a[100010], b[100010];
int n, i, x;
while(~scanf("%d", &n))
{
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for(i = 0;i<n;i++)
{
scanf("%d", &x);
a[x] = 1;
}
for(i = 0;i<n;i++)
{
scanf("%d", &x);
b[x] = 1;
}
int flag = 1;
for(i = 0;i<2*n;i++)
{
scanf("%d", &x);
if(i%2==0)
{
if(a[x]==1)
{
a[x] = 0;
}
else
{
flag = 0;
}
}
else
{
if(b[x]!=1)
{
flag = 0;
}
}
}
if(flag) printf("yes\n");
else printf("no\n");
}
}
本文介绍了一个用于判断函数是否为单射的程序实现。通过输入集合A和B及其对应值,程序能够验证函数是否满足单射条件:每个A集合中的元素唯一对应B集合中的一个值。示例输入输出展示了程序的有效性。
1092

被折叠的 条评论
为什么被折叠?



