HDU 3038 How Many Answers Are Wrong(种类并查集)
2014-05-05 07:59
405 查看
题目链接
食物链类似的题,主要是在于转化,a-b的和为s,转换为b比a-1大s。然后并查集存 此节点到根的差。
假如x的根为a,y的根为b:
b - y = rank[y]
a - x = rank[x]
y - x = s
可以推出b - a = rank[y] - rank[x] + s;
并查集 延迟更新什么的,都忘了啊。
还有这题,如果是x--的话,记得更新0的根。
View Code
食物链类似的题,主要是在于转化,a-b的和为s,转换为b比a-1大s。然后并查集存 此节点到根的差。
假如x的根为a,y的根为b:
b - y = rank[y]
a - x = rank[x]
y - x = s
可以推出b - a = rank[y] - rank[x] + s;
并查集 延迟更新什么的,都忘了啊。
还有这题,如果是x--的话,记得更新0的根。
#include <cstring> #include <cstdio> #include <string> #include <iostream> #include <algorithm> #include <vector> #include <queue> using namespace std; int o[200001]; int rank[200001]; int find(int x) { if(x == o[x]) return x; int t = find(o[x]); rank[x] = rank[o[x]] + rank[x]; return o[x] = t; } int main() { int n,m,i,a,b,s,ans,x,y; while(scanf("%d%d",&n,&m)!=EOF) { for(i = 0;i <= n;i ++) { o[i] = i; rank[i] = 0; } ans = 0; for(i = 0;i < m;i ++) { scanf("%d%d%d",&x,&y,&s); x -- ; a = find(x); b = find(y); if(a != b) { o[a] = b; rank[a] = rank[y] - rank[x] + s; } else { if(rank[x] != rank[y] + s) ans ++; } } printf("%d\n",ans); } return 0; }
View Code
相关文章推荐
- hdu 3038 How Many Answers Are Wrong (种类并查集)
- hdu 3038(How Many Answers Are Wrong)+3047(Zjnu Stadium)(种类并查集)
- hdu 3038 种类并查集How Many Answers Are Wrong
- hdu 3038 How Many Answers Are Wrong(种类并查集)2009 Multi-University Training Contest 13
- hdu 3038 How Many Answers Are Wrong 带权并查集
- HDU 3038 How Many Answers Are Wrong (带权并查集)
- HDU-3038 How Many Answers Are Wrong 并查集
- HDU 3038 How Many Answers Are Wrong(带权并查集)
- 杭电hdu 3038 how many answers are wrong 并查集求解
- hdu 3038 How Many Answers Are Wrong(带权并查集+树的性质)
- hdu 3038 How Many Answers Are Wrong(带权并查集)
- HDU 3038 How Many Answers Are Wrong(并查集)
- HDU - 3038 How Many Answers Are Wrong (带权并查集)
- HDU 3038 how many answers are wrong(带权并查集)
- HDU 3038 How Many Answers Are Wrong【带权并查集】
- hdu 3038 How Many Answers Are Wrong(并查集)
- HDU 3038 - How Many Answers Are Wrong(带权的并查集,其实俺真不知道为啥带权,俺只知道多了个父子关系)
- HDU 3038 How Many Answers Are Wrong(并查集)
- HDU 3038 How Many Answers Are Wrong (并查集)
- HDU---3038(How Many AnswersAre Wrong,带权并查集)