POJ 1386 Play on Words (to Tuola Road + and collection)

Source: Internet
Author: User
Tags in degrees

title Link:http://poj.org/problem?id=1386

Thinking Analysis: This question asks whether the word can be connected into a straight line, converted to the graph theory problem: The first letter and the tail letter of the word as a point, each word describes a letter from the beginning of the letter pointing to the edge,

Then all the words make up a direction graph, the question changes to determine whether there is a Euler road in the direction graph; There are two sufficient and necessary conditions for the existence of the Euler road in the direction diagram: The graph is connected and all points are in degrees equal to the degrees of the degree or only two points in the degree and out of the degrees, a point of penetration = out of degrees + 1, the other point into the degree = out of 1;

The code is as follows:

#include <cstdio>#include<cstring>#include<iostream>using namespacestd;Const intMax_n = -;Const intMax_m = ++Ten;intFa[max_n], show[max_n];int inch[Max_n], out[Max_n];intSet_visited[max_n];CharStr[max_m];voidInit () { for(inti =0; i < max_n; ++i) fa[i]=i; memset (inch,0,sizeof(inch)); memset ( out,0,sizeof( out)); Memset (Show,0,sizeof(show)); memset (set_visited,0,sizeof(set_visited));}intFind (inta) {    if(Fa[a] = =a)returnA; Else        returnFa[a] =Find (Fa[a]);}intUnion (intAintb) {    intFa_a =Find (a); intFa_b =Find (b); if(Fa_a = =fa_b)return-1; if(Fa_a >fa_b) Fa[fa_b]=fa_a; ElseFa[fa_a]=Fa_b; return 1;}intMain () {intcase_times, N, Len; scanf ("%d", &case_times);  while(case_times--) {scanf ("%d", &N);        Init ();  for(inti =0; I < n; ++i) {intL, R; scanf ("%s", str); Len=strlen (str); L= str[0] -'a'; R= Str[len-1] -'a'; inch[l]++;  out[r]++; SHOW[L]= Show[r] =1;        Union (l, R); }        intSet_count =0; intNot_equal =0; intIn_big_out =0, out_big_in =0; BOOLOK =true;  for(inti =0; i < max_n; ++i) {if(Show[i]) {intFa_i =Find (i); if(Set_visited[fa_i] = =0) {Set_count++; Set_visited[fa_i]=1; }                if(Set_count >1) {OK=false;  Break; }                if(inch[i]! = out[i]) not_equal++; if(inch[I] = = out[I] +1) In_big_out++; if(inch[I] +1== out[i]) out_big_in++; }        }        if(! (Not_equal = =0) || (out_big_in = =1&& In_big_out = =1&& In_big_out + out_big_in = =not_equal)))  OK=false; if(OK) printf ("Ordering is possible.\n"); Elseprintf ("The door cannot be opened.\n"); }    return 0;}

POJ 1386 Play on Words (to Tuola Road + and collection)

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.