How Many Answers Are Wrong----hdu3038(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038
题意:数组第 a 个元素到第 b 个元素之间的和为sum; 求有几句话是假的,如果与前面的话有冲突就为假; r[i]代表i的父节点到i的和;
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#define N 201000
#define INF 0xfffffff
using namespace std; int f[N],vis[N],r[N]; int Find(int x)
{
int k = f[x];
if(x != f[x])
{
f[x] = Find(f[x]);
r[x] += r[k];
}
return f[x];
}
int main()
{
int px, py, ans, i, n, m, x, y, sum;
while(scanf("%d%d", &n, &m)!=EOF)
{
for(i=;i<=n;i++)
f[i]=i,r[i]=;
ans = ;
while(m--)
{
scanf("%d%d%d", &x, &y, &sum);
x --;
px = Find(x);
py = Find(y);
if(px != py)
{
f[px] = py;
r[px] = r[y] -r[x] - sum;
}
else if(r[y]-r[x] != sum)
ans++;
}
printf("%d\n",ans);
}
return ;
}

上面的关系都是有方向的。。。
代码中的123处均与*处有关,大家可以画个图有助于理解;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
#define N 202100
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f int f[N], r[N]; int Find(int x)
{
int k = f[x];
if(x != f[x])
{
f[x] = Find(f[x]);
r[x] += r[k];///1;
}
return f[x];
} int main()
{
int n, m, x, y, sum; while(scanf("%d %d", &n, &m)!=EOF)
{
for(int i=; i<=n; i++)
f[i] = i, r[i] = ; int ans = ; while(m--)
{
scanf("%d %d %d", &x, &y, &sum);
x -- ; int px = Find(x);
int py = Find(y); if(px != py)
{
f[px] = py;///*
r[px] = r[y] - sum - r[x];///2;
}
else if(px == py && r[x]+sum != r[y])///
ans++;
}
printf("%d\n", ans);
}
return ;
}
How Many Answers Are Wrong----hdu3038(并查集)的更多相关文章
- 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 w ...
- *HDU3038 并查集
How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- How Many Answers Are Wrong(并查集)
Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he ...
- 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[ ...
- hdu 3038 How Many Answers Are Wrong(并查集)
题意: N和M.有N个数. M个回答:ai, bi, si.代表:sum(ai...bi)=si.如果这个回答和之前的冲突,则这个回答是假的. 问:M个回答中有几个是错误的. 思路: 如果知道sum( ...
- hdu3038 How Many Answers Are Wrong 种类并查集
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int ...
- [HDU3038]How Many Answers Are Wrong(并查集)
传送门 和某题类似,只不过奇偶换成了和. ——代码 #include <cstdio> #include <iostream> #define N 1000001 int n, ...
- hdu 3038 How Many Answers Are Wrong(并查集的思想利用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:就是给出n个数和依次m个问题,每个问题都是一个区间的和,然后问你这些问题中有几个有问题,有 ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
随机推荐
- mongo数据库查询结果不包括_id字段方法
db.GPRS_PRODUCT_HIS_FEE.find({"条件字段" : "412171211145135"},{_id:0}) db.GPRS_PRODU ...
- iOS开发--UILabel可以显示\n
UILabel*label; //设置换行 label.lineBreakMode = UILineBreakModeWordWrap; label.numberOfLines = ; 换行符还是“\ ...
- It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing ___Error Installing APK
一 : 根据以下路径,找到Instant Run中的选项 File —— Settings——Build,Execution,Deployment——Instant Run ...
- 日记整理---->2016-11-26
记录一些营销产品中的一些学习知识.我们在同一个时区,却有一辈子的时差. 一.关于mysql的注释问题 mysql的注释有以下三种,要注意是第二种的--后面至少要有一个空格. /*hello world ...
- 【Python系列】Python3获取控制台输入
""" 接收控制台的输入 How old are you? 18 How tall are you ? 180 How much do you weigh? 50 So ...
- 【Shell脚本编程系列】Shell脚本开发的习惯和规范
1.开头指定脚本解释器 #!/bin/sh或#!/bin/bash 2.开头加版本版权信息 #Date #Author #Mail #Function #Version 提示:可配置vim编辑文件时自 ...
- 【Spring源码分析系列】结构组成和容器的基本实现
beans包的层级结构 src/main/java:用于展现Spring的主要逻辑 src/main/resources:用于存放系统的配置文件 src/test/java:用于对主要逻辑单元进行测试 ...
- 【CSS系列】height:100%设置div的高度
一.div设置百分百高度实现描述 在html布局中body内第一个div盒子对象设置100%高度height样式,是无法成功显示100%高度的.这个是因为body高度默认值为自适应的,所以及时设置bo ...
- Linux 安装Ruby详解(在线和离线安装)
很多时候我们会发现,真实的生成环境很多都没有外网,只有内网环境,这个时候我们又需要安装Ruby,则不能提供yum命令进行在线安装了,这个时候我们就需要下载安装包进行离线安装.本文主要简单介绍如果离线安 ...
- [原]sublime Text2
sublime Text2 升级到 2.0.2 build 2221 64位 的破破解 sublime Text2 download website 链接: http://pan.baidu.com/ ...