1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. using namespace std;
  5. const int N=+;
  6. int p[N];
  7. int w[N];
  8. int find(int x)
  9. {
  10. if(p[x]==x)
  11. return x;
  12. int root=find(p[x]);
  13. w[x]+=w[p[x]];
  14. p[x]=root;
  15. return p[x];
  16. }
  17. int n,m;
  18. int main()
  19. {
  20. while(~scanf("%d%d",&n,&m))
  21. {
  22. for(int i=;i<=n;i++)
  23. p[i]=i,w[i]=;
  24. int ans=;
  25. for(int i=;i<=m;i++)
  26. {
  27. int a,b,c;
  28. scanf("%d%d%d",&a,&b,&c);
  29. int pa=find(a);
  30. int pb=find(b);
  31. if(pa==pb)
  32. {
  33. if(w[a]+c!=w[b])
  34. ans++;
  35. }
  36. else
  37. {
  38. p[pb]=pa;
  39. w[pb]=w[a]-w[b]+c;
  40. }
  41. }
  42. printf("%d\n",ans);
  43. }
  44. return ;
  45. }

Zjnu Stadium HDU - 3047 带权并查集板子题的更多相关文章

  1. 【poj 1962】Corporative Network(图论--带权并查集 模版题)

    P.S.我不想看英文原题的,但是看网上题解的题意看得我 炒鸡辛苦&一脸懵 +_+,打这模版题的代码也纠结至极了......不得已只能自己翻译了QwQ . 题意:有一个公司有N个企业,分成几个网 ...

  2. HDOJ 3047 带权并查集

    解题思路转自: http://blog.csdn.net/azheng51714/article/details/8500459 http://blog.csdn.net/acresume/artic ...

  3. HDU - 3038 带权并查集

    这道题我拖了有8个月... 今天放假拉出来研究一下带权的正确性,还有半开半闭的处理还有ab指向的一系列细节问题 #include<iostream> #include<algorit ...

  4. hdu 3038带权并查集

    #include<stdio.h> #include<string.h> #define N  200100 struct node { int x,count; }pre[N ...

  5. hdu 1829 带权并查集的运用类似于食物链但是更简单些

    #include<stdio.h> #define N 1100000 struct node { int x,y; }f[N],pre[N]; int find(int x) { if( ...

  6. K - Find them, Catch them POJ - 1703 (带权并查集)

    题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...

  7. poj1417(带权并查集+背包DP+路径回溯)

    题目链接:http://poj.org/problem;jsessionid=8C1721AF1C7E94E125535692CDB6216C?id=1417 题意:有p1个天使,p2个恶魔,天使只说 ...

  8. poj1182(带权并查集)

    题目链接:http://poj.org/problem?id=1182 题意:题目告诉有  3  种动物,互相吃与被吃,现在告诉你  m  句话,其中有真有假,叫你判断假的个数  (  如果前面没有与 ...

  9. [NOI2002] 银河英雄传说 (带权并查集)

    题目描述 公元五八○一年,地球居民迁至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶 ...

随机推荐

  1. 《自拍教程9》Python编程风格规范

    Python编程风格规范 根据Python官方提供的Python编程风格规范: Style Guide for Python Code, 即PEP8规范, https://www.python.org ...

  2. windows socket ipv6 SOCK_RAW

    bind处一直报错WSAEADDRNOTAVAIL10049,不知道为什么? WSAEADDRNOTAVAIL 10049 Cannot assign requested address. The r ...

  3. Prometheus监控k8s集合

    Prometheus监控k8s Prometheus监控k8s(1)-Prometheus简介 Prometheus监控k8s(2)-手动部署Prometheus Prometheus监控k8s(3) ...

  4. 使用FIO工具测试块存储性能

    Linux实例和Windows实例都推荐使用FIO工具测试块存储性能.   说明 您也可以使用其他工具测试块存储性能,但不同工具测试出来的硬盘基准性能会有差异,如dd.sysbench.iometer ...

  5. 一键安装MySQL5.6.43脚本

    [root@lamp ~]# cat /server/scripts/mysql-5.6.43_install.sh #!/bin/bash #卸载系统自带的Mysql /bin/rpm -e $(/ ...

  6. Windows+Python+Selenium基础篇之1-环境搭建

    1.所需工具包1.1Selenium for python1.2  Python  1.3  Notepad++或python IDE 2.  环境搭建2.1  下载和安装Pythonpython2. ...

  7. vue-cli项目传到服务器后打不开的问题

    1.vue-cli项目执行dev可以打开网站,直接点击文件或发布后却打不开的问题   webpack.prod.conf.js: output: { ....... publicPath:'./'  ...

  8. A——大整数加法(HDU1002)

    题目: I have a very simple problem for you. Given two integers A and B, your job is to calculate the S ...

  9. toj 3019 Hidden Password (最小表示法)

    Hidden Password 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交: 53 测试通过: 19 描述 Some time the progr ...

  10. Nginx Rewrite相关功能

    目录 Nginx Rewrite相关功能 ngx_http_rewrite_module模块指令: if指令: set指令: break指令: return指令: rewrite_log指令: rew ...