[HDU - 3047] Zjnu Stadium

The meaning of problems: input ABX, B represents the weight ratio of multi X-A, at 300 cycles, the number of errors described requirements

 

Ideas: Standard Weighted disjoint-set

 

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define maxn 200000 + 10
int father[maxn];
int val[maxn];
int Find(int x)
{
    if (x == father[x])
        return x;
    int temp = father[x];
    father[x] = Find(father[x]);
    val[x] += val[temp];
    return father[x];
}
int n, m;
int main()
{
    while (~scanf("%d %d", &n, &m))
    {
        for (int i = 1; i <= n; i++)
        {
            father[i] = i;
            val[i] = 0;
        }
        int ans = 0;
        for (int i = 1; i <= m; i++)
        {
            int u, v, w;
            scanf("%d %d %d", &u, &v, &w);
//            u -= 1;
            int ru = Find(u), rv = Find(v);
            if (ru == rv)
            {
                if (val[u] + w != val[v])
                    ans++;
            }
            else
            {
                father[rv] = ru;
                val[rv] = val[u] - val[v] + w;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Vikyanite/p/11388023.html