这道题我拖了有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 带权并查集的更多相关文章

  1. hdu 3038带权并查集

    #include<stdio.h> #include<string.h> #define N  200100 struct node { int x,count; }pre[N ...

  2. hdu 1829 带权并查集的运用类似于食物链但是更简单些

    #include<stdio.h> #define N 1100000 struct node { int x,y; }f[N],pre[N]; int find(int x) { if( ...

  3. Zjnu Stadium HDU - 3047 带权并查集板子题

    #include<iostream> #include<cstring> #include<cstdio> using namespace std; +; int ...

  4. 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 ...

  5. HDU - 3038 How Many Answers Are Wrong (带权并查集)

    题意:n个数,m次询问,每次问区间a到b之间的和为s,问有几次冲突 思路:带权并查集的应用.[a, b]和为s,所以a-1与b就能够确定一次关系.通过计算与根的距离能够推断出询问的正确性 #inclu ...

  6. hdu 3038 How Many Answers Are Wrong【带权并查集】

    带权并查集,设f[x]为x的父亲,s[x]为sum[x]-sum[fx],路径压缩的时候记得改s #include<iostream> #include<cstdio> usi ...

  7. How Many Answers Are Wrong (HDU - 3038)(带权并查集)

    题目链接 并查集是用来对集合合并查询的一种数据结构,或者判断是不是一个集合,本题是给你一系列区间和,判断给出的区间中有几个是不合法的. 思考: 1.如何建立区间之间的联系 2.如何发现悖论 首先是如何 ...

  8. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  9. HDU 3047 Zjnu Stadium(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...

随机推荐

  1. 819. Most Common Word 统计高频词(暂未被禁止)

    [抄题]: Given a paragraph and a list of banned words, return the most frequent word that is not in the ...

  2. 面试题: java多线程 背1

    如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只有一个目的,那就是更好的利用cpu的资源,因为所有的多线程代码都可以用单线程来实现.说这个话其 ...

  3. ubuntu 设置虚拟机和主机在同一网段

    一.在VMware中将网络连接方式设置为桥接 1.打开VM菜单栏->Settings 2.在弹出的Virtual Machine Setting对话框中点击Network Adapter,在右边 ...

  4. 34.UCASE() LCASE() 函数

    UCASE() 函数 UCASE 函数把字段的值转换为大写. SQL UCASE() 语法 SELECT UCASE(column_name) FROM table_name SQL UCASE() ...

  5. python pipelines 用法

    http://zacstewart.com/2014/08/05/pipelines-of-featureunions-of-pipelines.html http://blog.csdn.net/m ...

  6. Cookie存中文乱码的问题

    有个奇怪的问题:登录页面中使用Cookie存值,Cookie中要存中文汉字.代码在本地调试,一切OK,汉字也能顺利存到Cookie和从Cookie中读出,但是放到服务器上不管用了,好好的汉字成了乱码, ...

  7. 数据结构 Merge合并排序

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. CentOS7虚拟机安装Linux教程及安装后所遇到的问题

    1.VMware Workstation15下载. 官方链接:http://download3.vmware.com/software/wkst/file/VMware-workstation-ful ...

  9. Mr_matcher的细节1

    1.NodeHandle类(或者NodeHandle句柄)的私有名称 1)句柄可以让你通过构造函数指定命名空间 ros::NodeHandle nh("my_namespace") ...

  10. Entity Framework 6.0 Tutorials(4):Database Command Logging

    Database Command Logging: In this section, you will learn how to log commands & queries sent to ...