hdu 3038 - How Many Answers Are Wrong(并查集)
来源:互联网 发布:网络手游赚钱排行榜 编辑:程序博客网 时间:2024/05/23 02:00
题意:
已知多个描述,每个描述都给出一个区间的它的和,求这些描述中错误描述的个数。
思路:
思路和poj1733差不多,就是把利用并查集把所有区间的起点和终点都放到集合里面,其中s[x]用来表示区间(p[x], x]的和,其他合并区间和路径压缩的公式也很好推,具体的见代码。
另外需要说的是:杭电坑爹,任何题目都要写成多case。。。
代码如下:
const int M = 200005;int p[M], s[M];int find(int x){ int tmp = p[x]; p[x] = (p[x] == x ? x : find(p[x])); s[x] += s[tmp]; return p[x];}int main(){ int n, m, a, b, tmp, ans = 0; while(~scanf("%d%d", &n, &m)) { ans = 0; for(int i = 0; i <= n; ++i) p[i] = i, s[i] = 0; for(int i = 0; i < m; ++i) { scanf("%d%d%d", &a, &b, &tmp); int x = find(a-1); int y = find(b); if(x == y && s[b] - s[a-1] != tmp) ++ans; else if(x != y) { if(x>y) { p[x] = y; s[x] = s[b] - s[a-1] - tmp; } else { p[y] = x; s[y] = s[a-1] - s[b] + tmp; } } } printf("%d\n", ans); } return 0;}
- 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(并查集)
- 并查集 How Many Answers Are Wrong HDU 3038
- [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 并查集
- hdoj 3038How Many Answers Are Wrong 并查集
- HDOJ 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【并查集的简单应用】
- set -e and set -o pipefail
- SVN服务器搭建
- UVA 11176 Winning Streak
- Android如何防止apk程序被反编译
- Log4j.xml 配置模板
- hdu 3038 - How Many Answers Are Wrong(并查集)
- Git 使用的简单汇总 .
- ACM 中常用小算法(慢慢更新)
- 电路,报文,分组交换等的比较
- org.apache.cxf.interceptor.Fault:Could not send Message
- CopyUtils执行从read到write的数据拷贝
- 用SHELL 查看spid执行SQL
- he Struts dispatcher cannot be found. This is usually caused by using Struts tags without the assoc
- poj 3450 Corporate Identity (KMP+最长公共子串)