1 /*======================================================================
2 * Author : kevin
3 * Filename : ColoredSticks.cpp
4 * Creat time : 2014-07-31 10:13
5 * Description :
6 ========================================================================*/
7 #include <iostream>
8 #include <algorithm>
9 #include <cstdio>
10 #include <cstring>
11 #include <queue>
12 #include <cmath>
13 #define clr(a,b) memset(a,b,sizeof(a))
14 #define M 500005
15 using namespace std;
16 struct Trie{
17 Trie *next[26];
18 int num;
19 };
20 Trie *root;
21 int father[M+5],d[M+5];
22 int cnt = 1;
23 int CreateTrie(char *str)
24 {
25 Trie *p = root,*q;
26 for(int i = 0; str[i]; i++){
27 int id = str[i] - ‘a‘;
28 if(p->next[id] == NULL){
29 q = (Trie *)malloc(sizeof(Trie));
30 for(int j = 0; j < 26; j++){
31 q->next[j] = NULL;
32 q->num = 0;
33 }
34 p->next[id] = q;
35 p = p->next[id];
36 }
37 else{
38 p = p->next[id];
39 }
40 }
41 if(!(p->num))
42 p->num = cnt++;
43 return p->num;
44 }
45
46 void DelTrie(Trie *T)
47 {
48 int i;
49 if(T == NULL) return;
50 for(i = 0; i < 26; i++){
51 if(T->next[i] != NULL){
52 DelTrie(T->next[i]);
53 }
54 }
55 free(T);
56 }
57 int FindSet(int x)
58 {
59 if(x != father[x]){
60 father[x] = FindSet(father[x]);
61 }
62 return father[x];
63 }
64 void Union(int x,int y)
65 {
66 x = FindSet(x);
67 y = FindSet(y);
68 father[x] = y;
69 }
70 int main(int argc,char *argv[])
71 {
72 char str1[15],str2[15];
73 for(int i = 0; i < M; i++){
74 father[i] = i;
75 d[i] = 0;
76 }
77 root = (Trie*)malloc(sizeof(Trie));
78 for(int i = 0; i < 26; i++){
79 root->next[i] = NULL;
80 root->num = 0;
81 }
82 while(scanf("%s %s",str1,str2)!=EOF){
83 int x1 = CreateTrie(str1);
84 int x2 = CreateTrie(str2);
85 Union(x1,x2);
86 d[x1]++;
87 d[x2]++;
88 }
89 int cnt_father = 0,cnt_d = 0;
90 int t = FindSet(1);
91 for(int i = 1; i < cnt; i++){
92 int x = FindSet(i);
93 if(x == t) cnt_father++;
94 if(d[i] % 2){
95 cnt_d++;
96 }
97 }
98 if(cnt_father != cnt-1 || !(cnt_d == 0 || cnt_d == 2)){
99 printf("Impossible\n");
100 }
101 else{
102 printf("Possible\n");
103 }
104 DelTrie(root);
105 return 0;
106 }