50104. Students and Clubs

難度:4/5

Second Try: 4/5 Used Time: 33:35

I didn't debug once but I took me that long...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

struct stnt_node {
char name[64];
struct stnt_node *left;
struct stnt_node *right;
};
typedef struct stnt_node stnt_node;

struct club_node {
char name[64];
char leader[64];
struct club_node *left;
struct club_node *right;
struct stnt_node *stnt_set;
};
typedef struct club_node club_node;

stnt_node *find_stnt(stnt_node *root, char* s_name){
if(root == NULL){
return NULL;
}
else{
int rtv = strcmp(s_name, root->name);
if(rtv == 0) return root;
else if(rtv < 0) return find_stnt(root->left, s_name);
else return find_stnt(root->right, s_name);
}
}
stnt_node *insert_stnt(stnt_node *root, char* s_name){
struct stnt_node *now;
if(root == NULL){
now = (stnt_node*) malloc(sizeof(stnt_node));
strcpy(now->name, s_name);
now->left = NULL, now->right = NULL;
return now;
}
else{
if(strcmp(s_name, root->name) < 0) root->left = insert_stnt(root->left, s_name);
else root->right = insert_stnt(root->right, s_name);
return root;
}
}

club_node *find_club(club_node *root, char* c_name){
if(root == NULL){
return NULL;
}
else{
int rtv = strcmp(c_name, root->name);
if(rtv == 0) return root;
else if(rtv < 0) return find_club(root->left, c_name);
else return find_club(root->right, c_name);
}
}
club_node *insert_club(club_node *root, char* c_name, char* s_name){
struct club_node *now;
if(root == NULL){
now = (club_node*) malloc(sizeof(club_node));
strcpy(now->name, c_name);
strcpy(now->leader, s_name);
now->left = NULL, now->right = NULL, now->stnt_set = NULL;
return now;
}
else{
if(strcmp(c_name, root->name) < 0) root->left = insert_club(root->left, c_name, s_name);
else root->right = insert_club(root->right, c_name, s_name);
return root;
}
}

int k;
int q;

int main(){
scanf("%d", &k);
int n;
char s_name[64], c_name[64];

club_node* root = NULL;
for(int i = 0; i < k; i++){
scanf("%d %s %s", &n, s_name, c_name);
if(n == 0) root = insert_club(root, c_name, s_name);
else{
club_node* this_club = find_club(root, c_name);
this_club->stnt_set = insert_stnt(this_club->stnt_set, s_name);
}
}
scanf("%d", &q);
for(int i = 0; i < q; i++){
scanf("%d", &n);
if(n == 0){
scanf("%s", c_name);
club_node* this_club = find_club(root, c_name);
if(this_club != NULL) printf("%s\n", this_club->leader);
else printf("None\n");
}
else{
scanf("%s %s", s_name, c_name);
club_node* this_club = find_club(root, c_name);
if(this_club == NULL) printf("-1\n");
else if(strcmp(s_name, this_club->leader) == 0) printf("1\n");
else if(find_stnt(this_club->stnt_set, s_name) != NULL) printf("1\n");
else printf("0\n");
}
}
}


50104. Students and Clubs
https://aaronlin1229.github.io/judgegirl_50104/
Author
Akizumi
Posted on
July 17, 2023
Licensed under