UVA 10441Catenyms "Euler Road"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.