二、并查集

1. 例题

题目1012:畅通工程

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:10519

解决:4794

题目描述:

某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?

输入:

测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。 
    注意:两个城市之间可以有多条道路相通,也就是说
    3 3
    1 2
    1 2
    2 1
    这种输入也是合法的
    当N为0时,输入结束,该用例不被处理。

输出:

对每个测试用例,在1行里输出最少还需要建设的道路数目。

样例输入:
4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0
样例输出:
1
0
2
998
 #include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <ctype.h>
#include <stack> using namespace std; #define N 1000
int Tree [N]; int findRoot(int x) {
if (Tree[x] == -) return x; //相当于找到根结点了
else {
int tmp = findRoot(Tree[x]); //Tree[x] 存储着x的父节点,所以相当于向上走了一层
Tree[x] = tmp; // x既然不是根结点,就把它顺便直接挂到根结点下面,不用隔着很多层了,查找的时候比较快
return tmp;
}
} int main() {
int n, m;
while(scanf("%d", &n) != EOF && n != ) {
scanf("%d", &m);
for(int i = ; i <= n; i++) Tree[i] = -; // 初始化所有节点为孤立节点
while(m-- != ) { // 读入边信息; 注意这里m--后于判断0执行,以执行1次为例
int a, b;
scanf("%d%d", &a, &b);
a = findRoot(a);
b = findRoot(b);
if(a != b) Tree[a] = b; // 如果a, b不是一个集合里的,则并在一起; 这里把a并在了b下面
}
int ans = ;
for(int i = ; i <= n; i++) {
if(Tree[i] == -) ans++;
}
printf("%d\n", ans-); // 目前有多少个集合,就再修ans-1条路使其联通
} return ; return ;
}

题目1444:More is better

时间限制:3 秒

内存限制:100 兆

特殊判题:

提交:4491

解决:2037

题目描述:

Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.

输入:

The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)

输出:

The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.

样例输入:
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
样例输出:
4
2
 #include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <ctype.h>
#include <stack> using namespace std; #define N 10000001
int Tree [N]; int findRoot(int x) {
if (Tree[x] == -) return x; //相当于找到根结点了
else {
int tmp = findRoot(Tree[x]); //Tree[x] 存储着x的父节点,所以相当于向上走了一层
Tree[x] = tmp; // x既然不是根结点,就把它顺便直接挂到根结点下面,不用隔着很多层了,查找的时候比较快
return tmp;
}
} int sum[N]; int main() {
int n;
while(scanf("%d", &n) != EOF) {
for(int i = ; i < N; i++) {
Tree[i] = -;
sum[i] = ;
}
while(n-- != ) {
int a, b;
scanf("%d%d", &a, &b);
a = findRoot(a);
b = findRoot(b);
if(a != b) {
Tree[a] = b;
sum[b] += sum[a];
}
} int ans = ;
for(int i = ; i < N; i++) {
if(Tree[i] == - && sum[i] > ans) ans = sum[i]; // 统计最大值
}
printf("%d\n", ans);
} return ;
}

2. 练习题

1109题:

题目:

题目1109:连通图
时间限制: 秒内存限制: 兆特殊判题:否提交:4707解决:
题目描述:
给定一个无向图和其中的所有边,判断这个图是否所有顶点都是连通的。
输入:
每组数据的第一行是两个整数 n 和 m(<=n<=)。n 表示图的顶点数目,m 表示图中边的数目。如果 n 为 表示输入结束。随后有 m 行数据,每行有两个值 x 和 y(<x, y <=n),表示顶点 x 和 y 相连,顶点的编号从 开始计算。输入不保证这些边是否重复。
输出:
对于每组输入数据,如果所有顶点都是连通的,输出"YES",否则输出"NO"。
样例输入: 样例输出:
NO
YES

解答:

 #include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <ctype.h>
#include <stack> using namespace std; #define N 1002 int Tree[N]; int findRoot(int x) {
if(Tree[x] == -) return x;
else {
int tmp = findRoot(Tree[x]);
Tree[x] = tmp;
return tmp;
}
} int main() {
int n, m;
while(scanf("%d%d", &n, &m) != EOF) {
if(n == && m == ) return ; for(int i = ; i <= n; i++) Tree[i] = -; while(m-- != ) {
int a, b;
scanf("%d%d", &a, &b);
a = findRoot(a);
b = findRoot(b);
if(a != b) Tree[a] = b; // 即, 使得a的爸爸是b
} int ans = ;
for(int i = ; i <= n; i++) {
if(Tree[i] == -) ans++;
}
if(ans == ) printf("YES\n");
else printf("NO\n");
} return ;
}

1445题:

题目:

题目1445:How Many Tables
时间限制: 秒内存限制: 兆特殊判题:否提交:1857解决:
题目描述:
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.
One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.
For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs tables at least.
输入:
The input starts with an integer T(<=T<=) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(<=N,M<=). N indicates the number of friends, the friends are marked from to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
输出:
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
样例输入: 样例输出:

解答:

 #include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <ctype.h>
#include <stack> using namespace std; int Tree[]; int findRoot(int x) { if(Tree[x] == -) return x;
else{
int tmp = findRoot(Tree[x]); //这里注意一定是Tree[x]
Tree[x] = tmp;
return tmp;
}
} int main() {
int aa;
scanf("%d", &aa);
while(aa--) {
int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) {
Tree[i] = -;
}
while(m--) {
int a, b;
scanf("%d%d", &a, &b);
a = findRoot(a);
b = findRoot(b); if(a != b) {
Tree[a] = b;
}
} int count = ;
for(int i = ; i <= n; i++) {
if(Tree[i] == -) count++;
} // printf("%d\n", count);
cout << count << endl;
} return ;
}

1446题:

九度OJ-第5章-图论的更多相关文章

  1. 【九度OJ】题目1124:Digital Roots 解题报告

    [九度OJ]题目1124:Digital Roots 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1124 题目描述: T ...

  2. 【九度OJ】题目1444:More is better 解题报告

    [九度OJ]题目1444:More is better 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1444 题目描述: ...

  3. 【九度OJ】题目1012:畅通工程 解题报告

    [九度OJ]题目1012:畅通工程 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1012 题目描述: 某省调查城镇交通状况 ...

  4. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  5. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  6. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  7. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  8. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  9. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

  10. 九度OJ 1371 最小的K个数 -- 堆排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...

随机推荐

  1. oracle中number数据类型简单明了解释

    NUMBER (p,s) p和s范围: p 1-38 s -84-127 number(p,s),s大于0,表示有效位最大为p,小数位最多为s,小数点右边s位置开始四舍五入,若s>p,小数点右侧 ...

  2. Spring错误——Spring 单元测试——Test class should have exactly one public constructor

    背景:Spring 构建单元测试 错误 java.lang.Exception: Test class should have exactly one public constructor at or ...

  3. SSM登录跳转到登录页,登录页不能加载js和样式

    SSM登录跳转到登录页,登录页不能加载js和样式选用jsppage添加根路径. <% String rootPath = request.getContextPath(); %> < ...

  4. python基础知识点四

    网络编程(socket) 软件开发的架构: 两个程序之间通讯的应用大致通过从用户层面可以分为两种: 1是C/S,即客户端与服务端,为应用类的,比如微信,网盘等需要安装桌面应用的 2是B/S,即浏览器与 ...

  5. adt安装----只为测试使用adb命令,故无需安装过于复杂

    下载并安装Android SDK  借鉴自原文 https://blog.csdn.net/qq_15304853/article/details/79168248 官网(可FQ选择):http:// ...

  6. 清理SuperMap三维缓存

    一.iclient三维插件浏览器端缓存 方法1:遍历users子文件夹,发现指定目录有supermap,即删除 @echo offtitle 清理三维缓存@echo 开始清理三维缓存...for /f ...

  7. js的Timer方法

    如显示时间: <script> //获取时间 function mytime(){ var a = new Date(); var b = a.toLocaleTimeString(); ...

  8. 『流畅的Python』第1~4章笔记_数据结构、编码

    由于1~4章内容零散且基础,所以统计一下涉及到的内容,记录一下,方便查阅(第一张图右键新页面打开即可看到清晰大图)

  9. 『TensorFlow』梯度优化相关

    tf.trainable_variables可以得到整个模型中所有trainable=True的Variable,也是自由处理梯度的基础 基础梯度操作方法: tf.gradients 用来计算导数.该 ...

  10. python中的进程池和线程池

    Python标准模块-concurrent.futures #1 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 ...