/ Vijos / 题库 / 齿轮 /

题解

1 条题解

  • 2
    @ 2017-06-24 20:00:33
    //带权并查集+路径压缩,边权为下面齿轮与上面的转速比,精度也要处理一下。
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    using namespace std;
    int father[10010];
    double weight[1010];
    bool ans;
    char word[2][5] = {"No", "Yes"}; 
    void initial()
    {
        memset(father, 0, sizeof(father));
        ans = true;
    }
    int find(int x)
    {
        if(father[x] == 0)
        {
            weight[x] = 1;
            return x;
        }
        int orif = father[x];
        father[x] = find(father[x]);
        weight[x] *= weight[orif];
        return father[x];
    }
    bool merge(int u, int v, int x, int y)
    {
        int fu = find(u);
        int fv = find(v);
        double ratio = (x /(double) y) / (weight[u] / weight[v]);
        if(fu != fv)
        {
            father[fu] = fv;
            weight[fu] = ratio;
            return true;
        }
        return fabs(ratio - 1) < 0.000000001;
    }
    int main()
    {
        int n, m, t;
        int u, v, x, y;
        scanf("%d", &t);
        for(int k = 1; k <= t; k++)
        {
            initial();
            scanf("%d%d", &n, &m);
            for(int i = 0; i < m; i++)
            {
                scanf("%d%d%d%d", &u, &v, &x, &y);
                ans = ans && merge(u, v, x, y);
            }
            printf("Case #%d: %s\n", k, word[ans]);
        }
        return 0;
    }
    
  • 1

信息

ID
1997
难度
6
分类
(无)
标签
递交数
138
已通过
39
通过率
28%
被复制
2
上传者