poj2513Colored Sticks

来源:互联网 发布:photoshop cc mac版 编辑:程序博客网 时间:2024/05/21 09:28
#include<stdio.h>#include<string.h>#include<stdlib.h>int p[500005],rank[500005],degree[500005],sum,num=0;char a[15],b[15];typedef struct node{int key;struct node *next[26];}trie;trie *root;int n,m;void init(){for(int i=0;i<=500100;i++){p[i]=i;rank[i]=0;degree[i]=0;}}int find(int x){if(x==p[x]) return x;return p[x]=find(p[x]);}void combine(int a,int b){if(a==b) return;    if(rank[a]>rank[b]) p[b]=a;    else{    if(rank[a]==rank[b]) rank[b]++;    p[a]=b;    }}trie *New(){int i,k;trie *p;p=(trie*)malloc(sizeof(trie));p->key=-1;for(i=0;i<26;i++){p->next[i]=NULL;}return p;}int Insert(trie*root,char*s){int i,k;trie *p=root;i=0;while(s[i]){k=s[i++]-'a';if(p->next[k]==NULL) p->next[k]=New();p=p->next[k];}if(p->key==-1) p->key=++num;return p->key;}int main(){int i,j,k,x,y;init();root=New();while(scanf("%s%s",a,b)!=EOF){x=Insert(root,a);y=Insert(root,b);degree[x]++,degree[y]++;combine(find(x),find(y));}sum=0;for(i=1;i<num;i++) if(degree[i]%2) sum++;if(sum>2) printf("Impossible\n");else{for(i=2;i<num;i++){if(find(i)!=find(1)){printf("Impossible\n");return 0;}}printf("Possible\n");}return 0;}

0 0