Ultraviolet A 10129 Play on Words

Source: Internet
Author: User

UVA_10129

At first, I thought of a string as a vertex, which is equivalent to finding whether a path without repeating vertices can overwrite all vertices, but doing so will be troublesome.

Later I thought about it. In fact, I can regard a string as a directed edge, and 26 letters as vertices, in this way, it is converted to finding whether there is a path without repeating edge to cover all the sides. In fact, it is to find whether there is an Euler's path.

According to the Euler's road conclusion of the directed graph (Liu rujia Bai Shu P112), first, the graph must be connected. Second, the entry at most two points is not equal to the exit, in addition, the entry level must be 1 higher than the entry level, and the entry level of the other point must be 1 higher than the entry level.

To ensure that the graph is connected, we can use and query the set when reading the data. At the same time, we need to record the discrepancy between each vertex to prepare for subsequent work.

After reading the data, you can first determine whether the graph is connected. If the data is connected, you can calculate the output and inbound degrees of each vertex.

#include<stdio.h>
#include<string.h>
int indgr[30],outdgr[30],p[30];
char b[1010];
int find(int x)
{
return p[x]==x?x:(p[x]=find(p[x]));
}
int main()
{
int i,j,first,last,t,n,num,ok;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(indgr,0,sizeof(indgr));
memset(outdgr,0,sizeof(outdgr));
for(i=0;i<26;i++)
p[i]=i;
for(i=0;i<n;i++)
{
scanf("%s",b);
first=b[0]-'a';
last=b[strlen(b)-1]-'a';
outdgr[first]++;
indgr[last]++;
if(find(first)!=find(last))
p[find(first)]=find(last);
}
num=0;
ok=1;
for(i=0;!indgr[i]&&!outdgr[i];i++);
for(j=i+1;j<26;j++)
if((indgr[j]||outdgr[j])&&find(i)!=find(j))
{
ok=0;
break;
}
if(ok)
for(i=0;i<26;i++)
{
if(indgr[i]>outdgr[i])
{
if(indgr[i]-outdgr[i]>1)
{
ok=0;
break;
}
else
num++;
}
else if(outdgr[i]>indgr[i])
{
if(outdgr[i]-indgr[i]>1)
{
ok=0;
break;
}
else
num++;
}
}
if(!ok||num>2)
printf("The door cannot be opened.\n");
else
printf("Ordering is possible.\n");
}
return 0;
}

  

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.