带权并查集 - 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]的区间和为 ...
随机推荐
- Redis搭建(五):Cluster集群搭建
一.方案 1. 介绍 redis3.0及以上版本实现,集群中至少应该有奇数个节点,所以至少有三个节点,官方推荐三主三从的配置方式 使用哈希槽的概念,Redis 集群有16384个哈希槽,每个key通过 ...
- zookeeper集群安装的奇怪现象
zookeeper:配置的集群信息是domain:端口2888:端口3888: domain为内网静态ip:每次启动都不能相互连接报错误: [myid:3] - WARN [WorkerSende ...
- Linux Resin4.0 安装配置
Resin,是一个非常流行的application server,对servlet和JSP提供了良好的支持,性能优良,resin自身采用Java语言开发.Resin Pro版本支持缓存和负载均衡,收费 ...
- LoadRunner 脚本开发和常用函数
开发性能测试脚本原则.简单 正确 高效 在lr中,脚本运行是解释执行的.所以在运行时,需要先编译. 局部变量和全局变量 1.在init.action.end中定义的变量就是局部变量 2.在global ...
- ubuntu 下正确安装android手机驱动
1. 查看手机ID号. charlesxue@THSHIBA:~/setup/cocos2d-x/cocos2d-x-/projects/simpleGame/proj.android/bin$ ls ...
- 453. Minimum Moves to Equal Array Elements 一次改2个数,变成统一的
[抄题]: Given a non-empty integer array of size n, find the minimum number of moves required to make a ...
- 什么是Kali Linux?
什么是Kali Linux? Kali Linux是一个基于Debian的Linux发行版,旨在实现高级渗透测试和安全审计.Kali包含数百种工具,适用于各种信息安全任务,如渗透测试,安全研究,计算机 ...
- Java Http 请求
package zr.weixin.com.utils; import java.io.BufferedReader; import java.io.IOException; import java. ...
- Python 安装selenium
一.报错信息 No module named 'selenium' 二.系统环境 操作系统:Win10 64位 Python版本:Python 3.7.0 三.安装参考 1.使用pip安装seleni ...
- Bean管理注解的例子