POJ-Colored Sticks(字典树+并查集+欧拉回路)
来源:互联网 发布:java培训哪个机构最好 编辑:程序博客网 时间:2024/05/16 02:46
A - Colored Sticks
Time Limit:5000MS Memory Limit:128000KB 64bit IO Format:%lld & %lluDescription
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
这道题是比较经典的题目了。
就是给n根木棒,木棒两端都有颜色,判断能不能把木棒合并成一条,接触的木棒端必须是相同颜色的。
给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的。
貌似就是判断欧拉回路。
由图论知识可以知道,无向图存在欧拉路的充要条件为:
① 图是连通的;
② 所有节点的度为偶数,或者有且只有两个度为奇数的节点。
图的连通可以利用并查集去判断。
度数的统计比较容易。
数据比较大,首先需要将颜色的字符串和数字一一对应起来。
虽然可以用map,但是map效率不高,肯定会超时的。
最好的办法的是建立字典树。
将字符串和数字一一对应起来。
注意本题的数据有没有木棒的情况,要输出Possible。
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<limits.h>#include<math.h>#include<queue>#include<algorithm>using namespace std;#define maxn 500010#define maxs 26int parent[maxn];int degree[maxn];//度数int color;//颜色编号,从0开始,最后就是颜色总数 int find (int x){if(parent[x]==-1)return x;return find(parent[x]);}void bing(int a,int b){int t1=find(a);int t2=find(b);if(t1!=t2) parent[t1]=t2;}typedef struct Trie_Node{bool isword; struct Trie_Node *next[maxs];int id;}Trie;int insert(Trie *root,char *word){Trie *p=root;int i=0;while(word[i]!='\0'){if(p->next[word[i]-'a']==NULL) { Trie *temp=new Trie; temp->isword=0; for(int j=0;j<maxs;j++) temp->next[j]=NULL; temp->isword=0; temp->id=0; p->next[word[i]-'a']=temp; } p=p->next[word[i]-'a']; i++;}if(p->isword)return p->id;else{p->isword=1;p->id=color++;return p->id; } } void del(Trie *root) { for(int i=0;i<maxs;i++) if(root->next[i]!=NULL) del(root->next[i]); free(root); }int main(){char str1[20],str2[20];Trie *root=new Trie;for(int i=0;i<maxs;i++) root->next[i]=NULL; root->isword=0; root->id=0; color=0; memset(parent,-1,sizeof(parent)); memset(degree,0,sizeof(degree)); while(scanf("%s%s",&str1,&str2)!=EOF) { int t1=insert(root,str1); int t2=insert(root,str2); degree[t1]++; degree[t2]++; bing(t1,t2); } int cnt1=0,cnt2=0;//根据parent数组等于-1来找根结点 for(int i=0;i<color;i++) { if(parent[i]==-1)cnt1++; if(degree[i]%2==1)cnt2++; if(cnt1>1)break; if(cnt2>2)break; } if((cnt1==0 || cnt1==1) && (cnt2==0 || cnt2==2))//存在木棒根数为0的情况 printf("Possible\n"); else printf("Impossible\n"); del(root);}
0 0
- Colored Sticks --欧拉回路,并查集,字典树,
- POJ 2513 Colored Sticks 并查集 + 字典树 + 欧拉回路
- POJ 2513 Colored Sticks(欧拉回路判断+字典树Trie+并查集)
- 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(欧拉回路+字典树+并查集)
- 连单词成欧拉路 欧拉回路+字典树+并查集 POJ 2513 Colored Sticks
- poj-2513-Colored Sticks-字典树trie / hash +并查集 欧拉回路
- POJ-Colored Sticks(字典树+并查集+欧拉回路)
- POJ Colored Sticks 2513(字典树+并查集+欧拉回路)
- sublime text安装及package control的安装
- 借助 Lucene.Net 构建站内搜索引擎(上)
- Spring Mvc 的原理
- 判断是否为素数 + 分解质因数(利用了Miller_Rabin和素数筛选法)
- 用Android studio软件导入eclipse中编程的老软件
- POJ-Colored Sticks(字典树+并查集+欧拉回路)
- FMDB FMDB的使用方法
- HDOJ 5778 abs
- 温故而知新(一)TreeMap
- 使用impdp导入lob字段数据hang问题处理
- Javascript - 闭包
- Python——10模块
- 网页中的动态控件和静态控件
- Java 设计模式之组合模式的详解(结构模式)