Skip to content
GitLab
菜单
项目
群组
代码片段
帮助
帮助
支持
社区论坛
快捷键
?
提交反馈
登录/注册
切换导航
菜单
打开侧边栏
Xingyue MA
xepTemplateLibrary
提交
caed942d
提交
caed942d
编辑于
5月 24, 2021
作者:
xep
浏览文件
2021.5.24 AOE
上级
5dc39725
变更
1
Hide whitespace changes
Inline
Side-by-side
digraph_link.cpp
浏览文件 @
caed942d
#include
<iostream>
#include
<iostream>
#include
<cstdio>
#include
<cstdio>
#include
<utility>
#include
<utility>
#include
<algorithm>
/// <summary>
/// <summary>
/// 有向图的邻接表表示。暂时只考虑插入的情况。
/// 有向图的邻接表表示。暂时只考虑插入的情况。
...
@@ -8,36 +9,41 @@
...
@@ -8,36 +9,41 @@
/// </summary>
/// </summary>
/// <typeparam name="V"></typeparam>
/// <typeparam name="V"></typeparam>
/// <typeparam name="E"></typeparam>
/// <typeparam name="E"></typeparam>
template
<
typename
V
,
typename
E
>
template
<
typename
V
,
typename
E
>
class
Graph
{
class
Graph
{
public:
public:
struct
Edge
;
struct
Edge
;
struct
Vertex
{
struct
Vertex
{
V
vdata
;
V
vdata
;
Edge
*
adj
;
Edge
*
adj
;
Vertex
()
:
vdata
(),
adj
(
nullptr
){}
Vertex
()
:
vdata
(),
adj
(
nullptr
)
{}
};
};
struct
Edge
{
struct
Edge
{
E
edata
;
E
edata
;
int
to
;
int
to
;
Edge
*
link
;
Edge
*
link
;
Edge
(
int
to_
)
:
edata
(),
to
(
to_
),
link
(
nullptr
){}
Edge
(
int
to_
)
:
edata
(),
to
(
to_
),
link
(
nullptr
)
{}
Edge
(
int
to_
,
const
E
&
data
)
:
edata
(
data
),
to
(
to_
),
link
(
nullptr
){}
Edge
(
int
to_
,
const
E
&
data
)
:
edata
(
data
),
to
(
to_
),
link
(
nullptr
)
{}
};
};
Graph
(
int
size_
);
Graph
(
int
size_
);
~
Graph
();
~
Graph
();
Edge
*
add_edge
(
int
from
,
int
to
,
const
E
&
data
);
Edge
*
add_edge
(
int
from
,
int
to
,
const
E
&
data
);
//添加双向边
//添加双向边
void
add_biedge
(
int
a
,
int
b
,
const
E
&
data
);
void
add_biedge
(
int
a
,
int
b
,
const
E
&
data
);
Edge
*
first_edge
(
int
v
);
Edge
*
first_edge
(
int
v
);
inline
const
Edge
*
first_edge
(
int
v
)
const
{
return
const_cast
<
Graph
*>
(
this
)
->
first_edge
(
v
);
}
inline
int
get_size
()
const
{
inline
int
get_size
()
const
{
return
size
;
return
size
;
}
}
void
get_indegrees
(
int
*
indeg
)
const
;
Graph
(
const
Graph
&
)
=
delete
;
Graph
(
const
Graph
&
)
=
delete
;
Graph
(
Graph
&&
)
=
delete
;
Graph
(
Graph
&&
)
=
delete
;
Graph
&
operator
=
(
const
Graph
&
)
=
delete
;
Graph
&
operator
=
(
const
Graph
&
)
=
delete
;
...
@@ -49,7 +55,7 @@ protected:
...
@@ -49,7 +55,7 @@ protected:
};
};
template
<
typename
V
,
typename
E
>
template
<
typename
V
,
typename
E
>
Graph
<
V
,
E
>::
Graph
(
int
size_
)
:
size
(
size_
)
Graph
<
V
,
E
>::
Graph
(
int
size_
)
:
size
(
size_
)
{
{
vertices
=
new
Vertex
[
size
];
vertices
=
new
Vertex
[
size
];
}
}
...
@@ -75,7 +81,7 @@ Graph<V, E>::~Graph()
...
@@ -75,7 +81,7 @@ Graph<V, E>::~Graph()
// 注意 这是插入到第一个
// 注意 这是插入到第一个
template
<
typename
V
,
typename
E
>
template
<
typename
V
,
typename
E
>
typename
Graph
<
V
,
E
>::
Edge
*
Graph
<
V
,
E
>::
add_edge
(
int
from
,
int
to
,
const
E
&
data
)
typename
Graph
<
V
,
E
>::
Edge
*
Graph
<
V
,
E
>::
add_edge
(
int
from
,
int
to
,
const
E
&
data
)
{
{
Edge
*
e
=
new
Edge
(
to
,
data
);
Edge
*
e
=
new
Edge
(
to
,
data
);
e
->
link
=
vertices
[
from
].
adj
;
e
->
link
=
vertices
[
from
].
adj
;
...
@@ -91,7 +97,20 @@ void Graph<V, E>::add_biedge(int a, int b, const E& data)
...
@@ -91,7 +97,20 @@ void Graph<V, E>::add_biedge(int a, int b, const E& data)
}
}
template
<
typename
V
,
typename
E
>
template
<
typename
V
,
typename
E
>
typename
Graph
<
V
,
E
>::
Edge
*
Graph
<
V
,
E
>::
first_edge
(
int
v
)
typename
Graph
<
V
,
E
>::
Edge
*
Graph
<
V
,
E
>::
first_edge
(
int
v
)
{
{
return
vertices
[
v
].
adj
;
return
vertices
[
v
].
adj
;
}
template
<
typename
V
,
typename
E
>
void
Graph
<
V
,
E
>::
get_indegrees
(
int
*
indeg
)
const
{
std
::
fill
(
indeg
,
indeg
+
size
,
0
);
for
(
int
i
=
0
;
i
<
size
;
i
++
)
{
auto
*
e
=
vertices
[
i
].
adj
;
while
(
e
)
{
indeg
[
e
->
to
]
++
;
e
=
e
->
link
;
}
}
}
}
\ No newline at end of file
编辑
预览
Supports
Markdown
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录