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 ...
随机推荐
- keystone系列二:keystone源码分析
六 keystone架构 6.1 Keystone API Keystone API与Openstack其他服务的API类似,也是基于ReSTFul HTTP实现的. Keystone API划分为A ...
- day-4
/* 早上黑板上的倒计时变成了120小时 嗯 很快就要结束了 上午考试 据老师说很简单 老师 :"我就说说~"..... 下午改题 T3好辣脑子 感觉智商不够了 T2dp写丑了 然 ...
- HTML 学习整理
一.名词解释 一. HTML : Hypertext Markup Language 超文本标记语言 二. CSS : Cascading Style Sheet 层叠样式表 三. 浏览器: ...
- 周末充电之WPF(一).初试牛刀
追的剧已经赶上更新的速度了,突然觉得一下子就闲了.趁着这点时间,刚好学点 WPF .看到这边,好多人估计得感叹技术宅约等于单身狗,哈哈.好了,赶紧进入学习状态. 关注 WPF 或者说对它感兴趣其实多半 ...
- Xcode7 Xcode6 中添加pch文件
在Xcode7 和 Xcode6 中添加.pch文件是一样的,具体操作图文如下: 第一步:在Xcode的项目里,一般在Supporting Files 文件夹下创建,选中Supporting File ...
- 记:mysql 连接超时解决办法
错误描述:Timeout in IO operation 原连接字符串为:Server=182.180.50.118;port=3306;Database=test;Uid=root;Pwd=123; ...
- [转]CSS 模块
CSS 模块 如果你想知道 CSS 最近发展的转折点,你应该选择去观看 Christopher Chedeau 在2014年11月的 NationJS 大会上做的名称为 CSS in JS 的分享.不 ...
- Spring 对JDBC的支持(JdbcTemplate)
Spring对数据库的操作,使用JdbcTemplate对象 需要引入相关的jar文件 如版本:(Spring核心jar包就不列了) spring-jdbc-3.2.5.RELEASE.jar spr ...
- 01_JavaMail_03_邮件发送简单实例
[JavaMail中的核心类] 1.Session:类似Jdbc中的Connection的作用 2.MimeMessage:邮件信息类 3.Transport:发送器,用来发送邮件 [工程截图] [具 ...
- 【C++11】新特性——Lambda函数
本篇文章由:http://www.sollyu.com/c11-new-lambda-function/ 文章列表 本文章为系列文章 [C++11]新特性--auto的使用 http://www.so ...