nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路
来源:互联网 发布:js调用苹果摄像头 编辑:程序博客网 时间:2024/05/22 01:36
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=230
题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色必须相同,是否能把它们都连起来
思路:很明显的欧拉路径,但题目给的字符串数据很大,得用字典树存取。
代码如下:
#include "stdio.h"#include "string.h"#include "stdlib.h"#define N 505000int set[N],du[N];int find(int x){ if(set[x]==-1) return x; return set[x]=find(set[x]);}void bing(int a,int b){ int fa = find(a); int fb = find(b); if(fa!=fb) set[fa] = fb;}struct node{ struct node *next[26]; int num;};int id;void BFS(node *root);int Trie(node *root,char *s);void Date_process(int n){ int i=0; int t1,t2; char s1[15],s2[15]; node *root = (node *)malloc(sizeof(node)); for(i=0; i<26; ++i) root->next[i] = NULL; root->num = -1; id = 0; memset(du,0,sizeof(du)); memset(set,-1,sizeof(set)); for(i=0; i<n; ++i) { scanf("%s %s",s1,s2); t1 = Trie(root,s1); t2 = Trie(root,s2); du[t1]++; du[t2]++; bing(t1,t2); } BFS(root);}int main(){ int T; int i,n; scanf("%d",&T); while(T--) { scanf("%d",&n); getchar(); if(n==0) { printf("Possible\n"); continue; } Date_process(n); bool flag = true; int k = find(0); for(i=0; i<id; ++i) { if(find(i)!=k) flag = false; } if(!flag) //若该图不连通,Impossible { printf("Impossible\n"); continue; } int ans=0; for(i=0; i<id; ++i) { if(du[i]%2==1) ans++; } if(ans==2 || ans==0) //欧拉回路或者欧拉路径 Possible printf("Possible\n"); else printf("Impossible\n"); } return 0;}int Trie(node *root,char *s) //字典树{ int i=0; node *p=root; while(s[0]!='\0') { if(p->next[s[0]-'a']==NULL) { node *tt; tt = (node *)malloc(sizeof(node)); tt->num = -1; for(i=0; i<26; ++i) tt->next[i] = NULL; p->next[s[0]-'a'] = tt; p = tt; } else p = p->next[s[0]-'a']; s++; } if(p->num==-1) p->num = id++; return p->num;}void BFS(node *root) //深搜释放内存{ int i; if(root==NULL) return ; for(i=0; i<26; i++) { if(root->next[i]!=NULL) BFS(root->next[i]); } free(root);}
0 0
- nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路
- poj 2513 欧拉回路+并查集+字典树
- (NYoj 230)彩色棒 --欧拉通路,字典树,并查集
- POJ/PKU 2513 并查集+字典树+欧拉回路
- 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
- Grails2.4发布
- servlet一次性验证码书写步骤
- linux系统调度源码分析
- 同步和异步、阻塞和非阻塞
- 多线程WEB高并发压力测试软件JMeter
- nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路
- Javascript 语言精粹——对象 Object
- 栈-链式
- iPad2的VoiceOver是什么
- PHP实现Ajax注册用户名验证---示例
- 出现"已安装了存在签名冲突的同名数据包"的原因及解决办法
- 局部变量,静态局部变量,全局变量,静态全局变量在内存中的存放区别(转)
- java成员变量为null转换成json不包含此内容
- 一次心惊肉跳的服务器误删文件的恢复过程