POJ 2513 Colored Sticks(字典树+无向图欧拉路)

题目链接

无向图啊。。。

  1 #include <stdio.h>

  2 #include <string.h>

  3 #include <stdlib.h>

  4 int o1[510401],o[510401],num;

  5 struct node

  6 {

  7     int flag;

  8     struct node *next[26];

  9 };

 10 struct node *build()

 11 {

 12     int i;

 13     struct node *p;

 14     p = (struct node *)malloc(sizeof(struct node));

 15     p -> flag = 0;

 16     for(i = 0;i <= 25;i ++)

 17     {

 18         p -> next[i] = NULL;

 19     }

 20     return p;

 21 }

 22 int insert(struct node *head,char *str)

 23 {

 24     int i;

 25     struct node *p;

 26     p = head;

 27     for(i = 0;str[i];i ++)

 28     {

 29         if(p -> next[str[i]-'a'] == NULL)

 30         p -> next[str[i] - 'a'] = build();

 31         p = p -> next[str[i]-'a'];

 32     }

 33     if(p -> flag == 0)

 34     {

 35         p -> flag = num;

 36         num ++;

 37         return p -> flag;

 38     }

 39     else

 40     return p->flag;

 41 }

 42 int find(int x)

 43 {

 44     int r,t;

 45     r = x;

 46     while(x != o[x])

 47     x = o[x];

 48     while(r != x)

 49     {

 50         t = o[r];

 51         o[r] = x;

 52         r = t;

 53     }

 54     return x;

 55 }

 56 void merge(int x,int y)

 57 {

 58     x = find(x);

 59     y = find(y);

 60     if(x != y)

 61         o[x] = y;

 62 }

 63 int main()

 64 {

 65     char str1[20],str2[20];

 66     int i,x,y,z,k,ru;

 67     struct node *head;

 68     head = build();

 69     num = 1;

 70     for(i = 1;i <= 510000;i ++)

 71     o[i] = i;

 72     while(scanf("%s%s",str1,str2)!=EOF)

 73     {

 74         x = insert(head,str1);

 75         o1[x] ++;

 76         y = insert(head,str2);

 77         o1[y] ++;

 78         merge(x,y);

 79     }

 80     z = 1;

 81     k = find(1);

 82     for(i = 1;i <= num-1;i ++)

 83     {

 84         if(find(i) != k)

 85         {

 86             z = 0;

 87             break;

 88         }

 89     }

 90     ru = 0;

 91     for(i = 1;i <= num-1&&z;i ++)

 92     {

 93        if(o1[i]%2)

 94        ru ++;

 95     }

 96     if(z&&ru == 2)

 97     printf("Possible\n");

 98     else if(z&&!ru)

 99     printf("Possible\n");

100     else

101     printf("Impossible\n");

102     return 0;

103 }

你可能感兴趣的:(color)