题目链接:http://poj.org/problem?id=1182

解题思路来自discuss:http://poj.org/showmessage?message_id=152847

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 50050
int parent[MAXN];
int kind[MAXN];
int n,m,tag; void Initiate()
{
for(int i=;i<=n;i++){
parent[i]=i;
}
memset(kind,,(n+)*sizeof(kind[]));
} int Find(int x)
{
if(x==parent[x]){
return parent[x];
}
int tmp=Find(parent[x]);
kind[x]=(kind[x]+kind[parent[x]])%;
return parent[x]=tmp;
} void Union(int a,int b)
{
int r1=Find(a),r2=Find(b);
parent[r1]=r2;
kind[r1]=(kind[b]-kind[a]++tag-)%;
} int main()
{
// freopen("1.txt","r",stdin);
int a,b,ans=;
scanf("%d%d",&n,&m);
Initiate();
while(m--){
scanf("%d%d%d",&tag,&a,&b);
if(a>n||b>n||(tag==&&a==b)){ ans++;continue; }
if(Find(a)!=Find(b)){
Union(a,b);
}else {
if((kind[a]-kind[b]+)%!=(tag-))
ans++;
}
}
printf("%d\n",ans);
return ;
}

pku 1182(种类并查集)的更多相关文章

  1. Poj(1182),种类并查集

    题目链接:http://poj.org/problem?id=1182 再次熟练种类并查集,又积累点经验,和技巧,rank 0 2 1 先计算father[x] ,再更新rank[x]; #inclu ...

  2. pku 1703(种类并查集)

    题目链接:http://poj.org/problem?id=1703 思路;个人觉得本质上还是和带权并查集一样的,只不过多了一个MOD操作,然后就是向量关系图稍微改动一下就变成种类并查集了,对于本题 ...

  3. POJ 1182食物链(分集合以及加权两种解法) 种类并查集的经典

    题目链接:http://icpc.njust.edu.cn/Problem/Pku/1182/ 题意:给出动物之间的关系,有几种询问方式,问是真话还是假话. 定义三种偏移关系: x->y 偏移量 ...

  4. poj 1182:食物链(种类并查集,食物链问题)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44168   Accepted: 12878 Description ...

  5. poj 1182 食物链(种类并查集 ‘初心者’)

    题目链接:http://poj.org/problem?id=1182 借着这题可以好好理解一下种类并查集,这题比较简单但挺经典的. 题意就不解释了,中问题. 关于种类并查集结局方法也是挺多的 1扩增 ...

  6. hdu 1182 A Bug's Life(简单种类并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...

  7. 食物链(带权&种类并查集)

    食物链 http://poj.org/problem?id=1182 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9326 ...

  8. poj1182(种类并查集好题)

    不得不说,我得感谢@驱动幽灵百鬼夜行小肆,正是因为看明白了他给出的解析,我才完全弄懂种类并查集的,这里,我也不想去改其他的,就直接引用他的解题报告吧 转载:http://blog.csdn.net/c ...

  9. POJ1182 食物链---(经典种类并查集)

    题目链接:http://poj.org/problem?id=1182   食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submission ...

随机推荐

  1. 使用ViewSwitcher和ViewFlipper在不同布局中切换

    xml布局: <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:androi ...

  2. [转]ubuntu(12.04)下, 命令 ,内核 源代码的获取

    [转]ubuntu(12.04)下, 命令 ,内核 源代码的获取 http://blog.chinaunix.net/uid-18905703-id-3446099.html 1.命令:例如:要查看l ...

  3. SVN基于一个branch创建新branch

    在本地现有Branch(Checkout出来的目录)上,右键SVN,选择Branch/Tags,选择目录.比如https://sadcsc01.pmmr.com/web/Jaguar/branches ...

  4. ASCII Table

    ASCII Table ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 0 NUT 32 (space) 64 @ 96 . 1 SOH 33 ! 65 ...

  5. linux查看端口是否已开启和查看文件数

    查看端口是否开启 lsof -i:80 查看文件数 ls | wc -w

  6. Python科学计算(二)windows下开发环境搭建(当用pip安装出现Unable to find vcvarsall.bat)

    用于科学计算Python语言真的是amazing! 方法一:直接安装集成好的软件 刚开始使用numpy.scipy这些模块的时候,图个方便直接使用了一个叫做Enthought的软件.Enthought ...

  7. IOC框架的认识

    转:http://blog.csdn.net/wanghao72214/article/details/3969594 1 IoC理论的背景 我们都知道,在采用面向对象方法设计的软件系统中,它的底层实 ...

  8. Java实现 Base64、MD5、MAC、HMAC加密

    开始对那些基本的加密还不怎么熟练,然后总结了些,写了一个测试:支持 Base64.MD5.MAC.HMAC加密,长话短说,我们都比较喜欢自己理解,看代码吧! 采用的输UTF-8的格式... packa ...

  9. Codeforces Round #277.5 (Div. 2)

    题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...

  10. webservice之XFire的使用(java调用java)

    注意:xfire不支持java.util.List等集合,所以调用webservice传递的参数要为基本类型. 转自:http://zp9245.blog.163.com/blog/static/10 ...