HDU - 3038 带权并查集
这道题我拖了有8个月...
今天放假拉出来研究一下带权的正确性,还有半开半闭的处理还有ab指向的一系列细节问题
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 200010;
int p[maxn],r[maxn];
void init(int n){
memset(r,0,sizeof r);
for(int i=0;i<=n+2;i++)p[i]=i;
}
int find(int x){
if(x==p[x]) return x;
int oldp=p[x];
int t=find(p[x]);
r[x]+=r[oldp];
return p[x]=t;
}
void link(int fa,int fb,int a,int b,int c){
p[fb]=fa;
r[fb]=r[a]-r[b]+c;
}
int main(){
int n,m;
while(cin>>n>>m){
init(n);
int ans=0;
for(int i=1;i<=m;i++){
int a,b,c; scanf("%d%d%d",&a,&b,&c);
if(a>b)swap(a,b); a--;//反例: 仅一边 4 3 100 WA
int fa=find(a),fb=find(b);
if(fa==fb){
if(r[b]-r[a]!=c)ans++;
}else{
link(fa,fb,a,b,c);
}
}
// for(int i=1;i<=12;i++) cout<<"p["<<i<<"]="<<p[i]<<" "<<"r["<<i<<"]="<<r[i]<<endl;
printf("%d\n",ans);
}
return 0;
}
/*样例
100 3
1 3 24
5 10 30
3 10 60
*/
//如果父节点比子节点id大,那子节点的秩为负值也是没关系的(要是正就说明中间和为负值)
//4 10 30
//2 10 60 子节点比父节点大,正ok
//4 10 30
//5 10 60 子节点比父节点小,负ok
//另外有一点是b总是指向a(前面看出了ab总是单调的),所以用矢量求r[fb]总是利用r[a]+w=r[b]+r[fb] (不是-w)
//关于半闭半开还没有很好的领会(除了相等ab的合法处理以外),可能所有式子存在偏移那就不会影响整体的正确性?(只要确保a<=b的前提下使用),
//有点小疑问是这样会不会影响单一区间的正确结果?没有可视化做数据来测试真痛苦不做了
HDU - 3038 带权并查集的更多相关文章
- hdu 3038带权并查集
#include<stdio.h> #include<string.h> #define N 200100 struct node { int x,count; }pre[N ...
- 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 ...
- How Many Answers Are Wrong (HDU - 3038)(带权并查集)
题目链接 并查集是用来对集合合并查询的一种数据结构,或者判断是不是一个集合,本题是给你一系列区间和,判断给出的区间中有几个是不合法的. 思考: 1.如何建立区间之间的联系 2.如何发现悖论 首先是如何 ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- HDU 3047 Zjnu Stadium(带权并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...
随机推荐
- 455. Assign Cookies 满足欲望 分配饼干
[抄题]: Assume you are an awesome parent and want to give your children some cookies. But, you should ...
- 10.Python之Ansible自动化运维常用模块
Ansible中文权威文档:http://www.ansible.com.cn/docs/ Ansible从入门到精通:https://www.bilibili.com/video/av3361175 ...
- Sql Server - CURSOR (游标)
1.声明游标 DECLARE 游标名 CURSOR SELECT语句(注:此处一定是SELECT语句) 2.打开游标 OPEN 游标名 3.读取 ...
- 完美解决bootstrap模态框允许拖动后拖出边界的问题
使用bootstrap3版本 在网上看了很多方法,我觉得jquery-ui的实现方法是最简单有效的,具体实现方法 1.下载并引入jquery-ui插件 2.全局添加模态框允许拖动事件 $(docume ...
- ubuntu 下python安装及hello world
//@desn:ubuntu 下python安装及hello world //@desn:码字不宜,转载请注明出处 //@author:张慧源 <turing_zhy@163.com> ...
- 基于CentOS6定制自己的ISO安装光盘
警告:转载请注明出处 https://www.cnblogs.com/BoyTNT/p/9322927.html 1.目标 >> 基于CentOS-6.10-x86_64-minimal ...
- C#帮助类:MD5加密
/// <summary> /// MD5加密 /// </summary> public class Md5 { /// <summary> /// MD5加密 ...
- 数组中 reduce累计运算
let arr = [1,2,3,4]; let sum = (a, b) => a + b; arr.reduce(sum, 0); 最后输出10
- 扩展卢卡斯定理(Exlucas)
题目链接 戳我 前置知识 中国剩余定理(crt)或扩展中国剩余定理(excrt) 乘法逆元 组合数的基本运用 扩展欧几里得(exgcd) 说实话Lucas真的和这个没有什么太大的关系,但是Lucas还 ...
- 如何设置linux支持上传的文件中文不乱吗
一.背景: 1.由于客户的需求,需要a链接打开的pdf文件,支持中文名称的 二.步骤 ①.查看当前编码 locale ②.编辑 vi /etc/profile 打开后结尾处添加 export LA ...