当前位置:网站首页>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
边栏推荐
- 现货白银分析中的一些要点
- 【剑指 Offer】59 - I. 滑动窗口的最大值
- Golang client server login
- 3. About cookies
- Kirk borne's selection of learning resources this week [click the title to download directly]
- [demo] circular queue and conditional lock realize the communication between goroutines
- PIP related commands
- Reinforcement learning - learning notes 8 | Q-learning
- Improve application security through nonce field of play integrity API
- nest. Database for getting started with JS
猜你喜欢

Static routing configuration

idea彻底卸载安装及配置笔记

伺服力矩控制模式下的力矩目标值(fTorque)计算

Learn to make dynamic line chart in 3 minutes!

直播预约通道开启!解锁音视频应用快速上线的秘诀

RIP和OSPF的区别和配置命令

Kirk Borne的本周学习资源精选【点击标题直接下载】

Thread pool and singleton mode and file operation

Tips for short-term operation of spot silver that cannot be ignored

Comparison and selection of kubernetes Devops CD Tools
随机推荐
面试唯品会实习测试岗、抖音实习测试岗【真实投稿】
[paper sharing] where's crypto?
【剑指 Offer】59 - I. 滑动窗口的最大值
ip netns 命令(备忘)
抢占周杰伦
嵌入式C语言程序调试和宏使用的技巧
数据验证框架 Apache BVal 再使用
Summary of evaluation indicators and important knowledge points of regression problems
Charles+Postern的APP抓包
将模型的记忆保存下来!Meta&UC Berkeley提出MeMViT,建模时间支持比现有模型长30倍,计算量仅增加4.5%...
Introduction of common API for socket programming and code implementation of socket, select, poll, epoll high concurrency server model
Nunjuks template engine
The moveposition function of rigidbody2d of unity2d solves the problem of people or screen jitter when moving
微信网页调试8.0.19换掉X5内核,改用xweb,所以x5调试方式已经不能用了,现在有了解决方案
通过 Play Integrity API 的 nonce 字段提高应用安全性
Rules for filling in volunteers for college entrance examination
AntiSamy:防 XSS 攻击的一种解决方案使用教程
Recommend free online SMS receiving platform in 2022 (domestic and foreign)
学习open62541 --- [67] 添加自定义Enum并显示名字
Antisamy: a solution against XSS attack tutorial