zoukankan      html  css  js  c++  java
  • hdu 3047 Zjnu Stadium(并查集)

    还是自己划一下图就清楚了。。

    当a,b父亲节点不同时,

    x,y分别为a,b的父亲节点,a到x,b到y的距离已知,给出a,b间的距离,判断x到y的距离。。

    当a,b父亲节点相同时,

    直接判断距离是否相等就好了。。  注意是用向量表示的。。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<string>
     6 #include<queue>
     7 #include<algorithm>
     8 #include<map>
     9 #include<iomanip>
    10 #include<climits>
    11 #include<string.h>
    12 #include<cmath>
    13 #include<stdlib.h>
    14 #include<vector>
    15 #include<stack>
    16 #include<set>
    17 #define INF 1e7
    18 #define MAXN 10010
    19 #define maxn 1000010
    20 #define Mod 1000007
    21 #define N 1010
    22 using namespace std;
    23 typedef long long LL;
    24 
    25 int n, m;
    26 int fa[50010];
    27 int dis[50010];
    28 
    29 int findset(int x)
    30 {
    31     if (x == fa[x]) return x;
    32     int t = fa[x];
    33     fa[x] = findset(fa[x]);
    34     dis[x] += dis[t];
    35     return fa[x];
    36 }
    37 
    38 bool merg(int a, int b, int w)
    39 {
    40     int x = findset(a);
    41     int y = findset(b);
    42     if (x == y) {
    43         if (dis[a] + w != dis[b]) return false;
    44         return true;
    45     }
    46     fa[y] = x;
    47     dis[y] = dis[a] + w - dis[b];
    48     return true;
    49 }
    50 
    51 int main()
    52 {
    53     int a, b, w;
    54     while (~scanf("%d%d",&n,&m)) {
    55         for (int i = 0; i <= n; ++i)
    56             fa[i] = i, dis[i] = 0;
    57         int ans = 0;
    58         for (int i = 0; i < m; ++i) {
    59             scanf("%d%d%d", &a, &b, &w);
    60             if (!merg(a, b, w)) ans++;
    61         }
    62         printf("%d
    ",ans);
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    用iptables 实现本地端口转发
    hive查询
    IOS畅销榜
    java多线程系列7-停止线程
    java多线程系列6-阻塞队列
    java多线程系列5-死锁与线程间通信
    java多线程系列4-线程池
    java多线程系列3-线程同步
    java多线程系列2-线程控制
    java多线程系列1--线程实现与调度
  • 原文地址:https://www.cnblogs.com/usedrosee/p/4344075.html
Copyright © 2011-2022 走看看