HDU 3038 How Many Answers Are Wrong
来源:互联网 发布:复杂网络 动态画图 编辑:程序博客网 时间:2024/04/28 04:39
题意:有n个点,m条信息。每条信息的内容是两个点之间的和的大小。求有多少条信息与上面的信息是矛盾的。
思路:a到b 的大小为s,可以理解为b -(a-1) 为s
#include<cstdio>#define MAX 200005int f[MAX];int v[MAX];//到根节点的距离int ans;int finds(int x){ if(f[x]==-1)return x; int tmp=finds(f[x]); v[x]+=v[f[x]]; // 这里更新该路径上的所有点到根节点的距离 return f[x]=tmp; //压缩路径}void unite(int p1,int p2,int w){ int t1=finds(p1); int t2=finds(p2); if(t1!=t2) { f[t2]=t1; v[t2]=v[p1]-v[p2]+w; //这里的意思是 两个根节点的距离+vp2=vp1+w 要注意方向 // printf("s%d\n",v[t2]); } else { if(v[p2]-v[p1]!=w)ans++; }}int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF) { ans=0; for(int i=0;i<=n;i++) { f[i]=-1; v[i]=0; } for(int i=0;i<m;i++) { int a,b,w; scanf("%d%d%d",&a,&b,&w); a--; unite(a,b,w); } printf("%d\n",ans); }}
0 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
- 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
- jQuery是一个JavaScript库极大的简化JavaScript编程
- 表示右上角的符号用MathType这样编辑
- 啦啦啦
- The Best Online Tools To Know Everything About a Website
- C语言笔记系列(七)--存储类
- HDU 3038 How Many Answers Are Wrong
- C++—航班查询系统
- jquery基本选择器
- java关键字Transient
- 六种方式让你的企业迈入数据驱动时代
- hdu 1698 - Just a Hook(线段树区间更新)
- Linux常用命令之(三)
- 使用dex-method-counts.jar统计APK中方法数目
- 写在开始