`
lilisalo
  • 浏览: 1108415 次
文章分类
社区版块
存档分类
最新评论

POJ/PKU 2513 并查集+字典树+欧拉回路

 
阅读更多
Colored Sticks
Time Limit: 5000MS Memory Limit: 128000K
Total Submissions: 20750 Accepted: 5482

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.

Source

要仔细看题,木棍不仅仅只是可以一个方向放置,还可以倒过来,所以简单的说,这个题是一个无向图的欧拉回路判断
但是直接用map要超时。。我先开始就这样TLE了。后来看了下后面的discuss,发现要用字典树
于是又自己手动写了个字典树。
所以综上,我们需要:
1、字典树对每一个字符串进行标号
2、并查集判断整个图是否连通
3、判断无向图的欧拉回路是否存在
我的代码:

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics