#include<stdio.h>

#include<string.h>

#define N  200100

struct node {

int x,count;

}pre[N];

int find(int n) {

if(n!=pre[n].x) {

int h=pre[n].x;

pre[n].x=find(pre[n].x);

pre[n].count=pre[n].count+pre[h].count;

}

return pre[n].x;

}

int Union(int x,int y,int d) {

      int a=find(x);

 int b=find(y);

 if(a==b) {

 if(pre[x].count-pre[y].count==d)

 return 1;

 return 0;

 }

 else {

 pre[a].x=b;

 pre[a].count=pre[y].count-pre[x].count+d;

 return 1;

 }

}

int main() {

int n,m,a,b,c,count,sum,flag,i;

while(scanf("%d%d",&n,&m)!=EOF) {

for(i=0;i<=n;i++) {

pre[i].x=i;

pre[i].count=0;

}

count=0;

while(m--) {

scanf("%d%d%d",&a,&b,&c);

a--;

if(Union(a,b,c)==0)

count++;

}

printf("%d\n",count);

}

return 0;

}

hdu 3038带权并查集的更多相关文章

  1. HDU - 3038 带权并查集

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

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

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

  3. Zjnu Stadium HDU - 3047 带权并查集板子题

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

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

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

    题意:n个数,m次询问,每次问区间a到b之间的和为s,问有几次冲突 思路:带权并查集的应用.[a, b]和为s,所以a-1与b就能够确定一次关系.通过计算与根的距离能够推断出询问的正确性 #inclu ...

  6. hdu 3038 How Many Answers Are Wrong【带权并查集】

    带权并查集,设f[x]为x的父亲,s[x]为sum[x]-sum[fx],路径压缩的时候记得改s #include<iostream> #include<cstdio> usi ...

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

    题目链接 并查集是用来对集合合并查询的一种数据结构,或者判断是不是一个集合,本题是给你一系列区间和,判断给出的区间中有几个是不合法的. 思考: 1.如何建立区间之间的联系 2.如何发现悖论 首先是如何 ...

  8. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  9. HDU 3047 Zjnu Stadium(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...

随机推荐

  1. Service官方教程(2)*IntentService与Service示例、onStartCommand()3个返回值的含义。

    1.Creating a Started Service A started service is one that another component starts by calling start ...

  2. iOS9导入高德地图报错App Transport Security has blocked...

    App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Te ...

  3. Android系统的启动流程

    手机启动后首先会通过执行BootLoader来启动Linux内核,BootLoader是所有嵌入式设备开机启动执行的第一行代码,linux内核在启动过程中会加载各种设备的驱动同时初始化数据结构,并且开 ...

  4. windows系统里Cygwin中如何正确安装wget(图文详解)

    具体步骤,见如下: https://ftp.gnu.org/gnu/wget/ 解压到Cygwin的主目录中,一般是   你的Cygwin目录/home/当前用户名/ . 我这是如下     先./c ...

  5. 多个文本框点击复制 zClip (ZeroClipboard)有关问题

    <script type="text/javascript" src="js/jquery.min.js"$amp;>amp;$lt;/script ...

  6. spring实现模板文件下载

    前台 <form id="batchModel0" method="post" action="/common/download-file&qu ...

  7. Elasticsearch--集群管理_时光机&监控

    目录 Elasticsearch时光机 创建快照存储库 清理:删除旧的快照 监控集群状态和健康度 集群健康度API 索引统计API 状态API 节点信息API 节点统计API 集群状态API 挂起任务 ...

  8. LitePal用法详解

    一.首先我对数据库的操作基于LitePal的,是基于面向对象思想的,所以首先我先讲怎么使用LitePal 1.在build.garde(Module:app)里面的 dependencies{ //添 ...

  9. File文件存储

    文件存储的核心是Context提供了一个openFileOutput()与openFileInput()俩个方法 课程demo public class MainActivity extends Ap ...

  10. Python学习 Day 3 字符串 编码 list tuple 循环 dict set

    字符串和编码 字符 ASCII Unicode UTF-8 A 1000001 00000000 01000001 1000001 中 x 01001110 00101101 11100100 101 ...