/* ACM North Central Region, 1994-95 Problem D, Identifying Concurrent Events Ed Karrels, May 1996 */ #include #include #include typedef struct Node { int idx; char *name; struct Node *next, *child, *sibling; } Node; Node *head=0, *tail=0; int n_nodes = 0; Node *FindNode(char *str) { Node *p; p = head; while (p && strcmp(p->name, str)) p = p->next; if (!p) { fprintf(stderr, "name '%s' not found.\n", str); exit(1); } return p; } Node *IndNode(int i) { Node *p; p = head; while (i--) p = p->next; return p; } void FreeNodes() { Node *node; node = head; while (node) { head = node->next; free((char*)node); node = head; } head = tail = 0; n_nodes = 0; } void AddChild(Node *par, Node *child) { Node *node; if (par->child) { node = par->child; while (node->sibling) node = node->sibling; node->sibling = child; } else { par->child = child; } } Node *NewNode(char *name, Node *parent) { Node *node; node = (Node*)malloc(sizeof(Node)); node->idx = n_nodes++; node->name = strdup(name); if (parent) AddChild(parent, node); if (tail) { tail->next = node; tail = tail->next; } else { head = tail = node; } node->next = node->child = node->sibling = 0; return node; } #define REL(i, j) rel[i*n_nodes + j] void MarkKids(Node *node, Node *child, int *rel) { if (child) { REL(node->idx, child->idx) = 1; REL(child->idx, node->idx) = 1; MarkKids(node, child->child, rel); MarkKids(node, child->sibling, rel); } } void Print(int *rel) { Node *node; int i, j; node = head; while (node) { printf("%2d: %s, next=%s, child=%s, sibling=%s\n", node->idx, node->name, node->next ? node->next->name : "<>", node->child ? node->child->name : "<>", node->sibling ? node->sibling->name : "<>"); node = node->next; } for (i=0; iname, node2->name); */ AddChild(node1, node2); } node1 = head; while (node1) { MarkKids(node1, node1->child, rel); node1 = node1->next; } /* Print(rel); */ b = 0; for (i=0; iname, IndNode(j)->name); */ b++; } } } if (b) { printf("Case %d, %d concurrent events: \n", c++, b); if (b>2) b=2; for (i=0; iname, IndNode(j)->name); b--; } } } putchar('\n'); } else { printf("Case %d, no concurrent events.\n", c++); } FreeNodes(); free((char*)rel); scanf("%d", &a); } return 0; }