九度OJ-第5章-图论
二、并查集
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章-图论的更多相关文章
- 【九度OJ】题目1124:Digital Roots 解题报告
[九度OJ]题目1124:Digital Roots 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1124 题目描述: T ...
- 【九度OJ】题目1444:More is better 解题报告
[九度OJ]题目1444:More is better 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1444 题目描述: ...
- 【九度OJ】题目1012:畅通工程 解题报告
[九度OJ]题目1012:畅通工程 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1012 题目描述: 某省调查城镇交通状况 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ 1502 最大值最小化(JAVA)
题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...
- 九度OJ,题目1089:数字反转
题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...
- 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...
- 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...
- 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)
题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
- 九度OJ 1371 最小的K个数 -- 堆排序
题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...
随机推荐
- Docker Kubernetes 查询字段说明
Docker Kubernetes 查询字段说明 # 打印受支持的API版本 kubectl api-versions # 扩展 apiextensions.k8s.io/v1beta1 # 注册 ...
- @Transacitonal注解不生效之spring中expose-proxy的作用与原理
几年前记得整理过,@Transacitonal注解的方法被另外一个方法调用的时候,事务是不生效的. 如果大量代码已经这么写了,这个时候抽取出去不现实,怎么办呢? 答案就是在<aop:aspect ...
- Excel提取设定的多个关键字段
从一段文字中,提取事先设定的关键字段: 在M2单元格输入下列公式: =IFERROR(IF(FIND(O$2,Q2),O$2&" "),"")& ...
- Idea 全局替换指定字符
最近使用idea开发,刚接触不久,然后碰到需要全局替换的时候,懵逼了.之前使用eclipse 直接Ctrl+F 就可以操作了. 现在使用idea 摁Ctrl+F竟然只能搜,不能替换....尴尬的一匹. ...
- Codeforces 932G Palindrome Partition - 回文树 - 动态规划
题目传送门 通往???的传送点 通往神秘地带的传送点 通往未知地带的传送点 题目大意 给定一个串$s$,要求将$s$划分为$t_{1}t_{2}\cdots t_{k}$,其中$2\mid k$,且$ ...
- Java 泛型方法、泛型类、通配符、通配符上下限
泛型方法 泛型方法定义规则: 所有泛型方法声明都有一个类型参数声明部分(由尖括号分隔),该类型参数声明部分在方法返回类型之前. 每一个类型参数声明部分包含一个或多个类型参数,参数间用逗号隔开.一个泛型 ...
- Learning-Python【31】:操作系统基础知识
什么是操作系统 计算机系统由硬件和软件两部分组成.操作系统(OS,Operating System)是配置在计算机硬件上的第一层软件,是对硬件系统的首次扩充.它在计算机系统中占据了特别重要的地位:而其 ...
- HDU 4283 You Are the One ——区间dp
参考了许多大佬 尤其是https://blog.csdn.net/woshi250hua/article/details/7973824这一篇 ,最后我再加一点我的见解. 大意是 给定一个序列,序列 ...
- beyond compare 免费版
链接:https://pan.baidu.com/s/10lPUEpFPZU76HRbJfbZ2ZA 提取码:r2go
- Jupyter Notebook插入图片的4种方法
来自:https://blog.csdn.net/qq_33039859/article/details/78507316 方法1:  Markdown ...