hdu 3038 How Many Answers Are Wrong ( 带 权 并 查 集 )

时间:2022-09-12 21:08:47

How Many Answers Are Wrong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2961    Accepted Submission(s): 1149

Problem Description
TT and FF are ... friends. Uh... very very good friends -________-b

FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored).
hdu 3038  How Many Answers Are Wrong (  带 权 并 查 集  )
Then, FF can choose a continuous subsequence from it(for example the subsequence from the third to the fifth integer inclusively). After that, FF will ask TT what the sum of the subsequence he chose is. The next, TT will answer FF's question. Then, FF can redo this process. In the end, FF must work out the entire sequence of integers.

Boring~~Boring~~a very very boring game!!! TT doesn't want to play with FF at all. To punish FF, she often tells FF the wrong answers on purpose.

The bad boy is not a fool man. FF detects some answers are incompatible. Of course, these contradictions make it difficult to calculate the sequence.

However, TT is a nice and lovely girl. She doesn't have the heart to be hard on FF. To save time, she guarantees that the answers are all right if there is no logical mistakes indeed.

What's more, if FF finds an answer to be wrong, he will ignore it when judging next answers.

But there will be so many questions that poor FF can't make sure whether the current answer is right or wrong in a moment. So he decides to write a program to help him with this matter. The program will receive a series of questions from FF together with the answers FF has received from TT. The aim of this program is to find how many answers are wrong. Only by ignoring the wrong answers can FF work out the entire sequence of integers. Poor FF has no time to do this job. And now he is asking for your help~(Why asking trouble for himself~~Bad boy)

 
Input
Line 1: Two integers, N and M (1 <= N <= 200000, 1 <= M <= 40000). Means TT wrote N integers and FF asked her M questions.

Line 2..M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT answered FF that the sum from Ai to Bi is Si. It's guaranteed that 0 < Ai <= Bi <= N.

You can assume that any sum of subsequence is fit in 32-bit integer.

 
Output
A single line with a integer denotes how many answers are wrong.
 
Sample Input
10 5
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1
 
Sample Output
1
 
Source
   
 思路: 几个月没有做题,喵了个咪,这么个题,只是举了个栗子的题,就做掉了两天半。。。
        果断的无语,泪奔!!! 还是看了别人的解题报告,写了个这么粗糙的代码!!  感谢给出下面题解报告的牛牛orz  !

---- sum[x]表示区间[x,f[x]]的和,这个可以在路径压缩的时候更新,对于一组数据(u,v,w),令r1=Find(u),r2=Find(v),于是若r1==r2,此时u,v就有了相同的参考点,而sum[u]为区间[u,r1(r2)]的和,sum[v]为区间[v,r2(r1)]的和,于是只需判断w==sum[v]-sum[u]即可;若r1<r2,此时可以分为两种情况,(u,v,r1,r2)或者(u,r1,v,r2),对于情况1来说,此时father[r1]=r2,sum[r1]=sum[v]-(sum[u]-w);对于情况2有sum[r1]=w-sun[u]+sum[v].通过观察,我们可以发现情况1和情况2的结果是一样的,于是可以合并。同理,对于r1>r2这种情况也一样,这里就不在赘述了。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 222222
using namespace std; int sum[MAXN],father[MAXN];
int n,m; void Init(){
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++)
father[i]=i;
} int Find(int x)
{
if(x==father[x])
return x;
int tmp=father[x];
father[x]=Find(father[x]);
sum[x]+=sum[tmp];
return father[x];
} bool Union(int u,int v,int w)
{
int r1=Find(u);
int r2=Find(v);
if(r1==r2){
if(sum[u]==w+sum[v])
return true;
return false;
}else {
father[r1]=r2;
sum[r1]=sum[v]-sum[u]+w;
return true;
}
} int main()
{
int u,v,w,ans;
while(~scanf("%d%d",&n,&m)){
Init();
ans=;
while(m--){
scanf("%d%d%d",&u,&v,&w);
if(!Union(u-,v,w))ans++;
}
printf("%d\n",ans);
}
return ;
}

hdu 3038 How Many Answers Are Wrong ( 带 权 并 查 集 )的更多相关文章

  1. HDU 3038 How Many Answers Are Wrong&lpar;带权并查集&rpar;

    太坑人了啊,读入数据a,b,s的时候,我刚开始s用的%lld,给我WA. 实在找不到错误啊,后来不知怎么地突然有个想法,改成%I64d,竟然AC了 思路:我建立一个sum数组,设i的父亲为fa,sum ...

  2. HDU3038 How Many Answers Are Wrong —— 带权并查集

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 200 ...

  3. hdu3038How Many Answers Are Wrong&lpar;带权并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题解转载自:https://www.cnblogs.com/liyinggang/p/53270 ...

  4. HDU 1829 A Bug&&num;39&semi;s Life 【带权并查集&sol;补集法&sol;向量法】

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  5. HDU3038 How Many Answers Are Wrong&lbrack;带权并查集&rsqb;

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  6. 【HDU3038】How Many Answers Are Wrong - 带权并查集

    描述 TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is always ...

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

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

  8. How Many Answers Are Wrong&lpar;带权并查集&rpar;

    How Many Answers Are Wrong http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS ( ...

  9. HDU3038:How Many Answers Are Wrong&lpar;带权并查集&rpar;

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

随机推荐

  1. 分离EF connectionString里的db连接串

    创建EF模型后,自动生成的connectionString如下: <add name="TravelPPEntities" connectionString="me ...

  2. HTML 学习笔记&lpar;列表&rpar;

    HTML 列表 html中列表使用标签<ul>和 <ol>来实现,不同的行用标签<li>来实现 <li>中包含的就是列表每行的内容.列表包含有序列表&l ...

  3. 苹果审核Metadata Rejected

    最近提交了 公司的一个 app,收到了appStore拒绝的信息及邮件 拒绝后的状态改为了:metadata rejected 邮件里面有这样一句话 意思大致就是: 你的app先被显示为 Metada ...

  4. 《JavaScript高级程序设计》第6章 面向对象程序设计

    6.1 对象属性 6.1.1 属性类型 1. 数据属性 我们一般所说的属性就是数据属性,它用来将一个字符串名称映射到某个值上 数据属性的4个特性: configurable, enumerable, ...

  5. android - startActivity浅谈

    当执行startActivity(Intent intent, Bundle options)函数的时候,应用程序不是直接呼叫另外一个Activity,而是将intent传进Android框架中.An ...

  6. JavaScript循环之for&sol;in循环

    今天学到了JavaScript的语句篇.同其他常见编程语言如C.Java等一样,JavaScript中的语句包含:①表达式语句②复合语句和空语句③声明语句④条件语句⑤循环语句⑥跳转语句,当然JavaS ...

  7. Spark SQL笔记——技术点汇总

    目录 概述 原理 组成 执行流程 性能 API 应用程序模板 通用读写方法 RDD转为DataFrame Parquet文件数据源 JSON文件数据源 Hive数据源 数据库JDBC数据源 DataF ...

  8. Python基础篇&lpar;六&rpar;

    retun空值,后面的语句将不再被执行 >>> def test(): ...    print("just a test!") ...    return .. ...

  9. eclipse中maven下载不了私服上面的第三方包问题

    问题描述在nexus中上传了第三方jar包,在本地项目中添加了引用,但就是下载不下来. 转载文章 1. 打开windows -> preferences -> maven,勾选如图所示 2 ...

  10. 基于JAX-WS的webService开发实例

    最近因为工作原因接触到webService,所以记录下开发中碰到的问题,方便自己以后复习,顺便发扬一下开源精神.刚刚接触webServie如果有什么错误欢迎大家指正. 本地环境:myEclipse10 ...