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的更多相关文章

  1. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  2. POJ 1011 - Sticks DFS+剪枝

    POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    ...

  3. 搜索+剪枝——POJ 1011 Sticks

    搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...

  4. POJ 2452 Sticks Problem

    RMQ+二分....枚举 i  ,找比 i 小的第一个元素,再找之间的第一个最大元素.....                   Sticks Problem Time Limit: 6000MS ...

  5. 搜索 + 剪枝 --- POJ 1101 : Sticks

    Sticks Problem's Link:   http://poj.org/problem?id=1011 Mean: http://poj.org/problem?id=1011&lan ...

  6. poj 1011 Sticks

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 126238   Accepted: 29477 Descrip ...

  7. OpenJudge 2817:木棒 / Poj 1011 Sticks

    1.链接地址: http://bailian.openjudge.cn/practice/2817/ http://poj.org/problem?id=1011 2.题目: 总时间限制: 1000m ...

  8. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

  9. POJ 1011 Sticks 【DFS 剪枝】

    题目链接:http://poj.org/problem?id=1011 Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissio ...

随机推荐

  1. 排序算法 Java实现版

    8种排序之间的关系: 1. 直接插入排序 (1)基本思想: 在要排序的一组数中,假设前面(n-1)[n>=2] 个数已经是排好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数也是排好顺序 ...

  2. 【Python爬虫】安装 pyQuery 遇到的坑 Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?

    windows 64位操作系统下,用 Python 抓取网页,并用 pyQuery 解析网页 pyQuery是jQuery在python中的实现,能够以jQuery的语法来操作解析HTML文档,十分方 ...

  3. 1.1GTK+ 的简单程序HelloWorld

    1.1GTK+ 的简单程序HelloWorld 编译执行如图所看到的:

  4. struts.enable.DynamicMethodInvocation = true 动态方法调用(转)

    原文地址:http://blog.csdn.net/wfcaven/article/details/5937557 default.properties 在Struts 2的核心jar包-struts ...

  5. noip 2012 借教室 (线段树 二分)

    /* 维护区间最小值 数据不超int 相反如果long long的话会有一组数据超时 无视掉 ll int */ #include<iostream> #include<cstdio ...

  6. codevs 3336 电话网络

    #include<iostream> #include<cstdio> #include<cstring> #include<queue> #defin ...

  7. ASP.NET-FineUI开发实践-13(二)

    1.加图标和事件 上次已经通过DataSimulateTreeLevelField属性控制了树节点的显示,不用看也知道就是给指定列数据前面加个图标的HTML 可以在SimulateTreeHeper类 ...

  8. jQuery的选择器中的通配符[id^='code']或[name^='code']

    这两天在做一个专题的时候遇到了一个通配符的问题 //弹层操作$(function(){ //视频播放 $("a[href^='#video']").each(function(in ...

  9. (转)C#文件操作

    原文连接:http://www.cnblogs.com/wangshenhe/archive/2012/05/09/2490438.html 文件与文件夹操作主要用到以下几个类: 1.File类: 提 ...

  10. (转)ThinkPHP自定义标签

    第一:在当前应用下的Conf文件夹中config.php加两个配制项:             'TAGLIB_LOAD' => true,//加载标签库打开             'APP_ ...