第一次接触欧拉回路。虽然在离散数学里学过,敲代码还是第一次。

本题是说端点颜色相同的两根木棒可连接,能否将所有的木棒连成一条直线。

将颜色视为节点v,将木棒视为边e,构成图G。如果能找到一条一笔画的路经过所有边,那么便符合条件。也就是判断是否是欧拉回路。

欧拉回路的条件是:

(1) 图是连通的。

(2) 度数为基数的点的个数是两个,或者不存在。

连通可以通过用并查集判断。度数可以建一个数组保存当前点的度数。

值得注意的是有全空的数据,此时应该输出Possible。这点坑了我一下,在Discuss里发现有人提出了,改一下就A了。

#include <cstdio>
#include <cstring> const int maxn=;
int root[maxn];
int degree[maxn]; struct Node
{
int next[];
int id;
} dic[];
int index=;
int id=; int find(int x)
{
return root[x]?root[x]=find(root[x]):x;
} bool union_set(int a,int b)
{
a=find(a);
b=find(b);
if(a==b)
return false;
root[b]=a;
return true;
} int insert(char* s)
{
int now=;
int len=strlen(s);
for(int i=;i<len;i++)
{
int next=s[i]-'a';
if(dic[now].next[next]==)
dic[now].next[next]=++index;
now=dic[now].next[next];
}
if(dic[now].id==)
dic[now].id=++id;
return dic[now].id;
} int main()
{
// freopen("in.txt","r",stdin);
int num=;
char s1[],s2[];
while(~scanf("%s%s",s1,s2))
{
int a=insert(s1);
int b=insert(s2);
degree[a]++;
degree[b]++;
if(union_set(a,b))
num++;
}
if(num==id || id==)
{
int count=;
for(int i=;i<=id;i++) if(degree[i]&)
{
count++;
if(count==)
break;
}
if(count== || count==)
{
printf("Possible\n");
return ;
}
}
printf("Impossible\n");
}

POJ 2513 Colored Sticks 解题报告的更多相关文章

  1. poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)

    题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...

  2. [欧拉] poj 2513 Colored Sticks

    主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Tota ...

  3. poj 2513 Colored Sticks trie树+欧拉图+并查集

    点击打开链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27955   Accepted ...

  4. POJ 2513 Colored Sticks

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 28036   Accepted: 7428 ...

  5. poj 2513 Colored Sticks (trie 树)

    链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...

  6. POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27097   Accepted: 7175 ...

  7. poj 2513 Colored Sticks (trie树+并查集+欧拉路)

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 40043   Accepted: 10406 ...

  8. POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]

    题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...

  9. POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)

    题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...

随机推荐

  1. Python 问题集

    1.问题:打开Python的IDLE(集成开发环境/Integrated DeveLopment Environment) 然后在Python的shell中做如下动作时: >>>py ...

  2. 安卓热更新之Nuwa实现步骤

    安卓热更新之Nuwa实现步骤 最近热更新热修复的功能在安卓应用上越发火热,终于我的产品也提出了相应的需求. 经过两天的研究,搞定了这个功能,在这里还要多谢大神们的博客,大神们的原理分析很到位,不过对于 ...

  3. VS2010类似Eclipse文件查找功能-定位到

    快捷键:Ctrl + , 打开定位到窗口,可以在文件或类文件中查找内容.

  4. TeeChart 的应用

    TeeChart 是一个很棒的绘图控件,不过由于里面没有注释,网上相关的资料也很少,所以在应用的时候只能是一点点的试.为了防止以后用到的时候忘记,我就把自己用到的东西都记录下来,以便以后使用的时候查询 ...

  5. Demo学习: CalendarPanel

    CalendarPane 学习CalendarPanel控件的常用事件. DayClick(...) //点击日期块空白部分触发EventClick(...) //点击日期块上事件触发RangeSel ...

  6. Oracle wrap 测试的一些细节问题

    今天在做 wrap 的测试实验的时候,出现一个很奇怪的现象,就是加密不成功.具体表现为:1.加密后的文件大小为0kb. 2.加密后的文件仍然可视. 具体测试步骤如下: D:\Just4work\som ...

  7. 基于Emgu CV 的手势识别实现PPT的控制放映

    Emgu CV 简介         众所周知,Emgu CV是.NET平台下对OpenCV图像处理库的封装,也就是.NET版的OpenCV.开发者可以很方便的通过C#,VB等语言调用OpenCV函数 ...

  8. ubuntu系统软件

    1.办公软件:wps 2.浏览器:firefox,opera 3.思维导图:xmind 如果您有好的软件,请推荐给我,谢谢!

  9. ColorDialog组件设置字体颜色

    1.设置颜色 private void button4_Click(object sender, EventArgs e) { this.colorDialog1.ShowDialog(); this ...

  10. org.springframework.orm.jpa.JpaTransactionManager

    [第九章] Spring的事务 之 9.2 事务管理器 ——跟我学spring3 http://sishuok@com/forum/blogPost/list/0/2503.html