Colored Sticks
Time Limit: 5000MS | Memory Limit: 128000K | |
Total Submissions: 28302 | Accepted: 7481 |
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
The UofA Local 2000.10.14
非常沮丧的 RE 多次。
trie树用链表来实现吧:
结构体中的 next指针用来寻找下一个字母node。
isdict变量表示这个结点是不是一个词典中的单词。(必须有!)
然后遍历输入串,在树上行走。发现NULL指针就new一个。一定要注意 new后要把新结点的next指针都初始化为NULL。否则可能是野指针,导致RE。
首先要判断图的联通(用并查集后,所有点的root相同)
其次要判断入度为奇数的点,或者为0,或者为2。
以上两个条件都满足时,则必存在欧拉回路,否则必不存在欧拉回路。
提交记录:
1-N、RE。 记得 要初始化指针。否则结果不可预知。
N+1、AC。
非常沮丧的 RE 多次。
trie树用链表来实现吧:
结构体中的 next指针用来寻找下一个字母node。
isdict变量表示这个结点是不是一个词典中的单词。(必须有!)
struct node {
node * next[26];
bool isdict;
};
然后遍历输入串,在树上行走。发现NULL指针就new一个。一定要注意 new后要把新结点的next指针都初始化为NULL。否则可能是野指针,导致RE。
while(s[i]) {
int a = s[i] - 'a';
assert(a < 26);
if (cur->next[a] == NULL) {
cur->next[a] = new node;
memset(cur->next[a]->next, NULL, sizeof(cur->next[a]->next));
cur->next[a]->f = false;
}
cur = cur->next[a];
i++;
}
首先要判断图的联通(用并查集后,所有点的root相同)
其次要判断入度为奇数的点,或者为0,或者为2。
以上两个条件都满足时,则必存在欧拉回路,否则必不存在欧拉回路。
提交记录:
1-N、RE。 记得 要初始化指针。否则结果不可预知。
N+1、AC。
/*Source Code
Problem: 2513 User: 775700879
Memory: 87916K Time: 1391MS
Language: G++ Result: Accepted
Source Code*/
#include
#include
#include
#include
#include
#include