Colored Sticks
Time Limit: 5000MS |
Memory Limit: 128000K |
|
Total Submissions: 35734 |
Accepted: 9358 |
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.
/*
就是给n根木棒,木棒两端都有颜色,判断能不能把木棒合并成一条,接触的木棒端必须是相同颜色的。
给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的。
*/
用欧拉路来做,可以把木棒的两段看作是两个点,然后我们就可以放开思想做题了
我们知道无向图的欧拉路要满足两个特性1. 所有点的边数是奇数个的有0个或2个,2. 这个图要是联通的
这样就好办了,我们把每一个木棒的两段看成是两个点的边,然后就会得到点的边数了,当然一样的字母是同一个点,在然后我们就要用并查集的知识来看看图是否为联通图,
这里有一个难点就是我们必须用字典树的知识查找相同的字母,字典树我在以前写到的,
这个题目的代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int father[510005];
int deg[510005];
int cnt, ant;
typedef struct node
{
int x;
node *next[26];
}*Trie;
void init()
{
int i;
for ( i = 1;i < 510005; i++ )
{
father[i] = i;
}
memset(deg, 0, sizeof(deg));
ant = 0;
}
int search1(Trie root, char *s)
{
int k;
while ( *s )
{
if ( *(s+1) )
{
k = *s-'a';
if ( root->next[k] == NULL )
return 0;
root = root->next[k];
s++;
}
else
{
k = *s-'a';
if ( root->next[k] == NULL )
return 0;
if ( root->next[k]->x != -1 )
return root->next[k]->x;
return 0;
}
}
return 0;
}
void insert(Trie root, char *s)
{
int i, k;
while ( *s )
{
k = *s-'a';
if ( root->next[k] == NULL )
{
root->next[k] = new node;
if ( !*(s+1) )
root->next[k]->x = ++ant;
else
root->next[k]->x = -1;
for ( i = 0;i < 26; i++ )
{
root->next[k]->next[i] = NULL;
}
}
else
{
if ( !*(s+1) )
root->next[k]->x = ++ant;
}
root = root->next[k];
s++;
}
}
int find(int x)
{
while ( x != father[x] )
{
x = father[x];
}
return x;
}
void meget(int x, int y)
{
int fx = find(x);
int fy = find(y);
if ( fx != fy )
father[fx] = fy;
}
int euler()
{
int i;
int cnt = 0;
for ( i = 1;i <= ant; i++ )
{
if ( deg[i]%2 != 0 )
cnt++;
}
if ( cnt != 0&&cnt != 2 )
return 0;
int g = find(1);
for ( i = 2;i <= ant; i++ )
{
if ( g != find(i) )
return 0;
}
return 1;
}
int main()
{
int x, y;
int i;
char s1[15], s2[15];
init();
Trie root;
root = new node;
for ( i = 0;i < 26; i++ )
{
root->next[i] = NULL;
}
while ( ~scanf ( "%s %s", s1, s2) )
{
x = search1( root, s1 );
y = search1( root, s2 );
if ( x == 0 )
{
insert( root, s1 );
x = ant;
}
if ( y == 0 )
{
insert( root, s2 );
y = ant;
}
deg[x]++;
deg[y]++;
meget( x, y );
}
if ( euler() )
printf ( "Possible\n" );
else
printf ( "Impossible\n" );
}
代码菜鸟,如有错误请多包涵!!!
如有帮助记得支持我一下,谢谢!!!
版权声明:本文为qq_34952846原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。