• POJ2513(字典树+图的连通性判断)


    //用map映射TLE,字典树就AC了
    #include"cstdio" #include"set" using namespace std; const int MAXN=510005; const int N=26;//26个小写英文字母 struct node{ int val;//存放字符串的hash值 node* next[N]; }; node memory[MAXN]; int ant; node* root; node* create_tree() { node* p=&memory[ant++]; for(int i=0;i<N;i++) { p->next[i]=NULL; } return p; } void insert(char *s,int x) { node* p=root; for(int i=0;s[i];i++) { int k=s[i]-'a'; if(p->next[k]==NULL) p->next[k]=create_tree(); p=p->next[k]; } p->val=x; } 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 -1; p=p->next[k]; } return p->val; } int par[MAXN]; int rnk[MAXN]; void init() { for(int i=0;i<=MAXN;i++) { par[i]=i; rnk[i]=0; } } int fnd(int x) { if(par[x]==x) { return x; } return par[x]=fnd(par[x]); } void unite(int x,int y) { int a=fnd(x); int b=fnd(y); if(a==b) return; if(rnk[a]<rnk[b]) { par[a]=b; } else { par[b]=a; if(rnk[a]==rnk[b]) rnk[a]++; } } int deg[MAXN]; int cnt; /* bool judge() { int odd=0; for(int i=0;i<cnt;i++) { if(deg[i]%2==1) odd++; } if(odd!=0&&odd!=2) return false; int f=fnd(0); for(int i=1;i<cnt;i++) { if(f!=fnd(i)) return false; } return true; } */ int main() { root=create_tree(); init(); char a[11]; char b[11]; int numer=0; while(scanf("%s%s",a,b)!=EOF) { if(a[0]=='0') break; int y1,y2; y1=search(a); y2=search(b); if(y1==-1) { y1=cnt; insert(a,y1); cnt++; } if(y2==-1) { y2=cnt; insert(b,y2); cnt++; } deg[y1]++; deg[y2]++; unite(y1,y2); } int l=0; set<int> vec; for(int i=0;i<cnt;i++) { if(deg[i]%2==1) l++; } if((l==0)||l==2) //满足欧拉回路 { for(int i=0;i<cnt;i++) { int x=fnd(i); vec.insert(x); } if(vec.size()<=1) //满足图的连通性 { printf("Possible "); return 0; } } printf("Impossible "); return 0; }
  • 相关阅读:
    Android学习之简单的数据存储
    Android学习Tabhost、gallery、listview、imageswitcher
    Android学习之菜单
    Android学习之Notification
    android自定义控件
    Android简单登录系统
    Android学习自定义Dialog
    Android学习之DialogFragment
    Intent组件
    git
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5119587.html
Copyright © 2020-2023  润新知