Colored Sticks POJ - 2513
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.
分析
这道题其实是把同一种颜色当成同一个结点,然后在两个颜色结点之间连边,构成一个无向图,最后判断能不能“一笔画”,把问题转化一下就是欧拉通路问题。
欧拉图是指通过图(无向图或有向图)中所有边且每边仅通过一次通路,相应的回路称为欧拉回路。具有欧拉回路的图称为欧拉图(Euler Graph),具有欧拉通路而无欧拉回路的图称为半欧拉图。
相关定理
- 无向连通图 G 是欧拉图,当且仅当 G 不含奇数度结点(G的所有结点度数为偶数);
- 无向连通图 G 含有欧拉通路,当且仅当 G 有零个或两个奇数度的结点;
- 有向连通图 D 是欧拉图,当且仅当该图为连通图且 D 中每个结点的入度=出度;
- 有向连通图 D 含有欧拉通路,当且仅当该图为连通图且 D 中除两个结点外,其余每个结点的入度=出度,且此两点满足 deg-(u)-deg+(v)=±1。(起始点s的入度=出度-1,结束点t的出度=入度-1 或两个点的入度=出度);
- 一个非平凡连通图是欧拉图当且仅当它的每条边属于奇数个环;
- 如果图G是欧拉图且 H = G-uv,则 H 有奇数个 u,v-迹仅在最后访问 v ;同时,在这一序列的 u,v-迹中,不是路径的迹的条数是偶数。 (from 百度)
解题方法
这里用到了第2个定理,需要满足两个条件,首先是连通图,其次是有零个或两个奇度结点。
对题目中构成的无向图,我们只需要看每个点的度数即可,所以不需要把边存起来。
判断无向图是否连通可以用并查集,只要每个点的祖先都相同,则说明图联通。
在这之前,首先需要对每种颜色编号,对每种颜色节点标记以后,方便使用并查集和统计度数,这里用Trie树来给颜色编号。
详细参考这里
代码
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 500000;
struct TrieTree_Node {
bool flag; //标记到字典树从根到当前结点所构成的字符串是否为一个(颜色)单词
int id; //当前颜色(结点)的编号
TrieTree_Node *next[27];
TrieTree_Node() {
flag = false;
id = 0;
memset(next, 0, sizeof(next)); //0 <-> NULL
}
} root; //字典树根节点
int color = 0; //颜色编号指针,最终为颜色总个数
int degree[N + 1] = {0};//第id个结点的总度数
int ancestor[N + 1]; //第id个结点祖先
/* 并查集 */
int findAncestor(int x) {
if (ancestor[x] != x)
ancestor[x] = findAncestor(ancestor[x]);
return ancestor[x];
}
void unionSet(int a, int b) {
int fa = findAncestor(a);
int fb = findAncestor(b);
ancestor[fb] = fa;
}
/* 字典树 */
int add_color(const char *s) {
TrieTree_Node *p = &root; //从TrieTree的根节点出发搜索单词(单词不存在则创建)
for (int len = 0; s[len] != '\0'; ++len) {
int index = s[len] - 'a'; //a~z映射到1~26,作为字典树的每一层的索引
if (!p->next[index]) //当索引不存在时,构建索引
p->next[index] = new TrieTree_Node;
p = p->next[index];
}
if (p->flag) return p->id;//颜色单词已存在 返回其编号
else { //否则创建单词
p->flag = true;
p->id = ++color;
return p->id;//返回分配给新颜色的编号
}
}
int main() {
//#ifdef ONLINE_JUDGE
//#else
// freopen("in.txt", "r", stdin);
//#endif
char a[11], b[11];
for (int k = 1; k <= N; k++)ancestor[k] = k;
while (scanf("%s%s", a, b) != EOF) {
int i = add_color(a);
int j = add_color(b); //获得a、b颜色对应的编号
degree[i]++;
degree[j]++; //统计度数
unionSet(i, j); //合并i、j
}
int s = findAncestor(1);
//若图为连通图,则s为所有结点的祖先
//若图为非连通图,s为所有祖先中的其中一个祖先
int num = 0; //奇度结点个数
for (int i = 1; i <= color; i++) {
if (degree[i] % 2 == 1)num++;
if (num > 2 || findAncestor(i) != s) { //奇度结点数大于3 or 存在多个祖先,图不连通 欧拉通路必不存在
printf("Impossible\n");
return 0;
}
}
if (num == 1)printf("Impossible\n"); //奇度结点数等于1,欧拉通路必不存在
else printf("Possible\n"); //奇度结点数恰好等于0或2,存在欧拉路
return 0;
}