当前位置:网站首页>E - food chain
E - food chain
2022-07-06 06:14:00 【RCyyds】
Portal : The food chain
The question : The theme is simple , Just look at the topic directly .
Ideas : This is a classic weighted search topic .
According to convention , We need two arrays , One is the parent node array p, The other is recording x To p[x] The weight of d[x] Array , Stipulate here x eat p[x].
This question is The difficulty lies in the array of eating and being eaten d The relationship between .
1. If x And y It is the same kind and uses the common ancestor node , Then judge d[x]-d[y])%3 Is it equal to 0, be equal to 0 It means the same as what you said , Otherwise inconsistent ,ans++;
2. If x And y It is the same kind of ancestral node without common use , let px My father node is py, Work out d[px],d[px]=d[y]-d[x];
3. If x eat y And use the common ancestor node , Then judge d[x]-d[y]-1)%3 Is it equal to 0, be equal to 0 It means the same as what you said , Otherwise inconsistent ,ans++;
4. If x eat y And there is no common ancestor node , let px My father node is py, Work out d[px],d[px]=d[y]-d[x]+1;
5. If x,y Greater than n,ans++;
Code :
#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;
}
边栏推荐
- 「 WEB测试工程师 」岗位一面总结
- Eigen稀疏矩阵操作
- Clock in during winter vacation
- 数字三角形模型 AcWing 1015. 摘花生
- 【Postman】动态变量(也称Mock函数)
- 进程和线程的理解
- Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
- MySQL之基础知识
- 数学三大核心领域概述:代数
- How to use the container reflection method encapsulated by thinkphp5.1 in business code
猜你喜欢
请求转发与重定向
G - Supermarket
Huawei BFD configuration specification
Basic knowledge of error
曼哈顿距离和-打印菱形
全程实现单点登录功能和请求被取消报错“cancelToken“ of undefined的解决方法
B - The Suspects
Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
LeetCode 729. 我的日程安排表 I
[API interface tool] Introduction to postman interface
随机推荐
Arrays and collections
曼哈顿距离与曼哈顿矩形-打印回字型矩阵
【eolink】PC客户端安装
全程实现单点登录功能和请求被取消报错“cancelToken“ of undefined的解决方法
Company video accelerated playback
Postman核心功能解析-参数化和测试报告
isam2运行流程
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
SQLMAP使用教程(三)实战技巧二
二维码的前世今生 与 六大测试点梳理
[untitled]
【微信小程序】搭建开发工具环境
Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
PAT(乙级)2022年夏季考试
JDBC Requset 对应内容及功能介绍
IDEA 新UI使用
Réflexions sur la sécurité des données (réimpression)
Seven imperceptible truths in software testing
How to use the container reflection method encapsulated by thinkphp5.1 in business code
[Thesis code] SML part code reading