2513

/*
无向图,欧拉回路或者通路的判定。
*/

// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>

#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>

#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>

using namespace std;

// typedef
typedef long long LL;
typedef unsigned long long ULL;

// 
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
#define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
#define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
#define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
#define FORii(a,b,c) for(int ii=(a);ii<(b);ii+=c)
#define FORjj(a,b,c) for(int jj=(a);jj<(b);jj+=c)
#define FORkk(a,b,c) for(int kk=(a);kk<(b);kk+=c)

#define FF(i,a)    for(int i=0;i<(a);i++)
#define FFD(i,a)   for(int i=(a)-1;i>=0;i--)

#define Z(a) (a<<1)
#define Y(a) (a>>1)

const double eps = 1e-6;
const double INFf = 1e10;
const int INFi = 1000000000;
const double Pi = acos(-1.0);

template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
	if(x>y) return x;
	return y;
}
template<class T> inline T TMIN(T x,T y)
{
	if(x<y) return x;
	return y;
}
template<class T> inline void SWAP(T &x,T &y)
{
	T t = x;
	x = y;
	y = t;
}
template<class T> inline T MMAX(T x,T y,T z)
{
	return TMAX(TMAX(x,y),z);
}


// code begin
struct trie_node
{
	trie_node* next[26];
	int num;
};

trie_node mem[5000010];
int dx;
trie_node *root;
int num;
int dg[500001];
char in1[12],in2[12];

int father[500001],rak[500001];

trie_node* CreateNode()
{
	trie_node* p = &mem[dx++];
	FORi(0,26,1)
		p->next[i] = NULL;
	p->num = -1;
	return p;
}

int InsertNode(char *tar)
{
	int id;
	trie_node* p =root;
	bool f = false;
	while(*tar)
	{
		id = *tar-'a';
		if(p->next[id]==NULL)
		{
			p->next[id] = CreateNode();
			f = true;
		}
		p = p->next[id];
		tar++;
	}
	if(f)
	{
		father[num] = num;
		rak[num] = 1;
		p->num = num++;
	}
	return p->num;
}
bool Have()
{
	int cnt = 0;
	FORi(0,num,1)
	{
		if(dg[i]&1)
			cnt++;
	}
	if(cnt==2 || cnt==0) return true;
	return false;
}

int Findit(int i)
{
	if(father[i]!=i)
		father[i] = Findit(father[i]);
	return father[i];
}
void Joint(int a,int b)
{
	a = Findit(a);
	b = Findit(b);
	if(a==b) return;
	if(rak[a]<rak[b])
	{
		father[a] = b;
		rak[b]+=rak[a];
	}
	else
	{
		father[b] = a;
		rak[a]+=rak[b];
	}
}
bool Iscon()
{
	return rak[Findit(0)]==num;
}

int main()
{
	read;
	write;
	int a,b;
	num = 0;
	memset(dg,0,sizeof(dg));
	dx = 0;
	root = CreateNode();
	
	while(scanf("%s %s",in1,in2)==2)
	{
		a = InsertNode(in1);
		b = InsertNode(in2); 
		dg[a]++;
		dg[b]++;
		Joint(a,b);
	}

	//判断是否有欧拉回路或者通路
	if( num==0 || (Iscon() && Have()) ) printf("Possible\n");
	else printf("Impossible\n");
	return 0;
}
posted @ 2011-04-07 21:02  AC2012  阅读(184)  评论(0编辑  收藏  举报