题目链接

食物链类似的题,主要是在于转化,a-b的和为s,转换为b比a-1大s。然后并查集存 此节点到根的差。

假如x的根为a,y的根为b:

b - y = rank[y]

a - x = rank[x]

y - x = s

可以推出b - a = rank[y] - rank[x] + s;

并查集 延迟更新什么的,都忘了啊。

还有这题,如果是x--的话,记得更新0的根。

#include <cstring>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int o[];
int rank[];
int find(int x)
{
if(x == o[x]) return x;
int t = find(o[x]);
rank[x] = rank[o[x]] + rank[x];
return o[x] = t;
}
int main()
{
int n,m,i,a,b,s,ans,x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i = ;i <= n;i ++)
{
o[i] = i;
rank[i] = ;
}
ans = ;
for(i = ;i < m;i ++)
{
scanf("%d%d%d",&x,&y,&s);
x -- ;
a = find(x);
b = find(y);
if(a != b)
{
o[a] = b;
rank[a] = rank[y] - rank[x] + s;
}
else
{
if(rank[x] != rank[y] + s)
ans ++;
}
}
printf("%d\n",ans);
}
return ;
}

HDU 3038 How Many Answers Are Wrong(种类并查集)的更多相关文章

  1. hdu 3038 How Many Answers Are Wrong(并查集的思想利用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:就是给出n个数和依次m个问题,每个问题都是一个区间的和,然后问你这些问题中有几个有问题,有 ...

  2. hdu 3038 How Many Answers Are Wrong(并查集)

    题意: N和M.有N个数. M个回答:ai, bi, si.代表:sum(ai...bi)=si.如果这个回答和之前的冲突,则这个回答是假的. 问:M个回答中有几个是错误的. 思路: 如果知道sum( ...

  3. HDU 1829 A Bug's Life (种类并查集)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit: 15000/5000 MS (Java/Oth ...

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

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

  5. HDU 5285 wyh2000 and pupil(dfs或种类并查集)

    wyh2000 and pupil Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Other ...

  6. hdu3038 How Many Answers Are Wrong 种类并查集

    #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int ...

  7. hdu 3038 How Many Answers Are Wrong(种类并查集)2009 Multi-University Training Contest 13

    了解了种类并查集,同时还知道了一个小技巧,这道题就比较容易了. 其实这是我碰到的第一道种类并查集,实在不会,只好看着别人的代码写.最后半懂不懂的写完了.然后又和别人的代码进行比较,还是不懂,但还是交了 ...

  8. HDU - 3038 种类并查集

    思路:种类并查集的每个节点应该保存它的父节点以及他和父节点之间的关系.假设root表示根结点,sum[i-1]表示i到根结点的和,那么sum[j-1] - sum[i]可以得到区间[j, i]的和.那 ...

  9. 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany

    先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...

随机推荐

  1. ubuntu下安装mysql及卸载mysql方法

    1. 删除mysql a. sudo apt-get autoremove --purge mysql-server-5.0 b. sudo apt-get remove mysql-server c ...

  2. mahout 安装测试

    1 下载 在http://archive.apache.org/dist/mahout下载相应版本的mahout 版本,获取官网查看http://mahout.apache.org 相关的信息

  3. PHP数据采集curl常用的5个例子

    用php ,curl主要是抓取数据,当然我们可以用其他的方法来抓取,比如fsockopen,file_get_contents等.但是只能抓那些能直接访问的页面,如果要抓取有页面访问控制的页面,或者是 ...

  4. 报错mongoose.connection.db.collectionnames is not a function

    mongoose.connection.db.collectionNames方法已经无效 建议使用mongoose.connection.db.listCollections()

  5. Python基础二

    1.for循环后接else __author__ = "zhou" age_of_oldboy = 56 for i in range(3): guess_age = int(in ...

  6. Path之Data属性语法A命令

    <Path Width="300" Height="300" Fill="Red" Data="M 100,100 L 10 ...

  7. 使用curl进行https请求

    简单示例: /** * @param $url * @return array * 进行https请求,并且遇到location进行跳转 */ function https($url){ $resul ...

  8. python习题 (1):login

    #!/uer/bin/env python # _*_ coding: utf-8 _*_ import sys retry_limit = 3 retry_count = 0 account_fil ...

  9. 解决 eclipse中properties文件编码问题

    菜单——>Preferences——>General——>ContentTypes——>Text——>Java Properties File,设置Default enc ...

  10. 关于Ajax工作原理

    1.ajax技术的背景 不可否认,ajax技术的流行得益于google的大力推广,正是由于google earth.google suggest以及gmail等对ajax技术的广泛应用,催生了ajax ...