思路: 带权并查集+向量偏移

 #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的更多相关文章

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

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

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

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

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

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

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

  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(带权并查集)

    题目 带权并查集的博客~ 题目: 多组输入数据.n,m.你不知道[1,n]内任意区间内值的和. m次询问,a b 是端点,都在n的范围以内 : v表示 [a,b]的区间内值的和.对每次询问,判断v是否 ...

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

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

  9. HDU-3038 How Many Answers Are Wrong(带权并查集区间合并)

    http://acm.hdu.edu.cn/showproblem.php?pid=3038 大致题意: 有一个区间[0,n],然后会给出你m个区间和,每次给出a,b,v,表示区间[a,b]的区间和为 ...

随机推荐

  1. Tornado抽象方法抽象类

    #!/usr/bin/env python #抽象方法抽象类 import abc class Foo(metaclass=abc.ABCMeta): def f1(self): raise Exce ...

  2. 浏览器get请求到java后台的值是乱码

     get方式提交的参数编码,只支持iso8859-1编码. 因此,如果里面有中文,在后台就需要转换编码,如下 String zhongwen = request.getParameter(" ...

  3. 新建tomcat的server服务,在左侧项目浏览处,右键空白的地方,选择new,再选择other选项

    新建tomcat的server服务,在左侧项目浏览处,右键空白的地方,选择new,再选择other选项. 在弹出的窗口中,下拉滚动条找到Server,并单击next按钮. 在弹出的窗口中,找到第一个A ...

  4. 459. Repeated Substring Pattern 判断数组是否由重复单元构成

    [抄题]: Given a non-empty string check if it can be constructed by taking a substring of it and append ...

  5. 面试题:HashMap和ConcurrentHashMap的区别,HashMap的底层源码。

    Hashmap本质是数组加链表.根据key取得hash值,然后计算出数组下标,如果多个key对应到同一个下标,就用链表串起来,新插入的在前面. ConcurrentHashMap:在hashMap的基 ...

  6. HTTP请求:POST和GET的差异

    1,一般情况下应用目的不同:GET是从服务器上获取数据,POST是向服务器传送数据. 2,将数据提交到服务器的方式不同:GET是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各 ...

  7. 正确设置-Dfile.encoding参数

    正确设置-Dfile.encoding参数 摘自:https://blog.csdn.net/youge/article/details/6178265 2011年02月11日 10:18:00 阅读 ...

  8. 认识Filter

    1). Filter 是什么 ? ①. JavaWEB 的一个重要组件, 可以对发送到 Servlet 的请求进行拦截, 并对响应也进行拦截. ②. Filter 是实现了 Filter 接口的 Ja ...

  9. python 字符串,元组, 列表,字典之间的转换

    #-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...

  10. 【Azure Active Directory】单一登录 (SAML 协议)

    Azure Active Directory 支持 SAML 2.0 Web 浏览器单一登录 (SSO) 配置文件. 若要请求 Azure Active Directory 对用户进行身份验证时,云服 ...