hdu 3038 hdu 3047 poj 1192 带权并查集

hdu  3038

How Many Answers Are Wrong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6660    Accepted Submission(s): 2482


Problem Description
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



/*
* hdu 3038
* author  : mazciwong
* creat on: 2016-1-18
*/

/*
有点类似于poj的3028
(把所有东西放入ABC盘子,给出一系列命令,求所有错误次数,
都是用并查集来解,类似划分块内的元素有等价性这样,元素是每个东西到其中一个盘子的配对)

题意:给n个数,m条命令
每条命令给出一个连续子序列,和他们的和
求出所有错误的命令数,用并查集可以很容易解决

用一个sum[i]来储存前面i个的和
A~B的和是s,即sum[B]比sum[A-1]大s

*/


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 200000 + 5;
int n, m;
int fa[maxn];
int sum[maxn];

//并查集初始化
void init(int n)
{
	for (int i = 0; i < n; i++) 
	{
		fa[i] = i;
		sum[i] = 0;
	}
}

//并查集查询
int find(int x)
{
	if (x == fa[x])
		return x;
	else
	{
		int f = fa[x];
		fa[x] = find(fa[x]);
		sum[x] += sum[f];
		return fa[x];
	}
}

//并查集合并
bool unite(int x, int y, int c)  //y应该比x大c才对,不然就返回错
{
	int a = find(x), b = find(y);
	if (a == b)
	{
		if (sum[x] + c != sum[y])
			return false;
		return true;
	}
	else
	{
		fa[b] = a;//之前没比过,不等,就让b指向a
		sum[b] = sum[x] - sum[y] + c;
		return true;
	}
}

int main()
{
	int a, b, c;
	while (scanf("%d%d", &n, &m) != EOF)
	{
		int ans = 0;
		init(n + 1);
		for (int i = 0; i < m; i++)
		{
			scanf("%d%d%d", &a, &b, &c);
			if (!unite(a - 1, b, c))
				ans++;
		}
		printf("%d\n", ans);
	}
	return 0;
}



hdu 3047

Zjnu Stadium

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3192    Accepted Submission(s): 1230


Problem Description
In 12th Zhejiang College Students Games 2007, there was a new stadium built in Zhejiang Normal University. It was a modern stadium which could hold thousands of people. The audience Seats made a circle. The total number of columns were 300 numbered 1--300, counted clockwise, we assume the number of rows were infinite.
These days, Busoniya want to hold a large-scale theatrical performance in this stadium. There will be N people go there numbered 1--N. Busoniya has Reserved several seats. To make it funny, he makes M requests for these seats: A B X, which means people numbered B must seat clockwise X distance from people numbered A. For example: A is in column 4th and X is 2, then B must in column 6th (6=4+2).
Now your task is to judge weather the request is correct or not. The rule of your judgement is easy: when a new request has conflicts against the foregoing ones then we define it as incorrect, otherwise it is correct. Please find out all the incorrect requests and count them as R.
 

Input
There are many test cases:
For every case: 
The first line has two integer N(1<=N<=50,000), M(0<=M<=100,000),separated by a space.
Then M lines follow, each line has 3 integer A(1<=A<=N), B(1<=B<=N), X(0<=X<300) (A!=B), separated by a space.

 

Output
For every case: 
Output R, represents the number of incorrect request.
 

Sample Input
  
  
10 10 1 2 150 3 4 200 1 5 270 2 6 200 6 5 80 4 7 150 8 9 100 4 8 50 1 7 100 9 2 100
 

Sample Output
  
  
2
Hint
Hint: (PS: the 5th and 10th requests are incorrect)


/*
* hdu 3047
* author  : mazciwong
* creat on: 2016-1-19
*/

/*
跟hdu 3038一样
n个人,m条命令
每条命令,a,b,x   (b-a = x)
求出错的命令


题意:给n个数,m条命令
每条命令给出一个连续子序列,和他们的和
求出所有错误的命令数,用并查集可以很容易解决

用一个sum[i]来储存前面i个的和
A~B的和是s,即sum[B]比sum[A-1]大s

*/


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 200000 + 5;
int n, m;
int fa[maxn];
int sum[maxn];

//并查集初始化
void init(int n)
{
	for (int i = 0; i < n; i++)
	{
		fa[i] = i;
		sum[i] = 0;
	}
}

//并查集查询
int find(int x)
{
	if (x == fa[x])
		return x;
	else
	{
		int f = fa[x];
		fa[x] = find(fa[x]);
		sum[x] += sum[f];
		return fa[x];
	}
}

//并查集合并
bool unite(int x, int y, int c)  //y应该比x大c才对,不然就返回错
{
	int a = find(x), b = find(y);
	if (a == b)
	{
		if (sum[x] + c != sum[y])
			return false;
		return true;
	}
	else
	{
		fa[b] = a;//之前没比过,不等,就让b指向a
		sum[b] = sum[x] - sum[y] + c;
		return true;
	}
}

int main()
{
	int a, b, c;
	while (scanf("%d%d", &n, &m) != EOF)
	{
		int ans = 0;
		init(n + 1);
		for (int i = 0; i < m; i++)
		{
			scanf("%d%d%d", &a, &b, &c);
			if (!unite(a, b, c))  //只在这里改了一点点
				ans++;
		}
		printf("%d\n", ans);
	}
	return 0;
}


上面两道题的代码都是一样的...都是同一个题

可以参考之前在POJ写的题.也是差不多

 

挑战程序设计竞赛 POJ 1192食物链 带权并查集


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值