Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<map>
  4. #include<set>
  5. #include<algorithm>
  6. #define MAXN 1005
  7. using namespace std;
  8.  
  9. int N, M, cid = 0;
  10. bool adj[MAXN][MAXN];
  11. map<int, set<string> >attr;
  12. map<string, int>id;
  13.  
  14. void apply_id(string s){
  15.     if(!id.count(s)){
  16.         id[s] = cid++;
  17.     }
  18. }
  19.  
  20. int main(){
  21.     cin >> N >> M;
  22.     for(int i = 0;i < N;i++){
  23.         string cur_class, rel, opt;
  24.         cin >> cur_class >> rel >> opt;
  25.         if(rel == "is-a"){ // opt is a class
  26.             apply_id(cur_class);
  27.             apply_id(opt);
  28.             adj[id[cur_class]][id[opt]] = true;
  29.         } else if(rel == "has-a"){ // opt is an attribute
  30.             apply_id(cur_class);
  31.             attr[id[cur_class]].insert(opt);
  32.         }
  33.     }
  34.     // computing the transitive closure of the graph
  35.     int nodes = cid;
  36.     for(int i = 0;i < MAXN;i++)adj[i][i]=true;
  37.     for(int k = 0;k < nodes;k++){
  38.         for(int i = 0;i < nodes;i++){
  39.             for(int j = 0;j < nodes;j++){
  40.                 adj[i][j] = adj[i][j] || (adj[i][k] & adj[k][j]);
  41.                 if(adj[i][j]){
  42.                     attr[i].insert(attr[j].begin(), attr[j].end());
  43.                 }
  44.             }
  45.         }
  46.     }
  47.     for(int q = 1;q <= M;q++){
  48.         string cur_class, rel, opt;
  49.         cin >> cur_class >> rel >> opt;
  50.         if(rel == "is-a"){ // determine if there's a relation
  51.             apply_id(cur_class);
  52.             apply_id(opt);
  53.             string qout = adj[id[cur_class]][id[opt]]  ? "true" : "false";
  54.             cout << "Query " << q << ": "  << qout << endl;
  55.         } else if(rel == "has-a"){ // determine if you have the lements
  56.             apply_id(cur_class);
  57.             string qout;
  58.             if(id.count(opt)){ // check if we contain instance of a class
  59.                 bool result = false;
  60.                 for(auto inst: attr[id[cur_class]]){
  61.                    if(id.count(inst)){ // this instance is a class
  62.                        result  |= adj[id[inst]][id[opt]];
  63.                    }
  64.                 }
  65.                 qout = result ? "true":"false";
  66.             }else{ // check for attributes
  67.                 qout = attr[id[cur_class]].count(opt) ? "true" : "false";
  68.             }
  69.             cout << "Query " << q << ": "  << qout << endl;
  70.         }
  71.  
  72.     }
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement