POJ 2513 Colored Sticks (并查集 Trie树 欧拉回路)
来源:互联网 发布:龙华行知小学 编辑:程序博客网 时间:2024/05/20 23:36
Colored Sticks
Time Limit: 5000MS Memory Limit: 128000KTotal Submissions: 30515 Accepted: 8056
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 redred violetcyan blueblue magentamagenta cyan
Sample Output
Possible
Hint
Huge input,scanf is recommended.
先不说这题有多坑 先说这题的题意:
给一些棒子,每根棒子的两头有颜色,用两个字符串给出。问能不能将这些棒子首尾相接,使得首尾的颜色都相同。
刚开始想的非常简单,直接用map做个映射然后判断有没有欧拉路径(两个或者零个度为奇数的点)
直接TLE了,没办法,改用trie树存字符串,然后光荣的WA了
后来走投无路看了网上题解,说是有不连通的情况。好吧 是我自己的问题,果断加了一段并查集。
最坑的地方出现了 ,居然还是wa了
然后就是一段漫长的交题过程。。。。
中午回去睡了一觉,下午来看,想了想,加了一组什么都没有的特判(其实就是乱加了)
结果过了。
过了。。
说不下去了。。太难过了。。。
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int trie[750005][30];int ok[750005];int fa[750005];int id[750005];int sz,num;int getfa(int x){ return fa[x] != x? fa[x] = getfa(fa[x]) : x;}void init(){ for(int i=0;i<250005;i++)fa[i] = i; memset(trie,0,sizeof(trie)); memset(ok,0,sizeof(ok)); memset(id,0,sizeof(id)); sz = 1; num = 0;}int in(char *s) { int cur = 1; for (int i= 0; s[i]; i++) { int c = s[i]-'a'; if (!trie[cur][c]){ trie[cur][c]= ++sz; } cur = trie[cur][c]; }ok[cur]++; if(ok[cur]==1){id[cur] = ++num;return num;} return id[cur];}int main(){ char str1[15],str2[15]; init(); while(scanf("%s%s",str1,str2)==2){ int a,b; a = in(str1); b = in(str2); fa[getfa(a)] = getfa(b); } if(num == 0){cout<<"Possible"<<endl;return 0;} int flag,cnt =0,cnt2 = 0; for(int i=1;i<=sz;i++){ if(ok[i]%2!=0)cnt++; if(cnt>2)break; } for(int i=1;i<=num;i++)if(fa[i] == i)cnt2++; if(cnt2 == 1&&(cnt == 2|| cnt ==0))flag = 1; else flag = 0; printf("%s\n",flag?"Possible":"Impossible"); return 0;}
0 0
- POJ 2513 Colored Sticks(欧拉回路判断+字典树Trie+并查集)
- POJ 2513 Colored Sticks (并查集 Trie树 欧拉回路)
- poj-2513-Colored Sticks-字典树trie / hash +并查集 欧拉回路
- POJ 2513 Colored Sticks(Trie+并查集+欧拉回路)
- POJ Colored Sticks(2513) -trie树&欧拉回路
- POJ 2513 Colored Sticks 欧拉回路+trie树
- POJ 2513 Colored Sticks 并查集 + 字典树 + 欧拉回路
- POJ 2513 Colored Sticks(字典树,并查集,欧拉回路)
- POJ 2513 Colored Sticks(字典树+并查集连通性+欧拉回路)
- poj 2513 Colored Sticks 并查集 字典树 欧拉回路判断
- 并查集+欧拉回路+字典树 Colored Sticks POJ 2513
- poj 2513 Colored Sticks 欧拉回路(字典树 +并查集)
- POJ 2513 Colored sticks(字典树 + 欧拉回路 + 并查集)
- POJ 题目2513 Colored Sticks(字典树+并查集+欧拉回路)
- POJ 2513 - Colored Sticks(字典树+欧拉回路+并查集 = 好题!)
- POJ 2513 Colored Sticks(字典树+并查集+欧拉回路)
- poj 2513 Colored Sticks(欧拉回路 并查集 路径压缩 字典树)(困难)
- POJ 2513 Colored Sticks(欧拉回路+字典树+并查集)
- c++ 学习笔记(高级linux编程) day3
- 使用puppet来实现自动化安装openstack mysql节点
- 2014多校1011--hdu--4097--Killing Monsters(塔防,线段树超时。。)
- poj之最长公共子序列和最长公共子串
- HDU 4968(杭电多校#9 1009题)Improving the GPA (瞎搞)
- POJ 2513 Colored Sticks (并查集 Trie树 欧拉回路)
- Android手机目前常见的分辨率
- HDU 4969 Just a Joke
- 工厂三兄弟之抽象工厂模式
- java + jni + mingw实例开发(基于命令行窗口模式)
- c++ 学习笔记(高级linux编程) day4
- AndroidManifest.xml 主配置清单文件 各句解释
- Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
- Intersecting Lines(计算几何基础)