poj 2513 Colored Sticks (字典树,并查集,欧拉回路)
来源:互联网 发布:淘宝有燕窝吗 编辑:程序博客网 时间:2024/05/17 05:13
N根木棍,两头都有颜色,拼接的时候,必须将颜色一致的接在一起,问能否把全部木棍拼成一整根?
1.颜色映射到数字
map效率低,超时。因此用字典树。
2.判断能否形成通路
条件:所有点的度数都是偶数,或者只有两个点的度数是奇数
(dad是并查集的数组,判断是否全部连通)
for(int i=0;i<color;i++) { if(dad[i]==-1) dadcnt++; if(deg[i]%2!=0) degcnt++; if(dadcnt>1)break; if(degcnt>2) break; } if((degcnt==0||degcnt==2)&&(dadcnt==0||dadcnt==1)) { System.out.println("Possible"); } else System.out.println("Impossible");
总结:了解了字典树的功能并不局限,复习了下并查集。
完整代码:
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.*;class Node{Node[] next=new Node[26];int complete,num;}public class Main{static BufferedReader sc=new BufferedReader(new InputStreamReader(System.in));static int color=0;static int [] deg=new int[500010];static int [] dad=new int[500010]; static int insert(Node root,String s) { for(int i=0;i<s.length();i++) { if(root.next[s.charAt(i)-'a']==null) { Node tmp=new Node(); root.next[s.charAt(i)-'a']=tmp; } root=root.next[s.charAt(i)-'a']; } if(root.complete==1) { return root.num; } else { root.complete=1; root.num=color++; return root.num; } } static int find(int x) { //查到该点的老大,返回 if(dad[x]==-1) return x; else return dad[x]=find(dad[x]); } static void bind(int head,int rear) { int a=find(head); int b=find(rear); //查到各自的老大,如果不同,就连接 if(a!=b) dad[a]=b; }public static void main(String[] args) throws IOException { String s; Node root=new Node(); Arrays.fill(dad, -1); while((s=sc.readLine())!=null) { String [] str; str=s.split(" "); int head=insert(root,str[0]); int rear=insert(root,str[1]); //获得颜色的映射,此种颜色度数+1 deg[head]++; deg[rear]++; bind(head,rear); } int dadcnt=0,degcnt=0; for(int i=0;i<color;i++) { if(dad[i]==-1) dadcnt++; if(deg[i]%2!=0) degcnt++; if(dadcnt>1)break; if(degcnt>2) break; } if((degcnt==0||degcnt==2)&&(dadcnt==0||dadcnt==1)) { System.out.println("Possible"); } else System.out.println("Impossible"); System.exit(0); } }
阅读全文
0 0
- 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+并查集)
- 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-字典树trie / hash +并查集 欧拉回路
- POJ Colored Sticks 2513(字典树+并查集+欧拉回路)
- POJ 2513 Colored Sticks——字典树+并查集+欧拉回路
- JAVA中Stack和Heap的区别
- (三)浅谈Servlet
- 老鼠走迷宫
- VC项目的运行库MT、Md、MDd、MTd
- Why Lua?
- poj 2513 Colored Sticks (字典树,并查集,欧拉回路)
- 总结几道经典面试笔试题
- C语言快速排序实现方案(面向ACM、NOIP)
- activiti自定义流程之Activiti Modeler汉化
- Qt学习:QLabel的用法及其程序示例
- github的使用及在vscode中的使用
- 搜索引擎为什么不收录原创文章
- Everything up-to-date
- 堆--学习记录