数据结构之 图论---连通分量的个数(dfs搜索)
数据结构实验:连通分量个数
Time Limit: 1000MS Memory limit: 65536K
题目描述
输入
输出
示例输入
2
3 1
1 2
3 2
3 2
1 2
示例输出
2
1
#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
#define INF 99999999 using namespace std; int map[50][50];
int vis[50];
int n; void dfs(int dd)
{
int j;
for(j=1; j<=n; j++)
{
if(!vis[j] && map[dd][j]==1 )
{
vis[j]=1;
dfs(j);
}
}
} int main()
{
int t;
cin>>t; int m;
int u, v;
int cnt;
int i; while(t--)
{
cin>>n>>m; memset(map, 0, sizeof(map));
memset(vis, 0, sizeof(vis)); cnt=0;
while(m--)
{
cin>>u>>v;
map[u][v]=1;
map[v][u]=1;
} for(i=1; i<=n; i++)
{
if(!vis[i] )
{
dfs(i);
cnt++;
}
}
cout<<cnt<<endl;
}
return 0;
}
数据结构之 图论---连通分量的个数(dfs搜索)的更多相关文章
- 数据结构之 图论---基于邻接矩阵的广度优先搜索遍历(输出bfs遍历序列)
数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory limit: 65536K 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索( ...
- SDUT 1488 数据结构实验:连通分量个数
数据结构实验:连通分量个数 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 在无向图中,如 ...
- SDUT 2141 【TEST】数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem ...
- sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)
Balloons Time Limit: 1000MS Memory limit: 65536K 题目描述 Both Saya and Kudo like balloons. One day, the ...
- hdu 1312:Red and Black(DFS搜索,入门题)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1312:Red and Black(DFS搜索)
HDU 1312:Red and Black Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- DFS搜索题素数环
素数环: 输入整数1,2,3,4,5,···,n组成一个环,使得相邻两个整数之和均为素数. 输出时从整数1开始逆时针排列.同一个环应恰好输出一次.n<=16. Sample: input: 6 ...
- nyist oj 19 擅长排列的小明(dfs搜索+STL)
擅长排列的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 小明十分聪明.并且十分擅长排列计算.比方给小明一个数字5,他能立马给出1-5按字典序的全排列,假设你想 ...
- 蓝桥杯dfs搜索专题
2018激光样式 #include<bits/stdc++.h> using namespace std; /* dfs(i) 第i个激光机器 有两种选择:vis[i-1] == 0 时 ...
随机推荐
- ARC072/ABC059
AtCoder Regular Contest 072 / Beginner Contest 059 Announcement <br > 猛然发现今天有一场AC.....然后..显示手残 ...
- memcachq队列安装
memcacheq是集中的队列小软件,使用起来简单,便捷,高效. 下载 http://git.oschina.net/sgfoot/linux-tools/raw/master/memcacheq-0 ...
- 3.【nuxt起步】-下面以一个SPA单页程序为例子
spa:single page applcation 1.components目录新建header.vue,footer.vue Header.vue Footer.vue Pages/index.v ...
- 基于HTML5的PACS--HTML5图像处理
在此之前,此系统是结合DICOM的WADO标准,在浏览器里通过javascript操作返回的JPG图片.这种服务器端解析,客户端展现的方式,对实现图像的移动.缩放.旋转.测量等图像操作能够实现实时的交 ...
- Neural Networks for Machine Learning by Geoffrey Hinton (1~2)
机器学习能良好解决的问题 识别模式 识别异常 预測 大脑工作模式 人类有个神经元,每一个包括个权重,带宽要远好于工作站. 神经元的不同类型 Linear (线性)神经元 Binary thresho ...
- Python学习笔记8:标准库之正則表達式
Python拥有强大的标准库.从如今起,開始学习标准库中提供的一些经常使用功能. 首先看正則表達式(regular expression),它的主要功能是从字符串(string)中通过特定的模式(pa ...
- JAVA Timer定时器使用方法
JAVA Timer 定时器测试 MyTask.java:package com.timer; import java.text.SimpleDateFormat;import java.util. ...
- 1079. Total Sales of Supply Chain (25)【树+搜索】——PAT (Advanced Level) Practise
题目信息 1079. Total Sales of Supply Chain (25) 时间限制250 ms 内存限制65536 kB 代码长度限制16000 B A supply chain is ...
- What is the relationship between Xcode, Swift and Cocoa?
Xcode is an IDE for developing Swift or Objective-C applications, which can use the Cocoa API (which ...
- 基于websocket实现的web聊天室
# -*- coding:utf-8 -*- import socket import base64 import hashlib def get_headers(data): "" ...