Poj 2513 colored sticks (Euler's loop, Dictionary tree, and query set)

Source: Internet
Author: User

Question: given some sticks, both ends of the sticks are painted with color, to determine whether they can be connected to the beginning and end of the sticks, a straight line, requiring different sticks to be connected to one side must be the same color.

Go to: kuangbing

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.

Graph connectivity can be determined by querying sets.

It is easier to calculate the degree.

View code // trie is written with a pointer for the first time. It was originally a two-dimensional array and found that the array could not be opened, so we had to delete it and change it to a pointer. // to solve this problem, we knew the Euler's loop determination, trie # include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <queue> using namespace STD; const int n = 500010; char S1 [15], S2 [15]; int Fa [N], U, V, in [N], color = 1; struct trie {trie * Ch [26]; int val; void Init () {for (INT I = 0; I <26; I ++) CH [I] = NULL; val = 0;} trie () {for (INT I = 0; I <26; I ++) CH [I] = NULL; val = 0;} int idx (char c) {return C-'A';} int ins_find (char * s, trie * t) {int n = strlen (s); For (INT I = 0; I <n; I ++) {int c = idx (s [I]); If (! T-> CH [c]) {T-> CH [c] = new trie ();} t = T-> CH [c];} If (! T-> Val) T-> val = color ++; return T-> Val ;}} * TR; int find (int x) {If (Fa [x] <0) return X; return Fa [x] = find (Fa [x]);} int main () {// freopen ("in.txt", "r", stdin); memset (FA,-1, sizeof (FA); Tr = new trie (); while (scanf ("% S % s", S1, S2)> 0) {u = tr-> ins_find (S1, TR), V = tr-> ins_find (S2, tr); in [u] ++, in [v] ++; u = find (u), V = find (V); If (u! = V) Fa [u] = V;} int num1 = 0, num2 = 0; For (INT I = 1; I <color; I ++) {If (in [I] & 1) num1 ++; If (Fa [I] =-1) num2 ++ ;} if (num1 = 0 | num1 = 2) & num2 <2) puts ("possible"); else puts ("impossible"); 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.