6-19 Count Connected Components(20 分)
Write a function to count the number of connected components in a given graph.
Format of functions:
int CountConnectedComponents( LGraph Graph );
where LGraph
is defined as the following:
typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode{
Vertex AdjV;
PtrToAdjVNode Next;
};
typedef struct Vnode{
PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];
typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;
int Ne;
AdjList G;
};
typedef PtrToGNode LGraph;
The function CountConnectedComponents
is supposed to return the number of connected components in the undirected Graph
.
Sample program of judge:
#include <stdio.h>
#include <stdlib.h>
typedef enum {false, true} bool;
#define MaxVertexNum 10 /* maximum number of vertices */
typedef int Vertex; /* vertices are numbered from 0 to MaxVertexNum-1 */
typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode{
Vertex AdjV;
PtrToAdjVNode Next;
};
typedef struct Vnode{
PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];
typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;
int Ne;
AdjList G;
};
typedef PtrToGNode LGraph;
LGraph ReadG(); /* details omitted */
int CountConnectedComponents( LGraph Graph );
int main()
{
LGraph G = ReadG();
printf("%d\n", CountConnectedComponents(G));
return 0;
}
/* Your function will be put here */
Sample Input (for the graph shown in the figure):
8 6
0 7
0 1
2 0
4 1
2 4
3 5
Sample Output:
3
找图的连通分支
代码:
int CountConnectedComponents( LGraph Graph )
{
int vis[MaxVertexNum] = {};
int c = ;
for(int i = ;i < Graph -> Nv;i ++)
{
if(vis[i] == )
{
c ++;
vis[i] = ;
PtrToAdjVNode t;
int head = ,tail = ;
int s[];
s[tail ++] = i;
while(head < tail)
{
t = Graph -> G[s[head]].FirstEdge;
while(t)
{
if(!vis[t->AdjV])
{
vis[t->AdjV] = ;
s[tail ++] = t -> AdjV;
}
t = t -> Next;
}
head ++;
}
}
}
return c;
}
6-19 Count Connected Components(20 分)的更多相关文章
- L1-049 天梯赛座位分配 (20 分)
L1-049 天梯赛座位分配 (20 分)(Java解法) 天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所 ...
- PAT乙级:1014 福尔摩斯的约会 (20分)
PAT乙级:1014 福尔摩斯的约会 (20分) 题干 大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- [Swift]LeetCode323. 无向图中的连通区域的个数 $ Number of Connected Components in an Undirected Graph
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- 获取数值型数组中大于60的元素个数,给数值型数组中不足60分的加20分。(数组,for循环,if条件判断语句)
package com.Summer_0420.cn; /** * @author Summer * 获取数值型数组中大于60的元素个数 * 给数值型数组中不足60分的加20分 */ public c ...
- Codeforces E - Connected Components?
E - Connected Components? 思路: 补图bfs,将未访问的点存进set里 代码: #include<bits/stdc++.h> using namespace s ...
- 1120 Friend Numbers (20 分)
1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same ...
- PAT 1039 到底买不买(20)(20 分)
1039 到底买不买(20)(20 分) 小红想买些珠子做一串自己喜欢的珠串.卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖.于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要 ...
- 1116 Come on! Let's C (20 分)
1116 Come on! Let's C (20 分) "Let's C" is a popular and fun programming contest hosted by ...
随机推荐
- CCF地铁修建
问题描述 A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市决定在1号到n号枢纽间修建一条地铁. 地铁由很多段隧道组成,每段隧道连接两个交通枢纽.经过勘探,有m段隧道作为候选,两个交通 ...
- RabittMQ实践(一): RabbitMQ的安装、启动
安装: 启动监控管理器:rabbitmq-plugins enable rabbitmq_management 关闭监控管理器:rabbitmq-plugins disable rabbitmq_ ...
- 2. Add Two Numbers(2个链表相加)
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LabView和DLL中的参数问题
注意:在LabView中调用DLL函数时,一定要指定对应的参数类型,而不是选择“Adapt to Type”,否则会出错,不知道为什么书上是要选择“Adapt to Type”. 以下做个参考: 转自 ...
- Python3.x:自动生成IP写入文本
Python3.x:自动生成IP写入文本 ''' 生成ip写入文件 ''' import time time_start = time.time() #参数:number-生成条数:start-开始i ...
- 使用 log4js UDP 发送数据到 logstash
本文地址 http://www.cnblogs.com/jasonxuli/p/6532723.html 因为 nodejs 一般会部署在多台机器,并且每台机器会起多个进程,因此查看日志时往往要人工区 ...
- linux及安全《Linux内核设计与实现》第一章——20135227黄晓妍
<linux内核设计与实现>第一章 第一章Linux内核简介: 1.3操作系统和内核简介 操作系统:系统包含了操作系统和所有运行在它之上的应用程序.操作系统是指整个在系统中负责完成最基本功 ...
- 20145315 《Java程序设计》第九周学习总结
20145315 <Java程序设计>第九周学习总结 教材学习内容总结 第16章--整合数据库 16.1.1JDBC简介 应用程序通过通信协议对数据库进行指令交换,以进行对数据的的增删查找 ...
- HBuilder 获取通讯录
代码: var content=""; function getCallLog() { try{ plus.contacts.getAddressBook(plus.contact ...
- UVa 11889 最小公倍数
https://vjudge.net/problem/UVA-11889 题意: 输入两个整数A和C,求最小的整数B使得lcm(A,B)=C. 思路: 首先C是A的公倍数,如果C%A不为0肯定是无解的 ...