離散題目10
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description
給定一個數學函數F和兩個集合A,B,寫一個程序來確定函數是滿射。 如果每個可能的像至少有一個變量映射其上(即像集合B中的每個元素在A中都有一個或一個以上的原像),或者說值域任何元素都有至少有一個變量與之對應,那這個映射就叫做滿射。
Input
多組輸入直到文件結束,對於每組輸入,第一行先輸入一個n(A集合裏的元素個數),m(B集合裏的元素個數),k(F數學函數關係的條數)。
0 < n,m < 10000, 0 < k < n;
第二行輸入有n個元素,分別爲a1到an;
第三行輸入有m個元素,分別爲b1到bn;
接下來輸入有k行,分別爲集合A到B的關係
Output
(一組答案佔一行)
當滿足滿射關係時輸出Yes。
不滿足關係時輸出No。
Example Input
5 3 5
1 3 5 7 8
2 5 6
1 2
3 6
5 5
7 2
8 6
Example Output
Yes
思路同第九題
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int a[1000000],b[1000000];
int main()
{
int f,x,y,k,i,j,n,m;
while(~scanf("%d%d%d",&n,&m,&k))
{
f=1;
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<m;i++)
{
scanf("%d",&x);
b[x]=1;
}
for(i=0;i<k;i++)
{
scanf("%d%d",&x,&y);
if(a[x]==1&&b[y]>=1)
{
b[y]=2;
}
else
f=0;
}
for(i=0;i<100000;i++)
if(b[i]==1)
{
f=0;
break;
}
if(f)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}