当前位置:网站首页>Blue bridge: sympodial plant
Blue bridge: sympodial plant
2022-07-04 18:11:00 【A Bai|】
Problem description
w A plantation on the planet , Is divided into m * n A little lattice ( East west direction m That's ok , North South n Column ). There's a sympodial plant in each grid .
This plant has a characteristic , Its roots may extend north-south or East-West , So that it can be integrated with another lattice of plants .
If we tell you which cells are rooted , Can you tell me how many sympodial plants there are in this garden ?Input format
first line , Two integers m,n, Separate with spaces , Represents the number of rows in the grid 、 Number of columns (1<m,n<1000).
Next line , An integer k, It means that there are more k Row data (0<k<100000)
Next k That's ok , The first 2+k Line two integers a,b, Indicates that the number is a And the number is b My little lattice is rooted .The number of the grid line by line , From top to bottom , Number from left to right .
such as :5 * 4 The little lattice of , Number :
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20The sample input
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 17Sample explanation : See the figure below for the root combination ( Be careful :6 It is also a connected subset )
Sample output
5
Union checking set
Code :
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; } } }
边栏推荐
- 【每日一题】556. 下一个更大元素 III
- wuzhicms代码审计
- “在越南,钱就像躺在街上”
- [HCIA continuous update] network management and operation and maintenance
- 用于图数据库的开源 PostgreSQL 扩展 AGE被宣布为 Apache 软件基金会顶级项目
- Win32 API access route encrypted web pages
- android使用SQLiteOpenHelper闪退
- Zhijieyun - meta universe comprehensive solution service provider
- Cocoscreator event dispatch use
- Self reflection of a small VC after two years of entrepreneurship
猜你喜欢

I wrote a learning and practice tutorial for beginners!

超标量处理器设计 姚永斌 第6章 指令解码 摘录
12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]

【Hot100】32. 最长有效括号

股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
![[HCIA continuous update] overview of WLAN workflow](/img/0a/b3986307589a9f7379fe1dd707b9f8.png)
[HCIA continuous update] overview of WLAN workflow

The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia

ISO27001认证办理流程及2022年补贴政策汇总

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

补能的争议路线:快充会走向大一统吗?
随机推荐
Solve the El input input box For number number input problem, this method can also be used to replace the problem of removing the arrow after type= "number"
Dynamic programming stock problem comparison
DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌
Open source PostgreSQL extension age for graph database was announced as the top-level project of Apache Software Foundation
[test development] software testing - Basics
【210】PHP 定界符的用法
Why are some online concerts always weird?
78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
2022 national CMMI certification subsidy policy | Changxu consulting
【HCIA持续更新】网络管理与运维
【Hot100】31. 下一个排列
The controversial line of energy replenishment: will fast charging lead to reunification?
Face_recognition人脸识别之考勤统计
Redis master-slave replication
【系统分析师之路】第七章 复盘系统设计(结构化开发方法)
Ks007 realizes personal blog system based on JSP
[Huawei HCIA continuous update] SDN and FVC
用于图数据库的开源 PostgreSQL 扩展 AGE被宣布为 Apache 软件基金会顶级项目
Is BigDecimal safe to calculate the amount? Look at these five pits~~
设置窗体透明 隐藏任务栏 与全屏显示
