HDU 3038 带权并查集
2017-08-02 16:04
148 查看
可以清晰看出val - dist[y] + dist[x] 就是yy到祖先的距离dist[yy]
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
const int AX = 2e5+666;
const int MAXN = 4e4+666;
int pre[AX];
int dist[AX];
int ans ;
int n,m;
void init(){
for( int i = 1 ; i <= n ; i++ ){
pre[i] = i;
dist[i] = 0;
}
}
int find(int x){
if( x!= pre[x] ){
int t = pre[x];
pre[x] = find(pre[x]);
dist[x] += dist[t]; //压缩路径时多出的更新操作
}
return pre[x];
}
void mix(int x,int y,int val){
int xx = find(x);
int yy = find(y);
if( xx != yy ){
pre[yy] = xx ;
dist[yy] = val - dist[y] + dist[x]; //这一步的理解在开头给出
}else{
if( dist[yy] != val - dist[y] + dist[x] ) ans++;
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
ans = 0;
init();
int x,y,val;
while( m-- ){
scanf("%d%d%d",&x,&y,&val);
mix(x-1,y,val);
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- hdu 3038 How Many Answers Are Wrong(并查集)
- HDU 3038 How Many Answers Are Wrong (带权并查集)
- hdu 3038 How Many Answers Are Wrong(并查集变种)
- HDU 3038(并查集 带权值)
- 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)+3047(Zjnu Stadium)(种类并查集)
- HDU 3038 How Many Answers Are Wrong(带权并查集)
- HDU 3038 How Many Answers Are Wrong 带权并查集
- hdu 3038 hdu 3047 poj 1192 带权并查集
- 【HDU 3038 How Many Answers Are Wrong】+ 并查集
- 【HDU 3038】 How Many Answers Are Wrong (带权并查集)
- HDU 3038 带权并查集,区间
- hdu 3038D - How Many Answers Are Wrong [kuangbin带你飞]专题五 并查集
- hdu 3038 How Many Answers Are Wrong(带权并查集+树的性质)
- hdu 3038 How Many Answers Are Wrong(带权并查集)
- hdu 3038 - How Many Answers Are Wrong(并查集)
- hdu-3038 带权并查集