HDU 3038 How Many Answers Are Wrong 扩展并查集

http://acm.hdu.edu.cn/showproblem.php?pid=3038

TT and FF are ... friends. Uh... very very good friends -________-b

FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored).


Then, FF can choose a continuous subsequence from it(for example the subsequence from the third to the fifth integer inclusively). After that, FF will ask TT what the sum of the subsequence he chose is. The next, TT will answer FF's question. Then, FF can redo this process. In the end, FF must work out the entire sequence of integers.

Boring~~Boring~~a very very boring game!!! TT doesn't want to play with FF at all. To punish FF, she often tells FF the wrong answers on purpose.

The bad boy is not a fool man. FF detects some answers are incompatible. Of course, these contradictions make it difficult to calculate the sequence.

However, TT is a nice and lovely girl. She doesn't have the heart to be hard on FF. To save time, she guarantees that the answers are all right if there is no logical mistakes indeed.

What's more, if FF finds an answer to be wrong, he will ignore it when judging next answers.

But there will be so many questions that poor FF can't make sure whether the current answer is right or wrong in a moment. So he decides to write a program to help him with this matter. The program will receive a series of questions from FF together with the answers FF has received from TT. The aim of this program is to find how many answers are wrong. Only by ignoring the wrong answers can FF work out the entire sequence of integers. Poor FF has no time to do this job. And now he is asking for your help~(Why asking trouble for himself~~Bad boy)

Input

Line 1: Two integers, N and M (1 <= N <= 200000, 1 <= M <= 40000). Means TT wrote N integers and FF asked her M questions.

Line 2..M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT answered FF that the sum from Ai to Bi is Si. It's guaranteed that 0 < Ai <= Bi <= N.

You can assume that any sum of subsequence is fit in 32-bit integer.

Output

A single line with a integer denotes how many answers are wrong.

Sample Input

10 5
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1

Sample Output

1

这种题还是不熟练啊~

题目大意:给你n个点,和m条线段,告诉你m条线段的权值之和。让你判断其中逻辑错误的有几个。(当当前输入和以前的没有冲突 就可以认为是正确的 否则就是错误的)

思路:扩展并查集,已经写过两篇这种题目了。感觉就是自己设置偏移量(有方向的),然后就可以按照向量的加减法来推导关系了。可以看看这位博主的:

https://www.cnblogs.com/liyinggang/p/5327055.html

令附上另外两道题的题解:

https://mp.csdn.net/postedit/86650735

https://mp.csdn.net/postedit/86653185

下面的推导认为处理的区间是左开右闭的(因此在处理询问的时候要把区间左端点减1)。

当然你要认为处理的区间是左闭右开的也没问题,在处理询问时把区间右端点+1就好了,但是初始化范围也要改一下。

这道题呢,我们利用一个sum数组,sum[i]存储从结点i到其根结点的线段的权值。初始化的时候,可以把sum初始化为0,下面来推导关系,首先就是在Find函数的路径压缩的时候,怎么修改sum数组的值。

这种情况必然是B、C先连接,然后再跟A连接后查询C父节点时候的情况。那么sum[C]存储到是C到B的,sum[B]存储的是B到A的,那么C到A自然是sum[C]+sum[B],也符合向量的加法:C->A=C->B+B->A;再看合并操作:

根据向量的运算法则,我们有:A->B=A->C+C->D+D->B

也即sum[A]=-sum[C]+d+sum[D]

判断是否有矛盾的情况:(矛盾情况必然发生在两个点的根结点相同,但两者之间的距离出现了矛盾)

若此时读入A与B的距离d,那么如果没有矛盾的话,应该满足:A-B=A->C+C->B

sum[A]-sum[B]=d 若不满足,则说明当前输入是错误的,跳过就可以了。

#include<iostream>
#include<cstdio>
#include<stack>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
#include<iterator>
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int f[200005];
int sum[200005];
int n,m;

void init()
{
	for(int i=0;i<=n;i++)
	{
		f[i]=i;
		sum[i]=0;
	}
}

int Find(int x)
{
	if(f[x]==x)
		return x;
	int temp=f[x];
	f[x]=Find(f[x]);
	sum[x]=sum[x]+sum[temp];
	return f[x];
}

int Union(int x,int y,int d)
{
	int fx=Find(x),fy=Find(y);
	if(fx!=fy)
	{
		f[fx]=fy;
		sum[fx]=d-sum[x]+sum[y];
		return 1;
	}
	else
	{
		if(d!=sum[x]-sum[y])
			return 0;
	}
	return 1;
}

int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		init();
		int x,y,d;
		int cnt=0;
		for(int i=0;i<m;i++)
		{
			scanf("%d %d %d",&x,&y,&d);
			x--;
			if(!Union(x,y,d))
				cnt++;
		}
		printf("%d\n",cnt);
	}
	return 0;
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。在编写C程序时,需要注意变量的声明和定义、指针的使用、内存的分配与释放等问题。C语言中常用的数据结构包括: 1. 数组:一种存储同类型数据的结构,可以进行索引访问和修改。 2. 链表:一种存储不同类型数据的结构,每个节点包含数据和指向下一个节点的指针。 3. 栈:一种后进先出(LIFO)的数据结构,可以通过压入(push)和弹出(pop)操作进行数据的存储和取出。 4. 队列:一种先进先出(FIFO)的数据结构,可以通过入队(enqueue)和出队(dequeue)操作进行数据的存储和取出。 5. 树:一种存储具有父子关系的数据结构,可以通过中序遍历、前序遍历和后序遍历等方式进行数据的访问和修改。 6. 图:一种存储具有节点和边关系的数据结构,可以通过广度优先搜索、深度优先搜索等方式进行数据的访问和修改。 这些数据结构在C语言中都有相应的实现方式,可以应用于各种不同的场景。C语言中的各种数据结构都有其优缺点,下面列举一些常见的数据结构的优缺点: 数组: 优点:访问和修改元素的速度非常快,适用于需要频繁读取和修改数据的场合。 缺点:数组的长度是固定的,不适合存储大小不固定的动态数据,另外数组在内存中是连续分配的,当数组较大时可能会导致内存碎片化。 链表: 优点:可以方便地插入和删除元素,适用于需要频繁插入和删除数据的场合。 缺点:访问和修改元素的速度相对较慢,因为需要遍历链表找到指定的节点。 栈: 优点:后进先出(LIFO)的特性使得栈在处理递归和括号匹配等问题时非常方便。 缺点:栈的空间有限,当数据量较大时可能会导致栈溢出。 队列: 优点:先进先出(FIFO)的特性使得

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值