#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define MAX_STRING_LEN 105
#define MAX_DAYS 1005
typedef struct Node {
char name[MAX_STRING_LEN];
struct Node *track;
struct Node *next;
} Node;
struct Node *name_to_pointer(Node *head, char name[MAX_STRING_LEN]);
int can_reach_goal(Node *head, Node *goal);
void prune_newline(char str[MAX_STRING_LEN]);
int main(void) {
Node *home = malloc(sizeof (Node));
strcpy(home->name, "home");
Node *unsw = malloc(sizeof (Node));
strcpy(unsw->name, "unsw");
home->next = unsw;
home->track = unsw;
unsw->next = NULL;
unsw->track = NULL;
Node *name_end = unsw;
int days;
scanf("%d\n", &days);
int counter = 0;
char command;
char name1[MAX_STRING_LEN];
char name2[MAX_STRING_LEN];
while (counter < days) {
scanf("%c ", &command);
if (command == 's') {
fgets(name1, MAX_STRING_LEN, stdin);
prune_newline(name1);
Node *new_node = malloc(sizeof (Node));
strcpy(new_node->name, name1);
new_node->next = NULL;
new_node->track = NULL;
name_end->next = new_node;
name_end = new_node;
} else if (command == 't') {
fgets(name1, MAX_STRING_LEN, stdin);
prune_newline(name1);
fgets(name2, MAX_STRING_LEN, stdin);
prune_newline(name2);
Node *start_node = name_to_pointer(home, name1);
Node *end_node = name_to_pointer(home, name2);
start_node->track = end_node;
}
if (can_reach_goal(home, unsw) == 1) {
printf("YES\n");
} else {
printf("NO\n");
}
counter++;
}
}
struct Node *name_to_pointer(Node *head, char name[MAX_STRING_LEN]) {
Node *current = head;
while (current != NULL) {
if (strcmp(current->name, name) == 0) {
return current;
}
current = current->next;
}
return NULL;
}
int can_reach_goal(Node *head, Node *goal) {
Node *current = head;
int counter = 0;
while (current != NULL && current != goal && counter < MAX_DAYS) {
current = current->track;
counter++;
}
if (current == goal) {
return 1;
} else {
return 0;
}
}
void prune_newline(char str[MAX_STRING_LEN]) {
if (str[strlen(str) - 1] == '\n') {
str[strlen(str) - 1] = '\0';
}
}