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 ...
随机推荐
- iOS之AVPlayer的简单应用
1.引入系统框架 2.创建视频的url 3.创建播放项目 4.初始化播放器 5.设置播放页面 //引入系统文件 #import <AVFoundation/AVFoundation.h> ...
- openwrt chinadns
opkg update opkg install ip ipset resolveip iptables-mod-tproxy resolveip libopenssl luci-i18n-base- ...
- JVM笔记6:JVM类加载机制
虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验.转换解析.初始化,最终形成可以被虚拟机直接使用的Java类型,这就是虚拟机的类加载机制 从类被加载到虚拟机内存中开始,到卸载出内存为止 ...
- nyoj 24 素数距离问题
素数距离问题 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度. ...
- Eval()和DataBinder Eval(Container DataItem,)的区别及用法
ASP.NET 2.0改善了模板中的数据绑定操作把v1.x中的数据绑定语法DataBinder.Eval(Container.DataItem, fieldname)简化为Eval(fiel ...
- MySQL基本查询语句
创建一张表 create table user ( id ) not null, name ) not null, birthDate date not null, gender ) not null ...
- FromHandle函数
一 FromHandle() MFC 实际上是对内核对象HANDLE(如CDC的m_hDC,CWnd的m_hWnd)封装了这个句柄有关的所有操作,一个类生成一个新对象的时候这个句柄是无效的,要获得这个 ...
- Java_LIST使用方法和四种遍历arrayList方法
1.List接口提供的适合于自身的常用方法均与索引有关,这是因为List集合为列表类型,以线性方式存储对象,可以通过对象的索引操作对象. List接口的常用实现类有ArrayList和Linked ...
- 关于GC进行垃圾回收的时机
前言 今天查看一个同事的代码,发现代码中多处地方使用了GC.Collect()方法,我问他为什么这么做,他说感觉程序中定义了好多变量,怕GC回收不及时,用GC.Collect()可以手动掌控GC进行垃 ...
- linux mysql目录详解
1.mysql数据库目录 /var/lib/mysql 2.mysql配置文件目录 /usr/share/mysql 3.相关命令目录 /usr/bin 4.启动脚本目录