hdu 3038带权并查集
来源:互联网 发布:ubuntu ss 自动切换 编辑:程序博客网 时间:2024/06/04 19:24
#include<stdio.h>
#include<string.h>
#define N 200100
struct node {
int x,count;
}pre[N];
int find(int n) {
if(n!=pre[n].x) {
int h=pre[n].x;
pre[n].x=find(pre[n].x);
pre[n].count=pre[n].count+pre[h].count;
}
return pre[n].x;
}
int Union(int x,int y,int d) {
int a=find(x);
int b=find(y);
if(a==b) {
if(pre[x].count-pre[y].count==d)
return 1;
return 0;
}
else {
pre[a].x=b;
pre[a].count=pre[y].count-pre[x].count+d;
return 1;
}
}
int main() {
int n,m,a,b,c,count,sum,flag,i;
while(scanf("%d%d",&n,&m)!=EOF) {
for(i=0;i<=n;i++) {
pre[i].x=i;
pre[i].count=0;
}
count=0;
while(m--) {
scanf("%d%d%d",&a,&b,&c);
a--;
if(Union(a,b,c)==0)
count++;
}
printf("%d\n",count);
}
return 0;
}
#include<string.h>
#define N 200100
struct node {
int x,count;
}pre[N];
int find(int n) {
if(n!=pre[n].x) {
int h=pre[n].x;
pre[n].x=find(pre[n].x);
pre[n].count=pre[n].count+pre[h].count;
}
return pre[n].x;
}
int Union(int x,int y,int d) {
int a=find(x);
int b=find(y);
if(a==b) {
if(pre[x].count-pre[y].count==d)
return 1;
return 0;
}
else {
pre[a].x=b;
pre[a].count=pre[y].count-pre[x].count+d;
return 1;
}
}
int main() {
int n,m,a,b,c,count,sum,flag,i;
while(scanf("%d%d",&n,&m)!=EOF) {
for(i=0;i<=n;i++) {
pre[i].x=i;
pre[i].count=0;
}
count=0;
while(m--) {
scanf("%d%d%d",&a,&b,&c);
a--;
if(Union(a,b,c)==0)
count++;
}
printf("%d\n",count);
}
return 0;
}
- hdu 3038带权并查集
- hdu-3038 带权并查集
- hdu 3038 带权并查集
- hdu 3038 带权并查集
- HDU 3038 带权并查集
- hdu 2818 带权并查集
- hdu 3047 带权并查集
- hdu 3635 带权并查集
- HDU 3635 带权并查集
- HDU 3047 带权并查集
- HDU 3074 带权并查集
- HDU 3047 带权并查集
- hdu 3047 带权并查集
- hdu 3047 (带权并查集)@
- HDU 3047 带权并查集
- hdu 3047 带权并查集
- hdu 3038(带权并查集)
- HDU 3038 带权并查集,区间
- 常用HTTP Content-type头信息及文件类型对照
- iOS开发之Present ViewController Modally --弹出模态ViewController
- 第一篇博客
- Linux下搭建Android开发环境及Android真机调试
- Serializable,externalizable,comprable
- hdu 3038带权并查集
- NYOJ-244-16进制的简单运算-2013年10月17日17:19:41
- UIView翻转效果实现
- Ubuntu 12.04 安装 clang3.2
- linux 内核调试技巧
- linux usb系统【全面】
- View知识点
- 构建Windows驱动开发环境
- ios开发鸡肋之莫名奇妙的错误