POJ2513_Colored Sticks_KEY
题目大意:给你若干根木棍,每根木棍有前后两种颜色,连接两根木棍需要前后颜色相同,求能否将所有木棍连接在一起。
Solution:
不要将木棍看成点,将颜色看成点。
其实就是求是否存在欧拉路径。
有欧拉路径要满足两个条件:
图是连通图。
没有或只有两个入度为奇数的点。
判断连通性用并查集。
枚举每个点判断入度就好了。
code:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; struct trie{
int tr[][],v[],cnt,tot;
trie(){memset(tr,,sizeof tr),memset(v,,sizeof v),cnt=tot=;}
int add(char *a)
{
int len=strlen(a),now=;
for(int i=;i<len;i++){
if(!tr[now][a[i]-'a'])tr[now][a[i]-'a']=++cnt;
now=tr[now][a[i]-'a'];
}
if(!v[now])v[now]=++tot;
return v[now];
}
}T;
char S1[],S2[];
int into[],fa[];
int getf(int x){return x==fa[x]?x:fa[x]=getf(fa[x]);} int main()
{
// freopen("x.txt","r",stdin);
for(int i=;i<=;i++)fa[i]=i;
while(cin>>S1>>S2){
int k1=T.add(S1),k2=T.add(S2);
into[k1]++,into[k2]++;
fa[getf(k1)]=getf(k2);
}
int ans=;
for(int i=;i<=T.tot;i++)
if(fa[getf(i)]==i)ans++;
if(ans>)return puts("Impossible"),;
ans=;
for(int i=;i<=T.tot;i++){
if(into[i]&)ans++;
}
if(!ans||ans==)puts("Possible");
else puts("Impossible");
return ;
}
POJ2513_Colored Sticks_KEY的更多相关文章
随机推荐
- Zookeeper学习之路 (三)shell操作
Zookeeper的shell操作 Zookeeper命令工具 在启动Zookeeper服务之后,输入以下命令,连接到Zookeeper服务: [hadoop@hadoop1 ~]$ zkCli.sh ...
- PHP面试系列 之Linux(二)---- Linux系统定时任务
环境:ubuntu 16 一.cron实现定时任务 cron实现的定时任务是周期性循环执行的. 1.安装cron sudo apt-get install cron 2.添加定时任务(进行编辑) cr ...
- Git创建本地分支并关联远程分支
创建本地分支git branch 分支名 例如:git branch dev,这条命令是基于当前分支创建的本地分支,假设当前分支是master(远程分支),则是基于master分支创建的本地分支dev ...
- AFSoundManager
iOS audio playing (both local and streaming) and recording made easy through a complete and block-dr ...
- datatable的excel导入,其中有关于datatables的很多参数设置
datatable的excel导入,其中有关于datatables的很多参数设置 http://www.cnblogs.com/liyuhuan/p/5633095.html
- haproxy原理理解
1.haproxy使用最需要注意的点: 1.1连接数: 前端maxconn默认值为2000,非常有必要将其增加几倍. 1.2超时时间 timeout connect 60s # haproxy和服务端 ...
- mac系统 IDEA+JFinal+Tomcat+Maven搭建
1.下载Maven(http://maven.apache.org/download.cgi) 2.下载Tomcat(http://tomcat.apache.org/download-90.cgi) ...
- 关于keil不同容量和不同引脚大小的编译以及下载出错问题
如果遇到这个问题一般可能有四个原因(以STM32F103C8T6为例) 1.芯片型号没有选对 2.startup文件可能没有选对,startup文件常用的分为3种,startup_stm32f10x_ ...
- DataGuard的三种保护模式
(一)三种保护模式介绍1.最大性能模式这种模式保证数据库主库性能最大化,主备库之间数据是异步传输的.即,主备日志归档以后才会传输到备库,在备库上使用归档日志文件做恢复操作.这种模式提供在不影响prim ...
- vue+echarts实现可拖动节点的折现图(支持拖动方向和上下限的设置)
本篇文档主要是利用echarts实现可拖动节点的折现图,在echarts中找到了一个demo,传送门:https://echarts.baidu.com/examples/editor.html?c= ...