码迷,mamicode.com
首页 > 其他好文 > 详细

HDU-3038-How Many Answers Are Wrong

时间:2019-01-21 21:09:04      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:namespace   print   scan   eof   lse   none   memory   max   ack   

链接:https://vjudge.net/problem/HDU-3038#author=0

题意:

给出N和M

有M次记录,以l,r,v给出,表示l-r区间和为v问,有多少是错误的答案。

思路:

带权并查集。

sum[i] 表示i到F[i]的和。

代码:

#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
using namespace std;
const int MAXN = 500086;
int Father[MAXN];
int Sum[MAXN];

int Get_F(int x)
{
    if (x == Father[x])
        return x;
    int tmp = Father[x];
    Father[x] = Get_F(Father[x]);
    Sum[x] += Sum[tmp];
    return Father[x];
}

int main()
{
    int n,m;
    //while (scanf("%d%d",&n,&m))
    while (cin >> n >> m)
    {
        for (int i = 0; i <= n; i++)
            Father[i] = i;
        memset(Sum, 0, sizeof(Sum));
        int l, r, v;
        int cnt = 0;
        for (int i = 1; i <= m; i++)
        {
            scanf("%d%d%d", &l, &r, &v);
            l--;
            int tl = Get_F(l);
            int tr = Get_F(r);
            if (tl != tr)
            {
                Father[tl] = tr;
                Sum[tl] = Sum[r] + v - Sum[l];
            }
            else
            {
                if (Sum[l] - Sum[r] != v)
                    cnt++;
            }
        }
        printf("%d\n", cnt);
    }

    return 0;
}

  

HDU-3038-How Many Answers Are Wrong

标签:namespace   print   scan   eof   lse   none   memory   max   ack   

原文地址:https://www.cnblogs.com/YDDDD/p/10300726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!