题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038

  1. 题意:数组第 a 个元素到第 b 个元素之间的和为sum
  2.  
  3. 求有几句话是假的,如果与前面的话有冲突就为假;
  4.  
  5. r[i]代表i的父节点到i的和;
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<math.h>
  6. #define N 201000
  7. #define INF 0xfffffff
  8. using namespace std;
  9.  
  10. int f[N],vis[N],r[N];
  11.  
  12. int Find(int x)
  13. {
  14. int k = f[x];
  15. if(x != f[x])
  16. {
  17. f[x] = Find(f[x]);
  18. r[x] += r[k];
  19. }
  20. return f[x];
  21. }
  22. int main()
  23. {
  24. int px, py, ans, i, n, m, x, y, sum;
  25. while(scanf("%d%d", &n, &m)!=EOF)
  26. {
  27. for(i=;i<=n;i++)
  28. f[i]=i,r[i]=;
  29. ans = ;
  30. while(m--)
  31. {
  32. scanf("%d%d%d", &x, &y, &sum);
  33. x --;
  34. px = Find(x);
  35. py = Find(y);
  36. if(px != py)
  37. {
  38. f[px] = py;
  39. r[px] = r[y] -r[x] - sum;
  40. }
  41. else if(r[y]-r[x] != sum)
  42. ans++;
  43. }
  44. printf("%d\n",ans);
  45. }
  46. return ;
  47. }

上面的关系都是有方向的。。。

 代码中的123处均与*处有关,大家可以画个图有助于理解;

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <stack>
  7. #include <map>
  8. #include <vector>
  9. using namespace std;
  10. typedef long long LL;
  11. #define N 202100
  12. #define met(a, b) memset(a, b, sizeof(a))
  13. #define INF 0x3f3f3f3f
  14.  
  15. int f[N], r[N];
  16.  
  17. int Find(int x)
  18. {
  19. int k = f[x];
  20. if(x != f[x])
  21. {
  22. f[x] = Find(f[x]);
  23. r[x] += r[k];///1;
  24. }
  25. return f[x];
  26. }
  27.  
  28. int main()
  29. {
  30. int n, m, x, y, sum;
  31.  
  32. while(scanf("%d %d", &n, &m)!=EOF)
  33. {
  34. for(int i=; i<=n; i++)
  35. f[i] = i, r[i] = ;
  36.  
  37. int ans = ;
  38.  
  39. while(m--)
  40. {
  41. scanf("%d %d %d", &x, &y, &sum);
  42. x -- ;
  43.  
  44. int px = Find(x);
  45. int py = Find(y);
  46.  
  47. if(px != py)
  48. {
  49. f[px] = py;///*
  50. r[px] = r[y] - sum - r[x];///2;
  51. }
  52. else if(px == py && r[x]+sum != r[y])///
  53. ans++;
  54. }
  55. printf("%d\n", ans);
  56. }
  57. return ;
  58. }

How Many Answers Are Wrong----hdu3038(并查集)的更多相关文章

  1. 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 w ...

  2. *HDU3038 并查集

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

  3. How Many Answers Are Wrong(并查集)

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

  4. 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[ ...

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

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

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

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

  7. [HDU3038]How Many Answers Are Wrong(并查集)

    传送门 和某题类似,只不过奇偶换成了和. ——代码 #include <cstdio> #include <iostream> #define N 1000001 int n, ...

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

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

  9. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

随机推荐

  1. Nginx(七)-- 反向代理

    1.概念  反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器:并将从服务器上得到的结果返回给Internet上请求连接的 ...

  2. Apache HTTP Server 与 Tomcat 的三种连接方式介绍

    本文转载自IBM developer 首先我们先介绍一下为什么要让 Apache 与 Tomcat 之间进行连接.事实上 Tomcat 本身已经提供了 HTTP 服务,该服务默认的端口是 8080,装 ...

  3. Subversion权限详解

    1   背景假设厦门央瞬公司是一家电子元器件设备供应商,其中有个ARM部门,专门负责ARM芯片的方案设计.销售,并在北京.上海各设立了一个办事处.对于工作日志,原先采用邮件方式发给经理,但是这种方式有 ...

  4. System.Web.Mvc.AJAX缺少程序集引用

    今天在构建新项目的时候,出现这个错误,实际上这个出现过很多次了,但是每次都忘记之前怎么解决的,之前博客也不记得写过没有...

  5. .net写入文本到本地

    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"d:\test\ErrorLog.txt", t ...

  6. 【Linux基础学习】Ubuntu 常用命令大全

    一.文件目录类 1.建立目录:mkdir 目录名 2.删除空目录:rmdir 目录名 3.无条件删除子目录: rm -rf 目录名 4.改变当前目录:cd 目录名 (进入用户home目录:cd ~:进 ...

  7. WP8.1学习系列(第一章)——添加应用栏

    做过android开发的同学们应该都知道有个ActionBar的头部操作栏,而wp也有类似的一个固定在app页面里通常拥有的内部属性,就是应用栏.以前叫做ApplicationBar,现在wp和win ...

  8. Python tkinter 控件更新信息

    下面几个例子关于tkinter界面更新的,简单易懂,分享一下. 例子_1: 代码_1: from tkinter import Tk, Checkbutton, Label from tkinter ...

  9. css笔记——文本样式

    聊聊text-decoration.text-indent.text-transform.letter-spacing.word-spacing.vertical-align.下面是一些常用设置文本样 ...

  10. nginx(一)----ubuntu14.04下安装nginx

    /** * lihaibo * 文章内容都是根据自己工作情况实践得出. *如有错误,请指正 *转载请注明出处 */ 此文章中用到的软件下载地址: 链接: http://pan.baidu.com/s/ ...