带权并查集 - How Many Answers Are Wrong
思路: 带权并查集+向量偏移
#include <iostream>
using namespace std;
int n, m;
int pre[];
int f[]; // 到根节点的距离
int ans = ; void init()
{
for (int i = ; i <= n; i++) {
pre[i] = i;
f[i] = ;
}
} int Find(int x)
{
if (x == pre[x])
return x;
int y = pre[x];
pre[x] = Find(pre[x]);
f[x] += f[y];
return pre[x];
} void Union(int x, int y, int s)
{
int fx = Find(x);
int fy = Find(y);
if (fx != fy) {
pre[fx] = fy;
f[fx] = f[y] + s - f[x];
}
else if (f[x] - s != f[y])
ans++;
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
while (cin >> n >> m) {
ans = ;
init();
for (int i = ; i < m; i++) {
int a, b, s;
cin >> a >> b >> s;
Union(a - , b, s);
}
cout << ans << endl;
}
return ;
}
带权并查集 - How Many Answers Are Wrong的更多相关文章
- 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 ...
- HDU3038 How Many Answers Are Wrong —— 带权并查集
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 200 ...
- 【带权并查集】【HDU3038】【How Many Answers Are Wrong】d s
这个题看了2天!!!最后看到这篇题解才有所明悟 转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html ---by 墨染之樱 ...
- hdu3038How Many Answers Are Wrong(带权并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题解转载自:https://www.cnblogs.com/liyinggang/p/53270 ...
- HDU - 3038 How Many Answers Are Wrong (带权并查集)
题意:n个数,m次询问,每次问区间a到b之间的和为s,问有几次冲突 思路:带权并查集的应用.[a, b]和为s,所以a-1与b就能够确定一次关系.通过计算与根的距离能够推断出询问的正确性 #inclu ...
- hdu 3038 How Many Answers Are Wrong【带权并查集】
带权并查集,设f[x]为x的父亲,s[x]为sum[x]-sum[fx],路径压缩的时候记得改s #include<iostream> #include<cstdio> usi ...
- How Many Answers Are Wrong(带权并查集)
题目 带权并查集的博客~ 题目: 多组输入数据.n,m.你不知道[1,n]内任意区间内值的和. m次询问,a b 是端点,都在n的范围以内 : v表示 [a,b]的区间内值的和.对每次询问,判断v是否 ...
- How Many Answers Are Wrong (HDU - 3038)(带权并查集)
题目链接 并查集是用来对集合合并查询的一种数据结构,或者判断是不是一个集合,本题是给你一系列区间和,判断给出的区间中有几个是不合法的. 思考: 1.如何建立区间之间的联系 2.如何发现悖论 首先是如何 ...
- HDU-3038 How Many Answers Are Wrong(带权并查集区间合并)
http://acm.hdu.edu.cn/showproblem.php?pid=3038 大致题意: 有一个区间[0,n],然后会给出你m个区间和,每次给出a,b,v,表示区间[a,b]的区间和为 ...
随机推荐
- Animation组件
[Animation组件] Animation是Unity3D中老的动画组件,从4.x起已全面被MecAnim中的Animator组建所替代.但是4.x仍保留了Animation组件,所以了解此组件还 ...
- SpringMVC简单的文件上传
引入依赖包: <!-- 文件上传的依赖 --> <dependency> <groupId>commons-fileupload</groupId> & ...
- 深入剖析SolrCloud(一)
作者:洞庭散人 出处:http://phinecos.cnblogs.com/ 本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由 ...
- libevent源码分析
这两天没事,看了一下Memcached和libevent的源码,做个小总结. 1.入门 1.1.概述Libevent是一个用于开发可扩展性网络服务器的基于事件驱动(event-driven)模型的网络 ...
- 配置SecureCRT连接VirtualBox虚拟机中的Linux环境
在实际的运维中我们常常使用SecuriteCRT来远程控制Linux服务器.下面将详细介绍windows 7下通过VirtualBox搭建linux开发环境,并最终通过SecurityCRT来远程访问 ...
- 507. Perfect Number 因数求和
[抄题]: We define the Perfect Number is a positive integer that is equal to the sum of all its positiv ...
- “Device eth0 has different MAC address than expected, ignoring.”问题
配IP后进行激活的时候提示如下错误:("Device eth0 has different MAC address than expected, ignoring.") 百度了下, ...
- 5-有道爬虫demo(post)
爬取有道页面,实现中文翻译成英文: #_*_ coding: utf-8 _*_ ''' Created on 2018-7-12 @author: sss 功能:爬取有道翻译 ''' import ...
- Service和IntentService的区别
不知道大家有没有和我一样,以前做项目或者练习的时候一直都是用Service来处理后台耗时操作,却很少注意到还有个IntentService,前段时间准备面试的时候看到了一篇关于IntentServic ...
- canvas时钟demo
显示效果如下 源码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...