POJ 2513--colored Sticks "Dictionary tree number && find a link && Euler road"

Source: Internet
Author: User

Colored sticks
Time Limit: 5000MS Memory Limit: 128000K
Total Submissions: 32351 Accepted: 8536

Description

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

Input

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

Output

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

Sample Input

Blue redred Violetcyan blueblue magentamagenta Cyan

Sample Output

Possible

The necessary and sufficient conditions for the existence of the Euler road in the absence of graphs are:

The ① diagram is connected;

② the degree of all nodes is even, or there are only two nodes with an odd degree.


Here are two methods, one is the direct allocation of memory, time:985MS

#include <cstdio> #include <cstring> #include <iostream> #define MAXN 500500using namespace Std;struct    node {int id; Node *next[30];}; Node *root;node TREE[MAXN * 10];//first allocates memory.    int DU[MAXN], PER[MAXN], color;//color number int ans = 0;node *create () {Node *p = &tree[ans++];    for (int i = 0; i <; ++i) P--next[i] = NULL; return p;};    void Insert (char *s, int id) {node *p = root;        for (int i = 0; s[i]; ++i) {int k = s[i]-' a ';        if (P-next[k] = = NULL) P--next[k] = create ();    p = P->next[k]; } P-id = ID;}    int search (char *s) {node *p = root;        for (int i = 0; s[i]; ++i) {int k = s[i]-' a ';        if (P-next[k] = = NULL) return 0;    p = P-next[k]; } return p->id;}    void Init () {for (int i = 1; I <= maxn; ++i) per[i] = i;    memset (Du, 0, sizeof (DU)); color = 0;}    int find (int x) {int r = x; while (r! = Per[r]) R = Per[r];    Per[x] = r; return r;}    void join (int x, int y) {int fx = find (x);    int fy = find (y); PER[FX] = fy;}     BOOL Judge () {int sum = 0;    for (int i = 1; I <= color; ++i) if (du[i]% 2 = = 1) sum++;    if (sum! = 0 && sum! = 2) return false;    int k = find (1);    for (int i = 2; I <= color; ++i) if (k! = Find (i)) return false; return true;}    int main () {char s1[15], s2[15];    Init ();    Root = Create ();        while (CIN >> S1 >> s2) {int x = search (S1);        int y = search (s2);        if (!x) insert (s1,x = ++color);        if (!y) insert (s2,y = ++color);        du[x]++, du[y]++;    Join (x, y);    } if (judge ()) printf ("possible\n");    else printf ("impossible\n"); return 0;}

Second, dynamically allocating memory for a bit longer:1860MS

#include <cstdio> #include <cstring> #include <iostream> #define MAXN 500500using namespace Std;struct Node {int id;//the number of the record color node *next[30];}; Node *root;//node TREE[MAXN * 10];//first allocates memory. int DU[MAXN], per[maxn], color;//int ans = 0;//node *create () {//Node *p = &tree[ans++];//for (int i = 0; I &lt ; 26;    ++i)//P--next[i] = null;//return p;//};void insert (char *s, int id) {node *p = root;        for (int i = 0; s[i]; ++i) {int k = s[i]-' a ';        if (P-next[k] = = NULL) {Node *q = new node;        memset (Q-next,null,sizeof, Q-Next);         P--next[k]= Q;}    p = P->next[k]; } P-id = ID;}    int search (char *s) {node *p = root;        for (int i = 0; s[i]; ++i) {int k = s[i]-' a ';        if (P-next[k] = = NULL) return 0;    p = P-next[k]; } return p->id;}    void Init () {for (int i = 1; I <= maxn; ++i) per[i] = i; memset (Du, 0, sizeof (du)); color = 0;}    void Initroot () {root = new Node;memset (Root-Next, NULL, sizeof (Root->next));} int find (int x) {int r = x;    while (r! = Per[r]) R = Per[r];    Per[x] = r; return r;}    void join (int x, int y) {int fx = find (x);    int fy = find (y); PER[FX] = fy;}     BOOL Judge () {int sum = 0;    for (int i = 1; I <= color; ++i) if (du[i]% 2 = = 1) sum++;    if (sum! = 0 && sum! = 2) return false;    int k = find (1);    for (int i = 2; I <= color; ++i) if (k! = Find (i)) return false; return true;}    int main () {char s1[15], s2[15];    Init ();    Initroot ();    Root = Create ();        while (CIN >> S1 >> s2) {int x = search (S1);        int y = search (s2);        if (!x) insert (s1,x = ++color);        if (!y) insert (s2,y = ++color);        du[x]++, du[y]++;    Join (x, y);    } if (judge ()) printf ("possible\n");    else printf ("impossible\n"); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2513--colored Sticks "Dictionary tree number && find a link && Euler road"

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.