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的更多相关文章

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

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

  2. POJ 2513 Colored Sticks

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

  3. 周赛-Colored Sticks 分类: 比赛 2015-08-02 09:33 7人阅读 评论(0) 收藏

    Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 32423 Accepted: 8556 Desc ...

  4. POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)

    Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...

  5. [欧拉] poj 2513 Colored Sticks

    主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Tota ...

  6. poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)

    题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...

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

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

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

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

  9. Colored Sticks (字典树哈希+并查集+欧拉路)

    Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27704   Accepted: 7336 Description You ...

  10. POJ 2513 Colored Sticks 字典树、并查集、欧拉通路

    Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...

随机推荐

  1. fedora 23 安装genymotion解决方案

    由于学习android开发,都说genymotion模拟器给力,我就尝试了下,安装过程参考 :但出现这种错误:缺少库 libjpeg.so.8 ,我就各种goole和百度找到库(链接地址),解压之后放 ...

  2. 一个不错的flash 模板

    听到好听的背景音乐,而且效果也挺不错的,忽然感觉flash好强大呀 1.模板浏览地址:http://www.cssmoban.com/cssthemes/5229.shtml 2.模板演示地址:htt ...

  3. 界面动态加载时报NullPointException

    今天在做环境监测的模拟软件时,登陆页面报NullPointException 一般像我们初始化Button时,是Button btn=(Button)findViewById(R.id.button1 ...

  4. MVC小系列(十五)【MVC+ZTree实现对树的CURD及拖拽操作】

    根据上一讲的可以加载一棵大树,这讲讲下如果操作这颗大树 <link href="../../Scripts/JQuery-zTree/css/zTreeStyle/zTreeStyle ...

  5. Linq语法

    希望能帮助一些linq新手. 开门见山 读这篇文章之前,我先说下,每一种搜索结果集,我都以三种方式变现出来,为啦更好的理解,希望不要嫌我啰嗦. 1.简单的linq语法 //1 var ss = fro ...

  6. 用Android-X86和VirtualBox打造高性能Android开发环境

    不知道有多少Android开发着对Android虚拟机的那悲剧的性能有意见,反正我的看法是:那速度实在是太坑爹了! 为什么Android虚拟机比iOS和WP7的虚拟机要慢很多呢?原因如下: 1. An ...

  7. effective c++(03)之const使用方法

    char greeting[] = "hello"; char* p = greeting; //non-const pointer,non-const data const ch ...

  8. swift入门-day02

    1.函数 2.闭包 3.构造函数基础 4.重载构造函数 5.KVC构造函数 6.遍历构造函数 7.懒加载 8.只读属性 1.函数 掌握函数的定义 掌握外部参数的用处 掌握无返回类型的三种函数定义方式 ...

  9. jfinal不能正确加载html网页,总是报错的解决方法

    今天自学jfinal,遇到一个很奇怪的问题,render("/index.html");总是报错. 仔细看错误日志,才发现原来是因为html网页放到了WEB-INF文件夹下面了,所 ...

  10. ZIP压缩文件夹中上个月的文件,并将备份文件拷贝到服务器

    遍历文件夹的子文件夹下的所有文件,将上个月的文件集中到一起,然互压缩,并copy到服务器的映射磁盘. static void Main(string[] args) { //原始文件存放的位置 Dir ...