Sticks POJ2513-Colored

Source: Internet
Author: User

 

Reprinted please indicate the source: Thank you

Http://user.qzone.qq.com/289065406/blog/1304742541

 

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.

 

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.

Basic method:

When the input n nodes are n trees, there is a forest of N trees. At this time, each tree has a unique node (Root ), the ancestor of the node is itself. Then, by constantly inputting the edge, we can get the relationship between a certain two nodes (sets) and then combine these two nodes (sets). Then these two sets constitute a new set, all nodes in the set have a common new ancestor, which is the root of the Set (tree.

At last, as long as any node is enumerated and all of them have the same ancestor, it can prove that the graph is connected.

 

However, simply using and querying a set Will time out, because this will lead to the average O (n/2) time spent each time the ancestor looks for a node. Worst case, when N = 50 W, O (n/2) is about 25 ms, it takes 50 W * 25 ms to determine whether 50 W nodes have a common ancestor, tie dingchao, not forget =

 

Therefore, the path must be compressed when a node K is used and queried. When a node K's ancestor is searched several times, the path is constantly searched through the parent node for ancestor nodes, by the way, the ancestor of all the nodes passing through from K to the final ancestor node s points to S, so that the time for future searches can be reduced to O (1)

 

Because the concurrent query set must use the subscript of the array and the stored object, int is a convenient method, but the "color node" of the question is string, which is not convenient to use and query the set, that is, map cannot be used. Although STL map is based on hash, it is not efficient. It will time out when used in this question.

 

Therefore, you can use the trie dictionary tree to obtain the int ID corresponding to each color word. It can be said that using trie to map string to int one by one is the key to subsequent processing of this question. The method for dynamically creating a dictionary tree goes to Baidu. I will not mention it here. Here I will use a diagram to briefly describe how to use the trie dictionary tree to identify the first color word blue:

 

This topic involves multiple basic data structures and algorithms. It is very comprehensive and representative. It is indeed beneficial to answer question.

 

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

 

PS: note that when creating a trietree linked list, C ++ does not have null. Use 0 instead of null.

 

 

 

/* Trietree + mergeset + eulerpath * // memory time // 77460 K 2047 MS # include <iostream> using namespace STD; const int large = 500000; // 25 W sticks, class trietree_node with 50 million endpoints // dictionary Tree node {public: bool flag; // indicates whether the string consisting of the dictionary tree from the root to the current node is one (color) int ID; // The current color (node) number trietree_node * Next [27]; trietree_node () // initial {flag = false; id = 0; memset (next, 0, sizeof (next); // 0 <-> null} root; // dictionary root node int color = 0; // color number pointer, eventually, the total number of colors is int. Degree [large + 1] = {0}; // The total degree of the ID node int ancestor [large + 1]; // ID node ancestor/* searches for the final ancestor of the x node */INT find (int x) {If (ancestor [x]! = X) ancestor [x] = find (ancestor [x]); // return ancestor [X];} /* merge A and B sets */void union_set (int A, int B) {int Pa = find (a); int Pb = find (B ); ancestor [Pb] = PA; // use the ancestor of A as the ancestor return of B;} // use the dictionary tree to construct the int ing from string s to number int Hash (char * s) {trietree_node * P = & root; // search for words from the root node of trietree (created if the word does not exist) int Len = 0; while (s [Len]! = '\ 0') {int Index = s [Len ++]-'A'; // lowercase letter ~ Z maps to numbers 1 ~ 26. If (! P-> next [Index]) // when the index does not exist, construct the index p-> next [Index] = new trietree_node; P = p-> next [Index];} if (p-> flag) // The color word already exists return p-> ID; // return its number else // otherwise create the word {P-> flag = true; p-> id = ++ color; return p-> ID; // return the ID allocated to the new color} int main (void) {/* Initial the merge-Set */For (int K = 1; k <= large; k ++) // initialization, each node acts as an independent set ancestor [k] = K; // For a set with only one node X, the ancestor of X is itself/* input */char a [11], B [11]; while (CIN> A> B) {/* creat the trietree */INT I = hash (a); I Nt j = hash (B); // obtain the numbers of colors A and B/* Get all nodes 'degree */degree [I] ++; degree [J] ++; // record the number of occurrences of A and B colors (total degree)/* creat the merge-Set */union_set (I, j );} /* judge the Euler-path */int s = find (1); // If the graph is a connected graph, S is the ancestor of all nodes. // If the graph is not a connected graph, S is the number of nodes where one of the ancestor's int num = 0; // The number of nodes with an odd degree for (INT I = 1; I <= color; I ++) {If (degree [I] % 2 = 1) num ++; If (Num> 2) // The number of knots with an odd degree is greater than 3, euler's path must not exist {cout <"impossible" <Endl; return 0;} If (find (I )! = S) // multiple ancestors exist, the figure is a forest, not connected to {cout <"impossible" <Endl; return 0 ;}} if (num = 1) // if the number of knots with an odd number of degrees is equal to 1, there must be no cout for the Euler's path <"impossible" <Endl; else // The number of knots with an odd number of degrees is exactly equal to 2 or does not exist, there is Euler's cout <"possible" <Endl; 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.