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[ ...
随机推荐
- RPI学习--环境搭建_刷卡+wiringPi库安装
1,镜像地址 http://www.raspberrypi.org/downloads/ 2,Windows下刷写工具 Win32 Disk Imager 3,安装wiringPi库 (这里在连网状态 ...
- flash 和 第三方程序交互
一.flash 端 修改 1.flash cs6 修改脚本 为as3 2.修改 按钮 实例 名 (不是sprite列表中的名字 实例名称 和这个名字 是2个) 3.时间轴上 添加代码 当前选择 A ...
- 设置隐藏activity的标题栏
values->styles.xml中设置:stylename="AppTheme"parent="Theme.AppCompat.Light.NoActionBa ...
- T-sql语句
在用代码编辑数据库时,首先要启动WAMPSERVER 1.创建表 create table Car ( Code varchar(50) primary key , Name varchar(50) ...
- JS获取客户端Mac和IP
JS获取硬件信息是通过ActiveX进行获取的,因此只能IE浏览器支持,火狐不支持 而且必须降低浏览器安全级别,因此不到万不得以一般不会采用这种方式 <html> <head> ...
- JS中关于 一个关于计时器功能效果的实现
optionSearch(); function optionSearch() { //定义一个清除计时器的变量 var timer = null; //自选标题区域 $("#optiona ...
- 关于VS2015找不到WIN32的解决办法
问题: 原配的Visual Studio 2015专业版不像之前的版本,在新建项目里面是找不到Win32模板的,那么怎么才能新建Win32项目和Win32控制台应用程序呢?先看如下图所示: 解决: 1 ...
- iOS log第三方库之间的对比
- Redis - 介绍及安装
Redis属于key-value数据库,与传统的数据库存在很大区别,Redis以命令的方式代替了复杂的SQL语句,并且属于内存库性质,所以运行速度非常快.内存数据会生成数据库文件保证数据持久化. Re ...
- CodeForces 414D (贪心)
problem Mashmokh and Water Tanks 题目大意 给你一棵树,k升水,p块钱,进行一次游戏. 在游戏进行前,可以在任意个节点上放置1升水(总数不超过k) 游戏进行若干轮,每轮 ...