当前位置:网站首页>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;
}
边栏推荐
- LVS load balancing cluster of efficient multi-purpose cluster (NAT mode)
- Market status and development prospect prediction of global colorimetric cup cover industry in 2022
- Mobile terminal - uniapp development record (public request encapsulation)
- Problems encountered in fuzzy query of SQL statements
- Uipath practice (08) - selector
- Caijing 365 stock internal reference: what's the mystery behind the good father-in-law paying back 50 million?
- Notes | numpy-11 Array operation
- 论文阅读_中文NLP_ELECTRA
- The process of browser accessing the website
- Market status and development prospects of the global autonomous marine glider industry in 2022
猜你喜欢
Learning practice: comprehensive application of cycle and branch structure (I)
On typescript and grammar
[research materials] 2021 China's game industry brand report - Download attached
2022-02-12 daily clock in: problem fine brush
Sdl2 + OpenGL glsl practice (Continued)
[luatos sensor] 1 light sensing bh1750
JDBC database operation
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
Cross platform plug-in flutter for displaying local notifications_ local_ notifications
随机推荐
2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
Cross platform plug-in flutter for displaying local notifications_ local_ notifications
The least operation of leetcode simple problem makes the array increment
Pyqt control part (II)
Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
C primre plus Chapter 10 question 6 inverted array
Crazy scientist
Day 51 - tree problem
【SQL注入】联合查询(最简单的注入方法)
Retirement plan fails, 64 year old programmer starts work again
Market status and development prospect prediction of global fermented plant protein industry in 2022
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
[luatos sensor] 2 air pressure bmp180
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
Triangular rasterization
关于开学的准备与专业认知
Symbol of array element product of leetcode simple problem
Market status and development prospect prediction of the near infrared sensor industry of the global Internet of things in 2022
Mount NFS in kubesphere
[set theory] binary relation (example of binary relation operation | example of inverse operation | example of composite operation | example of limiting operation | example of image operation)