题目链接: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(并查集)的更多相关文章

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

  2. *HDU3038 并查集

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  3. How Many Answers Are Wrong(并查集)

    Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he ...

  4. 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[ ...

  5. hdu 3038 How Many Answers Are Wrong(并查集)

    题意: N和M.有N个数. M个回答:ai, bi, si.代表:sum(ai...bi)=si.如果这个回答和之前的冲突,则这个回答是假的. 问:M个回答中有几个是错误的. 思路: 如果知道sum( ...

  6. hdu3038 How Many Answers Are Wrong 种类并查集

    #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int ...

  7. [HDU3038]How Many Answers Are Wrong(并查集)

    传送门 和某题类似,只不过奇偶换成了和. ——代码 #include <cstdio> #include <iostream> #define N 1000001 int n, ...

  8. hdu 3038 How Many Answers Are Wrong(并查集的思想利用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:就是给出n个数和依次m个问题,每个问题都是一个区间的和,然后问你这些问题中有几个有问题,有 ...

  9. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

随机推荐

  1. 【代码审计】iCMS_v7.0.7 keywords.admincp.php页面存在SQL注入漏洞分析

      0x00 环境准备 iCMS官网:https://www.icmsdev.com 网站源码版本:iCMS-v7.0.7 程序源码下载:https://www.icmsdev.com/downloa ...

  2. 系统头文件cmath,cstdlib报错

    >C:\Program Files (x86)\Microsoft Visual Studio\\Community\VC\Tools\MSVC\\include\cstdlib(): erro ...

  3. cannot access android.support.v4.app.BaseFragmentActivityJB的解决

    //implementation 'com.android.support:appcompat-v7:26.1.0' 改成implementation 'com.android.support:app ...

  4. Java的Integer和int有什么区别

    Java是面向对象的编程语言,一切都是对象,但是为了编程的方便还是引入了基本数据类型,为了能够将这些基本数据类型当成对象操作,Java为每一个基本数据类型都引入了对应的包装类型(wrapper cla ...

  5. UIWindow 详解及使用场景

    首先来看一下UIWindow 继承关系 方法和属性 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIWindow : UIView //window的屏幕,默认是 [ ...

  6. Material Design系列第四篇——Defining Shadows and Clipping Views

    Defining Shadows and Clipping Views This lesson teaches you to Assign Elevation to Your Views Custom ...

  7. 【cs229-Lecture7】支持向量机(SVM)

    SVM不错的学习资料: 百度网盘链接: http://pan.baidu.com/s/1hqw0Rnm 密码: asec blog:http://www.blogjava.net/zhenandaci ...

  8. thinkphp 多对多关联模型(转)

    先建立一个模型 1 2 3 4 5 6 7 8 9 10 11 12 <?php  class UserModel extends RelationModel{      protected $ ...

  9. Visual Studio 2013附加进程调试IE加载的ActiveX Control无效解决方法

    默认Attach to选择了Automatically determine the type of code to debug,显示Native Code.但附加进程到iexplore.exe断点无法 ...

  10. 去掉VS2010代码中文注释的红色下划线

    VS2010代码中文注释出现红色下划线,代码看上去很不美观,发现是由于安装Visual Assist X插件造成的. 解决办法:打开VAX的Options对话框,取消Advanced --> U ...