POJ 2513 - Colored Sticks - [尤拉路][圖的連通性][字典樹]
題目連結:
http://poj.org/problem?id=2513
http://bailian.openjudge.cn/practice/2513?lang=en_US
Time Limit: 5000MS Memory Limit: 128000K
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 red
red violet
cyan blue
blue magenta
magenta cyan
Sample Output
Possible
Hint
Huge input,scanf is recommended.
題意:
給出若干木棒,每個木棒兩個端點有兩個顏色,兩個木棒的端點顏色相同,則可以接在一起。問是否把所有木棒接成一根。
題解:
(參考http://blog.csdn.net/lyy289065406/article/details/6647445)
將每個顏色都看成一個節點,木棒就能看成一條連線兩個端點的無向邊。問題就變成在給定一個無向圖問是否存在尤拉路
尤拉路存在的充要條件是:① 圖是連通的; ② 所有節點的度為偶數,或者有僅有兩個度數為奇數的節點。
這道題,求頂點的度數很好求,但是怎麼判斷圖是否連通呢?通常來說,DFS/BFS或者並查集都能做。
不過,本題字典樹的用處就是代替map,將輸入字串快速hash成一個值。
AC程式碼(BFS判圖的連通性):
#include<bits/stdc++.h> using namespace std; const int maxn=250000+10; int degree[2*maxn]; vector<int> G[2*maxn]; namespace Trie { const int SIZE=maxn*10; int sz; int idtot; struct TrieNode{ int ed; int nxt[26]; }trie[SIZE]; void init() { idtot=0; sz=1; } int insert(const string& s) { int p=1; for(int i=0;i<s.size();i++) { int ch=s[i]-'a'; if(!trie[p].nxt[ch]) trie[p].nxt[ch]=++sz; p=trie[p].nxt[ch]; } if(!trie[p].ed) trie[p].ed=++idtot; return trie[p].ed; } }; bool vis[2*maxn]; int bfs(int s) { int res=0; memset(vis,0,sizeof(vis)); queue<int> Q; Q.push(s), vis[s]=1, res++; while(!Q.empty()) { int u=Q.front(); Q.pop(); for(int i=0;i<G[u].size();i++) { int v=G[u][i]; if(!vis[v]) Q.push(v), vis[v]=1, res++; } } return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s1,s2; Trie::init(); while(cin>>s1>>s2) { int u=Trie::insert(s1), v=Trie::insert(s2); G[u].push_back(v); G[v].push_back(u); degree[u]++; degree[v]++; } int cnt=0; for(int i=1;i<=Trie::idtot;i++) { if(degree[i]%2) cnt++; } if(cnt==1 || cnt>2) cout<<"Impossible\n"; else { if(bfs(1)<Trie::idtot) cout<<"Impossible\n"; else cout<<"Possible\n"; } }
AC程式碼(並查集判圖的連通性):
#include<cstdio> #include<cstring> using namespace std; const int maxn=250000+5; namespace Trie { const int SIZE=maxn*10; int sz; int idcnt; struct TrieNode{ int ed; int nxt[26]; }trie[SIZE]; void init() { idcnt=0; sz=1; } int insert(char *s) { int len=strlen(s), p=1; for(int i=0;i<len;i++) { int ch=s[i]-'a'; if(!trie[p].nxt[ch]) trie[p].nxt[ch]=++sz; p=trie[p].nxt[ch]; } return (trie[p].ed)?(trie[p].ed):(trie[p].ed=++idcnt); } }; int par[2*maxn],ran[2*maxn]; int find(int x){return (par[x]==x)?x:(par[x]=find(par[x]));} void unite(int x,int y) { x=find(x), y=find(y); if(x==y) return; if(ran[x]<ran[y]) par[x]=y; else par[y]=x, ran[x]+=(ran[x]==ran[y]); } int degree[2*maxn]; int main() { Trie::init(); memset(degree,0,sizeof(degree)); for(int i=1;i<2*maxn;i++) par[i]=i,ran[i]=0; char s[2][13]; while(scanf("%s %s",s[0],s[1])!=EOF) { int u=Trie::insert(s[0]); int v=Trie::insert(s[1]); if(find(u)!=find(v)) unite(u,v); degree[u]++; degree[v]++; } int cnt=0; int z=find(1); for(int i=1;i<=Trie::idcnt;i++) { if(find(i)!=z) { printf("Impossible\n"); return 0; } if(degree[i]%2) cnt++; if(cnt>2) { printf("Impossible\n"); return 0; } } if(cnt==1) { printf("Impossible\n"); return 0; } else { printf("Possible\n"); return 0; } }