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 ...
随机推荐
- SD 一轮集训 day1 carcar
可以发现每条边只能选一次或者两次,并且最后每个点的度数(∑邻接边选的次数和)都是偶数(代表有欧拉回路). 然后根据题意列一个 n 行 m+1 列的01矩阵,每一行代表一个异或方程组(每个点的度数是偶数 ...
- 【分块】bzoj3196 Tyvj 1730 二逼平衡树
分块 或 树套树. 在每个块中维护一个有序表,查询时各种二分,全都是分块的经典操作,就不详细说了. 块的大小定为sqrt(n*log2(n))比较快. #include<cstdio> # ...
- Mybatis添加&&删除&&更新
mapper <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC & ...
- jQuery当中的事件(第六章ppt)
bink绑定事件 hover合成事件 代码示例: <!DOCTYPE html> <html> <head> <title>test1.html< ...
- Android UI 统一修改Button控件的样式,以及其它系统控件的默认样式
先介绍下修改原理:首先打开位于android.widget包下面的Button.java文件,这里有一句关键的代码如下: public Button(Context context, Attribut ...
- Maven设置snapshot无法在远程仓库下载的问题解决
检查步骤如下: 1.检查nexus是否纳入public版本中: 2.配置中是否启用snapshots功能.以下方法两种设置都可以,任选一个即可. 一种是在项目pom.xml使用: <reposi ...
- Shell中 调用/引用/包含 另外的脚本文件的两种方法
脚本 first (测试示例1) #!/bin/bash echo 'your are in first file' 问)在当前脚本文件中调用另外一个脚本文件? 方法一: 使用 source 脚本 s ...
- 针对标签中设置 disabled="true",$("#id").serialize()获取不到value的解决方法
今天给<select>增加disabled="true", 发现$("#form").serialize()的结果不包含select标签的值,解决办 ...
- jquery.mobile 中 collapsible-set collapsible listview 共同布局问题
最近项目用上了jquery.mobile这货,在手机上做点简单的显示.之前只知道有这个框架,没把玩过. 特别是事件绑定方面,相比桌面系统下浏览器用着各种不爽,不得要领. 如下图,在做后台系统时,一般左 ...
- css的checkbox样式变化
1.CSS body{font-family:'微软简行楷'} ul li{list-style:none; margin:10px;color:#4985d7;} .myCheck { displa ...