国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[poj 2513] Colored Sticks (trie+歐拉路)

2019-11-08 18:52:42
字體:
來源:轉載
供稿:網友

題目大意:給定一些木棒,木棒兩端都涂上顏色,求是否能將木棒首尾相接,連成一條直線,要求不同木棒相接的一邊必須是相同顏色的。

我們把顏色看做點,木棒看做邊,這道題就轉為了求是否存在歐拉路。

而判斷歐拉路只要看兩點 ①圖連通 ②所有節點的度為偶數或只有兩個奇數度節點。

判斷①我們可以使用并查集且必須壓縮路徑,否則會超時。而并查集需要利用數組下標,但讀入為字符串,并且有25w條邊,使用hash會超時。

這時候我們可以用trie(字典樹),關于字典樹可以自行百度。

#include <cstdio>#include <iostream>#include <cstring>#define maxn 500000using namespace std;struct Trie{	Trie* next[27];	bool flag;	int id;	Trie()    {        flag=false;        id=0;        memset(next,0,sizeof(next));    } }root;int tot;int dg[maxn+5];int fa[maxn+5];int trie(char *ch){	Trie* tree=&root;	int len=0;	while (ch[len]!='/0')	{		int pub=ch[len++]-'a';		if (!tree->next[pub]) tree->next[pub]=new Trie();		tree=tree->next[pub];	}	if (tree->flag) return tree->id;	else 	{		tree->flag=true;		tree->id=++tot;		return tree->id=tot;	}}int getf(int x){	if (fa[x]!=x) fa[x]=getf(fa[x]);	return fa[x];}void ice_chair(int x,int y){	int fx=getf(y);	int fy=getf(x);	fa[fy]=fx;	return ;}char a[15],b[15];int main(){	register int i,j;	for (i=1;i<=maxn;i++)	{		fa[i]=i;	}	while(cin>>a>>b)	{		int x=trie(a);		int y=trie(b);		dg[x]++;		dg[y]++;		ice_chair(x,y);	}	int s=getf(1);	int ans=0;	for (i=1;i<=tot;i++)	{		if (dg[i]%2==1) ans++;		if (ans>2)		{			PRintf("Impossible");			return 0;		}		if (getf(i)!=s)		{			printf("Impossible");			return 0;			}	}	if (ans==1) printf("Impossible");	else printf("Possible");	return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 伊宁县| 宝山区| 甘南县| 建水县| 沭阳县| 唐河县| 金寨县| 米易县| 碌曲县| 双辽市| 合川市| 富源县| 个旧市| 临海市| 抚松县| 启东市| 伊通| 喜德县| 夏津县| 郧西县| 叶城县| 平和县| 北海市| 镇平县| 九江市| 阜平县| 高密市| 金川县| 库伦旗| 阜宁县| 陈巴尔虎旗| 长葛市| 山阳县| 江永县| 乌兰浩特市| 军事| 澄江县| 报价| 西宁市| 昂仁县| 洱源县|