poj 2513Colored Sticks
http://poj.org/problem?id=2513
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std; const int sign_node=;
const int max_node=;
int f[max_node];
int du[max_node];
int num=;
int ch[max_node][sign_node];
int val[max_node]; struct Trie
{
int sz;
void clear(){sz=; memset(ch[],,sizeof(ch[]));}
int idx(char c) {return c-'a';} int insert(char *s)
{
int u=,n=strlen(s);
for(int i=; i<n; i++){
int c=idx(s[i]);
if(!ch[u][c]){
memset(ch[sz],,sizeof(ch[sz]));
val[sz]=;
ch[u][c]=sz++;
}
u=ch[u][c];
}
if(!val[u]) val[u]=++num;
return val[u];
}
}; int find(int x)
{
if(x!=f[x])
f[x]=find(f[x]);
return f[x];
} void merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy){
f[fx]=fy;
}
} void inti()
{
memset(du,,sizeof(du));
for(int i=; i<=max_node; i++)
f[i]=i;
} int main()
{
Trie trie;
char a[],b[];
inti();
trie.clear();
while(scanf("%s%s",a,b)!=EOF)
{
int id1=trie.insert(a);
int id2=trie.insert(b);
du[id1]++;
du[id2]++;
merge(id1,id2);
}
int ans=,ans1=;
for(int i=; i<=num; i++)
{
if(du[i]&)
ans++;
if(ans>||find()!=find(i))
{
printf("Impossible\n");
return ;
}
}
if(ans==)
printf("Impossible\n");
else
printf("Possible\n");
return ;
}
poj 2513Colored Sticks的更多相关文章
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- POJ 1011 - Sticks DFS+剪枝
POJ 1011 - Sticks 题意: 一把等长的木段被随机砍成 n 条小木条 已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析: 1. 该长度必能被总长整除 ...
- 搜索+剪枝——POJ 1011 Sticks
搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...
- POJ 2452 Sticks Problem
RMQ+二分....枚举 i ,找比 i 小的第一个元素,再找之间的第一个最大元素..... Sticks Problem Time Limit: 6000MS ...
- 搜索 + 剪枝 --- POJ 1101 : Sticks
Sticks Problem's Link: http://poj.org/problem?id=1011 Mean: http://poj.org/problem?id=1011&lan ...
- poj 1011 Sticks
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 126238 Accepted: 29477 Descrip ...
- OpenJudge 2817:木棒 / Poj 1011 Sticks
1.链接地址: http://bailian.openjudge.cn/practice/2817/ http://poj.org/problem?id=1011 2.题目: 总时间限制: 1000m ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
- POJ 1011 Sticks 【DFS 剪枝】
题目链接:http://poj.org/problem?id=1011 Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
随机推荐
- Java 删除当前文件夹及其文件夹下的全部文件
删除文件夹2以下的全部文件及其文件夹 演示样例文件夹例如以下:D:/test/1/2 1.使用FileUtils类.静默删除 String url = "D:/test/1/2"; ...
- How To Cluster Rabbit-MQ--reference
Foreword This explanation of clustering Rabbit-MQ assumes that you’ve had some experience with Rabbi ...
- 谈谈iOS中粘性动画以及果冻效果的实现
在最近做个一个自定义PageControl——KYAnimatedPageControl中,我实现了CALayer的形变动画以及CALayer的弹性动画,效果先过目: https://github.c ...
- jQuery创建ajax关键词数据搜索
在web开发过程当中,我们经常需要在前台页面输入关键词进行数据的搜索,我们通常使用的搜索方式是将搜索结果用另一个页面显示,这样的方式对于搭建高性能网站来说不是最合适的,今天给大家分享一下如何使用 jQ ...
- jsp页面可以巧用模态框
jsp页面使用模态框配合ajax出来的效果真的没话说,当然你也可以使用模态框配合action,但是在删除和更新的时候传值有点麻烦,用ajax 就没有这些问题 ,比如删除代码的时候在js文件中传值可以这 ...
- 学习JAVA第一部分总结
把自己这几天的学习情况记录下来. 第一章,认识JAVA,了解JAVA的运行机制,虚拟机. 第二章,了解java的注释,标识符,关键字.. 第三章,基本的数据类型,byte short int long ...
- (转)dedecms [field:array runphp='yes']标签使用技巧
field支持用array获取任意字段的值:(支持标记:文章内容模板的 {dede:field name=’array’ /}.arclist.arclistsg.loop.sql 标签) 我们平时常 ...
- (转)php5中类的学习
类的结构: 类的内部能可能有三种东西,就是常量(constant),属性(property)和方法(method),功能可以理解成类外部的常量,变量和函数. 复制代码代码如下: <?ph ...
- iOS在Cocoa Touch Static Library使用CocoaPods
1.在XCode中新建静态库工程:DDLogLib. 2.添加对外暴露接口的头文件DDLogLibHeader.h 3.命令行进入DDLogLib目录,运行pod init,并修改Podfile 4. ...
- UITextView textViewShouldEndEditing
最近做到UITextView, 在取消键盘事件上我以为和UITextField差不多,于是我这样写: UITextView *textView = [[UITextView alloc] initWi ...