当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
GTSAM中李群的运用
Investment strategy discussion and market scale prediction report of China's solid state high power amplifier industry from 2022 to 2028
(5) Explanation of yolo-v3 core source code (3)
CoordinatorLayout+NestedScrollView+RecyclerView 上拉底部显示不全
IPv6 comprehensive experiment
Arrays and collections
GTSAM中李群的運用
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
Properties file
Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
随机推荐
【微信小程序】搭建开发工具环境
职场进阶指南:大厂人必看书籍推荐
Manhattan distance and Manhattan rectangle - print back font matrix
對數據安全的思考(轉載)
Function of contenttype
使用Nacos管理配置
【Postman】Collections-运行配置之导入数据文件
多线程应用的测试与调试
RestTemplate、Feign实现Token传递
MySQL之基础知识
Detailed explanation of P problem, NP problem, NPC problem and NP hard problem
Dynamic programming -- knapsack problem
IDEA 新UI使用
GTSAM中李群的運用
假设检验学习笔记
Sqlmap tutorial (III) practical skills II
[postman] collections - run the imported data file of the configuration
F - True Liars (种类并查集+DP)
功能安全之故障(fault),错误(error),失效(failure)
GTSAM中ISAM2和IncrementalFixedLagSmoother说明