杭电hdu 3038 how many answers are wrong 并查集求解

原创 2012年03月23日 10:57:44

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

经过网上的搜罗,基本上明白了这个题的意思,网站http://hi.baidu.com/fp_lv/blog/item/a471530a86df7b90d53f7c2c.html讲解的很清楚,我这里只记录下网址和我的代码,以便以后随时查看。

#include <stdio.h>
#define MAX 200001

int father[MAX];
int sum[MAX];//存储从1到i相加的结果

int n,m;
int ai, bi, si;

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

int getfather(int x)
{
	if(x == father[x])
		return x;
	int now = getfather(father[x]);//
	sum[x] += sum[father[x]];//需要我研究的地方
	father[x] = now;
	return now;
}

bool join(int a, int b)
{
	int fa = getfather(a);
	int fb = getfather(b);
	if(fa == fb)return false;
	father[fb] = fa;
	sum[fb] = sum[a] - sum[b] + si;//
	return true;
}

int main()
{
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);
	
	while(~scanf("%d%d",&n,&m)){
		init();
		int ans = 0;
		while(m--){
			scanf("%d%d%d",&ai, &bi, &si);
			ai --;
			if(!join(ai, bi)&&sum[bi] - sum[ai] != si)ans ++;
		}
		printf("%d\n", ans);
	}
	return 0;
}


相关文章推荐

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 ...
  • F_cff
  • F_cff
  • 2017年04月20日 10:19
  • 159

hdu3038 How Many Answers Are Wrong--种类并查集

原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=3038 一:原题内容 Problem Description TT an...
  • LaoJiu_
  • LaoJiu_
  • 2016年04月10日 13:45
  • 284

HDU3038 How Many Answers Are Wrong(带权并查集)

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

HDU3038 How Many Answers Are Wrong 带权并查集

1.题目描述: How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3...

hdu 3038 How Many Answers Are Wrong 带权并查集

点击打开链接题目链接

[HDU 3038]How Many Answers Are Wrong[并查集]

题目链接:[HDU 3038]How Many Answers Are Wrong[并查集] 题意分析: 给出区间值的和,问这些话中有多少个是和之前矛盾的。 解题思路: 用一个dis数组记录点到根节点...

hdu 3038 How Many Answers Are Wrong (种类并查集)

解题思路:这是并查集里面很特殊的一类题目,不仅仅需要考虑点与点之间的集合关系,点与点之间还有一定的关系,姑且把他称之为边权,解决这个问题就是要用到这种思想,针对每一条线段,我们把两个端点提取出来,把他...
  • wchhlbt
  • wchhlbt
  • 2017年04月28日 14:45
  • 80

hdu 3038 How Many Answers Are Wrong(带权并查集+树的性质)

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

HDU 3038 How Many Answers Are Wrong(维护节点间关系的并查集)

How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J...
  • Yasola
  • Yasola
  • 2017年07月03日 09:20
  • 104

HDU 3038 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...
  • Twillz
  • Twillz
  • 2017年07月11日 19:06
  • 88
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:杭电hdu 3038 how many answers are wrong 并查集求解
举报原因:
原因补充:

(最多只允许输入30个字)