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 ...
随机推荐
- Zookeeper简介与安装
Zookeeper:A Distributed Coordination Service for Distributed Applications. 一.Zookeeper简介 众所周知,协同服务是分 ...
- Linux下pcapy的安装问题
在安装pcapy包的时候 cd pcapy-0.10.8/ python setup.py install 报错 error trying to exec ‘cc1plus’: execvp: No ...
- linux 软连接方式实现上传文件存储目录的无缝迁移
背景: 由于前期的磁盘空间规划与后期的业务要求不符合.原先/home被用于用户上传文件的存储目录,但是由于上传文件的逐渐增多,而原来的/home目录的空间不足,需要给/home目录进行扩容.同时各个应 ...
- sea.js 学习
开篇:终于学习了sea.js的使用了,因为它是一个模块加载工具,所以首先要了解javascript的模块编程,然后对sea.js的了解和使用 javascript 模块编程 为什么要模块化编程,为了让 ...
- jqery基础知识
选择器按属性,子元素,表单匹配元素 <!doctype html> <html lang="en"> <head> <meta chars ...
- 20151221jquery学习笔记---日历UI
妹的,这几天真是无语了,参加了一个无聊的比赛,简直浪费时间,好几天没学jquery啊,今天学了一点,不过快要期末考试了,估计得攒到寒假了啊. 日历(datepicker) UI, 可以让用户更加直观的 ...
- 趣拍proguard配置
# Add project specific ProGuard rules here.# By default, the flags in this file are appended to flag ...
- 常见错误总结_1_对java类进行修改后,无法按修改的类型加载
1.这是因为没有run的原因,对类进行修改一定要run一遍 2.至于要不要重新tomcat部署,取决于你是修改了变量还是方法,拿不定的时候都重新加载一遍看看.
- Oracle start with.connect by prior子句实现递归查询
Oracle中的select语句可以用start with...connect by prior子句实现递归查询,connect by 是结构化查询中用到的,其基本语法是: select ... fr ...
- 简单JavaSE数据类型入门
新的一节学习了JavaSE,今天主要来说一下Java的数据类型及其输出,借用Xmind,可以生动形象的向大家来解释Java的数据类型: 其中数值整数型举例: public class A02{ pub ...