/* Secret Agent Man */ void read_int(char *string, int *x, int *i) { sscanf(string+(*i), "%d", x); (*i) += abs(*x) / 10 + 2 + ((*x) < 0 ? 1 : 0); } int give(char *string, int rec, int *i) /* Based on the observation that it is not necessary to perform any kind of breadth-first search. The head of the organization can reach all of the spies, so you just have to find one spy that can reach the recipient spy. */ { int agent; int a2; for (agent = 0; 1; agent ++) { while (1) { read_int(string, &a2, i); if (a2 < 0) break; if (a2 == rec) return agent; } if (strlen(string+(*i)) < 2) break; } return -1; } void main(void) { char line[80]; int i; int rec; /* recipient of message */ int n; /* number of messages to process */ int who; /* who to give message to */ int m; gets(line); sscanf(line, "%d", &n); for (m = 1; m <= n; m ++) { gets(line); i = 0; read_int(line, &rec, &i); who = give(line, rec, &i); if (who == -1) printf("Message %d cannot be delivered. Agent #%d is unreachable.\n", m, rec); else printf("Message %d, destined for agent #%d, should be given to agent #%d.\n", m, rec, who); } }