Noi2001食物链-并查集
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<string> 6 #include<algorithm> 7 #include<cmath> 8 #include<vector> 9 #include<queue> 10 #include<set> 11 using namespace std; 12 int par[50001],dis[50001]; 13 void get_par(int v){ 14 if(par[v]!=v){ 15 get_par(par[v]); 16 dis[v]=(dis[par[v]]+dis[v])%3; 17 par[v]=par[par[v]]; 18 } 19 } 20 int get_kind(int x,int y){ 21 get_par(x); 22 get_par(y); 23 if(par[x]!=par[y]) return -1; 24 int a=(dis[y]-dis[x])%3; 25 if(a<0) a+=3; 26 return a; 27 } 28 void add_relation(int x,int y,int d){ 29 get_par(x); 30 int a=(-d+1-dis[x])%3; 31 if(a<0) a+=3; 32 dis[par[x]]=a; 33 par[par[x]]=y; 34 35 } 36 int main(){ 37 int n,k,ans=0; 38 scanf("%d%d",&n,&k); 39 for(int i=1;i<=n;i++) par[i]=i; 40 for(int i=0;i<k;i++){ 41 int d,x,y; 42 scanf("%d%d%d",&d,&x,&y); 43 if(x>n||y>n){ 44 ans++; 45 continue; 46 } 47 int a=get_kind(x,y); 48 if(a==-1) add_relation(x,y,d); 49 else if(a!=(d-1)) ans++; 50 } 51 printf("%d\n",ans); 52 return 0; 53 }
代码第32、33行的顺序一定不能反!要注意。
转载于:https://www.cnblogs.com/rdzrdz-acm/p/6511508.html
Noi2001食物链-并查集相关推荐
- POJ 1182 食物链 [并查集 带权并查集 开拓思路]
传送门 P - 食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit ...
- poj 1182 食物链 并查集
转自一位大佬的最全题解 https://blog.csdn.net/c0de4fun/article/details/7318642 #include <cstdio> #include ...
- poj 1182 食物链 (并查集)
http://poj.org/problem?id=1182 重点依旧是用rank记录相对于根节点的关系,并及时跟新rank的值. code: #include<cstdio> using ...
- 【poj 1182】食物链 并查集应用
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59239 Accepted: 17332 Description 动物王 ...
- POJ1182 食物链---(经典种类并查集)
题目链接:http://poj.org/problem?id=1182 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- 并查集/poj1182 noi2001食物链eat
题意 有三类动物A,B,C,题中给出两种关系: 1 x y :x y 同类 2 x y :x吃y 对于假话的定义: 1.当前的话与前面的某些真的话冲突,就是假话: 2.当前的话中X或Y比N大,就是假话 ...
- 【并查集】noi2001食物链
P2024 [NOI2001]食物链 //这是一道比我年纪大的题啊啊啊啊啊QAQ 加权并查集 三倍并查集好厉害qwq 图源洛谷题解 贴代码qwq 1 #include<cstdio> 2 ...
- P2024 [NOI2001]食物链[扩展域并查集]
题目来源:洛谷 题目描述 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B 吃 C,C 吃 A. 现有 N 个动物,以 1 - N 编号.每个动物都是 A,B,C ...
- POJ-1182 食物链(并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 75814 Accepted: 22528 Description ...
最新文章
- String类型转date
- 眼睛很疼(2005-5-10)
- leetcode 388. Longest Absolute File Path | 388. 文件的最长绝对路径(栈+DFS)
- Windows平台上安装搭建iPhone/iPad的开发环境
- Linux下的微秒级定时器: usleep, nanosleep, select, pselect
- Abaqus2019+VS2019+Fortran子程序安装关联全过程亲测有效
- 仿淘手游交易平台网站源码
- 一位考研党亲自整理的大学计算机网络基础知识总结(不看后悔版)
- 网站打开慢解决办法——在Google Chrome浏览器中安装ReplaceGoogleCDN插件
- 如何在国内快速访问Github
- 北京大学C语言学习第8天
- npm安装失败及解决办法 error network tunneling socket could not be established
- 全国各地级市、区县房价最全数据(更新至2021年7月)
- Java代码给csv文件加水印_如何给CSV数据包的图片批量加上水印
- LIN总线协议详解10(LIN的API)
- 神奇的口袋【北京大学】
- Android studio 安装配置SDK
- 计算机网络概述——计算机网络
- 百度云非会员下载限速套路:
- 解决LEDE无线做中继不成功问题
热门文章
- CSS: 首字母字体变大时下划线不对齐的解决方法
- 4、线程范围内的数据共享之ThreadLocal
- 【HDU2795】Billboard(线段树)
- 对list字段属性的部分修改的application
- sql server 2005 T-SQL @@MAX_PRECISION (Transact-SQL)
- openresty 前端开发入门四之Redis篇
- springmvc使用spring自带日期类型验证
- 为了更高效的开发代码,这里列出了一些webstorm的快捷键和zencoding
- 锗钛项圈真的可以治颈椎病吗
- 交叉编译成windows版