Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zhhf96
C-Plus-Plus-TheAlgorithms
提交
418e876d
C
C-Plus-Plus-TheAlgorithms
项目概览
zhhf96
/
C-Plus-Plus-TheAlgorithms
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
C-Plus-Plus-TheAlgorithms
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
418e876d
编写于
8月 07, 2020
作者:
F
Filip Hlasek
提交者:
GitHub
8月 07, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix linter warnings for connected_components_with_dsu. (#992)
上级
426a12da
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
15 addition
and
13 deletion
+15
-13
graph/connected_components_with_dsu.cpp
graph/connected_components_with_dsu.cpp
+15
-13
未找到文件。
graph/connected_components_with_dsu.cpp
浏览文件 @
418e876d
...
...
@@ -2,19 +2,20 @@
#include <set>
#include <vector>
int
N
;
// denotes number of nodes;
int
number_of_nodes
;
// denotes number of nodes;
std
::
vector
<
int
>
parent
;
std
::
vector
<
int
>
siz
;
std
::
vector
<
int
>
connected_set_size
;
void
make_set
()
{
// function the initialize every node as it's own parent
for
(
int
i
=
1
;
i
<=
N
;
i
++
)
{
for
(
int
i
=
1
;
i
<=
number_of_nodes
;
i
++
)
{
parent
[
i
]
=
i
;
siz
[
i
]
=
1
;
connected_set_size
[
i
]
=
1
;
}
}
// To find the component where following node belongs to
int
find_set
(
int
v
)
{
if
(
v
==
parent
[
v
])
if
(
v
==
parent
[
v
])
{
return
v
;
}
return
parent
[
v
]
=
find_set
(
parent
[
v
]);
}
...
...
@@ -22,30 +23,31 @@ void union_sets(int a, int b) { // To join 2 components to belong to one
a
=
find_set
(
a
);
b
=
find_set
(
b
);
if
(
a
!=
b
)
{
if
(
siz
[
a
]
<
siz
[
b
])
if
(
connected_set_size
[
a
]
<
connected_set_size
[
b
])
{
std
::
swap
(
a
,
b
);
}
parent
[
b
]
=
a
;
siz
[
a
]
+=
siz
[
b
];
connected_set_size
[
a
]
+=
connected_set_size
[
b
];
}
}
int
no_of_connected_components
()
{
// To find total no of connected components
std
::
set
<
int
>
temp
;
// temp set to count number of connected components
for
(
int
i
=
1
;
i
<=
N
;
i
++
)
temp
.
insert
(
find_set
(
i
));
for
(
int
i
=
1
;
i
<=
number_of_nodes
;
i
++
)
temp
.
insert
(
find_set
(
i
));
return
temp
.
size
();
}
// All critical/corner cases have been taken care of.
// Input your required values: (not hardcoded)
int
main
()
{
std
::
cin
>>
N
;
parent
.
resize
(
N
+
1
);
siz
.
resize
(
N
+
1
);
std
::
cin
>>
number_of_nodes
;
parent
.
resize
(
number_of_nodes
+
1
);
connected_set_size
.
resize
(
number_of_nodes
+
1
);
make_set
();
int
edges
;
int
edges
=
0
;
std
::
cin
>>
edges
;
// no of edges in the graph
while
(
edges
--
)
{
int
node_a
,
node_b
;
int
node_a
=
0
,
node_b
=
0
;
std
::
cin
>>
node_a
>>
node_b
;
union_sets
(
node_a
,
node_b
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录