Gotta Write A Code
C++博客
::
首頁
::
新隨筆
::
聯系
::
聚合
::
管理
posts - 33, comments - 33, trackbacks - 0
<
2025年5月
>
日
一
二
三
四
五
六
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
常用鏈接
我的隨筆
我的評論
我參與的隨筆
留言簿
(5)
給我留言
查看公開留言
查看私人留言
隨筆分類
CUDA(1)
Windows Programming(4)
算法題解(22)
隨筆檔案
2012年5月 (1)
2012年3月 (9)
2011年11月 (4)
2011年10月 (1)
2011年9月 (1)
2011年7月 (1)
2011年6月 (3)
2011年5月 (1)
2011年4月 (1)
2011年3月 (2)
2011年1月 (2)
2010年12月 (1)
2010年11月 (6)
搜索
最新評論
1.?re: DX筆記[未登錄]
OrOrOrz!!
--diryboy
2.?re: 作品:動態語言AnyC 1.0
@so
其實里面的代碼存在bug...
--qqdy
3.?re: 作品:動態語言AnyC 1.0
游戲腳本高級編程的代碼很好啊。
--so
4.?re: 作品:動態語言AnyC 1.0
仰慕!!我剛開始學習編譯呢
--coreBugZJ
5.?re: AnyC:添加類型限制[未登錄]
Orz!!
--diryboy
閱讀排行榜
1.?逆序數及其求法(10779)
2.?Poj 3310 判環+度(5977)
3.?水文一篇--基于CUDA的矩陣相乘(4613)
4.?Poj2010 - 堆的應用(2475)
5.?水文:淺析PE File(2349)
評論排行榜
1.?作品:動態語言AnyC 1.0(4)
2.?poj 3074(3)
3.?ACM/ICPC杭州站 - hdu3680(3)
4.?水題四道 3-30(3)
5.?POJ Challenge - 2011.04.10部分題解(3)
Poj 1386 歐拉回路
題意:如果單詞A的結尾字母與單詞B的首字母相同,那么可以認為是A到B相通。給出一系列單詞,求這些詞按照某種排列能否串通。
題解:
如果直接按照題意建模,以單詞為頂點,邊表示兩兩相通,那么將會得到哈密頓回路模型。顯然是很難解的。
換一種方式,以字母為頂點,邊表示傳送的單詞,那么就得到歐拉回路模型的圖,可以按照歐拉定理求解。
以下給出Euler圖的相關知識:
Euler回路:G中經過每條邊一次且僅一次的回路
Euler路徑:G中經過每條邊一次且僅一次的路徑
無向圖存在Euler回路定理:當它是連通圖+頂點度數為偶數
無向圖存在Euler路徑定理:當它是連通圖+除兩個頂點度為奇數外,其余為偶數
有向圖存在Euler回路定理:當它是連通圖+頂點入度 == 出度
有向圖存在Euler路徑定理:當它是連通圖+除一個頂點的入度和出度的差的絕對值小1外,其余相等
代碼:
#include
<
stdio.h
>
#include
<
string
.h
>
const
int
N
=
30
;
class
UnionSet
{
private
:
int
parent[N];
int
rank[N];
int
size;
public
:
UnionSet(
int
_size):size(_size)
{
init();
}
~
UnionSet()
{
}
void
init()
{
for
(
int
i
=
0
; i
<
size;
++
i)
{
parent[i]
=
-
1
;
rank[i]
=
1
;
}
}
int
root(
int
_x)
{
int
r
=
_x;
while
(parent[r]
>=
0
)
r
=
parent[r];
int
i
=
_x;
int
j;
while
(parent[i]
>=
0
)
{
j
=
parent[i];
parent[i]
=
r;
i
=
j;
}
return
r;
}
int
Union(
int
_r1,
int
_r2)
{
if
(_r1
==
_r2)
return
_r1;
else
{
int
root1
=
root(_r1);
int
root2
=
root(_r2);
if
(root1
==
root2)
return
root1;
if
(rank[root1]
>
rank[root2])
{
parent[root2]
=
root1;
rank[root1]
+=
rank[root2];
}
else
{
parent[root1]
=
root2;
rank[root2]
+=
rank[root1];
}
}
}
int
getRank(
int
_x)
{
return
rank[_x];
}
}
;
char
buf1[
1024
];
void
Test()
{
int
In[
30
]
=
{
0
}
;
int
Out[
30
]
=
{
0
}
;
bool
visited[
30
]
=
{
false
}
;
UnionSet Set(
28
);
int
n;
scanf(
"
%d
"
,
&
n);
bool
flag
=
false
;
int
start
=
0
;
for
(
int
i
=
0
; i
<
n;
++
i)
{
scanf(
"
%s
"
,buf1);
int
len
=
strlen(buf1);
Set.Union(buf1[
0
]
-
'
a
'
,buf1[len
-
1
]
-
'
a
'
);
In[buf1[len
-
1
]
-
'
a
'
]
++
;
Out[buf1[
0
]
-
'
a
'
]
++
;
visited[buf1[
0
]
-
'
a
'
]
=
true
;
visited[buf1[len
-
1
]
-
'
a
'
]
=
true
;
if
(
!
flag)
{
start
=
buf1[
0
]
-
'
a
'
;
flag
=
true
;
}
}
for
(
int
i
=
0
; i
<
26
;
++
i)
{
if
(i
!=
start)
{
if
(visited[i]
&&
(Set.root(start)
!=
Set.root(i)))
{
printf(
"
The door cannot be opened.\n
"
);
return
;
}
}
}
int
cntIn
=
0
;
int
cntOut
=
0
;
for
(
int
i
=
0
; i
<
26
;
++
i)
{
if
(visited[i])
{
if
(In[i]
!=
Out[i])
{
if
(In[i]
-
Out[i]
==
-
1
)
{
cntIn
++
;
}
else
if
(In[i]
-
Out[i]
==
1
)
{
cntOut
++
;
}
else
{
printf(
"
The door cannot be opened.\n
"
);
return
;
}
}
}
}
if
((cntIn
!=
cntOut)
||
((cntIn
==
cntOut)
&&
(cntIn
>
1
)))
{
printf(
"
The door cannot be opened.\n
"
);
}
else
printf(
"
Ordering is possible.\n
"
);
}
int
main()
{
//
freopen("data.txt","r",stdin);
int
tc;
scanf(
"
%d
"
,
&
tc);
for
(
int
i
=
0
; i
<
tc;
++
i)
{
Test();
}
return
0
;
}
posted on 2011-06-02 11:56
bennycen
閱讀(1537)
評論(0)
編輯
收藏
引用
只有注冊用戶
登錄
后才能發表評論。
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Powered by:
C++博客
Copyright ©2025 bennycen
久久精品无码一区二区无码
|
性做久久久久久久久老女人
|
99久久这里只有精品
|
久久99热国产这有精品
|
国产三级精品久久
|
久久精品免费一区二区
|
久久久青草久久久青草
|
亚洲国产日韩综合久久精品
|
久久久亚洲欧洲日产国码二区
|
a级毛片无码兔费真人久久
|
久久久国产99久久国产一
|
久久精品国产半推半就
|
久久亚洲sm情趣捆绑调教
|
久久高清一级毛片
|
9久久9久久精品
|
亚洲中文字幕久久精品无码喷水
|
AAA级久久久精品无码片
|
无码乱码观看精品久久
|
93精91精品国产综合久久香蕉
|
欧美久久综合性欧美
|
久久人人爽人人爽人人片AV东京热
|
久久综合丁香激情久久
|
久久久久女人精品毛片
|
久久人人爽人人人人爽AV
|
国产精品一区二区久久国产
|
久久天天躁狠狠躁夜夜avapp
|
精品久久久久久国产免费了
|
亚洲精品国产成人99久久
|
91久久精一区二区三区大全
|
久久人人爽爽爽人久久久
|
久久人人爽人人爽人人片av麻烦
|
欧美亚洲国产精品久久
|
久久久国产亚洲精品
|
久久亚洲日韩看片无码
|
亚洲成色www久久网站夜月
|
国内精品九九久久精品
|
亚洲成色www久久网站夜月
|
久久久久亚洲AV成人片
|
av国内精品久久久久影院
|
免费精品久久天干天干
|
久久精品国产亚洲AV久
|