当前位置:网站首页>蓝桥:合根植物
蓝桥:合根植物
2022-07-04 16:09:00 【阿白|】
问题描述
w星球的一个种植园,被分成 m * n 个小格子(东西方向m行,南北方向n列)。每个格子里种了一株合根植物。
这种植物有个特点,它的根可能会沿着南北或东西方向伸展,从而与另一个格子的植物合成为一体。
如果我们告诉你哪些小格子间出现了连根现象,你能说出这个园中一共有多少株合根植物吗?输入格式
第一行,两个整数m,n,用空格分开,表示格子的行数、列数(1<m,n<1000)。
接下来一行,一个整数k,表示下面还有k行数据(0<k<100000)
接下来k行,第2+k行两个整数a,b,表示编号为a的小格子和编号为b的小格子合根了。格子的编号一行一行,从上到下,从左到右编号。
比如:5 * 4 的小格子,编号:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20样例输入
5 4
16
2 3
1 5
5 9
4 8
7 8
9 10
10 11
11 12
10 14
12 16
14 18
17 18
15 19
19 20
9 13
13 17样例说明:其合根情况参考下图(注意:6也是一个连通子集)
样例输出
5
并查集
代码:
package p1.text; import java.util.*; /** * @author WitMoy * @version V1.8 * @date : 2022-07-02 18:16 */ public class Main { private static final Scanner sc = new Scanner(System.in); private static int place[] = new int[1005]; private static int rank[] = new int[1005]; public static void main(String[] args) { int m, n, k; m = sc.nextInt(); n = sc.nextInt(); k = sc.nextInt(); int ans = 0; init(m * n); for(int i = 0; i < k; i++){ int a = sc.nextInt(); int b = sc.nextInt(); join(a,b); } HashSet<Integer> hs = new HashSet<>(); for(int i = 1; i < n * m; i++){ hs.add(place[i]); } System.out.println(hs.size()); } private static void init(int n){ for(int i = 1; i <= n; i++){ place[i] = i; rank[i] = 1; } } private static int find(int x){ if(place[x] == x) return x; return place[x] = find(place[x]); } private static void join(int x, int y){ x = find(x); y = find(y); if(x == y) return; if(rank[x] > rank[y]){ place[y] = x; }else{ if(rank[x] == rank[y]) rank[y]++; place[x] = y; } } }
边栏推荐
- 【209】go语言的学习思想
- With an annual income of more than 8 million, he has five full-time jobs. He still has time to play games
- 设置窗体透明 隐藏任务栏 与全屏显示
- Face_recognition人脸识别之考勤统计
- People in the workplace with a miserable expression
- 补能的争议路线:快充会走向大一统吗?
- 图像检索(image retrieval)
- Dynamic programming stock problem comparison
- 超标量处理器设计 姚永斌 第6章 指令解码 摘录
- Flask lightweight web framework
猜你喜欢

Recast of recastnavigation

创业两年,一家小VC的自我反思
![[test development] software testing - Basics](/img/43/514016f270574fe711e0e15b581022.png)
[test development] software testing - Basics

DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌

"In Vietnam, money is like lying on the street"

【华为HCIA持续更新】SDN与FVC

【HCIA持续更新】WLAN工作流程概述

Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions

The Block:USDD增长势头强劲

mysql5.7安装教程图文详解
随机推荐
"In Vietnam, money is like lying on the street"
High school physics: force, object and balance
数学分析_笔记_第7章:多元函数的微分学
Performance test of Gatling
Is BigDecimal safe to calculate the amount? Look at these five pits~~
With an estimated value of 90billion, the IPO of super chip is coming
Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see
2022 national CMMI certification subsidy policy | Changxu consulting
Is it science or metaphysics to rename a listed company?
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
Why are some online concerts always weird?
股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
【Proteus仿真】基于VSM 串口printf调试输出示例
shell脚本的替换功能实现
【HCIA持续更新】WLAN概述与基本概念
【每日一题】871. 最低加油次数
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
[test development] software testing - Basics
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
mysql5.7安装教程图文详解
