Poj-colored Sticks (dictionary tree + check set + Euler loop) __ Dictionary tree

Source: Internet
Author: User
A-colored Sticks Time Limit:5000MS Memory Limit:128000KB 64bit IO Format:%lld &%llu Submit Status Practice POJ 2513

Description you are given a bunch of wooden sticks. Each endpoint of the stick is colored with some color. Are it possible to align the sticks in a straight line such this colors of the endpoints that's touch are the same Or?

Input input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than. There is no more than 250000 sticks.

Output If The sticks can is aligned in the desired way, output a single line saying Possible, otherwise output impossible.

Sample Input

Blue Red
red Violet
cyan Blue
Blue Magenta
Magenta Cyan

Sample Output

Possible

This problem is a more classical one. is to give n a stick, the two ends of the stick have color, judge whether the stick can be combined into one, contact the stick end must be the same color.

Given some sticks, the two ends of the stick are painted with color, ask whether the stick can be connected to the end of a straight line, requiring different stick to the side must be the same color.

Seems to be judging the Euler loop.

From the knowledge of graph theory, the necessary and sufficient conditions for the existence of Euler paths in the direction-free graphs are:

the ① graph is connected;

② all nodes have an even number of degrees, or a node with an odd number of two degrees.

the connection of graphs can be judged by using and searching the set.

the statistics of degrees are easier.

The data is larger, first you need to match the color string and the number one by one.

Although you can use map, the map is inefficient and will definitely time out.

The best way is to build a dictionary tree.

match the string to the number one by one.

Note that the data there is no stick of the situation, to output possible.

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <
math.h> #include <queue> #include <algorithm> using namespace std;
#define MAXN 500010 #define MAXS int PARENT[MAXN];
	int degree[maxn];//degree int color;//color number, starting from 0, and finally the total number of colors int find (int x) {if (parent[x]==-1) return x;
Return find (Parent[x]);
	} void Bing (int a,int b) {int t1=find (a);
	int T2=find (b);
if (t1!=t2) parent[t1]=t2;
    typedef struct TRIE_NODE {bool Isword;
	struct Trie_node *next[maxs];
int id;
}trie;
	int Insert (Trie *root,char *word) {Trie *p=root;
	int i=0;
		 	while (word[i]!= ' ") {if (p->next[word[i]-' a ']==null) {Trie *temp=new Trie;
		 	temp->isword=0;
		 	for (int j=0;j<maxs;j++) temp->next[j]=null;
		 	temp->isword=0;
		    temp->id=0;
		 p->next[word[i]-' a ']=temp;
		 } p=p->next[word[i]-' a '];
	i++;
	} if (P->isword) return p->id;
		else {p->isword=1; p->id=color++;
		Return p->id;  
 	    } void del (Trie *root) {for (int i=0;i<maxs;i++) if (root->next[i]!=null) del (root->next[i));
 Free (root);
	int main () {char str1[20],str2[20];
	Trie *root=new Trie;
	 for (int i=0;i<maxs;i++) root->next[i]=null;
	 root->isword=0;
	 root->id=0;
	 color=0;
	 Memset (parent,-1,sizeof (parent)); 
	 memset (degree,0,sizeof (degree));
	 	while (scanf ("%s%s", &AMP;STR1,&AMP;STR2)!=eof) {int T1=insert (ROOT,STR1);
	 	int T2=insert (ROOT,STR2);
	 	degree[t1]++;
	 	degree[t2]++;
	 Bing (T1,T2);
	 	The int cnt1=0,cnt2=0;//is equal to the parent array-one to find the root node for (int i=0;i<color;i++) {if (parent[i]==-1) cnt1++;
	 	if (degree[i]%2==1) cnt2++;
	 	if (cnt1>1) break;
	 if (cnt2>2) break;
	 } if ((Cnt1==0 | | cnt1==1) && (cnt2==0 | | cnt2==2))//The presence of a stick root number is 0 printf ("possible\n");
	 else printf ("impossible\n");
del (root);
 }


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.