poj 2513 -- Colored Sticks ( Trie + 并查集 + 欧拉 )
来源:互联网 发布:知乎 杜嘟嘟 编辑:程序博客网 时间:2024/05/16 09:21
WA了很久的一道题了,今天终于过了
判断条件有两个
1 、出现次数为奇数的颜色<=2
2 、判断图是否连通
用Trie处理效率较高,判断连通可以将颜色映射成数字用并查集
# include <cstdio># include <iostream># include <set># include <map># include <vector># include <list># include <queue># include <stack># include <cstring># include <string># include <cstdlib># include <cmath># include <algorithm>using namespace std ;struct Dic{ int id ; int vis ; int next [ 30 ] ;} dic [ 3000000 ] ;int cnt ;int pre [ 510000 ] ;int top ;int find ( int x ){ if ( pre [ x ] != x ) pre [ x ] = find ( pre [ x ] ) ; return pre [ x ] ;}void Union ( int x , int y ){ pre [ find ( x ) ] = find ( y ) ;}int addword ( char t [ ] ){ int num = 0 ; int len = strlen ( t ) ; for ( int i = 0 ; i < len ; i ++ ) { int tmp = t [ i ] - 'a' ; if ( dic [ num ] . next [ tmp ] == 0 ) { dic [ num ] . next [ tmp ] = cnt ; num = cnt ++ ; } else num = dic [ num ] . next [ tmp ] ; } dic [ num ] . vis ++ ; if ( dic [ num ] . vis == 1 ) { dic [ num ] . id = top ++ ; } return dic [ num ] . id ;}int findword ( char * t ){ int num = 0 ; int len = strlen ( t ) ; for ( int i = 0 ; i < len ; i ++ ) { int tmp = t [ i ] - 'a' ; if ( dic [ num ] . next [ tmp ] == 0 ) return false ; else num = dic [ num ] . next [ tmp ] ; } return dic [ num ] . id ;}int main ( ){ cnt = 1 ; top = 1 ; for ( int i = 1 ; i < 510000 ; i ++ ) pre [ i ] = i ; char s [ 12 ] , t [ 13 ] ; while ( scanf ( "%s%s" , s , t ) != EOF ) { int x = addword ( s ) ; int y = addword ( t ) ; Union ( x , y ) ; } int flag = 1 ; int sum = 0 ; for ( int i = 1 ; i < cnt ; i ++ ) { if ( dic [ i ] . vis % 2 ) sum ++ ; } if ( sum > 2 ) flag = 0 ; if ( flag == 0 ) cout << "Impossible" << endl ; else { for ( int i = 2 ; i < top ; i ++ ) if ( find ( i ) != find ( 1 ) ) flag = 0 ; if ( flag ) cout << "Possible" << endl ; else cout << "Impossible" << endl ; }}
- poj 2513 -- Colored Sticks ( Trie + 并查集 + 欧拉 )
- [ACM] POJ 2513 Colored Sticks (Trie树,欧拉通路,并查集)
- poj 2513 Colored Sticks(trie+并查集+欧拉通路)
- POJ 2513-Colored Sticks(连接木棍-trie树+并查集+欧拉通路)
- POJ 2513 Colored Sticks (Trie + 并查集 + 欧拉通路)
- POJ 2513 Colored Sticks(Trie+并查集+欧拉回路)
- Poj 2513 Colored Sticks//Trie+并查集+欧拉通路
- POJ 2513 Colored Sticks(trie 欧拉通路 并查集)
- POJ 2513 Colored Sticks(欧拉回路判断+字典树Trie+并查集)
- POJ 2513 Colored Sticks (并查集 Trie树 欧拉回路)
- POJ 2513 Colored Sticks (Trie字典树+欧拉通路+并查集) 解题报告
- POJ 2513 Colored Sticks (Trie树,欧拉通路,并查集)
- poj-2513-Colored Sticks-字典树trie / hash +并查集 欧拉回路
- POJ 2513 Colored Sticks <trie+并查集+欧拉路径>
- POJ-2513 Colored Sticks【并查集+Trie+欧拉路】
- poj 2513 Colored Sticks trie树+欧拉图+并查集
- PKU 2513 Colored Sticks - Trie树+并查集+欧拉通路
- pku 2513 Colored Sticks(Trie树,并查集,欧拉通路)
- 怎样写英语日记
- Can't create handler inside thread that has not called Looper.prepare() 终极解决方法
- screenX,clientX的区别
- doc由C盘进入E盘
- useful Linux command
- poj 2513 -- Colored Sticks ( Trie + 并查集 + 欧拉 )
- jQuery参考实例 1.0 jQuery的哲学
- UILabel的属性和方法
- getch getche getchar的区别
- 图像压缩(裁剪)
- 隔夜利率再现过山车 央行或加码回笼力度
- php获取网页内容方法总结
- SOA-SSH分层架构
- Set Start Transaction after SAP Logon