POJ 2513 Colored Sticks(字典树+欧拉路径)

2024-03-27 23:32

本文主要是介绍POJ 2513 Colored Sticks(字典树+欧拉路径),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11158

Colored Sticks
Time Limit: 5000MS Memory Limit: 128000KB 64bit IO Format: %I64d & %I64u

Submit Status

Description

You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

Input

Input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than 10 characters. There is no more than 250000 sticks.

Output

If the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.

Sample Input

blue red
red violet
cyan blue
blue magenta
magenta cyan

Sample Output

Possible

Hint

Huge input,scanf is recommended.
开始想用map<string,int>写的,但是string是cin输入啊,而hint的提示要用scanf。这可怎么办?后来发现了神奇的字典树,轻便高效值得拥有!映射的问题解决了,然后就是欧拉路径/回路的判断了。
相关知识点:
欧拉回路和欧拉路径的判断
欧拉回路:
无向图:每个顶点的度数都是偶数,则存在欧拉回路。
有向图:每个顶点的入度都等于出度,则存在欧拉回路。
欧拉路径:
无向图:当且仅当该图所有顶点的度数为偶数 或者 除了两个度数为奇数外其余的全是偶数。
有向图:当且仅当该图所有顶点 出度=入度 或者 一个顶点 出度=入度+1,另一个顶点 入度=出度+1,其他顶点 出度=入度。
本题和hdu 1116(上一篇博客)稍有不同,这是无向图,那个是有向图(单词不可能倒着写)。判定条件不要混淆。相关博客: http://blog.csdn.net/zzran/article/details/9106329
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=5e5+5;
struct node{int num;bool is_word;node *next[26];node(){num=is_word=0;memset(next,NULL,sizeof(next));}
};
node *root;
int n,c[maxn],f[maxn];
void insert(char s[]){node *p=root;for(int i=0;s[i];i++){int dex=s[i]-'a';if(p->next[dex]==0)  p->next[dex]=new node();p=p->next[dex];}if(p->is_word==0){ // important!  because of num++p->num=++n;  //num is dex of stringp->is_word=1;}
}
int find(int x){if(x==f[x]) return x;f[x]=find(f[x]);return f[x];
}
int getdex(char s[]){node *p=root;for(int i=0;s[i];p=p->next[s[i]-'a'],i++);  // not need lengthreturn  p->num;
}
int main()
{//freopen("cin.txt","r",stdin);n=0;memset(c,0,sizeof(c));memset(f,0,sizeof(f));root=new node();char a[15],b[15];int adex,bdex;while(~scanf("%s%s",a,b)){insert(a);insert(b);adex=getdex(a);bdex=getdex(b);c[adex]++;  //all countc[bdex]++;if(!f[adex]) f[adex]=adex;  // U gather。if(!f[bdex]) f[bdex]=bdex;f[find(adex)]=f[find(bdex)];}int oddsum=0;bool judge=1;for(int i=1;i<=n;i++){if(c[i]&1) oddsum++;if(find(i)!=f[1]){judge=0;break;}}if((oddsum==0 || oddsum==2)&&judge) puts("Possible");else puts("Impossible");return 0;
}


这篇关于POJ 2513 Colored Sticks(字典树+欧拉路径)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/853763

相关文章

Windows系统下如何查找JDK的安装路径

《Windows系统下如何查找JDK的安装路径》:本文主要介绍Windows系统下如何查找JDK的安装路径,文中介绍了三种方法,分别是通过命令行检查、使用verbose选项查找jre目录、以及查看... 目录一、确认是否安装了JDK二、查找路径三、另外一种方式如果很久之前安装了JDK,或者在别人的电脑上,想

Python中Windows和macOS文件路径格式不一致的解决方法

《Python中Windows和macOS文件路径格式不一致的解决方法》在Python中,Windows和macOS的文件路径字符串格式不一致主要体现在路径分隔符上,这种差异可能导致跨平台代码在处理文... 目录方法 1:使用 os.path 模块方法 2:使用 pathlib 模块(推荐)方法 3:统一使

一文教你解决Python不支持中文路径的问题

《一文教你解决Python不支持中文路径的问题》Python是一种广泛使用的高级编程语言,然而在处理包含中文字符的文件路径时,Python有时会表现出一些不友好的行为,下面小编就来为大家介绍一下具体的... 目录问题背景解决方案1. 设置正确的文件编码2. 使用pathlib模块3. 转换路径为Unicod

MySQL9.0默认路径安装下重置root密码

《MySQL9.0默认路径安装下重置root密码》本文主要介绍了MySQL9.0默认路径安装下重置root密码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录问题描述环境描述解决方法正常模式下修改密码报错原因问题描述mysqlChina编程采用默认安装路径,

python 字典d[k]中key不存在的解决方案

《python字典d[k]中key不存在的解决方案》本文主要介绍了在Python中处理字典键不存在时获取默认值的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录defaultdict:处理找不到的键的一个选择特殊方法__missing__有时候为了方便起见,

python获取当前文件和目录路径的方法详解

《python获取当前文件和目录路径的方法详解》:本文主要介绍Python中获取当前文件路径和目录的方法,包括使用__file__关键字、os.path.abspath、os.path.realp... 目录1、获取当前文件路径2、获取当前文件所在目录3、os.path.abspath和os.path.re

hdu2544(单源最短路径)

模板题: //题意:求1到n的最短路径,模板题#include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#i

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M