[欧拉] poj 2513 Colored Sticks
主题链接:
id=2513
|
Colored Sticks
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 Sample Output Possible Hint
Huge input,scanf is recommended.
Source |
[ problem_id=2513" style="text-decoration:none">Submit 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的更多相关文章
- poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)
题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...
- POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)
http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明 ...
- POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)
下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...
- poj 2513 Colored Sticks (trie树+并查集+欧拉路)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 40043 Accepted: 10406 ...
- POJ 2513 Colored Sticks
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 28036 Accepted: 7428 ...
- POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27097 Accepted: 7175 ...
- poj 2513 Colored Sticks (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
- POJ 2513 Colored Sticks - from lanshui_Yang
题目大意:给定一捆木棒,每根木棒的每个端点涂有某种颜色.问:是否能将这些棒子首位项链,排成一条直线,且相邻两根棍子的连接处的颜色一样. 解题思路:此题是一道典型的判断欧拉回路或欧拉通路的问题,以木棍的 ...
随机推荐
- 基于Spring提供支持不同设备的页面
基于Spring来检测访问Web页面的设备是很简单的,在这个经验中我们讲到过.通常不同的设备访问我们是通过响应式设计来统一处理各种设备的尺寸的.但是如果希望针对不同的设备,显示不同的内容呢? Spri ...
- VS2008下编译BOOST 1.39的ASIO库
由于全部编译BOOST库需要的时间太长,而且耗费空间,况且我只需要用ASIO库,所以就没有必要全部编译了. boost库到www.boost.org上下载. 编译很简单,假设你的boost存放的目录是 ...
- Android各代码层获取系统时间的方法
1. 在java层,long now = SystemClock.uptimeMillis(); 2. 在native层,nsecs_t now = systemTime(SYSTEM_TIME_MO ...
- HDU4939Stupid Tower Defense (有思想的dp)
Stupid Tower Defense Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Oth ...
- 屏幕编程 F4的帮组用法
PROCESS ON VALUE-REQUEST. * 设置帮助(工作中心) FIELD wa_zppt026-arbpl MODULE mdl_arbpl_f4. *&---------- ...
- 编写在浏览器中不弹出警告的ActiveX控件
我们在编写ActiveX控件时,如果用在浏览器中,经常都会弹出现在运行的脚本不安全的提示, 如果给客户使用,将会带来极大不便.按照MSDN的介绍通常有两种一种是实现IObjectSafe接口,一种是通 ...
- 【IUML】支持向量机SVM
从1995年Vapnik等人提出一种机器学习的新方法支持向量机(SVM)之后,支持向量机成为继人工神经网络之后又一研究热点,国内外研究都很多.支持向量机方法是建立在统计学习理论的VC维理论和结构风险最 ...
- Cocos2d-x3.3RC0的Android编译Activity启动流程分析
本文将从引擎源代码Jni分析Cocos2d-x3.3RC0的Android Activity的启动流程,以下是具体分析. 1.引擎源代码Jni.部分Java层和C++层代码分析 watermark/2 ...
- windows系统port监听
通常情况下.假设想发现全部已经使用的和正在监听的port,我们能够使用netstat命令. netstat并不是一个port扫描工具.假设你想扫描计算机开放了哪些port的话.建议使用本文介绍的方法. ...
- Makefile条件推断 ——————————【Badboy】
使用条件推断,能够让make依据执行时的不同情况选择不同的执行分支. 条件表达式能够是比較变量的值,或是比較变量和常量的值. 一.演示样例 以下的样例,推断$(CC)变量是否"gcc&quo ...