首页 > 试题广场 >

第一题

[编程题]第一题
  • 热度指数:16467 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
该题的目的是要你统计图的连通分支数。

输入描述:
每个输入文件包含若干行,每行两个整数i,j,表示节点i和j之间存在一条边。
(请使用循环输入,可以参考该网址:https://www.nowcoder.com/discuss/276)


输出描述:
输出每个图的联通分支数。
示例1

输入

1 4
4 3
5 5

输出

2
头像 Coming680
发表于 2022-02-11 12:07:31
并查集简单运用 #include<iostream> #include<map> using namespace std; map<int,int> mp; int find(int x){ if(x != mp[x]) return mp 展开全文
头像 健康快乐最重要
发表于 2020-02-21 13:13:16
并查集的板子,一开始不会做,后来多做了几道就差不多会了。普通并查集只是顺序的节点号。但是本题给出的节点号不是顺序的,所以这里没有采用数组的方式,而是采用了map散列表的方式。当然通过dfs也可以很好的解决这个题目。通过对所有节点进行dfs。只要没有全遍历到,那就sum++; #include< 展开全文
头像 用户抉择
发表于 2021-03-25 09:36:25
#include <stdio.h> #define N 1000010 int dad[N],h[N]; void Initial() {     for(int i=0;i<N; 展开全文
头像 给我就亿下
发表于 2023-03-26 16:45:32
#include <iostream> using namespace std; const int MAXN = 1000001; int father[MAXN]; int height[MAXN]; bool visit[MAXN]; void Initial (){ fo 展开全文
头像 牛客600247800号
发表于 2022-02-16 19:00:12
并查集 #include <iostream> #include <vector> using namespace std; const int MAXN=1000010; vector<int> s(MAXN, -1); vector<bool> 展开全文
头像 2030ssxx
发表于 2024-09-18 20:00:54
//就是最原始的并查集 #include <iostream> using namespace std; int father[1000000]; int height[1000000]; int visit[1000000]; void Initial() { for(int 展开全文
头像 XwwwwL
发表于 2023-03-03 19:55:18
#include <iostream> using namespace std; int father[1000010]; int height[1000010]; bool visit[1000010]; void Init() { for (int i = 0; i & 展开全文
头像 loveC--
发表于 2024-03-16 15:06:22
并查集的简单板子 属实没想到要申请1百万个 #include<iostream> using namespace std; #define N 1000010 int father[N]; bool visit[N]; int Find(int x) { if (father[x] 展开全文
头像 牛客206588308号
发表于 2025-02-23 13:22:05
#include<iostream> using namespace std; const int maxn=1000000; int father[maxn]; int height[maxn]; bool visit[maxn]; void initial(){ for(in 展开全文
头像 不红红黑路同
发表于 2022-03-05 14:24:38
#include <iostream> using namespace std; //习题11.2 第一题 const int maxn=1000010; int father[maxn]; int height[maxn]; int visit[maxn]; //初始化 voi 展开全文