当前位置:网站首页>POJ 1182 :食物链(并查集)[通俗易懂]
POJ 1182 :食物链(并查集)[通俗易懂]
2022-07-07 16:53:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
Time Limit: 1000MS | Memory Limit: 10000K | |
|---|---|---|
Total Submissions: 43526 | Accepted: 12679 |
Description
动物王国中有三类动物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句话有的是真的。有的是假的。
当一句话满足下列三条之中的一个时,这句话就是假话,否则就是真话。 1) 当前的话与前面的某些真的话冲突。就是假话; 2) 当前的话中X或Y比N大。就是假话。 3) 当前的话表示X吃X,就是假话。 你的任务是依据给定的N(1 <= N <= 50,000)和K句话(0 <= K <= 100,000),输出假话的总数。
Input
第一行是两个整数N和K,以一个空格分隔。
下面K行每行是三个正整数 D,X,Y。两数之间用一个空格隔开。当中D表示说法的种类。 若D=1,则表示X和Y是同类。 若D=2,则表示X吃Y。
Output
仅仅有一个整数。表示假话的数目。
Sample Input
100 7
1 101 1
2 1 2
2 2 3
2 3 3
1 1 3
2 3 1
1 5 5Sample Output
3好吧。。
这篇的确是最具体的。。点击打开链接
看了之后对并查集的了解更加深了。。。
弱菜在此感谢博主。。
还有这道题我用多重输入就错了。。
害我弄了好久。。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<sstream>
#include<cmath>
using namespace std;
#define M 100500
int n;
int k;
int d;
int x;
int y;
int p[M];
int r[M];
int ans;
void start()
{
for(int i=1; i<=n; i++)
{
p[i]=i; //初始化并查集
r[i]=0; //初始化时。每一个元素作为一个集合,其元素为1
}
}
int find(int x) //并查集的find
{
if( p[x]==x )
return x;
else
{
int t = p[x];
p[x] = find ( p[x] );
r[x] = (r[x]+r[t])%3;
return p[x];
}
}
void Kruskal(int x, int y, int d)
{
int xx = find(x);
int yy = find(y);
p[yy] = xx;
r[yy] = (r[x]-r[y]+3+(d-1))%3;
}
int main()
{
scanf("%d%d", &n, &k);
start();
ans = 0;
for(int i=1; i<=k; i++)
{
scanf("%d%d%d", &d, &x, &y);
if(x>n || y>n || (d==2&&x==y))
ans++;
else if( find(x) == find(y) )
{
if( d==1 && r[x]!=r[y] )
ans++;
if( d==2 && (r[x]+1)%3 != r[y] )
ans++;
}
else
Kruskal(x, y, d);
}
printf("%d\n", ans);
return 0;
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116605.html原文链接:https://javaforall.cn
边栏推荐
- Save the memory of the model! Meta & UC Berkeley proposed memvit. The modeling time support is 30 times longer than the existing model, and the calculation amount is only increased by 4.5%
- go语言的字符串类型、常量类型和容器类型
- Redis的发布与订阅
- 6. About JWT
- CVPR 2022 - learning non target knowledge for semantic segmentation of small samples
- Reinforcement learning - learning notes 8 | Q-learning
- 企业展厅设计中常用的三种多媒体技术形式
- How many times is PTA 1101 B than a
- Download, installation and development environment construction of "harmonyos" deveco
- [sword finger offer] 59 - I. maximum value of sliding window
猜你喜欢

How to choose the appropriate automated testing tools?

Creative changes brought about by the yuan universe

低代码助力企业数字化转型会让程序员失业?

NAT地址转换

Do you know all four common cache modes?

RISCV64

Static routing configuration

Reinforcement learning - learning notes 8 | Q-learning

Basic concepts and properties of binary tree
![Kirk borne's selection of learning resources this week [click the title to download directly]](/img/df/98aa3edf0a70b870684963d52e7c72.png)
Kirk borne's selection of learning resources this week [click the title to download directly]
随机推荐
I feel cheated. Wechat tests the function of "size number" internally, and two wechat can be registered with the same mobile number
Charles+drony的APP抓包
小程序中实现付款功能
Complete e-commerce system
单臂路由和三层交换的简单配置
【剑指 Offer】59 - I. 滑动窗口的最大值
Wechat web debugging 8.0.19 replace the X5 kernel with xweb, so the X5 debugging method can no longer be used. Now there is a solution
行业案例|数字化经营底座助力寿险行业转型
2022-07-04 matlab reads video frames and saves them
go语言的字符串类型、常量类型和容器类型
清华、剑桥、UIC联合推出首个中文事实核查数据集:基于证据、涵盖医疗社会等多个领域
Thread factory in thread pool
[论文分享] Where’s Crypto?
Five network IO models
PTA 1101 B是A的多少倍
Do you know all four common cache modes?
Tips for short-term operation of spot silver that cannot be ignored
App capture of charles+drony
微信网页调试8.0.19换掉X5内核,改用xweb,所以x5调试方式已经不能用了,现在有了解决方案
pip相关命令