当前位置:网站首页>【acwing】240. food chain
【acwing】240. food chain
2022-07-05 04:41:00 【The wind is a little strong】
subject

Train of thought

How to merge different query sets , For the same kind (1) Come on :
For different classes (2) Come on :
Code 1
#include<iostream>
using namespace std;
const int N=50005;
int p[N],d[N];
int n,m;
int find(int x){
// Record the distance to the root node
if(p[x]!=x){
int t=find(p[x]);
d[x]+=d[p[x]];
p[x]=t;
}
return p[x];
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
p[i]=i; // All parent nodes are themselves
}
int res=0;
while(m--){
int t,x,y;
cin>>t>>x>>y;
if(x>n || y>n){
//x,y Greater than n The scope of the , This sentence is a lie
res++;
}else{
int px=find(x);//x The root node
int py=find(y);//y The root node
if(t==1){
//x and y It's the same kind
if(px==py && (d[x]-d[y])%3!=0){
// In the same tree , The distance difference between two points %3 Not for 0, It means it's not the same kind
res++;
}else if(px != py){
//x,y Not in the same tree
p[px]=py; //x The root node of is connected to y On the root node of
d[px]=d[y]-d[x];
}
}else{
//x eat y
if(px==py && (d[x]-d[y]-1)%3!=0){
//x,y In the same tree ,x Distance ratio y Freshman , namely (dx-dy-1)%3==0
res++;
}else if(px != py){
//x,y Not in the same tree ,
p[px]=py;//x The root node of is connected to y On the root node of
d[px]=d[y]+1-d[x];
}
}
}
}
cout<<res<<endl;
return 0;
}
Train of thought two
Union checking set ( Expanding domain )
This topic is mainly to open three expanded domains , That is, the natural enemy domain , Homogeneous domain , And predatory areas .
If x,y It's the same kind , however x Of Predatory domain Yes y, that
If x,y It's the same kind , however x Of Natural enemy territory Yes y, that
If x,y It's the same kind , however x Of Homogeneous domain Yes y, that
If x yes y The natural enemies of , however x Of Homogeneous domain There is y, that
If x yes y The natural enemies of , however x Of Natural enemy territory There is y, that
If x yes y The world of , however x Of Predatory domain There is y, that
Let's make the present X The predation domain of is X+N, At present X The natural enemy domain of is X+N+N
Code 2
// Let's make the present X The predation domain of is X+N, At present X The natural enemy domain of is X+N+N
#include<iostream>
using namespace std;
const int N=50000*3+5;
int p[N];
int n,m;
int find(int x){
if(x!=p[x]){
p[x]=find(p[x]);
}
return p[x];
}
int main(){
cin>>n>>m;
for(int i=1;i<=3*n;i++){
p[i]=i;
}
int res=0;
while (m -- ){
int k,x,y;
cin>>k>>x>>y;
if(x>n || y>n){
res++;
}else if(k==1){
//x and y similar
if(find(x)==find(y+n) || find(x)==find(y+n+n)){
//x stay y In the predatory domain ,x stay y In the field of natural enemies
res++;
}else{
p[find(x)]=p[find(y)]; //x and y similar
p[find(x+n)]=p[find(y+n)]; //x Predatory domain and y Predatory domain of the same
p[find(x+n+n)]=p[find(y+n+n)]; //x Natural enemy domain and y The natural enemy domain of
}
}else{
//x yes y The natural enemies of
if(x==y || find(x)==find(y) || find(x)==find(y+n)){
//x and y Is the same ,x and y It's the same kind ,x yes y The predator-prey area
res++;
}else{
p[find(x)]=p[find(y+n+n)]; //y Add... To your natural enemy domain x
p[find(x+n)]=p[find(y)]; //x In the predator-prey domain y
p[find(x+n+n)]=p[find(y+n)]; //x Natural enemy domain and y The predatory domain of is the same
}
}
}
cout<<res<<endl;
return 0;
}
边栏推荐
- Function (error prone)
- Key review route of probability theory and mathematical statistics examination
- A survey of automatic speech recognition (ASR) research
- [Chongqing Guangdong education] National Open University 2047t commercial bank operation and management reference test in autumn 2018
- Error statuslogger log4j2 could not find a logging implementation
- 包 类 包的作用域
- After the deployment of web resources, the navigator cannot obtain the solution of mediadevices instance (navigator.mediadevices is undefined)
- Leetcode 222 number of nodes of complete binary tree
- [groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
- level17
猜你喜欢
![[finebi] the process of making custom maps using finebi](/img/3a/d638dbac6a26c37087ec9550c35e63.png)
[finebi] the process of making custom maps using finebi

Special information | finance, accounting, audit - 22.1.23

WeNet:面向工业落地的E2E语音识别工具

JVM 原理和流程简介
![[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording](/img/11/b55f85ef9e1f22254218cb9083b823.png)
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
![[Business Research Report] Research Report on male consumption trends in other economic times -- with download link](/img/08/7ea490c46c3f64af3e78d07b19b3e3.jpg)
[Business Research Report] Research Report on male consumption trends in other economic times -- with download link

Introduce Hamming distance and calculation examples

Sword finger offer 04 Search in two-dimensional array

windows下Redis-cluster集群搭建

Aperçu en direct | Services de conteneurs ACK flexible Prediction Best Practices
随机推荐
[AI bulletin 20220211] the hard core up owner has built a lidar and detailed AI accelerator
[thingsboard] how to replace the homepage logo
Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
[finebi] the process of making custom maps using finebi
首席信息官如何利用业务分析构建业务价值?
The 22nd Spring Festival Gala, an immersive stage for the yuan universe to shine into reality
Official announcement! The third cloud native programming challenge is officially launched!
[crampon programming] lintcode decoding Encyclopedia - 872 termination process
American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
Components in protective circuit
History of web page requests
User behavior collection platform
[groovy] closure (Introduction to closure class closure | this, owner, delegate member assignment and source code analysis)
直播預告 | 容器服務 ACK 彈性預測最佳實踐
Mxnet imports various libcudarts * so、 libcuda*. So not found
Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
Chapter 6 text processing tools for shell programming (awk)
Pointer function (basic)
2022-2028 global and Chinese virtual data storage Market Research Report
Error statuslogger log4j2 could not find a logging implementation