一、题面

HDU3038

二、分析

用并查集可以方便的判断两个位置是否有关系,这种关系可以通过是否有公共父节点判断,如果有公共父节点则可以直接判断是否正确,如果没有公共父节点,就可以把这个条件与之前的联系起来。同时需要设定sum,表示当前点到父节点的权值,这个权值方便后面的判断,这里有几种情况。

假设输入为$(x,y,w)$,那么对应父节点$fx = find(x), fy = find(y)$:

1.如果$fx==fy$:那么可以直接判断$w == sum[x] - sum[y]$.

2.如果$ x < y < fx < fy$:这里需要更新并查集的信息,$par[fx] = fy, sum[fx] = sum[y] - (sum[x] - w)$.

3.如果$ x < fx < y < fy$:此时,$par[fx] = fy, sum[fx] = sum[y] + (w - sum[x])$.

4.如果$ x < y < fy < fx$:此时,$par[fy] = fx, sum[fy] = sum[x] - sum[y] - w$.

分析上面四种情况,可以得出2和3可以合并,对于4,仔细分析一下, 相当于就是3的情况,只是因为$par[fy] = fx$,只是这样可以保证$sum$的值为正。注意在路径压缩时,对$sum$进行更新就可以了。

细心的朋友可能发现了,还有$x = y$的情况呢。对于这种情况,我们可以直接对输入的左值整体再往左移一下,就是输入的$u$变成$u-1$,这个对结果是没有影响的。但是如果不减,我们就要考虑$x=y$的情况了,前面我们已经初始化了$sum$初值为0,并且$x=y$的左右两边的情况我们是不能保证完全清楚的。所以这样把$x=y$变成了$(x-1,y]$这个区间的形式,就让问题又归到了我们上面讨论的情况,更简单了。

三、AC代码

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <fstream>
#include <map> using namespace std; const int MAXN = 2e5+;
int par[MAXN];
int sum[MAXN]; void Init()
{
for(int i = ; i < MAXN; i++)
par[i] = i;
memset(sum, , sizeof(sum));
} int Find(int x)
{
if(par[x] == x)
return x;
int fx = par[x];
par[x] = Find(fx);
sum[x] = sum[fx] + sum[x];
return par[x];
} bool Union(int x, int y, int w)
{
int fx = Find(x);
int fy = Find(y);
// if(fx != fy)
// {
// par[fx] = fy;
// sum[fx] = w + sum[y] - sum[x];
// return true;
// }
// else
// {
// return w == sum[x] - sum[y];
// }
if(fx < fy)
{
par[fx] = fy;
sum[fx] = w + sum[y] - sum[x];
return true;
}
else if(fx > fy)
{
par[fy] = fx;
sum[fy] = sum[x] - w - sum[y];
return true;
}
else
{
return w == sum[x] - sum[y];
} } int main()
{
// freopen("input.txt", "r", stdin);
int N, M;
while(scanf("%d %d", &N, &M)!=EOF)
{
Init();
int a, b, w, ans = ;
for(int i = ; i < M; i++)
{
scanf("%d %d %d", &a, &b, &w);
if(!Union(a-, b, w))
ans++;
}
printf("%d\n", ans);
}
return ;
}

HDU_3038 How Many Answers Are Wrong 【带权并查集】的更多相关文章

  1. HDU3038 How Many Answers Are Wrong —— 带权并查集

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 200 ...

  2. hdu3038How Many Answers Are Wrong(带权并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题解转载自:https://www.cnblogs.com/liyinggang/p/53270 ...

  3. HDU3038 How Many Answers Are Wrong[带权并查集]

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  4. 【HDU3038】How Many Answers Are Wrong - 带权并查集

    描述 TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is always ...

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

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  6. How Many Answers Are Wrong(带权并查集)

    How Many Answers Are Wrong http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS ( ...

  7. HDU3038:How Many Answers Are Wrong(带权并查集)

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

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

    太坑人了啊,读入数据a,b,s的时候,我刚开始s用的%lld,给我WA. 实在找不到错误啊,后来不知怎么地突然有个想法,改成%I64d,竟然AC了 思路:我建立一个sum数组,设i的父亲为fa,sum ...

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

  10. 【带权并查集】【HDU3038】【How Many Answers Are Wrong】d s

    这个题看了2天!!!最后看到这篇题解才有所明悟 转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html   ---by 墨染之樱 ...

随机推荐

  1. C# ShowDialog时窗体贱传值得方法

    用C#开发应用的时候,通常需要多个窗体!有时候为了打开窗体的时候禁止操作父窗体,我们一般采用模态对话框的方法,也算就是用ShowDialog()方法. 假设有两个窗体A和B,A作为主窗体使用ShowD ...

  2. 824. Goat Latin山羊拉丁文

    [抄题]: A sentence S is given, composed of words separated by spaces. Each word consists of lowercase ...

  3. struts2 与 spring 整合

    1. 首先把所有jar包导入工程 2.在struts2的核心配置文件(在src文件目录下)中添加如下配置: <!-- 将Struts的对象交给Spring管理 所以需要导入Spring和Stru ...

  4. R list frame, matrix

    list是个筐,什么都可以往里装,包括它自身.数据框是个二维数组,同列必须同类型,行随意.

  5. Django框架 之 数据库与ORM

    浏览目录 数据库的配置 ORM表模型 ORM之增(create,save) ORM之删(delete) ORM之改(update和save) ORM之查(filter,value) 一.数据库的配置 ...

  6. Swing滚动条重写

    Swing滚动条重写 摘自:https://blog.csdn.net/qq_40064948/article/details/81738191 未验证 Swing滚动条重写 2018年08月16日 ...

  7. 实践作业3:白盒测试----小组分工讨论DAY2

    白盒测试需要通过检查软件内部的逻辑结构,对软件中的逻辑路径进行覆盖测试;在程序不同地方设立检查点,检查程序的状态,以确定实际运行状态与预期状态是否一致.我们小组在下课时候,在东九教学楼教师休息室进行了 ...

  8. java IO 对象流 反序列化和序列化

    例: 重点:需要序列化的对象必须实现Serializable接口 //需要序列化的对象 public class User implements Serializable { private Stri ...

  9. 日期多选插件Kalendae.js

    在项目中要实现日期多选的功能,于是在网上找到Kalendae.js,此文主要记录本人对于Kalendae.js的一些用法,以便以后查阅,希望对读者也有所帮助 主要内容如下: Kalendaejs一句话 ...

  10. LibreOJ 6002 最小路径覆盖(最大流)

    题解:最小路径覆盖=总点数减去最大匹配数,拆点,按照每条边前一个点连源点,后一个点连汇点跑最大流,即可跑出最大匹配数,然后减一减就可以了~ 代码如下: #include<queue> #i ...