How Many Answers Are Wrong (HDU - 3038)(带权并查集)
并查集是用来对集合合并查询的一种数据结构,或者判断是不是一个集合,本题是给你一系列区间和,判断给出的区间中有几个是不合法的。
思考:
1.如何建立区间之间的联系
2.如何发现悖论
首先是如何建立联系,我们可以用一张图表示
假如说区间【fx,x】是之前建立的区间,他们之间和为sum[x],fx和x的联系可以用集合来存储,同理【fy,y】也是如此。当给出了一个新的区间【x,y】时,且区间和为s。
就产生了两种情况了,如果fx == fy 那么这两个区间是有关联的区间,也就是【x,y】之间的和是可以求出的。可以把这个图看成一个向量。
区间【x,y】的和就是可以写成sum[x] - sum[y]。
判断给出的s与向量法计算的区间和是否相等就可以判断是否是悖论。
当然如果fx != fy就需要建议新的区间关系。首先将fy指向fx,这代表fx是区间的左端点,计算sum【fy】= sum【x】- sum【y】+ s;这里同样用的是向量法。
这样建立联系与判断悖论都可以表达了,接下来就是一些细节了,比如在更新区间的时候要进行路径的压缩,压缩的过程中需要对权值进行更新,目的是使每个已知区间最大化。
解题思路:这个题乍一看可能要线段树或树状数组,其实没有必要,区间和可以理解为前缀和相减。
每个节点记录前缀和,对每个询问先判断两个节点是否连通,这便是带权并查集干的事了,若联通则权值相减看是否为给定值,若不为则矛盾;若不联通则两棵树合并为一棵(注意有顺序)同时计算出新的子节点的权值。
AC代码
#include<bits/stdc++.h>
using namespace std; const int maxn = + ; int pre[maxn] ,sum[maxn];//sum是该节点到其根的和,如sum【3】
//,3的根是1,就是1到3的和 int find(int x){
if(x == pre[x]) return x;
else { int root = find(pre[x]);
sum[x] += sum[pre[x]];
return pre[x] = root;
}
} void init(int n){
for(int i = ; i <= n;i++){
pre[i] = i;
sum[i] = ;
}
}
int ans = ;
void unions(int x,int y,int s){
int fx = find(x);
int fy = find(y);
if(fx != fy){
pre[fy] = fx;
sum[fy] = sum[x] - sum[y] + s;
}
else if(sum[y] - sum[x] != s)
ans ++; }
int main(){
int n,m;
//cin >> n >> m;
while(cin >> n >> m){
init(n);
int x,y;
int s;
ans = ;
while(m--){
cin >> x >> y >> s;
x --;
unions(x,y,s);
} cout << ans << endl;
} return ;
}
How Many Answers Are Wrong (HDU - 3038)(带权并查集)的更多相关文章
- HDU - 3038 带权并查集
这道题我拖了有8个月... 今天放假拉出来研究一下带权的正确性,还有半开半闭的处理还有ab指向的一系列细节问题 #include<iostream> #include<algorit ...
- hdu 3038带权并查集
#include<stdio.h> #include<string.h> #define N 200100 struct node { int x,count; }pre[N ...
- HDU 3038 How Many Answers Are Wrong 【YY && 带权并查集】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 ...
- hdu 1829 带权并查集的运用类似于食物链但是更简单些
#include<stdio.h> #define N 1100000 struct node { int x,y; }f[N],pre[N]; int find(int x) { if( ...
- Zjnu Stadium HDU - 3047 带权并查集板子题
#include<iostream> #include<cstring> #include<cstdio> using namespace std; +; int ...
- 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 ...
- 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 ...
- HDU3038 How Many Answers Are Wrong —— 带权并查集
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 200 ...
随机推荐
- openstack stein部署手册 7. nova-compute
# 安装程序包 yum install -y openstack-nova-compute # 变更配置文件 cd /etc/nova mv nova.conf nova.conf.org cat & ...
- tpcc-mysql测试mysql5.6 (EXT4文件系统)
操作系统版本:CentOS release 6.5 (Final) 2.6.32-431.el6.x86_64 #1 内存:32G CPU:Intel(R) Xeon(R) CPU E5-2450 ...
- springboot 集成rabbitMQ
package com.jd.ng.shiro.config.rabbitMQconfig; import com.jd.ng.shiro.rabbitMqListener.SimpleMessage ...
- 【leetcode】1090. Largest Values From Labels
题目如下: We have a set of items: the i-th item has value values[i] and label labels[i]. Then, we choose ...
- java.nio.channels.IllegalBlockingModeException
报错信息如下: Exception in thread "main" java.nio.channels.IllegalBlockingModeException at java. ...
- JVM虚拟机运行机制
JVM虚拟机运行机制 什么是JVM?虚拟机是物理机器的软件实现.Java是用在VM上运行的WORA(Write Once Run Anywhere)概念而开发的.编译器将Java文件编译为Java . ...
- 使用随机森林实现OSM路网城市多车道信息提取
Multilane roads extracted from the OpenStreetMap urban road network using random forests.,DOI:10.111 ...
- 20180813-Java 重写(Override)与重载(Overload)
Java 重写(Override)与重载(Overload) class Animal{ public void move(){ System.out.println("动物可以移动&quo ...
- 学习日记12、list集合中根据某个字段进行去重复操作
List<T_CusBankCardInfoModel> blist = B_BLL.GetListByCusId(CusIds).Distinct(new ModelComparer() ...
- pytest相关问题解析
1. 如果你想查询在你的环境下有哪些pytest的active plugin可以使用: py.test --traceconfig 会得到一个扩展的头文件名显示激活的插件和他们的名字.同时也会打印出当 ...