当前位置:网站首页>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;
}边栏推荐
- Hire cashier (differential constraint)
- Notes | numpy-07 Slice and index
- Symbol of array element product of leetcode simple problem
- Kept hot standby and haproxy
- Summary of training competition (Lao Li's collection of questions)
- What is UUID
- JDBC database operation
- Hj35 serpentine matrix
- Market status and development prospect prediction of global neutral silicone sealant industry in 2022
- Learning record of arouter principle
猜你喜欢

String matching: find a substring in a string

STM32 reverse entry

Leetcode simple question: the key with the longest key duration

Review the configuration of vscode to develop golang

Without 50W bride price, my girlfriend was forcibly dragged away. What should I do
![[XSS bypass - protection strategy] understand the protection strategy and better bypass](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[XSS bypass - protection strategy] understand the protection strategy and better bypass

Kept hot standby and haproxy

联发科技2023届提前批IC笔试(题目)

Contents of welder (primary) examination and welder (primary) examination in 2022

关于开学的准备与专业认知
随机推荐
[USACO 2009 Dec S]Music Notes
C Primer Plus Chapter 10, question 14 3 × 5 array
Why does I start with =1? How does this code work?
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
Flutter monitors volume to realize waveform visualization of audio
RT thread flow notes I startup, schedule, thread
Without 50W bride price, my girlfriend was forcibly dragged away. What should I do
Oracle SQL table data loss
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)
[research materials] 2021 China's game industry brand report - Download attached
移动端——uniapp开发记录(公共请求request封装)
[develop wechat applet local storage with uni app]
Network security textual research recommendation
Learn to use the idea breakpoint debugging tool
Market status and development prospect prediction of global fermentation acid industry in 2022
Wechat applet waterfall flow and pull up to the bottom
Leetcode simple question: check whether two string arrays are equal
"Niuke brush Verilog" part II Verilog advanced challenge
Hj35 serpentine matrix
第十九届浙江省 I. Barbecue