标签:style blog io color ar os for sp strong
此题中涉及三个小算法,这是一个无向图判断欧拉回路,
字典树分配一下他们的id就好了,还有卡住的一点就是零图是欧拉图。
还有我做了一个小小的剪芝,但是没有什么效果,只减少了30ms
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int cnt;
int par[500050];
char s1[20],s2[20];
int deg[500050];
struct node {
int id;
node *next[26];
node (){
for(int i=0;i<26;i++)
next[i]=NULL;
id=0;
}
}*root;
int Insert(char s[]){
int len = strlen (s);
node *p=root;
for(int i=0;i<len;i++){
if(p->next[s[i]-'a']==NULL)
p->next[s[i]-'a']=new node();
p=p->next[s[i]-'a'];
}
if(!p->id) return p->id=++cnt;
else return p->id;
}
int findset(int x){
if(x!=par[x])
par[x]=findset(par[x]);
return par[x];
}
void unite(int x,int y){
int px=findset(x);
int py=findset(y);
if(px!=py){
par[py]=px;
}
}
bool con(){
int k=0;
for(int i=1;i<=cnt;i++){
if(findset(i)==i) k++;
if(k>1) return 0;//剪枝处
}
return 1;
}
bool oula(){
int k=0;
for(int i=1;i<=cnt;i++){
if(deg[i]&1) k++;
if(k>2) return 0;//剪枝处
}
if(k==0||k==2) return 1;
return 0;
}
int main(){
root = new node ();
for(int i=0;i<500050;i++)
par[i]=i;
memset(deg,0,sizeof(deg));
cnt=0;
while(scanf("%s%s",s1,s2)!=EOF){
int u=Insert(s1);
int v=Insert(s2);
deg[u]++;
deg[v]++;
unite(u,v);
}
if(cnt==0) puts("Possible");//当他们是零图的时候
else {
if(oula()&&con()) puts("Possible");
else puts("Impossible");
}
}poj 2513 Colored Sticks 欧拉回路(字典树 +并查集)
标签:style blog io color ar os for sp strong
原文地址:http://blog.csdn.net/u013076044/article/details/40711291