HDU 3038
http://acm.hdu.edu.cn/showproblem.php?pid=3038
题意:[1-n]的区间,有m个询问,每个询问表示[a,b]的和是s,问一共有多少组矛盾
sum[i]表示i到根节点的和,求区间和用sum[b]-sum[a-1]
为方便描述先把a--
我是把b的父亲接在a的父亲上,下面图都是如此
1、当b和a在同一个集合,只需判断[a,b]间和是否是s,算法用向量表示,如下图

if(sum[b]-sum[a]!=s)ans++;
2、a、b不在同一个集合,把b的父亲连在a上,sum[pb]的算法如下图向量表示

sum[pb]=-sum[b]+s+sum[a];
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map> using namespace std; int fa[],sum[]; int find(int x){
if(x!=fa[x]){
int pre=fa[x];
fa[x]=find(fa[x]);
sum[x]+=sum[pre];
}
return fa[x];
} int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++)
fa[i]=i;
int ans=;
for(int i=;i<m;i++){
int a,b,s;
scanf("%d%d%d",&a,&b,&s);
a--;
int pa=find(a);
int pb=find(b);
if(pa!=pb){
fa[pb]=pa;
sum[pb]=-sum[b]+s+sum[a];
}
else{
if(sum[b]-sum[a]!=s)ans++;
}
}
printf("%d\n",ans);
}
return ;
}
HDU 3038的更多相关文章
- 类似区间计数的种类并查集两题--HDU 3038 & POJ 1733
1.POJ 1733 Parity game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5744 Accepted: ...
- hdu 3038 How Many Answers Are Wrong
http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 MS ( ...
- hdu 3038(扩展并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:给出区间[1,n],下面有m组数据,l r v区间[l,r]之和为v,每输入一组数据,判断 ...
- HDU 3038 - How Many Answers Are Wrong - [经典带权并查集]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- (并查集 添加关系)How Many Answers Are Wrong --Hdu --3038
链接: http://acm.hdu.edu.cn/showproblem.php?pid=3038 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 3038 How Many Answers Are Wrong 【YY && 带权并查集】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 ...
- hdu 3038 How Many Answers Are Wrong(并查集的思想利用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:就是给出n个数和依次m个问题,每个问题都是一个区间的和,然后问你这些问题中有几个有问题,有 ...
- HDU 3038 How Many Answers Are Wrong(种类并查集)
题目链接 食物链类似的题,主要是在于转化,a-b的和为s,转换为b比a-1大s.然后并查集存 此节点到根的差. 假如x的根为a,y的根为b: b - y = rank[y] a - x = rank[ ...
- HDU 3038 How Many Answers Are Wrong(带权并查集)
传送门 Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, ...
随机推荐
- JSON Viewer
http://jsonviewer.codeplex.com/ jsoneditor https://github.com/josdejong/jsoneditor
- 为什么html5用的jQuery Mobile在手机浏览器/微信中打开字体很小
头部加入 <header> <metaname="viewport"content="width=device-width, initial-scale ...
- wordpress为不同的category添加不同的模板
在category中新建了三个:NEWS,EVENTS,BLOG,当点击这三个category时想使用不同的template生成不同风格的页面,该怎么实现? 一般来说,wordpress的catego ...
- px和em的区别
px和em的区别 2012-06-21 23:01:06| 分类: CSS|字号 订阅 在如今这个提倡可用性设计以及用户体验设计的网络时代,CSS也是要一同参与其中的.大部分人在CSS代码编写中总是 ...
- Gramar
一.And 并列关系(and) in addition / and / similarly / likewise / as well as / besides / furthermore / also ...
- 最简单的PHP socket echo server。
常有人困惑php的socket服务,现在有libevent和多线程了,但是我还是整一个select的 <?php $addr = '0.0.0.0'; $port = 1234; $socket ...
- 二模 (3) day1
第一题: 题目描述: 一个数列定义如下:f(1) = 1,f(2) = 1,f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.给定 A,B 和 n 的值,要求计算 ...
- js打印数组查看
alert() 是不能查看数组,对象的console.log(数组变量); 然后你用火狐的friebug 在控制台查看
- Java爬虫,信息抓取的实现
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/23272657 今天公司有个需求,需要做一些指定网站查询后的数据的抓取,于是花了点 ...
- 算法导论-钢条切割 C# 递归实现
下班前看到有位兄弟写 钢条切割问题,尝试实现C#版, 还没有实现最优版,分享一下. int[] parr; private void button1_Click(object sender, Even ...