九度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 ...
随机推荐
- Python GIL锁
GIL全局解释器锁:为了解决多线程修改同一块数据. python的线程是调用操作系统的源生线程,启动时就是调用C语言的C源生接口,python调用C语言接口的线程去执行任务时,必须上下文对应关系传给C ...
- ubuntu16.04安装wordpress
ubuntu16.04安装wordpress和centos7安装wordpress存在一定的差异. 当然共性大于差异. 共性是lamp环境. wordpress的必备环境. 先共性再差异. 一.搭建l ...
- vue 文件目录结构详解
vue 文件目录结构详解 本篇文章主要介绍了vue 文件目录结构详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 项目简介 基于 vue.js 的前端开发环境,用于前后 ...
- tensorflow中batch normalization的用法
网上找了下tensorflow中使用batch normalization的博客,发现写的都不是很好,在此总结下: 1.原理 公式如下: y=γ(x-μ)/σ+β 其中x是输入,y是输出,μ是均值,σ ...
- [原][算法][earth]三段smooth,传入时间,返回距离。仿谷歌视角飞跃处理
算法需求: 传入[0~1]的时间time,返回[0~1]的路程. 整个路程distance[0~1]分为三段路径: 第一段:在0.25time的时间里,速度从0,位置从distance:0加速移动到距 ...
- IDEA中更改Tomcat服务器的URL
先别参考以下的做法,貌似出了点状况,后续修正!!! 直接上图吧 看IDEA的右上角 点击黄色方框内的Edit Configurations,进入以下的编辑面 后面四个框内的都可以修改,当然不要修改和其 ...
- Java项目中使用log记录日志的一些总结
本文介绍了一下自己在Java项目中使用log的一些总结,从日志的作用.日志的选用.日志级别介绍.日志记录的一些最佳实践几个方面阐述. 日志的作用 主要作用包括: 1.出问题后定位当时问题 2.显示程序 ...
- Java版 人脸识别SDK dem
虹软人脸识别SDK之Java版,支持SDK 1.1+,以及2.0版本,滴滴,抓紧上车! 前言由于业务需求,最近跟人脸识别杠上了,本以为虹软提供的SDK是那种面向开发语言的,结果是一堆dll······ ...
- 把腾讯视频嵌入到html中
---------------------------------------------------------------------------------------------------- ...
- Angular4学习笔记(七)- ViewChild和ViewChildren
基础 ViewChild ViewChild 装饰器用于获取模板视图中的元素或直接调用其组件中的方法.它支持 Type 类型或 string 类型的选择器,同时支持设置 read 查询条件,以获取不同 ...