HDU - 3038 How Many Answers Are Wrong (带权并查集)
题意: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 (带权并查集)的更多相关文章
- 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 ...
- HDU 3038 How Many Answers Are Wrong(带权并查集)
太坑人了啊,读入数据a,b,s的时候,我刚开始s用的%lld,给我WA. 实在找不到错误啊,后来不知怎么地突然有个想法,改成%I64d,竟然AC了 思路:我建立一个sum数组,设i的父亲为fa,sum ...
- HDU3038 How Many Answers Are Wrong —— 带权并查集
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 200 ...
- hdu3038How Many Answers Are Wrong(带权并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题解转载自:https://www.cnblogs.com/liyinggang/p/53270 ...
- HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- HDU3038 How Many Answers Are Wrong[带权并查集]
How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- 【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 ...
- hdu 3038 How Many Answers Are Wrong(种类并查集)2009 Multi-University Training Contest 13
了解了种类并查集,同时还知道了一个小技巧,这道题就比较容易了. 其实这是我碰到的第一道种类并查集,实在不会,只好看着别人的代码写.最后半懂不懂的写完了.然后又和别人的代码进行比较,还是不懂,但还是交了 ...
- How Many Answers Are Wrong(带权并查集)
How Many Answers Are Wrong http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS ( ...
- HDU3038:How Many Answers Are Wrong(带权并查集)
How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
随机推荐
- 为用户分配角色 C#
开发网站时,在后台管理系统中,如果有多类角色,将会涉及到为角色分配用户的功能,或者是为用户选择角色.为用户分配角色相对来说操作的数据量比较小,因为系统所设定的角色不会有很多种.而如果是为角色分配用户, ...
- 用 grunt-contrib-connect 构建实时预览开发环境 实时刷新
本文基本是参照着 用Grunt与livereload构建实时预览的开发环境 实操了一遍,直接实现能实时预览文件列表,内容页面.不用刷新页面了,这比以前开发网页程序都简单. 这里要用到的 Grunt 插 ...
- CF 990A. Commentary Boxes【数学/模拟】
[链接]:CF [题意]:对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价. [分析]:分情况讨论,自己多举几个栗子. [代码]: #include<cstdio&g ...
- 转 IntelliJ IDEA 快捷键
https://www.cnblogs.com/clwydjgs/p/9390488.html 一.视图查看 Ctrl+F12 查看file,method结构图.类继承机构图 (不知道方法结构,Ctr ...
- fiddler进行弱网测试
fiddler模拟限速的原理 弱网测试原帖连接:http://blog.csdn.net/eleven521/article/details/19089671 我们可以通过fiddler来模拟限速,因 ...
- Codeforces 311E Biologist
Discription SmallR is a biologist. Her latest research finding is how to change the sex of dogs. In ...
- 3.4常用类(java学习笔记)Math和Random
一.Math 这个类包含执行指数.对数.平方根法.三角函数等基本的数字运算. Math中还包含一些静态常量供我们调用. 如PI圆周率,E. 1.abs(),返回该数的绝对值. public class ...
- 【R笔记】apply函数族
(1) apply apply函数通过对数组,矩阵,或非空维数值的数据框的“边缘”(margin)即行或列运用函数.返回值为向量,数组或列表. 函数形式 apply(X, MARGIN, ...
- Swift中TableViewCell便利构造器写法
目前为止比较方便的一种方法,如果有更好的写法请通知我,谢谢!
- JNI之数据类型
1. JNIEnv 作用 JNIEnv 概念 : 是一个线程相关的结构体, 该结构体代表了 Java 在本线程的运行环境 ; JNIEnv 与 JavaVM : 注意区分这两个概念; -- JavaV ...