离散题目9
Time Limit: 1000MS
Memory Limit: 65536KB
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
注意奇偶数下表代表的不同
代码:
#include <bits/stdc++.h> using namespace std; int s[100050]; int t[100050]; int main() { int n, x, i; while(~scanf("%d", &n)) { memset(s, 0, sizeof(s)); memset(t, 0, sizeof(t)); for(i=0; i<n; i++) { scanf("%d", &x); s[x] = 1; } for(i=0; i<n; i++) { scanf("%d", &x); t[x] = 1; } int flag = 1; for(i=0; i<2*n; i++) { scanf("%d", &x); if(i%2==0) { if(s[x]==1) { s[x] = 0; } else { flag = 0; s[x] = 2; } } else { if(t[x]!=1) { flag = 0; } } } if(flag) printf("yes\n"); else printf("no\n"); } return 0; }