当前位置:网站首页>POJ 1182: food chain (parallel search) [easy to understand]
POJ 1182: food chain (parallel search) [easy to understand]
2022-07-07 19:11:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
Time Limit: 1000MS | Memory Limit: 10000K | |
---|---|---|
Total Submissions: 43526 | Accepted: 12679 |
Description
There are three kinds of animals in the animal kingdom A,B,C, The food chain of these three kinds of animals forms an interesting ring .A eat B, B eat C,C eat A.
existing N Animals , With 1-N Number .
Every animal is A,B,C One of the , But we don't know what kind it is . There are two ways of saying it N Describe the food chain relationship formed by animals : The first is ”1 X Y”, Express X and Y It's the same kind .
Another way of saying it ”2 X Y”. Express X eat Y. This man is right N Animals . Use the two statements above . Say... Sentence by sentence K Sentence . this K Some sentences are true . Some are fake .
When a sentence satisfies one of the following three , This is a lie , Otherwise, it's the truth . 1) The current words conflict with some of the real words in front . It's a lie ; 2) In the current conversation X or Y Than N Big . It's a lie . 3) The current words mean X eat X, It's a lie . Your task is based on the given N(1 <= N <= 50,000) and K Sentence (0 <= K <= 100,000), The total number of output lies .
Input
The first line is two integers N and K, Separated by a space .
below K Each row is three positive integers D,X,Y. Separate the two numbers with a space . among D The type of statement . if D=1, said X and Y It's the same kind . if D=2, said X eat Y.
Output
There is only one integer . The number of lies .
Sample Input
100 7
1 101 1
2 1 2
2 2 3
2 3 3
1 1 3
2 3 1
1 5 5
Sample Output
3
ok ..
This article is indeed the most specific .. Click to open the link
After reading it, I have a deeper understanding of union search ...
Weak food would like to thank the blogger ..
Also, I made a mistake when I used multiple input for this problem ..
It took me a long time ..
#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; // Initialize and query set
r[i]=0; // On initialization . Each element as a set , Its element is 1
}
}
int find(int x) // And look up the collection 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;
}
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116605.html Link to the original text :https://javaforall.cn
边栏推荐
- Cadre de validation des données Apache bval réutilisé
- Flipping game (enumeration)
- For friends who are not fat at all, nature tells you the reason: it is a genetic mutation
- The top of slashdata developer tool is up to you!!!
- 前首富,沉迷种田
- First time in China! The language AI strength of this Chinese enterprise is recognized as No.2 in the world! Second only to Google
- 初识缓存以及ehcache初体验「建议收藏」
- RISCV64
- 企业展厅设计中常用的三种多媒体技术形式
- Datasimba launched wechat applet, and datanuza accepted the test of the whole scene| StartDT Hackathon
猜你喜欢
Learn open62541 -- [67] add custom enum and display name
10 schemes to ensure interface data security
2022上半年朋友圈都在传的10本书,找到了
学习open62541 --- [67] 添加自定义Enum并显示名字
单臂路由和三层交换的简单配置
Redis
Will low code help enterprises' digital transformation make programmers unemployed?
微信网页调试8.0.19换掉X5内核,改用xweb,所以x5调试方式已经不能用了,现在有了解决方案
Basic operation of chain binary tree (implemented in C language)
In the first half of 2022, I found 10 books that have been passed around by my circle of friends
随机推荐
[tpm2.0 principle and Application guide] Chapter 16, 17 and 18
【Base64笔记】「建议收藏」
Do you know all four common cache modes?
Reinforcement learning - learning notes 8 | Q-learning
Redis的发布与订阅
Standard ACL and extended ACL
Flipping game (enumeration)
Redis
DeSci:去中心化科学是Web3.0的新趋势?
Usage of PHP interview questions foreach ($arr as $value) and foreach ($arr as $value)
Tsinghua, Cambridge and UIC jointly launched the first Chinese fact verification data set: evidence-based, covering many fields such as medical society
[information security laws and regulations] review
Recommend free online SMS receiving platform in 2022 (domestic and foreign)
嵌入式面试题(算法部分)
高温火烧浑不怕,钟薛高想留清白在人间
Multimodal point cloud fusion and visual location based on image and laser
Short selling, overprinting and stock keeping, Oriental selection actually sold 2.66 million books in Tiktok in one month
Policy mode - unity
App capture of charles+drony
App capture of charles+postern