当前位置:网站首页>E - 食物链
E - 食物链
2022-07-06 06:08:00 【RCyyds】
传送门:食物链
题意:题意简单,直接看题目即可。
思路:这是个经典的带权并查集题目了。
根据惯例,我们需要两个数组,一个是父亲节点数组p,另一个为记录x到p[x]的权值的d[x]数组,在这里规定x吃p[x]。
这道题的难点在于吃与被吃的数组d之间的关系。
1.假如x与y是同类且用共同的祖宗节点,那么就判定d[x]-d[y])%3是否等于0,等于0就代表与所说的话一致,反之不一致,ans++;
2.假如x与y是同类且没用共同的祖宗节点,就让px的父亲节点为py,算出d[px],d[px]=d[y]-d[x];
3.假如x吃y且用共同的祖宗节点,那么就判定d[x]-d[y]-1)%3是否等于0,等于0就代表与所说的话一致,反之不一致,ans++;
4.假如x吃y且没用共同的祖宗节点,就让px的父亲节点为py,算出d[px],d[px]=d[y]-d[x]+1;
5.假如x,y大于n,ans++;
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=5e4+5;
int p[N],d[N];
int find(int x)
{
if(x!=p[x])
{
int t=find(p[x]);
d[x]+=d[p[x]];
p[x]=t;
}
return p[x];
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for (int i = 1; i <= n; i ++ ) p[i] = i;
int ans=0;
while(k--)
{
int t,x,y;
scanf("%d%d%d",&t,&x,&y);
int px=find(x),py=find(y);
if (x > n || y > n) ans ++ ;
else
{
if(t==1)
{
if(px==py&&(d[x]-d[y])%3) ans++;
else if(px!=py)
{
p[px]=py;
d[px]=d[y]-d[x];
}
}
else{
if(px==py&&(d[x]-d[y]-1)%3) ans++;
else if(px!=py)
{
p[px]=py;
d[px]=d[y]-d[x]+1;
}
}
}
}
printf("%d",ans);
return 0;
}
边栏推荐
- Eigen sparse matrix operation
- [postman] collections configuration running process
- Commodity price visualization
- 【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
- 2022 software testing workflow to know
- 「 WEB测试工程师 」岗位一面总结
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- 對數據安全的思考(轉載)
- LeetCode 729. 我的日程安排表 I
- How Huawei routers configure static routes
猜你喜欢
MPLS test report
What are the test sites for tunnel engineering?
SQLMAP使用教程(三)实战技巧二
Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
HCIA review
[web security] nodejs prototype chain pollution analysis
浅谈专项测试之弱网络测试
曼哈顿距离和-打印菱形
[Thesis code] SML part code reading
Buuctf-[gxyctf2019] no dolls (xiaoyute detailed explanation)
随机推荐
LeetCode 731. 我的日程安排表 II
数字三角形模型 AcWing 1015. 摘花生
Significance of unit testing
Buuctf-[gxyctf2019] no dolls (xiaoyute detailed explanation)
职场进阶指南:大厂人必看书籍推荐
全程实现单点登录功能和请求被取消报错“cancelToken“ of undefined的解决方法
How to recover Huawei router's forgotten password
「 WEB测试工程师 」岗位一面总结
对数据安全的思考(转载)
MySQL之基础知识
Pat (Grade B) 2022 summer exam
在线问题与离线问题
Investment strategy discussion and market scale prediction report of China's solid state high power amplifier industry from 2022 to 2028
[Thesis code] SML part code reading
Linux regularly backs up MySQL database
功能安全之故障(fault),错误(error),失效(failure)
JMeter做接口测试,如何提取登录Cookie
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Eigen sparse matrix operation
Function of contenttype