当前位置:网站首页>Joint search set
Joint search set
2022-07-19 01:02:00 【casperwu】
Union checking set
1. Common operations and optimization
Merge two sets Query the ancestor node of an element In the process of query, you can do path compression
2. Use scenarios
It is often used in topics with transitive properties , And the transmission is bidirectional , That is, it can be understood as undirected graph relationship .
3. Example

3.1 Understanding of topic meaning
Use prefixes and ideas : Si Before presentation i In number 1 Number of S[l~r] There are odd numbers of 1 de Equivalent to S[r] - S[l-1] It's odd Equivalent to S[r] And S[l-1] It's different in parity ; The same can be inferred , S[l-r] There are even numbers of 1 Equivalent to S[r] And S[l-1] The parity of is the same .
Therefore, the meaning of the question can be simplified as : Given two and the parity between them , Judge whether there is contradiction according to the given conditions .
3.2 Code
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int N = 20004;
int fa[N]; // And look up the array
int d[N]; // Maintain the distance to the root node
unordered_map<int, int> s; // For discretizing points
int n, m;
int find(int x) {
if(x != fa[x]) {
int root = find(fa[x]);
// In the mold 2 Under the state of , Addition is equivalent to Exclusive or operation
d[x] ^= d[fa[x]];
fa[x] = root;
}
return fa[x];
}
int get(int x) {
if(s.count(x) == 0) s[x] = ++n;
return s[x];
}
int main() {
cin >> n >> m;
n = 0;
// And search set initialization
for(int i = 0; i < N; i++) fa[i] = i;
int ans = m;
for(int i = 1; i <= m; i++) {
int a, b;
string op;
cin >> a >> b >> op;
// Use similar prefixes and ideas , therefore b need -1
a = get(a - 1), b = get(b);
int t = 0;
if(op == "odd") t = 1;
int pa = find(a), pb = find(b);
if(pa == pb) {
if((d[a] ^ d[b]) != t) {
ans = i - 1;
break;
}
} else {
fa[pa] = pb;
d[pa] = d[a] ^ d[b] ^ t;
}
}
cout << ans;
return 0;
}
边栏推荐
- Business is too busy. Is there really no reason to have time for automation?
- @ConditionalOnMissingBean 如何實現覆蓋第三方組件中的 Bean
- 【着色器实现Wave效果_Shader效果第一篇】
- PySide2嵌入外部程序
- Relationship between sampling rate and signal-to-noise ratio
- Hcia-r & s self use notes (7) (network layer) TTL and loop test, protocol number
- [shader realizes wave effect _shader effect Chapter 1]
- 实力认证|海泰方圆强势上榜《嘶吼2022网络安全产业图谱》20大领域
- Comment conditionalonmissingbean implémente l'écrasement des haricots dans les composants tiers
- Greenplum6.x客户端连接
猜你喜欢

64 bit interrupt assembly cannot be used

那一朵最美的黄花

“husky install“ command already exists in prepare script, skipping./Users/982471938qq.com/Desktop

【无标题】
Master-slave separation of database

从22顶会看对比学习在推荐的应用
![[paper reading] yolov7: trainable bag of freebies sets new state of the art for real-time object detectors](/img/f3/ca04fbcc6c2766594b8081b627b2da.png)
[paper reading] yolov7: trainable bag of freebies sets new state of the art for real-time object detectors

Some friends asked me if I could learn programming if my English was poor?
![[shader realizes wave effect _shader effect Chapter 1]](/img/e1/fc5549f41d90979465bd71d1110688.png)
[shader realizes wave effect _shader effect Chapter 1]
![[natural language processing] [multimodal] multimodal overview: visual language pre training model](/img/4a/24be4efe9b0347033a9615cf54ceb5.png)
[natural language processing] [multimodal] multimodal overview: visual language pre training model
随机推荐
Leetcode-5 longest palindrome substring
C language basic drill (5)
乐观锁和悲观锁在kubernetes中的应用
Acquisition of network security B module coding information of national vocational college skills competition
PySide2嵌入外部程序
VSCodeUserSetup-x64-1.65.0.exe
数据库整理
Why processing data indicators
Hcia-r & s self use notes (8) IP address basis, subnet mask, subnet Division
Switch and router technology: Integrated Experiment of hot backup routing protocols HSRP, HSRP and spvstp
Interpretation of the process of generating redistemplate
全国职业院校技能大赛网络安全B模块 编码信息获取
【无标题】
常见链表题及其 Go 实现
Business is too busy. Is there really no reason to have time for automation?
18. Sum of four numbers (15 enhanced version) [ans.add (arrays.aslist (nums[i], nums[j], nums[l], nums[r])]
@ConditionalOnMissingBean 如何实现覆盖第三方组件中的 Bean
Tiktok Zhua Bao Tong Sha version -version21.5
By voting for the destruction of STI by Dao, seektiger is truly community driven
NXP i.mx8m plus enables edge machine learning, enabling iac-imx8mp-kit development board