題目鏈接:http://poj.org/problem?id=2513
題意:n個木棍,木棍兩個端點分別塗上色,問能否將所有木棍都連接起來,要求是木棍連接的兩個端點顏色必須相同。
思路:這題用STL會超時,否則可以直接用map+dsu,所以只能用hash,用字典樹作出string'到int的映射,然後用並查集判斷歐拉回路。
無向圖判斷歐拉回路的條件是:
①所有頂點的度數均爲偶數(這道題連接之後的木棍兩個端點度數爲1)
②圖是連通的(可用並查集判斷,所有頂點只有一個公共祖先即符合條件)
(話說並查集真是個神奇的東西..)
poj 2315:
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <list>
#include <set>
//#define ONLINE_JUDGE
#define eps 1e-6
#define INF 0x7fffffff //INT_MAX
#define inf 0x3f3f3f3f //int??????????????????
#define FOR(i,a) for((i)=0;i<(a);(i)++) //[i,a);
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define MEM3(a) memset(a,0x3f,sizeof(a))
#define MEMS(a) memset(a,'\0',sizeof(a))
#define LL __int64
const double PI = acos(-1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
using namespace std;
template<class T>
T Mint(T a, T b, T c) {
if (a>b) {
if (c>b)
return b;
return c;
}
if (c>a)
return a;
return c;
}
template<class T>
T Maxt(T a, T b, T c) {
if (a>b) {
if (c>a)
return c;
return a;
}
else if (c > b)
return c;
return b;
}
const int maxn=500010;
int T,n,m;
int f[maxn],degree[maxn];
int color;
int find(int x){//路徑壓縮
return f[x]==-1?x:find(f[x]);
}
void Union(int a,int b){//合併
int ra=find(a);
int rb=find(b);
if(ra==rb) return ;
f[ra]=rb;
}
struct Trie{//構造字典樹
Trie *next[26];//單詞指針
int id;//顏色編號
bool isword;//判斷是否結束
};
Trie *root;
int hash(char *str){//利用字典樹構造字符串到編號int的映射
int len=strlen(str);
Trie *p=root,*q;
for1(i,0,len){
int id=str[i]-'a';
if(p->next[id]==NULL){
q=(Trie*)malloc(sizeof(Trie));
for1(j,0,26)
q->next[j]=NULL;
q->isword=false;
q->id=0;
p->next[id]=q;
}
p=p->next[id];
}
if(p->isword){//若顏色單詞已經存在,返回其編號
return p->id;
}
else{
p->isword=true;
p->id=color++;//若顏色單詞不存在,分配編號並返回
return p->id;
}
}
void del(Trie *T){
if(T==NULL) return ;
for1(i,0,26){
if(T->next[i]!=NULL)
del(T->next[i]);
}
free(T);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
#endif
MEM2(f);
MEM1(degree);
root = (Trie *) malloc (sizeof(Trie));
for1(i,0,26)//初始化根節點
root->next[i]=NULL;
root->isword=false;
root->id=0;
color=0;//初始化顏色編號
char s1[25],s2[25];
int cnt1=0,cnt2=0;
while(~scanf("%s%s",s1,s2)){
int t1=hash(s1);//記錄s1,s2的顏色編號
int t2=hash(s2);
degree[t1]++;//記錄s1,s2顏色編號出現的次數,即度數
degree[t2]++;
Union(t1,t2);//合併
}
for1(i,0,color){
if(f[i]==-1) cnt1++;
if(degree[i]&1) cnt2++;
if(cnt1>1) break;//若存在多個祖先,則圖爲森林,不連通
if(cnt2>2) break;//若度數爲奇數的節點數>3,歐拉回路必不存在
}
if((cnt1==0||cnt1==1)&&(cnt2==0||cnt2==2)){//數據略坑,有空數據的情況
printf("Possible\n");
}
else{
printf("Impossible\n");
}
return 0;
}
.