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注解的简单了解
部分信息来自<Thinking In Java> 注解也成为元数据.什么是元数据?就是“关于数据的数据” 注解为我们在代码中添加信息提供了一种形式化的方法,使我们可以在稍后某个时刻非常方便 ...
- [PWA] 8.Unobtrusive update: Delete old cache and only keep one, hard refresh to let new SW to take control
So once you modify the code, service worker will auto create a new one and it won't take control ove ...
- hook研究结果备忘
hook研究结果: 最近一周时间仔细研究了一下hook,也许不能称之为研究吧.顶多是让别人的思想拿过来抄袭一遍而已,写点结果也算对得起自己的这几天的苦心了. 1,首先从同事旁边听到了hook,然后看的 ...
- 12、SQL Server 行列转换
SQL Server 行转列 在SQL Server 2005中PIVOT 用于将列值转换为列名(行转列),在SQL Server 2000中是没有这个关键字的 只能用case语句实现. --创建测试 ...
- java工程中使用freemarker例子
新建java project,引入freemarker.jar, 本工程是用的版本:freemarker-2.3.20 版本 java工程目录如下: test.ftl文件 HTML代码 name : ...
- 织梦DEDECMS小说模块使用和安装全攻略
转之--http://www.51dedecms.com/news/dedecms/2012/0223/3380.html 小说模块功能很强大,可以用他做小说或者漫画站.他们都可以按某章节收费或免费供 ...
- linux 'more' command.
more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会 ...
- [置顶] VS自带工具:dumpbin的使用
有时候我们想查看一个exe引用了哪些动态库,或者我们想看某个动态库包含哪些接口函数,这个时候可以使用dumpbin.exe工具: 1.输入Dumpbin -imports calldll.exe查看它 ...
- 【转】 iOS开发数据库篇—SQLite简单介绍
开始学SQLite啦, 原文: http://www.cnblogs.com/wendingding/p/3868893.html iOS开发数据库篇—SQLite简单介绍 一.离线缓存 在项目开发中 ...
- MySQL 管理
MySQL 管理 启动及关闭 MySQL 服务器 首先,我们需要通过以下命令来检查MySQL服务器是否启动: ps -ef | grep mysqld 如果MySql已经启动,以上命令将输出mysql ...