带权并查集 - How Many Answers Are Wrong
思路: 带权并查集+向量偏移
#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的更多相关文章
- 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 ...
- HDU3038 How Many Answers Are Wrong —— 带权并查集
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 200 ...
- 【带权并查集】【HDU3038】【How Many Answers Are Wrong】d s
这个题看了2天!!!最后看到这篇题解才有所明悟 转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html ---by 墨染之樱 ...
- hdu3038How Many Answers Are Wrong(带权并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题解转载自:https://www.cnblogs.com/liyinggang/p/53270 ...
- 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(带权并查集)
题目 带权并查集的博客~ 题目: 多组输入数据.n,m.你不知道[1,n]内任意区间内值的和. m次询问,a b 是端点,都在n的范围以内 : v表示 [a,b]的区间内值的和.对每次询问,判断v是否 ...
- How Many Answers Are Wrong (HDU - 3038)(带权并查集)
题目链接 并查集是用来对集合合并查询的一种数据结构,或者判断是不是一个集合,本题是给你一系列区间和,判断给出的区间中有几个是不合法的. 思考: 1.如何建立区间之间的联系 2.如何发现悖论 首先是如何 ...
- HDU-3038 How Many Answers Are Wrong(带权并查集区间合并)
http://acm.hdu.edu.cn/showproblem.php?pid=3038 大致题意: 有一个区间[0,n],然后会给出你m个区间和,每次给出a,b,v,表示区间[a,b]的区间和为 ...
随机推荐
- 如何POST一个JSON格式的数据给java接口,获得返回数据
/** * 模拟post进行url请求 * @param string $url * @param json $post_data */ public function request_post($u ...
- Unity3D Asset 导入&导出
[Unity3D Asset 导入&导出] 通过Assets->Export Package..菜单可以导出当前选中Assets.若没有选中Assets,则会导出全部assets. 在弹 ...
- linux单用户模式修改密码
Linux使用版本: Centos 7 救援模式: 1,在虚拟机设置里查看光驱是否开启启动,要保证设置状态里的两个选项都已选择. 2,先将Centos系统关机,然后在VMware左侧选中这台虚拟机并右 ...
- 【bzoj1018】[SHOI2008]堵塞的交通traffic
1018: [SHOI2008]堵塞的交通traffic Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 2887 Solved: 954[Submit ...
- spring框架 事务 xml配置方式
user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.Ora ...
- vmware 安装不成功导致的问题解决以及右键菜单添加打开终端命令
转自http://blog.csdn.net/puweilan/article/details/8609952 在VMware安装Ubuntu完成后,一直停留在VMware Easy Install, ...
- SQLAlchemy(ORM框架)
SQLAlchemy SQLAlchemy概述 2 3 4 5 6 7 8 9 10 11 12 13 MySQL-Python mysql+mysqldb://<user>:&l ...
- IDEA小技巧:添加代码快捷方式
非常怀恋eclipse的的代码快捷方式tryc,今天给IDEA也添加了一个
- javascript总结13:循环语句
1 While循环 While(条件表达式){ 只要条件表达式结果为true,循环一直执行,当条件表达式结果为false的时候,循环终止 } While循环语句需现在循环体外定义变量. 2 for循环 ...
- wpf 依赖属性注册解释
这个解释的很明白了 http://www.cnblogs.com/xiongpq/archive/2010/06/29/1767905.html