Poj2513: colored sticks (Dictionary tree + Euler path + and query set)

Source: Internet
Author: User

Http://poj.org/problem? Id = 2513

Description

You are given a bunch of wooden sticks. each endpoint of each stick is colored with some color. is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

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 10 characters. there is no more than 250000 sticks.

Output

If the sticks can be aligned in the desired way, output a single line saying possible, otherwise output impossible.

Sample Input

blue redred violetcyan blueblue magentamagenta cyan

Sample output

Possible

Hint

Huge input, scanf is recommended.

General question:

Given some wood sticks, both ends of the wood sticks are painted with color, to determine whether it can be connected to the beginning and end of the wood sticks, a straight line, different wood sticks must be connected to one side of the same color.

It is known from graph theory knowledge,The necessary and sufficient condition for an undirected graph to have an Euler's path is:

① The graph is connected;

② All nodes have an even degree or only two nodes with an odd degree.

Analysis: Euler's path problem. check whether there is Euler's path (concept of Euler's loop)

1. Theorem: a sufficient condition for undirected graph G to have Euler's path is that G is a connected graph, and G has only two odd-degree nodes or no odd-degree nodes.
(1) When G is a connected graph with only two odd nodes, the Euler's path of G must use the two nodes as the endpoint.
(2) When G is a connected graph without a singular node, G must have an Euler loop.

2. A directed graph D has an Euler's path. if and only when D is connected, and except two vertices, the inbound degrees of other vertices are equal to the outbound degrees. In these two special vertices, the inbound degree of one vertex is 1 larger than the outbound degree, and the inbound degree of the other vertex is 1 smaller than the outbound degree. inference: A directed graph D is an Euclidean (with Euler's loop). if and only if D is connected, the outbound degrees of all vertices are equal to the inbound degrees.

3. The trie tree is a common method for storing names.

Solution: Check the set to determine whether to connect. Use trie to store each color. Check whether it meets the requirements.

Question Analysis: If you don't want to look at the problem solution, you don't even know to use the Euler's path algorithm. This question has never been resolved before... There is a pitfall in this question. It is also possible if you do not input or output anything. Euler's path is a stroke.
# Include <iostream> # include <string. h> # include <stdio. h> # include <stdlib. h> # define n 500002 using namespace STD; typedef struct node {int flag; struct node * Next [26];} * tree, node; char a [10], B [10]; int du [n + 10], Bin [n + 10], num; int findx (INT X) {int r = X; while (bin [R]! = R) r = bin [R]; Int J = x, K; while (J! = R) {k = bin [J]; bin [J] = r; j = K;} return r;} void merge (int x, int y) {int FX = findx (x); int FY = findx (y); If (FX! = FY) Bin [FY] = FX;} void creat (Tree & T) {T = (tree) malloc (sizeof (node); t-> flag = 0; for (INT I = 0; I <26; I ++) T-> next [I] = NULL;} int inseart (Tree & T, char * s) {int T, L, flag2 = 0; L = strlen (s); tree P = t; for (INT I = 0; I <L; I ++) {T = s [I]-'A'; If (p-> next [T] = NULL) {creat (p-> next [T]); flag2 = 1;} p = p-> next [T];} If (flag2 = 1) {num ++; P-> flag = num; return p-> flag;} return p-> flag;} void delete1 (tree p) {for (INT I = 0; I <26; I ++) {If (p-> next [I]) {delete1 (p-> next [I]) ;}} free (p );} int main () {tree T; creat (t); For (INT I = 0; I <= N; I ++) {bin [I] = I; du [I] = 0;} num = 0; int sum = 0; while (scanf ("% S % s", a, B )! = EOF) {int Tx = inseart (t, a); Du [TX] ++; int ty = inseart (T, B); Du [ty] ++; merge (TX, Ty) ;}for (Int J = 1; j <= num; j ++) {If (Du [J] % 2) sum ++; if (sum> 2) {printf ("impossible \ n"); delete1 (t); Return 0 ;}} if (sum = 1) printf ("impossible \ n"); else {sum = 0; // int Ss = findx (1); For (INT I = 1; I <= num; I ++) {if (I = bin [I]) sum ++; // This question is flawed. If no input is made, CRL + Z is the output "possible ";} if (sum = 1 | sum = 0) printf ("Possible \ n"); else printf ("impossible \ n");} delete1 (t ); return 0 ;}

Solution:

We can solve this problem by using the knowledge of the Central Europe road in graph theory. First, we can regard the two ends of the sticks as nodes and the sticks as edges.The same color is the same node.

The problem is converted:

Specifies whether a graph exists."One stroke"Each point is painted and each side is painted.

This isCheck whether there is Euler's Euler-path in the graph..

Looking back at the classic "Seven bridges", I believe many of you will soon understand what Euler's road is. I will not explain it here.

 

It is known from graph theory knowledge,The necessary and sufficient condition for an undirected graph to have an Euler's path is:

① The graph is connected;

② All nodes have an even degree or only two nodes with an odd degree.

 

Where① Graph connectivityIt is troublesome to use the program to judge. Let's take a look.

Let's talk about it first.② About the degreeMethod:

 

Blue

Magenta

Viotlet

Cyan

Red

The node degrees are measured by the number of occurrences of colors. In the example, if blue appears three times (whether outbound or inbound), the blue node degrees are 3. Similarly, we can also obtain the degree of all other nodes through input, so we have:

Blue = 3

Red = 2

Violet = 1

Cyan = 2

Magenta = 2

 

 

You can use a one-dimensional array to record the data, and then modulo 2 to determine the parity of the color node.

As long as the number of knots in odd degrees is = 1 or> = 3, even if the ① graph is connected, the Euler's path does not exist.

 

However, if the number of knots in odd degrees is 0 or = 2① Graph connectivityProof:

 

Proof① Graph connectivity, UseAnd check the set mergesetIs a very efficient method.


Knowledge points:

1. dictionary tree;

2. Euler's path: it also examines whether or not it is a connected graph;

3. query the set and its optimization method (path compression ).

Output:

Possible: odd degree node COUNT = 0 or = 2 and graph connection

Impossible: odd degree node COUNT = 1 or> = 3 or graph not connected

Poj2513: colored sticks (Dictionary tree + Euler path + and query set)

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.