/* Wed Nov 12 20:37:57 JST 2003 : Start */ /* Wed Nov 12 21:30:40 JST 2003 : Wrong answer */ /* Wed Nov 12 21:42:52 JST 2003 : Accepted */ #include int A, b; char atom[2][300][3]; int katom; int atomid[26][27]; int adj[2][300][300]; typedef struct _around_t { int atom; int type; } around_t; typedef struct _atom_t { int self; int naround; around_t around[12]; } atom_t; atom_t satom[2][300]; int aroundcmp(const void *_x, const void *_y) { int i; around_t *x = (around_t *)_x, *y = (around_t *)_y; if (x->atom < y->atom) return -1; if (x->atom > y->atom) return +1; if (x->type < y->type) return -1; if (x->type > y->type) return +1; return 0; } int atomcmp(const void *_x, const void *_y) { int i; atom_t *x = (atom_t *)_x, *y = (atom_t *)_y; if (x->self < y->self ) return -1; if (x->self > y->self ) return +1; if (x->naround < y->naround) return -1; if (x->naround > y->naround) return +1; for (i = 0; i < x->naround; i++) { if (x->around[i].atom < y->around[i].atom) return -1; if (x->around[i].atom > y->around[i].atom) return +1; if (x->around[i].type < y->around[i].type) return -1; if (x->around[i].type > y->around[i].type) return +1; } return 0; } int main() { int i, j, m; int c1, c2, a1, a2; int equal; while (1) { if (0 == scanf("%d %d", &A, &b)) { break; } katom = 0; for (i = 0; i < 26; i++) { for (j = 0; j < 27; j++) { atomid[i][j] = -1; } } for (m = 0; m < 2; m++) { for (i = 0; i < 300; i++) { for (j = 0; j < 300; j++) { adj[m][i][j] = 0; } } for (i = 0; i < A; i++) { scanf("%s", atom[m][i]); c1 = atom[m][i][0] - 'A'; if (atom[m][i][1] == '\0') { c2 = 0; } else { c2 = atom[m][i][1] - 'a' + 1; } if (atomid[c1][c2] < 0) { atomid[c1][c2] = katom; katom++; } } for (i = 0; i < b; i++) { scanf("%d %d", &a1, &a2); scanf("%d", &adj[m][a1-1][a2-1]); adj[m][a2-1][a1-1] = adj[m][a1-1][a2-1]; } for (i = 0; i < A; i++) { c1 = atom[m][i][0] - 'A'; if (atom[m][i][1] == '\0') { c2 = 0; } else { c2 = atom[m][i][1] - 'a' + 1; } satom[m][i].self = atomid[c1][c2]; satom[m][i].naround = 0; for (j = 0; j < A; j++) { if (adj[m][i][j] > 0) { c1 = atom[m][j][0] - 'A'; if (atom[m][j][1] == '\0') { c2 = 0; } else { c2 = atom[m][j][1] - 'a' + 1; } satom[m][i].around[ satom[m][i].naround ].atom = atomid[c1][c2]; satom[m][i].around[ satom[m][i].naround ].type = adj[m][i][j]; satom[m][i].naround++; } } qsort(satom[m][i].around, satom[m][i].naround, sizeof(around_t), aroundcmp); } qsort(satom[m], A, sizeof(atom_t), atomcmp); } /* for (m = 0; m < 2; m++) { for (i = 0; i < A; i++) { printf("%d :", satom[m][i].self); for (j = 0; j < satom[0][i].naround; j++) { printf(" %d(%d)", satom[m][i].around[j].atom, satom[m][i].around[j].type); } printf("\n"); } printf("\n"); } */ equal = 1; for (i = 0; i < A; i++) { if (satom[0][i].self != satom[1][i].self || satom[0][i].naround != satom[1][i].naround) { equal = 0; goto quit; } for (j = 0; j < satom[0][i].naround; j++) { if (satom[0][i].around[j].atom != satom[1][i].around[j].atom || satom[0][i].around[j].type != satom[1][i].around[j].type) { equal = 0; goto quit; } } } quit:; if (equal) { printf("EQUAL\n"); } else { printf("NOT EQUAL\n"); } } return 0; }