二、并查集

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. JNI与底层调用

    交叉编译 在一个平台下,编译出另一个平台能够执行的二进制的代码 平台:windows,mac os,linux 处理器:x86,arm,mips 交叉编译的原理 源代码->编译->链接-& ...

  2. 复旦高等代数 II(17级)每周一题

    本学期将继续进行高等代数每周一题的活动.计划从第一教学周开始,到第十六教学周为止(根据法定节假日安排,中间个别周会适当地停止),每周的周末将公布1道思考题(共16道),供大家思考和解答.每周一题通过“ ...

  3. style,ng-style, ng-attr-style的对比

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...

  4. Bytom 技术 FAQ

    比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom 1.如何 ...

  5. CSS基础【2】:CSS常见属性

    CSS常见属性 文字属性 font-style 作用:规定文字样式 格式:font-style: italic; 取值: normal:正常的,默认就是正常的 italic:倾斜的 font-weig ...

  6. python tar 打包

    import os import tarfile def make_targz_one_by_one(output_filename, source_dir): tar = tarfile.open( ...

  7. pytest文档13-allure2生成html报告(史上最详细)

    前言 allure是一个report框架,支持java的Junit/testng等框架,当然也可以支持python的pytest框架,也可以集成到Jenkins上展示高大上的报告界面. 环境准备 1. ...

  8. Python自学:第三章 使用列表中的各个值

    bicycles = ['trek','cannondale','redline','specialized'] message = "My first bicycle was a &quo ...

  9. port 执行命令的封装和参数详解

    下面代码摘自rebar_utils.erl -module(tt7). %-export([start/0]). -compile(export_all). -define(FAIL, abort() ...

  10. android --------- 嵌套unity出现 your hardware does not support this application,sorry!

    最近遇见一个这个的问题 ,我在Android端接入Unity3D时出现的问题 问题是打开app直接弹出下面的弹框 点击ok 就退出了 遇到这样的问题 是因为libs文件夹的so文件出现了问题: 解决办 ...