HDU--3308 How Many Answers Are Wrong
来源:互联网 发布:账户到期 数据库 编辑:程序博客网 时间:2024/06/08 00:46
这一道题是水题一道,但是我能说我做了将近三个小时吗。。。就是一直在纠结如何存储端点的值。在百思不得其解得的时候终于搜了一下,结果呵呵。把闭区间改成半开半闭区间不就完事了吗、、、剩下的完全就是加权并查集了。
下面的代码是让大的点做根,小的点做的是叶子,那么叶子的权值就是正值了。。。
#include <cstdio>#include <cstring>#include <cstdlib>#define N 200005int pa[N];__int64 rank[N];int find(int x){if(x!=pa[x]){int tmp=pa[x];pa[x]=find(tmp);rank[x]+=rank[tmp];}return pa[x];}int initial(int n){for(int i=0;i<=n;i++){pa[i]=i;}memset(rank,0,sizeof(rank));//n在不大的情况下,它可能较费些时间}int main(){int n,m,count;while(scanf("%d%d",&n,&m)==2){count=0;initial(n);while(m--){int x,y;__int64 Rank;//防止出现超过2^31 int越界,当然unsigned也行,不过到边的感觉让我没有安全感scanf("%d%d%I64d",&x,&y,&Rank);--x;int px=find(x),py=find(y);if(px!=py){pa[px]=py;//取较大的点做根rank[px]=-rank[x]+rank[y]-Rank;//画一下,很容易明白}else{if(rank[y]-rank[x]!=Rank)count++;}}printf("%d\n",count);}return 0;}
0 0
- HDU--3308 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 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
- IT职业生涯:开源即是开放的简历
- retain的setter中第一次,release如何操作
- 错误““NameSpace.Test_Index.Dispose(bool)”: 没有找到适合的方法来重写
- Android快捷键
- Linux下.pro文件的写法简介
- HDU--3308 How Many Answers Are Wrong
- 输入一个字符串 保留大写字母和空格输出 例如输入He Is Dog 输出为H I D
- hdu Max Sum Plus Plus(动态规划+分段最大子段和)
- 帮您快速入门 TI 的 Codec Engine
- POJ1733 - Parity game - 并查集
- Median of Two Sorted Arrays Java
- 购物网站的简单菜单栏
- C/C++中调用LUA函数
- Linux下autoconf和automake使用