daily pastebin goal
31%
SHARE
TWEET

Untitled

a guest Dec 16th, 2018 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX 100000
  5. typedef struct member {
  6.     char name[42];
  7.     struct member *l;
  8.     struct member *r;
  9. }Member;
  10. typedef struct tree {
  11.     char name[42];
  12.     Member *member;
  13.     struct tree *l;
  14.     struct tree *r;
  15. }Tree;
  16. Member *buildMember(Member *root, char *person) {
  17.     if(root == NULL) {
  18.         Member *current = (Member *)malloc(sizeof(Member));
  19.         strcpy(current->name,person);
  20.         current->l = NULL;
  21.         current->r = NULL;
  22.         return current;
  23.     }
  24.     if(strcmp(person, root->name) < 0) root->l = buildMember(root->l, person);
  25.     else if(strcmp(person, root->name) > 0) root->r = buildMember(root->r, person);
  26.     return root;
  27. }
  28. Tree *buildTree(Tree *root, char *cname, char *person) {
  29.     // printf("club %s\n", cname);
  30.     if(root == NULL) {
  31.         Tree *current = (Tree *)malloc(sizeof(Tree));
  32.         strcpy(current->name,cname);
  33.         // printf("hey club %s leader %s\n",current->name, person);
  34.         current->l = NULL;
  35.         current->r = NULL;
  36.         current->member = buildMember(current->member, person);
  37.         return current;
  38.     }
  39.  
  40.     if(strcmp(cname, root->name) < 0) root->l = buildTree(root->l, cname, person);
  41.     else if(strcmp(cname, root->name) > 0) root->r = buildTree(root->r, cname, person);
  42.     else {    /* add a new member */
  43.         root->member = buildMember(root->member, person);
  44.     }
  45.     return root;
  46. }
  47.  
  48. // for queries
  49. Tree *search(Tree *root, char *cname) {
  50.     if(root == NULL) return NULL;
  51.     if(strcmp(root->name, cname) == 0) {
  52.         // printf("club %s leader %s\n", root->name, root->member->name);
  53.         return root;
  54.     }
  55.  
  56.     if(strcmp(cname, root->name) < 0) return search(root->l, cname);
  57.     else if(strcmp(cname, root->name) > 0) return search(root->r, cname);
  58.     return root;
  59. }
  60. int findMember(Member *root, char *person) {
  61.     // printf("leader %s\n", root->name);
  62.     if(root == NULL) return 0;
  63.     if(strcmp(root->name, person) == 0) return 1;
  64.     else if(strcmp(person, root->name) < 0) return findMember(root->l, person);
  65.     else if(strcmp(person, root->name) > 0) return findMember(root->r, person);
  66.     return 0;
  67. }
  68. int main () {
  69.     int k; scanf("%d", &k);
  70.     int inst;
  71.     char person[42], cname[42];
  72.  
  73.     Tree *root = NULL;
  74.  
  75.     for(int i = 0; i < k; i++) {
  76.         scanf("%d%s%s", &inst, person, cname);
  77.         root = buildTree(root, cname, person);
  78.     }
  79.  
  80.     int q; scanf("%d", &q);
  81.     for(int i = 0; i < q; i++) {
  82.         scanf("%d", &inst);
  83.         if(inst == 0) {
  84.             scanf("%s", cname);
  85.             Tree *newroot = search(root, cname);
  86.             if(newroot == NULL) printf("None\n");
  87.             else printf("%s\n", (newroot->member)->name);
  88.         }
  89.         else {
  90.             scanf("%s%s", person, cname);
  91.             Tree *newroot = search(root, cname);
  92.             // printf("club %s\n", newroot->name);
  93.             if(newroot == NULL) printf("-1\n");
  94.             else if(findMember(newroot->member, person)) printf("1\n");
  95.             else printf("0\n");
  96.         }
  97.     }
  98. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top