hdu3038 How Many Answers Are Wrong 种类并查集
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int n, m;
const int maxn = ;
int fa[maxn];
int sum[maxn]; int Find(int x){
if (x == fa[x])
return x;
else{
int t = fa[x];
fa[x] = Find(fa[x]);
sum[x] += sum[t];
return fa[x];
}
} bool Set_Union(int x, int y, int s){
int fx = Find(x);
int fy = Find(y);
if (fx == fy && sum[x] != sum[y] + s)
return false;
else if(fx != fy){
if (fx > fy){
fa[fy] = fx;
sum[fy] = sum[x] - s - sum[y];
}
else{
fa[fx] = fy;
sum[fx] = s + sum[y] - sum[x];
}
}
return true;
} void init(){
memset(sum, , sizeof(sum)); //必须初始化为零
for (int i = ; i <= n; i++){
fa[i] = i;
}
} int main(){
while (scanf("%d %d", &n, &m) != EOF){
init(); //初始化
int ans = ;
while (m--){
int a, b, s;
scanf("%d%d%d", &a, &b, &s);
a--;
if (!Set_Union(a, b, s)){
ans++;
}
}
printf("%d\n", ans);
}
//system("pause");
return ;
}
hdu3038 How Many Answers Are Wrong 种类并查集的更多相关文章
- hdu3038 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 ...
- HDU 3038 How Many Answers Are Wrong(种类并查集)
题目链接 食物链类似的题,主要是在于转化,a-b的和为s,转换为b比a-1大s.然后并查集存 此节点到根的差. 假如x的根为a,y的根为b: b - y = rank[y] a - x = rank[ ...
- [HDU3038]How Many Answers Are Wrong(并查集)
传送门 和某题类似,只不过奇偶换成了和. ——代码 #include <cstdio> #include <iostream> #define N 1000001 int n, ...
- hdu 3038 How Many Answers Are Wrong(种类并查集)2009 Multi-University Training Contest 13
了解了种类并查集,同时还知道了一个小技巧,这道题就比较容易了. 其实这是我碰到的第一道种类并查集,实在不会,只好看着别人的代码写.最后半懂不懂的写完了.然后又和别人的代码进行比较,还是不懂,但还是交了 ...
- hdu3038(种类并查集,推荐)
题目大意:有n次询问,给出a到b区间的总和,问这n次给出的总和中有几次是和前面已近给出的是矛盾的?? 很有意思的一道题目,要是没有做过种类并查集,我肯定会以为这种题目是线段树题目...... 思路:我 ...
- HDU3038【种类并查集】
题意: 给出m组区间[a,b],以及其区间的和,问有矛盾的有几组: 思路: 种类并查集. 主要是几个关系:同类元素的关系,父亲与儿子的关系,不同类元素的关系: 我们可以类似看作一个前缀和,sum[x] ...
- POJ1703Find them, Catch them[种类并查集]
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42416 Accepted: ...
- POJ1733 Parity game —— 种类并查集
题目链接:http://poj.org/problem?id=1733 Parity game Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- POJ1984 Navigation Nightmare —— 种类并查集
题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K T ...
随机推荐
- Linux fcntl函数详解
功能描述:根据文件描述词来操作文件的特性. 文件控制函数 fcntl -- file control 头文件: #include <unistd.h> #include ...
- iOS开发之---判断是否是手机号
iOS开发之---判断是否是手机号
- 三种方法打印 main函数的返回地址的值(old EIP)(用途,你懂得!)
这里能够简单的改动随意函数的返回地址.能够做到自己定义EIP的指向,就可以运行当前进程空间的随意指令,这里仅仅是让大家更清楚栈帧结构,没有涉及跨进程的inline HOOK 等,后面会陆续讲下读取随意 ...
- 《从零開始学Swift》学习笔记(Day 61)——Core Foundation框架之内存管理
原创文章,欢迎转载. 转载请注明:关东升的博客 在Swift原生数据类型.Foundation框架数据类型和Core Foundation框架数据类型之间转换过程中,尽管是大部分是能够零开销桥接,零开 ...
- c++vector简单实现
const int DEFAULT_CAP = 3; template <typename T> class vector { // int capacity; T* _data; int ...
- 常用SQL备忘录
联表删除: delete t1,t2 from table_name t1 left join t2 on t1.id=t2.id where t1.id=23 (ps:该语句在mysql 5.0之前 ...
- inherited在消息中的作用(编译器根据inherited所在的函数,直接转换成对祖先类同名动态函数的调用,或者转换成对DefaultHandler的调用)
好奇一下.看来Object Pascal确实与Windows深入结合了. unit Unit1; interface uses Windows, Messages, SysUtils, Variant ...
- JOptionPane常用提示框
//JOptionPane.showMessageDialog(parentComponent, message, title, messageType, icon); JOptionPane.sho ...
- HDU 5687 Problem C
Problem C Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- WinDbg调试高内存的.Net进程Dump
WinDbg的学习路径,艰难曲折,多次研究进展不多,今日有所进展,记录下来. 微软官方帮助文档非常全面:https://msdn.microsoft.com/zh-cn/library/windows ...