欧拉通路+并查集+字典树
题意是说 木棍两头有颜色,怎么排让它连成一串。颜色相同可以接起来。
最开始想用map水过去,一直TLE。怒点字典树天赋……花了一上午。
字典树不多介绍,我的节点开小了,CE好几次,才想起
“There is no more than 250000 sticks.”
“A word is a sequence of lowercase letters no longer than 10 characters.”
250000*10*2 的节点数才行。
①:空数据输出 Possible
②:250000木棍,颜色数量极限就是500000
③:无向图判断欧拉通路即可,不需要分别计算入度出度,最后算一下奇数的点 是不是0或者2就好。
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
using namespace std;
int n;
int io[510001];
int fa[510001];
int father(int x)
{
if(x!=fa[x])
fa[x]=father(fa[x]);
return fa[x];
}
struct Trie
{
int word[510000][26];
int sz,cot;
int ex[510000*10];
Trie()
{
sz=1;//节点
cot=1;//单词数
memset(word,0,sizeof(word));
memset(ex,0,sizeof(ex));
}
int insert(char *s)
{
int u=0,c,len=strlen(s);
for(int i=0; i<len; i++)
{
c=s[i]-'a';
if(!word[u][c])
{
word[u][c]=sz++;
}
u=word[u][c];
}
if(ex[u]==0)ex[u]=cot++;
return ex[u];
}
}wo;
int main()
{
char a[11],b[11];
int u,v;
for(int i=1; i<510000; i++)
{
fa[i]=i;
io[i]=0;
}
while(scanf("%s%s",a,b)!=EOF)
{
u=wo.insert(a);
v=wo.insert(b);
//printf("%d %d\n",u,v);
io[v]++,io[u]++;
u=father(u),v=father(v);
if(u==v)continue;
fa[v]=u;
}
n=wo.cot;
bool flag=1;
int block=0;
int odd=0;
for(int i=1; i<n; i++)
{
if(io[i]&1)odd++;
if(father(i)==i)block++;
if(block>1||odd>2)
{
flag=0;
break;
}
}
if(odd==1)flag=0;
if(flag)puts("Possible");
else puts("Impossible");
}
POJ 2513 Colored Sticks,布布扣,bubuko.com
原文地址:http://blog.csdn.net/dongshimou/article/details/37507129