主题链接:

http://poj.org/problem?

id=2513

Colored Sticks
Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 30955   Accepted: 8159

Description

You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

Input

Input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than 10 characters. There is no more than 250000 sticks.

Output

If the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.

Sample Input

blue red
red violet
cyan blue
blue magenta
magenta cyan

Sample Output

Possible

Hint

Huge input,scanf is recommended.

Source

[

problem_id=2513" style="text-decoration:none">Submit]   [Go Back]   [

problem_id=2513" style="text-decoration:none">Status]  
[Discuss]

题目意思:

有n条木棒。每条木棒两端有两种颜色,求这些木棒是否能能在一起,使得前一个木棒的后端颜色和前一个木棒的前端颜色一样。

解题思路:

欧拉通路+字典树+并查集

注意是无向边。注意要推断连通性。直接用map映射会超时,自己写字典树。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; #define Maxn 550000 int dei[Maxn],a[Maxn],cnt,la; struct Node
{
struct Node * next[30];
int nu;
}node[Maxn*15],root; int Insert(char *a)
{
Node * p=&root; while(*a)
{
if(p->next[*a-'a'+1]==NULL)
{
node[++la].nu=0;
memset(node[la].next,NULL,sizeof(node[la].next));
p->next[*a-'a'+1]=&node[la];
}
p=p->next[*a-'a'+1];
a++;
}
if(!p->nu)
p->nu=++cnt;
return p->nu; } int Find(int x)
{
int temp=x;
while(x!=fa[x])
x=fa[x];
while(fa[temp]!=x)
{
int cur=fa[temp];
fa[temp]=x;
temp=cur;
}
return x;
}
void Unio(int a,int b)
{
a=Find(a),b=Find(b);
if(a!=b)
fa[a]=b;
}
int main()
{ memset(dei,0,sizeof(dei));
string a,b;
char sa[15],sb[15];
cnt=la=0;
for(int i=1;i<=Maxn-5000;i++)
fa[i]=i; memset(root.next,NULL,sizeof(root.next));
root.nu=0; while(~scanf("%s%s",sa+1,sb+1))
{
int a=Insert(sa+1);
int b=Insert(sb+1); dei[a]++,dei[b]++;
Unio(a,b);
}
bool ans=true;
int nui=0,nuo=0,la=-1; for(int i=1;i<=cnt;i++)
{
if(la==-1)
la=Find(i);
else if(la!=Find(i))
{
ans=false;
break;
}
if(dei[i]&1)
nui++;
}
if(!ans||nui>2||nui==1)
printf("Impossible\n");
else
printf("Possible\n");
//system("pause"); return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

[欧拉] 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 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明 ...

  3. POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)

    下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...

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

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

  5. POJ 2513 Colored Sticks

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

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

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

  7. poj 2513 Colored Sticks (trie 树)

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

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

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

  9. POJ 2513 Colored Sticks - from lanshui_Yang

    题目大意:给定一捆木棒,每根木棒的每个端点涂有某种颜色.问:是否能将这些棒子首位项链,排成一条直线,且相邻两根棍子的连接处的颜色一样. 解题思路:此题是一道典型的判断欧拉回路或欧拉通路的问题,以木棍的 ...

随机推荐

  1. POJ3678【错误总会让自己有收获的】

       首先我是的确确定了LRJ那个代码也是判断一个点的两种状态是否在一个连通分量内.    关于自己做的,自己又确定了一些,让自己那样先,比如说对于 3 6 1 AND这样3 6都已经确定的点,自己用 ...

  2. SAP自带的创建报表工具

    SAP自带的工具有quickview和query两个主要的工具,当然还有其他的 quickview和query的区别主要是query支持系统之间的传输,quickview只能是用户的客户端创建使用,不 ...

  3. 很好用的一个类:TJvAppXMLFileStorage

    以前写软件的时候,在处理软件配置信息的时候,总是要写很多重复的代码,把配置信息写到Ini文件当中.昨天到网上查了一下,发现JVCL中提供了一个非常好用的类TJvAppXMLFileStorage,可以 ...

  4. .NET Page页面事件执行顺序,以及其作用(OnPreInit()、OnInit()等)

    以按钮事件为测试标准 1. OnPreInit //检查 IsPostBack 属性来确定是不是第一次处理该页. //创建或重新创建动态控件. //动态设置主控页. //动态设置 Theme 属性. ...

  5. Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性加入图标

    注:(图中每个条目和图标都是由代码动态生成) 代码动态布局,并须要为每个条目设置图标,此时用到了 android:drawableLeft="@drawable/icon"  父x ...

  6. Swift - 给表格的单元格UITableViewCell添加图片,详细文本标签

    表格UITableView中,每一单元格都是一个UITableViewCell.其支持简单的自定义,比如在单元格的内部,添加图片和详细文本标签. 注意UITableViewCell的style: (1 ...

  7. QT5.6,5.7,5.8的新特征以及展望

    https://wiki.qt.io/New_Features_in_Qt_5.6 (跨平台High-DPI,改进WebEngine到45,支持WIN 10,Canvas3D,3D) https:// ...

  8. PropertyPlaceholderConfigurer类的使用注意

    如果你在spring的applicationcontext.xml中需要使用属性配置文件,那PropertyPlaceholderConfigurer这个类就是必须的. <bean class= ...

  9. C++基础之---union联合体大小分析

    #include <iostream> using namespace std; union un { int a[7]; double b; char c[10]; int d[3]; ...

  10. tracert路由跟踪命令分析判断

    可能有的会使用路由跟踪命令 ,可是却看不太明确显示出来的结果.结合我的来说明一下. (1)tracert命令介绍 tracert是路由跟踪命令,通过该命令的返回结果,能够获得本地到达目标主机所经过的网 ...