题意:n个数,m次询问,每次问区间a到b之间的和为s,问有几次冲突

思路:带权并查集的应用。[a, b]和为s,所以a-1与b就能够确定一次关系。通过计算与根的距离能够推断出询问的正确性

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 200010; int f[MAXN],arr[MAXN];
int m,n; int find(int x) {
if (f[x] == x)
return x;
int tmp = find(f[x]);
arr[x] += arr[f[x]];
return f[x] = tmp;
} int main() {
while (scanf("%d%d", &n, &m) != EOF) {
for (int i = 0; i <= n; i++)
f[i] = i;
memset(arr, 0, sizeof(arr));
int ans = 0;
for (int t = 0; t < m; t++) {
int a,b,s;
scanf("%d%d%d", &a, &b, &s);
a--;
int f1 = find(a);
int f2 = find(b);
if (f1 != f2) {
f[f2] = f1;
arr[f2] = arr[a]+s-arr[b];
}
else if (arr[b]-arr[a] != s)
ans++;
}
printf("%d\n", ans);
}
return 0;
}

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. (8)python 类和对象

    一.类和对象 python可以动态的添加删除变量和方法 类中的方法至少要有一个参数,第一个参数会被绑定到类的实例上,通常命名为self 1.构造函数 类中的构造方法名为 __init__(self,. ...

  2. Apache Kafka 企业级消息队列

    1.大纲 了解 Apache Kafka是什么 掌握Apache Kafka的基本架构 搭建Kafka集群 掌握操作集群的两种方式 了解Apache Kafka高级部分的内容 2.消息系统的作用是什么 ...

  3. 福州三中基训day2

    今天讲的BFS,不得不说,福建三中订的旅馆是真的劲,早餐极棒!!! 早上和yty大神边交流边听zld犇犇讲BFS,嘛,今天讲的比较基础,而且BFS也很好懂(三天弄过一道青铜莲花池的我好像没资格说),除 ...

  4. 训练指南 UVALive - 5135 (双连通分量)

    layout: post title: 训练指南 UVALive - 5135 (双连通分量) author: "luowentaoaa" catalog: true mathja ...

  5. jenkins配置maven工程指定不同环境的配置文件打包

    打包命令 这里我们指定配置文件问test 这个是在pom.xml里面定义的, 里面有test,production和devlop三个定义 在不同环境使用Jenkins的时候,-P后面加上不同的参数 我 ...

  6. [BZOJ 2768] 冠军调查

    Link:https://www.lydsy.com/JudgeOnline/problem.php?id=2768 Solution: 一道比较基础的最大流的题目 一般看到将点分为两类的题目就要往网 ...

  7. 详细User-Agent大全

    目录: Android: Android 0.* Android 1.* Android 2.* Android 3.* Android 4.* Windows Phone OS BlackBerry ...

  8. Exercise01_02

    public class Five{ public static void main(String[] args){ for(int i=0;i<5;i++){ System.out.print ...

  9. tomcat访问(access)日志配置、记录Post请求参数(转)

    一.配置与说明 tomcat访问日志格式配置,在config/server.xml里Host标签下加上 <Valve className="org.apache.catalina.va ...

  10. Debian安装VirtualBox增强工具

    切换到root用户: apt-get install build-essential 或者 apt-get install gcc make apt-get install dkms apt-get ...