当前位置:网站首页>食物链(DAY 79)
食物链(DAY 79)
2022-07-27 01:06:00 【张学恒】
1:题目
动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形。
A 吃 B,B 吃 C,C 吃 A。
现有 N 个动物,以 1∼N 编号。
每个动物都是 A,B,C 中的一种,但是我们并不知道它到底是哪一种。
有人用两种说法对这 N 个动物所构成的食物链关系进行描述:
第一种说法是 1 X Y,表示 X 和 Y 是同类。
第二种说法是 2 X Y,表示 X 吃 Y。
此人对 N 个动物,用上述两种说法,一句接一句地说出 K 句话,这 K 句话有的是真的,有的是假的。
当一句话满足下列三条之一时,这句话就是假话,否则就是真话。
当前的话与前面的某些真的话冲突,就是假话;
当前的话中 X 或 Y 比 N 大,就是假话;
当前的话表示 X 吃 X,就是假话。
你的任务是根据给定的 N 和 K 句话,输出假话的总数。
输入格式
第一行是两个整数 N 和 K,以一个空格分隔。
以下 K 行每行是三个正整数 D,X,Y,两数之间用一个空格隔开,其中 D 表示说法的种类。
若 D=1,则表示 X 和 Y 是同类。
若 D=2,则表示 X 吃 Y。
输出格式
只有一个整数,表示假话的数目。
数据范围
1≤N≤50000,
0≤K≤100000
输入样例:
100 7
1 101 1
2 1 2
2 2 3
2 3 3
1 1 3
2 3 1
1 5 5
输出样例:
3
2:代码实现
#include<iostream>
using namespace std;
const int N=1000010;
int n,m;
int p[N],d[N];
int find(int x)
{
if(p[x]!=x)
{
int t=find(p[x]);
d[x]+=d[p[x]];
p[x]=t;
}
return p[x];
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)p[i]=i;
int res=0;
while (m -- ){
int t,x,y;
scanf("%d%d%d",&t,&x,&y);
if(x>n || y>n)res++;
else
{
int px=find(x),py=find(y);
if(t==1)
{
if(px==py && (d[x]-d[y])%3)res++;
else if(px!=py)
{
p[px]=py;
d[px]=d[y]-d[x];
}
}
else
{
if(px==py && (d[x]-d[y]-1)%3)res++;
else if(px!=py)
{
p[px]=py;
d[px]=d[y]+1-d[x];
}
}
}
}
printf("%d\n",res);
return 0;
}
边栏推荐
- Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round
- Kubeadmin到底做了什么?
- Play a parallel multithreaded mcu-mc3172
- 2649: 段位计算
- [二分查找中等题] LeetCode 34. 在排序数组中查找元素的第一个和最后一个位置
- Marqueeview realizes sliding display effect
- 八皇后编程实现
- 阿里云解决方案架构师张平:云原生数字化安全生产的体系建设
- Social wechat applet of fanzhihu forum community
- Plato Farm全新玩法,套利ePLATO稳获超高收益
猜你喜欢

Inftnews | ggac and China Aerospace ases exclusively produce "China 2065 Collection Edition"

Portraiture5全新升级版磨皮滤镜插件神器

在线问题反馈模块实战(十五):实现在线更新反馈状态功能

安全员及环保员岗位职责

Use the most primitive method to manually implement the common 20 array methods

A math problem cost the chip giant $500million!

After two years of graduation, I switched to software testing and got 12k+, and my dream of not taking the postgraduate entrance examination with a monthly salary of more than 10000 was realized

Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round

数模1232

Baidu cloud face recognition
随机推荐
Plato Farm通过LaaS协议Elephant Swap,为社区用户带来全新体验
数据湖(二十):Flink兼容Iceberg目前不足和Iceberg与Hudi对比
[动态规划中等题] LeetCode 198. 打家劫舍 740. 删除并获得点数
IDEA 连接数据库查询数据后控制台表头中文乱码的解决方法
CS224W fall 1.2 Applications of Graph ML
be based on. NETCORE development blog project starblog - (16) some new functions (monitoring / statistics / configuration / initialization)
{“errcode“:44001,“errmsg“:“empty media data, hint: [1655962096234893527769663], from ip: 222.72.xxx.
Worthington过氧化物酶活性的6种测定方法
一个测试类了解BeanUtils.copyProperties
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
对象创建的流程分析
杀毒软件 clamav 的安装和使用
185. All employees with the top three highest wages in the Department (mandatory)
智能指针shared_ptr、unique_ptr、weak_ptr
[SQL simple question] leetcode 627. change gender
Hcip day 14 notes
手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
How to visit the latest version of burpsuite pro in vain
win10/win11无损扩大C盘空间,跨盘合并C、E盘
毕业2年转行软件测试获得12K+,不考研月薪过万的梦想实现了