当前位置:网站首页>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; } } }
边栏推荐
- Flask 轻量web框架
- Set the transparent hidden taskbar and full screen display of the form
- 【HCIA持续更新】网络管理与运维
- Is it science or metaphysics to rename a listed company?
- ITSS运维能力成熟度分级详解|一文搞清ITSS证书
- Dynamic programming stock problem comparison
- I2C子系统之适配器的设备接口分析(i2c-dev.c文件分析)
- [Huawei HCIA continuous update] SDN and FVC
- 股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
- The Block:USDD增长势头强劲
猜你喜欢

Tutorial on the use of Huawei cloud modelarts (with detailed illustrations)

How to test MDM products

解决el-input输入框.number数字输入问题,去掉type=“number“后面箭头问题也可以用这种方法代替

With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute

Recast of recastnavigation

To sort out messy header files, I use include what you use

【HCIA持续更新】WLAN概述与基本概念
![[HCIA continuous update] overview of WLAN workflow](/img/0a/b3986307589a9f7379fe1dd707b9f8.png)
[HCIA continuous update] overview of WLAN workflow

Self reflection of a small VC after two years of entrepreneurship

Weima, which is going to be listed, still can't give Baidu confidence
随机推荐
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
How to test MDM products
华为云ModelArts的使用教程(附详细图解)
ITSS运维能力成熟度分级详解|一文搞清ITSS证书
无心剑中译伊丽莎白·毕肖普《一门技艺》
TCP两次挥手,你见过吗?那四次握手呢?
2022年DCMM认证全国各地补贴政策汇总
The controversial line of energy replenishment: will fast charging lead to reunification?
[proteus simulation] printf debugging output example based on VSM serial port
Set the transparent hidden taskbar and full screen display of the form
【Hot100】31. 下一个排列
Rainfall warning broadcast automatic data platform bwii broadcast warning monitor
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
Implementation of shell script replacement function
People in the workplace with a miserable expression
国产数据库TiDB初体验:简单易用,快速上手
[HCIA continuous update] WAN technology
New technology releases a small program UNIPRO to meet customers' mobile office scenarios
Introduction of time related knowledge in kernel
Recast of recastnavigation
