Colored Sticks
poj2513:http://poj.org/problem?id=2513
题意:就是求一个欧拉回路。
题解:本题是判断欧拉通路是否存在,但是如果是用map的话就会超时,这里采用了trie树,有发现trie树的一种用法。神奇 啊
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 1100000
using namespace std;
const int N=3e5+;
int head[N],cnt,num,vis[N],deg[N];
struct Node{
int v;
int next;
}edge[N*];
void init(){
memset(head,-,sizeof(head));
cnt=num=;
memset(vis,,sizeof(vis));
memset(deg,,sizeof(deg));
} void add(int u,int v){
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].v=u;
edge[cnt].next=head[v];
head[v]=cnt++;
}
void DFS(int u){
if(vis[u])return;
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
DFS(edge[i].v);
}
}
struct Nod { //0为无效值
int lnk[], val;
void init() {
memset(lnk, , sizeof(lnk));
val = ;
}
};
const char BASE = 'a';
struct Trie {
Nod buf[maxn];
int len;
void init() {
buf[len=].init();
}
int insert(char * str, int val) {
int now = ;
for(int i = ; str[i]; i ++) {
int & nxt = buf[now].lnk[str[i]-BASE];
if(!nxt) buf[nxt=++len].init();
now = nxt;
}
buf[now].val = val;
return now;
}
int search(char * str) {
int now = ;
for(int i = ; str[i]; i ++) {
int & nxt = buf[now].lnk[str[i]-BASE];
if(!nxt) return ;
now = nxt;
}
return buf[now].val;
}
} trie;
char s1[],s2[];
int main(){
trie.init();
init();
while(~scanf("%s%s",&s1,&s2)){
if(trie.search(s1)==)trie.insert(s1,++num);
if(trie.search(s2)==)trie.insert(s2,++num);
int c1=trie.search(s1),c2=trie.search(s2);
add(c1,c2);
deg[c1]++;
deg[c2]++;
}
bool flag=true;
int ct=;
DFS();
for(int i=;i<=num;i++){
if(!vis[i]){
flag=false;
break;
}
}
for(int i=;i<=num;i++){
if(deg[i]%==)
ct++;
}
if(flag){
if(ct==||ct==){
printf("Possible\n");
}
else
printf("Impossible\n");
}
else
printf("Impossible\n"); }
Colored Sticks的更多相关文章
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
- POJ 2513 Colored Sticks
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 28036 Accepted: 7428 ...
- 周赛-Colored Sticks 分类: 比赛 2015-08-02 09:33 7人阅读 评论(0) 收藏
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 32423 Accepted: 8556 Desc ...
- POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
- poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)
题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...
- POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27097 Accepted: 7175 ...
- poj 2513 Colored Sticks (trie树+并查集+欧拉路)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 40043 Accepted: 10406 ...
- Colored Sticks (字典树哈希+并查集+欧拉路)
Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27704 Accepted: 7336 Description You ...
- POJ 2513 Colored Sticks 字典树、并查集、欧拉通路
Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...
随机推荐
- c# 枚举基础有[flags]和没有的的区别
枚举提供成组的常数值,它们有助于使成员成为强类型以及提高代码的可读性.在 C# 中,使用 enum 来声明枚举. 枚举分为简单枚举和标志枚举两种. 基本语法示例 enum Day { Sun, Mon ...
- Bind开启IPv6功能
[root@localhost sbin]# ./named -v bind 9.5.1-p3-v3.0.9 1,服务器开启IPv6服务 网卡配置v6地址 [root@localhost ~]# if ...
- xilinx仿真库的作用(原创)
① secureip库:硬核(HARD IP)仿真(功能仿真和时序仿真)模型(hard IP simulation model),比如PowerPC.PCIE.SRIO.DDR等.Simulation ...
- CAS 单点登录,通过ticket 获取登录用户
string url =SSOValidate+"?service=" + WebValidate + "&ticket=" + Ticket + &q ...
- ImageButton 在IE 10 下的异常
最近在项目中遇到一个棘手问题,在IE10中,一些图片按钮点了毫无反应,其他浏览器(包括IE9)都正常:查看后台,发现如下异常信息: Input string was not in a correct ...
- Ajax.Utility.RegisterTypeForAjax(typeof(_Default)) 的使用
语句的作用:页面注册方法的作用. Ajax.Utility.RegisterTypeForAjax(typeof(命名空间.类名)); 首先要在配置文件里配置 <httpHandlers> ...
- linux下apache-tomcat的安装
一.JDK安装 1.安装JDK软件包 本例使用的JDK安装包为jdk-6u19-linux-x64.bin,该包是一个编译好的二进制可执行程序包,只需要执行即可安装. 首先进入存放JDK安装包的目录( ...
- Java中的static关键字解析(转自海子)__为什么main方法必须是static的,因为程序在执行main方法的时候没有创建任何对象,因此只有通过类名来访问。
Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键 ...
- 关于cnpm的一点小bug
在实际工作中,一个项目完成后,在上线前,常常需要把代码进行压缩,一般是用gulp或者 webpack 进行压缩.(小妹是用gulp) gulp是运行在node 环境下的. 所以首先,下载并安装了nod ...
- javee学习-通过ServletContext对象实现数据共享
1,设置值. ServletContext context = this.getServletConfig().getServletContext();//获得ServletContext对象 // ...