Click Here to go back to the homepage.

Functional Fun Solution:


#include <bits/stdc++.h>
using namespace std;

int main(){
    // #ifndef ONLINE_JUDGE
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif

    string line;
    while(getline(cin, line)){
        bool injective = true, surjective, invalid = false;
        stringstream ss(line);
        vector<string> domain;
        vector<string> codomain;
        string token;
        ss >> token;
        while(ss >> token){
            domain.push_back(token);
        }
        getline(cin, line);
        ss.clear();
        ss.str(line);
        ss >> token;
        while(ss >> token){
            codomain.push_back(token);
        }
        getline(cin, line);
        int relationships = stoi(line);
        set<string> domainMapped;
        set<string> codomainMapped;
        string x, y;
        while(relationships--){
            getline(cin, line);
            ss.clear();
            ss.str(line);
            ss >> x >> token >> y;
            if (!domainMapped.insert(x).second){
                invalid = true;
            }
            if(!codomainMapped.insert(y).second){
                injective = false;
            }      
        }
        surjective = codomainMapped.size() == codomain.size();
        if(invalid){
            cout << "not a function" << endl;
        } else {
            if(injective){
                if(surjective){
                    cout << "bijective" << endl;
                } else {
                    cout << "injective" << endl;
                }
            } else if(surjective) {
                cout << "surjective" << endl;
            } else {
                cout << "neither injective nor surjective" << endl;
            }
        }
    }
	return 0;
}