HDU-3038 How Many Answers Are Wrong
2014-01-19 12:08
363 查看
题目链接
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; #define maxn 200020 int n,m; int p[maxn],weight[maxn]; //并查集祖先结点 并查集权值 int find(int x) { if( p[x] == x ) return x; int t = p[x]; p[x] = find( p[x] ); weight[x] += weight[t]; return p[x]; } void merge( int x,int y,int a,int b,int v ) { if( x > y ) { p[y] = x; weight[y] = weight[a]-v-weight[b]; } else { p[x] = y; weight[x] = v + weight[b] - weight[a]; } } void init() { memset( weight,0,sizeof(weight) ); for( int i = 0; i <= n; i++ ) p[i] = i; } int main() { //freopen( "data.in","r",stdin ); while(scanf("%d%d",&n,&m) != EOF) { init(); int a,b,v,re = 0; for(int i = 0;i < m;i++) { scanf("%d%d%d",&a,&b,&v); a--; int x = find(a); int y = find(b); if(x == y && weight[a] != weight[b] + v) { re++; } else if(x != y) merge(x,y,a,b,v); } printf("%d\n",re); } return 0; }
相关文章推荐
- How Many Answers Are Wrong HDU - 3038
- hdu 3038 How Many Answers Are Wrong(带权并查集)
- HDU 3038 How Many Answers Are Wrong
- D - How Many Answers Are Wrong HDU - 3038(带权并查集)
- hdu 3038 How Many Answers Are Wrong
- hdu 3038 How Many Answers Are Wrong (带权并查集入门)
- HDU 3038 How Many Answers Are Wrong (并查集)---并查集看不出来系列-1
- HDU 3038 How Many Answers Are Wrong 带权并查集
- hdu 3038 How Many Answers Are Wrong (并查集)
- hdu 3038 How Many Answers Are Wrong(带权并查集)
- D - How Many Answers Are Wrong HDU - 3038 【关系并查集】
- HDU 3038 How Many Answers Are Wrong(带权并查集)
- hdu 3038 How Many Answers Are Wrong(并查集)
- D - How Many Answers Are Wrong HDU - 3038
- 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