当前位置:网站首页>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; } } }
边栏推荐
- Device interface analysis of the adapter of I2C subsystem (I2C dev.c file analysis)
- [test development] software testing - Basics
- 【每日一题】556. 下一个更大元素 III
- Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
- 【HCIA持续更新】WLAN工作流程概述
- 要上市的威马,依然给不了百度信心
- Large scale service exception log retrieval
- 大规模服务异常日志检索
- [daily question] 871 Minimum refueling times
- Self reflection of a small VC after two years of entrepreneurship
猜你喜欢
【HCIA持续更新】WLAN工作流程概述
爬虫初级学习
Load test practice of pingcode performance test
Ks007 realizes personal blog system based on JSP
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
要上市的威马,依然给不了百度信心
MVC mode and three-tier architecture
【Hot100】32. 最长有效括号
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
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"
随机推荐
General environmental instructions for the project
Load test practice of pingcode performance test
ISO27001 certification process and 2022 subsidy policy summary
Large scale service exception log retrieval
Face_ Attendance statistics of recognition face recognition
比李嘉诚还有钱的币圈大佬,刚在沙特买了楼
项目通用环境使用说明
大厂面试总结大全二
【209】go语言的学习思想
Flask 轻量web框架
wuzhicms代码审计
你应该懂些CI/CD
Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction
S5PV210芯片I2C适配器驱动分析(i2c-s3c2410.c)
The Block:USDD增长势头强劲
【Proteus仿真】基于VSM 串口printf调试输出示例
Pytoch deep learning environment construction
五千字讲清楚团队自组织建设 | Liga 妙谈
MySQL常用增删改查操作(CRUD)
The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia