POJ-2513 Colored Sticks

Colored Sticks
Time Limit: 5000MS Memory Limit: 128000K
   

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来水过去,不幸TLE……看来Hint还是要在意的。

不能用map,怎么对单词进行映射呢?自己写hash?效率好不到哪去。其实忘记了,自己不是会写基本的Trie树嘛?!既然是无向图。每个单词一旦出现,就可以映射、数度数,那么这些单词正好可以用字典树来存,同时映射出编号。

Trie,不只是用来处理前缀的一个数据结构,其实它本身就可以进行哈希,因为Trie就是按照前缀进行一一映射。

其余只需要判断欧拉通路以及并查集判断连通性,就可以了。

P.S. 注意啦!一开始RE!真是白痴,Trie存的结点数不是单词个数,是字母个数!

代码如下:

/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
/****************************************/
const int M = 250005, N = 5e5+5;
int tot, head[N], deg[N], cnt, fa[N], nxt;
struct Node {
	int u, v, next;
}edge[2*M];
struct Trie {
	int id;
	bool being;
	Trie* next[26];
}word[N*10], *root;

Trie* New()
{
	memset(&word[nxt], 0, sizeof(Trie));
	return &word[nxt++];
}

void add(int u, int v)
{
	edge[tot].u = u;
	edge[tot].v = v;
	edge[tot].next = head[u];
	head[u] = tot++;
}

bool Euler()
{
	int one = 0;
	for(int i = 0; i < cnt; i++) {
		if(deg[i] & 1) {
			one++;
			if(one > 2) return false;
		}
	}
	if(one == 1) return false;
	return true;
}

int Find(int x)
{
	if(x != fa[x]) fa[x] = Find(fa[x]);
	return fa[x];
}

void Union(int x, int y)
{
	int fx = Find(x), fy = Find(y);
	if(fx != fy) fa[fy] = fx;
}

bool connect()
{
	for(int i = 0; i < cnt; i++) {
		fa[i] = i;
	}
	for(int i = 0; i < tot; i++) {
		int u = edge[i].u, v = edge[i].v;
		Union(u, v);
	}
	int root = Find(0);
	for(int i = 1; i < cnt; i++) {
		if(Find(i) != root) return false; 
	}
	return true;
}

int Hash(char *s)
{
	Trie* p = root;
	int i = 0;
	while(s[i] != '\0') {
		int c = s[i++] - 'a';
		if(!p->next[c]) {//该结点不存在则创建
			p->next[c] = New();
		}
		p = p->next[c];
	}//插入整个单词
	if(p->being) {
		return p->id;
	}//有这个单词则返回它的id
	else {
		p->being = true;
		return p->id = cnt++;//否则映射
	}
}

int main()
{
#ifdef J_Sure
	freopen("000.in", "r", stdin);
	freopen("999.out", "w", stdout);
#endif
	cnt = 0;
	memset(deg, 0, sizeof(deg));
	tot = 0;
	memset(head, -1, sizeof(head));
	nxt = 0;
	root = New();
	char s1[15], s2[15];
	while(~scanf("%s%s", s1, s2)) {
		int u = Hash(s1), v = Hash(s2);
		deg[u]++; deg[v]++;
		add(u, v); add(v, u);
	}
	bool flag = Euler() && connect();
	if(flag) puts("Possible");
	else puts("Impossible");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值