Title Link: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=41674
Test instructions: Given some words, you can connect a word if it has the same tail letter as the first letter of the other. Ask if you can use each word once, connect all the words, and then output the sequence with the smallest dictionary order.
Code:
#include <stdio.h>#include <ctime>#include <math.h>#include <limits.h>#include <complex>#include <string>#include <functional>#include <iterator>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <map>#include <list>#include <bitset>#include <sstream>#include <iomanip>#include <fstream>#include <iostream>#include <ctime>#include <cmath>#include <cstring>#include <cstdio>#include <time.h>#include <ctype.h>#include <string.h>#include <assert.h>using namespace STD;intTintNstrings[1010];structedge{intTo, next;intIndexBOOLFlag;} edge[ .];inthead[ -], tot;voidInit () {tot =0;memset(head,-1,sizeof(head));}voidAddedge (intUintVintIndex) {edge[tot].to = v; Edge[tot].next = Head[u]; Edge[tot].index = index; Edge[tot].flag =false; Head[u] = tot++;}intin[ -], out[ -];intCntintans[1010];voidDfsintu) { for(inti = Head[u]; I! =-1; i = edge[i].next) {if(!edge[i].flag) {Edge[i].flag =true; DFS (EDGE[I].TO); ans[cnt++] = Edge[i].index; } }}intMain () {intTCin>> T; while(t--) {scanf("%d", &n); for(inti =0; I < n; i++)Cin>> S[i]; Sort (s, S + N); Init ();memset(In,0,sizeof(in));memset(Out,0,sizeof(out));intStart = -; for(inti = n-1; I >=0; i--) {intU = s[i][0] -' A ';intv = s[i][s[i].length ()-1] -' A '; Addedge (U,v,i); out[u]++; in[v]++;if(U < start) start = U;if(v < start) start = V; }intCC1 =0, CC2 =0; for(inti =0; I < -; i++) {if(Out[i]-in[i] = =1) {cc1++; start = i; }Else if(Out[i]-in[i] = =-1) cc2++;Else if(Out[i]-in[i]! =0) CC1 =3; }if(! (CC1 = =0&& CC2 = =0) || (CC1 = =1&& CC2 = =1))) {printf("***\n");Continue; } CNT =0; DFS (start);if(cnt! = N) {printf("***\n");Continue; } for(inti = n1; I >=0; i--) {cout<< S[ans[i]];if(I! =0)printf(".");Else printf("\ n"); } }return 0;}
Copyright NOTICE: Reprint please indicate the source.
UVA 10441Catenyms "Euler Road"