当前位置:网站首页>1118 birds in forest (25 points)
1118 birds in forest (25 points)
2022-07-03 04:54:00 【vs5】
The main idea of the topic : Given several photos , There are several birds in the photo tree , Judge how many trees and birds there are , Then give several queries , Judge whether the two birds are in the same tree .
The title says The number of the bird must be 1 Continuous to a certain number , So the number of birds is the maximum number , It doesn't matter if you don't understand it , Direct use set Save every bird , The final length is the quantity .
The number of birds has been solved , The number of trees is actually the number of connected blocks , You can use it here dfs,bfs, Or it can be realized by searching the set , But later we need to check whether the two birds are in the same tree , Therefore, it is convenient to write this question with the combination of search sets .
#include <iostream>
#include <vector>
using namespace std;
const int N = 10010;
int n,x,a,b,num,p[N];
vector<int>v[N];
int find(int x)
{
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main()
{
int tt;
cin >> tt;
for(int k = 0; k < tt; k ++)
{
cin >> n;
for(int i = 0; i < n; i ++)
{
cin >> x;
v[k].push_back(x);
num = max(x,num);
}
}
for(int i = 1; i <= num; i ++) p[i] = i;
int cur = num;// The number of connected blocks
for(int i = 0; i < tt; i ++)
for(int j = 0; j < v[i].size() - 1; j ++)
{
int a = find(v[i][j]),b = find(v[i][j + 1]);
if(a != b) p[a] = b,cur --;
}
cout << cur << ' ' << num << '\n';
int op;
cin >> op;
while(op --)
{
cin >> a >> b;
if(find(a) == find(b)) puts("Yes");
else puts("No");
}
return 0;
}边栏推荐
- Review the old and know the new: Notes on Data Science
- M1 Pro install redis
- [research materials] 2021 China's game industry brand report - Download attached
- Market status and development prospects of the global autonomous marine glider industry in 2022
- Market status and development prospects of the global automatic tea picker industry in 2022
- Truncated sentences of leetcode simple questions
- [research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
- [research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
- Preparation for school and professional cognition
- [tools run SQL blind note]
猜你喜欢

Use Sqlalchemy module to obtain the table name and field name of the existing table in the database

Compile and decompile GCC common instructions

Kept hot standby and haproxy

data2vec! New milestone of unified mode

Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
![[tools run SQL blind note]](/img/c3/86db4568b221d2423914990a88eec2.png)
[tools run SQL blind note]
![[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)](/img/2a/362f3b0491f721d89336d4f468c9dd.jpg)
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)

STM32 reverse entry

String matching: find a substring in a string
![[research materials] 2022q1 game preferred casual game distribution circular - Download attached](/img/13/5a67c5d08131745759fdc70a71cf0f.jpg)
[research materials] 2022q1 game preferred casual game distribution circular - Download attached
随机推荐
MC Layer Target
Market status and development prospect prediction of the global fire alarm sensor industry in 2022
雇佣收银员(差分约束)
C Primer Plus Chapter 10, question 14 3 × 5 array
Number of uniform strings of leetcode simple problem
String matching: find a substring in a string
SSM framework integration
Coordinatorlayout appbarrayout recyclerview item exposure buried point misalignment analysis
Uipath practice (08) - selector
Kept hot standby and haproxy
Current market situation and development prospect prediction of global direct energy deposition 3D printer industry in 2022
5-36v input automatic voltage rise and fall PD fast charging scheme drawing 30W low-cost chip
Handler understands the record
[research materials] 2022q1 game preferred casual game distribution circular - Download attached
2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
JDBC database operation
ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)
M1 Pro install redis
关于开学的准备与专业认知
Introduction to JVM principle