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 ...
随机推荐
- 抽象语法树简介(ZZ)
转载自: http://www.cnblogs.com/cxihu/p/5836744.html (一)简介 抽象语法树(abstract syntax code,AST)是源代码的抽象语法结构的树状 ...
- 一步步疑难解析 —— Python 异步编程构建博客
声明:该项目学习资源主要来自廖雪峰的Python教程,参见 http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6 ...
- C# 通过串口发送短信
手机短信群发作为企业日常通知,公告,天气预报等信息的一个发布平台,在于成本低,操作方便等诸多特点,成为企业通讯之首选.本文介绍短信的编码方式,AT指令以及用C#实现串口通讯的方法. 前言目前,发送短信 ...
- dokcer常用命令
文章来自于 CSDN docker常用命令详解 docker常用命令分类 常用命令 docker run -d --name express-docker-demo --restart=always ...
- CF 1009A Game Shopping 【双指针/模拟】
Maxim wants to buy some games at the local game shop. There are n games in the shop, the i-th game c ...
- 51nod 1137 矩阵乘法【矩阵】
1137 矩阵乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出2个N * N的矩阵M1和M2,输出2个矩阵相乘后的结果. Input 第1行 ...
- HDU 多校1.8
- ZOJ 3332 Strange Country II (竞赛图构造哈密顿通路)
链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3332 本文链接:http://www.cnblogs.com/Ash-l ...
- Centos7 下安装mysql
#prepare for install yum install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm ...
- mysql对表的操作
创建表 简单的方式 CREATE TABLE person ( number INT(11), name VARCHAR(255), birthday DATE ); 或者是 CREATE TABLE ...