kuangbin_UnionFind D (HDU 3038)
加权并查集 似乎就是在想这题的时候突然理解了之前看E题没看懂的标准加权解法
值得注意的技巧 为了让区间之前连成树 形式设定为为(l, r] 接受l的输入后先自减一下就可以了
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std; int father[],sum[];
int n,m,ans;
int find(int x)
{
if(father[x] != x) {
int tmp = father[x];
father[x] = find(father[x]);
sum[x] += sum[tmp];
}
return father[x];
}
void merge(int x,int y,int s)
{
int tx = find(x);
int ty = find(y);
if(tx == ty) {
if(sum[x] - sum[y] != s)
{
ans++;
//printf("tx = %d;ty = %d;\n", tx, ty);
}
return;
}
else
{
father[tx] = ty;
sum[tx] = sum[y] - sum[x] + s;
}
}
int main()
{
while(~scanf("%d%d", &n,&m))
{
for(int i = ; i <= n; ++i)
{
father[i] = i;
sum[i] = ;
}
//printf("father(10) = %d\n",father[10]);
//printf("find(10) = %d\n",find(10));
ans = ;
while(m--)
{
int x, y, s;
scanf("%d%d%d", &x, &y, &s);
x--;
merge(x, y, s);
}
printf("%d\n",ans);
}
return ;
}
kuangbin_UnionFind D (HDU 3038)的更多相关文章
- 类似区间计数的种类并查集两题--HDU 3038 & POJ 1733
1.POJ 1733 Parity game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5744 Accepted: ...
- HDU 3038
http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:[1-n]的区间,有m个询问,每个询问表示[a,b]的和是s,问一共有多少组矛盾 sum[i]表示i ...
- hdu 3038 How Many Answers Are Wrong
http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 MS ( ...
- hdu 3038(扩展并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:给出区间[1,n],下面有m组数据,l r v区间[l,r]之和为v,每输入一组数据,判断 ...
- 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 ...
- (并查集 添加关系)How Many Answers Are Wrong --Hdu --3038
链接: http://acm.hdu.edu.cn/showproblem.php?pid=3038 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 3038 How Many Answers Are Wrong 【YY && 带权并查集】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 2000/1000 ...
- hdu 3038 How Many Answers Are Wrong(并查集的思想利用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:就是给出n个数和依次m个问题,每个问题都是一个区间的和,然后问你这些问题中有几个有问题,有 ...
- HDU 3038 How Many Answers Are Wrong(种类并查集)
题目链接 食物链类似的题,主要是在于转化,a-b的和为s,转换为b比a-1大s.然后并查集存 此节点到根的差. 假如x的根为a,y的根为b: b - y = rank[y] a - x = rank[ ...
随机推荐
- jQuery 自定义扩展,与$冲突处理
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- declare 关键字在Oracle中的应用。
一般用在trigger或匿名存储过程中使用.如 declare a number;begina:=1;end;
- iOS App上架流程(2016详细版
http://www.jianshu.com/p/b1b77d804254 iOS App上传项目遇到的问题 http://www.jianshu.com/p/9195cd991fc7
- Oracle 获取用户表的字段定义
获取用户表列表: select * from user_tables; select * from all_tables; select * from dba_tables; 获取表的字段: sele ...
- 配置VS2010具有代码提示功能
Visual Assist X是一款非常好的Microsoft Visual Studio插件,可以支持Microsoft Visual Studio 2003,Microsoft Visual St ...
- 黑马程序员——【Java基础】——面向对象(二)异常机制、包(Package)
---------- android培训.java培训.期待与您交流! ---------- 一.异常机制 (一)异常概述 1.异常:就是程序在运行时出现不正常情况. 2.异常类:程序在运行时,出现的 ...
- Git ~ 回到过去 , 进入未来 ~ Git
概述 我们已经成功的添加了一个 readme.txt文件 , 现在是时候 继续工作了 , 于是 我们开始尝试一下 Git给我们所带来的便利下面修改read.txt 改成如下内容 为了尝试 Git 给我 ...
- 用python做些有意思的事——分析QQ聊天记录——私人订制
之前,写了这篇文章,用python提取全部群成员的发言时间,并简单做了下分析.先补充一下,针对特定单个群成员(这里以 小小白 为例)消息记录的获取. 代码比较简单,主要是正则表达式的书写.(附: ...
- 【LeetCode】Best Time to Buy and Sell Stock IV
Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...
- java作业4
(一) 请查看String.equals()方法的实现代码,注意学习其实现方法.(发表到博客作业上) (二) 整理String类的Length().charAt(). getChars().rep ...