您的位置:首页 > 其它

HDU 3038 How Many Answers Are Wrong

2011-06-27 14:34 225 查看
http://acm.hdu.edu.cn/showproblem.php?pid=3038

#include<iostream>
using namespace std;
#define N 200001
int a,b,k;
struct node{
int father;
int len;
}p
;
void init(int n){
for(int i=0;i<=n;i++){
p[i].father=i;
p[i].len=0;
}
}
int find(int x){
if(x==p[x].father)
return p[x].father;
int r=find(p[x].father);
p[x].len+=p[p[x].father].len;
return p[x].father=r;
}
bool merge(int x,int y){
int fx=find(x);
int fy=find(y);
if(fx==fy) return p[y].len-p[x].len!=k;
p[fy].father=fx;
p[fy].len=p[x].len-p[y].len+k;
return false;
}
int main(void){
int n,m;
while(~scanf("%d%d",&n,&m)){
int c=0;
init(n);
while(m--){
scanf("%d%d%d",&a,&b,&k);
a--;
if(merge(a,b))
c++;
}
printf("%d/n",c);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: