離散題目12(判斷是否爲函數 c++處理)

離散題目12
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
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 <iostream>
#include<cstdio>
#include<sstream>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int a[2000000],b[2000000],c[2000000];
int main()
{
    string line;
    int i,j,l1,l2,f,x,k,y,n;
    while(getline(cin,line))
    {
        l1=l2=0;
        f=1;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        stringstream s(line);
        while(s>>x)
        {
            a[x+1000000]=1;
        }
        getline(cin,line);
        stringstream ss(line);
        while(ss>>x)
        {
            b[x+1000000]=1;
        }
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            if(a[x+1000000]>=1&&b[y+1000000]==1)
            {
                a[x+1000000]++;
            }
            if(a[x+1000000]>2)//函數x唯一
            {
                f=0;
            }
        }
        if(f)
            printf("yes\n");
        else
            printf("no\n");

        getline(cin,line);

    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章