当前位置:网站首页>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
边栏推荐
- “解密”华为机器视觉军团:华为向上,产业向前
- 面试唯品会实习测试岗、抖音实习测试岗【真实投稿】
- 体总:安全有序恢复线下体育赛事,力争做到国内赛事应办尽办
- Hash, bitmap and bloom filter for mass data De duplication
- The moveposition function of rigidbody2d of unity2d solves the problem of people or screen jitter when moving
- Disk storage chain B-tree and b+ tree
- coming! Gaussdb (for Cassandra) new features appear
- 虚拟数字人里的生意经
- DeSci:去中心化科学是Web3.0的新趋势?
- 抢占周杰伦
猜你喜欢

企业展厅设计中常用的三种多媒体技术形式

基于图像和激光的多模态点云融合与视觉定位

I feel cheated. Wechat tests the function of "size number" internally, and two wechat can be registered with the same mobile number
![[software test] from the direct employment of the boss of the enterprise version, looking at the resume, there is a reason why you are not covered](/img/73/cbbe82fd6bdfa8177f5bfcf683010d.jpg)
[software test] from the direct employment of the boss of the enterprise version, looking at the resume, there is a reason why you are not covered

Will low code help enterprises' digital transformation make programmers unemployed?

Skills of embedded C language program debugging and macro use

Nunjuks template engine

Tsinghua, Cambridge and UIC jointly launched the first Chinese fact verification data set: evidence-based, covering many fields such as medical society

DeSci:去中心化科学是Web3.0的新趋势?

Industry case | digital operation base helps the transformation of life insurance industry
随机推荐
云安全日报220707:思科Expressway系列和网真视频通信服务器发现远程攻击漏洞,需要尽快升级
Disk storage chain B-tree and b+ tree
企业展厅设计中常用的三种多媒体技术形式
[论文分享] Where’s Crypto?
nest. Database for getting started with JS
Nunjuks template engine
[paper sharing] where's crypto?
Industry case | digital operation base helps the transformation of life insurance industry
面试唯品会实习测试岗、抖音实习测试岗【真实投稿】
Desci: is decentralized science the new trend of Web3.0?
基于图像和激光的多模态点云融合与视觉定位
Discuss | frankly, why is it difficult to implement industrial AR applications?
Antisamy: a solution against XSS attack tutorial
Some key points in the analysis of spot Silver
Introduction of common API for socket programming and code implementation of socket, select, poll, epoll high concurrency server model
云景网络科技面试题【杭州多测师】【杭州多测师_王sir】
PIP related commands
国内的软件测试会受到偏见吗
Sports Federation: resume offline sports events in a safe and orderly manner, and strive to do everything possible for domestic events
回归问题的评价指标和重要知识点总结