带权并查集,设f[x]为x的父亲,s[x]为sum[x]-sum[fx],路径压缩的时候记得改s

  1. #include<iostream>
  2. #include<cstdio>
  3. using namespace std;
  4. const int N=200005;
  5. int n,m,s[N],f[N],ans;
  6. int read()
  7. {
  8. int r=0,f=1;
  9. char p=getchar();
  10. while(p>'9'||p<'0')
  11. {
  12. if(p=='-')
  13. f=-1;
  14. p=getchar();
  15. }
  16. while(p>='0'&&p<='9')
  17. {
  18. r=r*10+p-48;
  19. p=getchar();
  20. }
  21. return r*f;
  22. }
  23. int zhao(int x)
  24. {
  25. if(f[x]==x)
  26. return x;
  27. int nw=zhao(f[x]);
  28. s[x]+=s[f[x]];
  29. return f[x]=nw;
  30. }
  31. int main()
  32. {
  33. while(~scanf("%d%d",&n,&m))
  34. {
  35. for(int i=0;i<=n;i++)
  36. f[i]=i,s[i]=0;
  37. ans=0;
  38. while(m--)
  39. {
  40. int x=read()-1,y=read(),z=read(),fx=zhao(x),fy=zhao(y);
  41. if(fx!=fy)
  42. {
  43. f[fy]=fx;
  44. s[fy]=s[x]-s[y]+z;
  45. }
  46. else if(s[y]-s[x]!=z)
  47. ans++;
  48. }
  49. printf("%d\n",ans);
  50. }
  51. return 0;
  52. }

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

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

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

  2. HDU 3038 How Many Answers Are Wrong(带权并查集)

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

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

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

  4. hdu3038How Many Answers Are Wrong(带权并查集)

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

  5. HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】

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

  6. HDU3038 How Many Answers Are Wrong[带权并查集]

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

  7. 【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 ...

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

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

  9. How Many Answers Are Wrong(带权并查集)

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

  10. HDU3038:How Many Answers Are Wrong(带权并查集)

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

随机推荐

  1. POJ - 1062 昂贵的聘礼(最短路Dijkstra)

    昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u SubmitStatus Descr ...

  2. MVC Page分页控件

    MVCPage帮助类 控制器代码 public ActionResult Article(int? page) { //Session["ArticleClass"] = cont ...

  3. ORACLE 8i 遇到报错:ORA-01631: max # extents (505) reached in table

    近期在客户的一个8i生产库上使用statspack.发现alert中有报错: Mon Jun 16 13:17:52 2014 Errors in file /oracle/8.1.7/admin/p ...

  4. SSM整理笔记3——配置解析

    github:https://github.com/lakeslove/SSM 项目的目录结构如下 首先,配置web.xml <?xml version="1.0" enco ...

  5. powerdesigner 导入mysql数据库(步骤及注意点)

    参考博客 PowerDesigner中导入MYSQL数据库结构的步骤 mysql ODBC 在64位下提示找不到odbc驱动问题 PowerDesigner逆向工程导入MYSQL数据库总结

  6. Delphi服务端和PHP客户端通过Socket通信

    在开始之前看下效果 PHP页面作为客户端发送请求给作为服务端的Delphi应用程序 PHP客户端页面打开如下 Delphi服务端应用程序打开如下 每次PHP页面刷新一下,Delphi的文本框都显示&q ...

  7. linux下使用无线网卡的命令行方法(wifi,iwconfig)

    原文地址:linux下使用无线网卡的命令行方法(wifi,iwconfig) 作者:andyhzw (1)首先关闭开发板的有线网卡 [root@FriendlyARM /]# ifconfig eth ...

  8. POJ 2017 Speed Limit (直叙式的简单模拟 编程题目 动态属性很少,难度小)

                                                                                                      Sp ...

  9. Oracle:ORA-01790: expression must have same datatype as corresponding expression

    今天有现场报sql错误,错误sql语句 类似  select * from  tableA  where (exists  条件A   or exists 条件B), 单独执行  select * f ...

  10. bzoj 2301 [HAOI2011]Problem b(莫比乌斯反演+分块优化)

    题意:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 1≤n≤50000,1≤a≤b≤50000, ...