寒假Day28:HDU3038-How Many Answers Are Wrong-带权并查集

How Many Answers Are Wrong

 HDU - 3038 

题面:

 1 TT and FF are ... friends. Uh... very very good friends -________-b
 2 
 3 FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored).
 4 
 5 Then, FF can choose a continuous subsequence from it(for example the subsequence from the third to the fifth integer inclusively). After that, FF will ask TT what the sum of the subsequence he chose is. The next, TT will answer FF's question. Then, FF can redo this process. In the end, FF must work out the entire sequence of integers.
 6 
 7 Boring~~Boring~~a very very boring game!!! TT doesn't want to play with FF at all. To punish FF, she often tells FF the wrong answers on purpose.
 8 
 9 The bad boy is not a fool man. FF detects some answers are incompatible. Of course, these contradictions make it difficult to calculate the sequence.
10 
11 However, TT is a nice and lovely girl. She doesn't have the heart to be hard on FF. To save time, she guarantees that the answers are all right if there is no logical mistakes indeed.
12 
13 What's more, if FF finds an answer to be wrong, he will ignore it when judging next answers.
14 
15 But there will be so many questions that poor FF can't make sure whether the current answer is right or wrong in a moment. So he decides to write a program to help him with this matter. The program will receive a series of questions from FF together with the answers FF has received from TT. The aim of this program is to find how many answers are wrong. Only by ignoring the wrong answers can FF work out the entire sequence of integers. Poor FF has no time to do this job. And now he is asking for your help~(Why asking trouble for himself~~Bad boy)
16 Input
17 Line 1: Two integers, N and M (1 <= N <= 200000, 1 <= M <= 40000). Means TT wrote N integers and FF asked her M questions.
18 
19 Line 2..M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT answered FF that the sum from Ai to Bi is Si. It's guaranteed that 0 < Ai <= Bi <= N.
20 
21 You can assume that any sum of subsequence is fit in 32-bit integer.
22 
23 Output
24 A single line with a integer denotes how many answers are wrong.
25 
26 Sample Input
27 10 5
28 1 10 100
29 7 10 28
30 1 3 32
31 4 6 41
32 6 6 1
33 Sample Output
34 1
View Code

题意:

判断每个区间是否和之前的区间冲突,是的话ans++

注意:

多组输入,

dis需要清空

记录权值

int getf(int x)
{
    if(f[x]==x)
        return x;
    int t=getf(f[x]);
    dis[x]+=dis[f[x]];
    //当前点到父节点距离加上父节点到根节点距离就是当前点到根节点距离 
    return f[x]=t;

//    dis[x]+=dis[f[x]];
//    return f[x]=getf(f[x]);   WA
}
 1 #include<string.h>
 2 #include<iostream>
 3 #include<stdio.h>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<vector>
 7 #include<map>
 8 #include<cmath>
 9 using namespace std;
10 #define inf 0x3f3f3f3f
11 const int N =200050;
12 const int mod=1e9+7;
13 typedef long long ll;
14 
15 int f[N],dis[N];
16 
17 int getf(int x)
18 {
19     if(f[x]==x)
20         return x;
21     int t=getf(f[x]);
22     dis[x]+=dis[f[x]];
23     //当前点到父节点距离加上父节点到根节点距离就是当前点到根节点距离 
24     return f[x]=t;
25 
26 //    dis[x]+=dis[f[x]];
27 //    return f[x]=getf(f[x]);   WA
28 }
29 
30 int main()
31 {
32     int n,m;
33     while(~scanf("%d%d",&n,&m))
34     {
35         memset(dis,0,sizeof(dis));
36         for(int i=1; i<=n; i++)
37             f[i]=i;
38         int ans=0;
39         for(int i=1; i<=m; i++)
40         {
41             int a,b,c;
42             scanf("%d%d%d",&a,&b,&c);
43             a--;
44             int t1=getf(a);
45             int t2=getf(b);
46             if(t1!=t2)
47             {
48                 f[t2]=t1;
49                 dis[t2]=dis[a]+c-dis[b];
50                 // 更新b的祖先t2到a的祖先t1(也就是根)的距离 d(t2->t1)
51             }
52             else
53             {
54                 if(dis[b]-dis[a]!=c)
55                     ans++;
56             }
57         }
58         printf("%d\n",ans);
59     }
60     return 0;
61 }
View Code

待解决:

针对上述题目:

a--不执行的话会wa

我的理解,避免端点重合不起来,左开右闭

但是对于这组数据又解释不通(a--后,我觉得是1,但是答案是0,不存在冲突)

10 3
1 10 10
1 4 2
4 8 10

Thinking

所有的代码都会在原有基础上越写越简单,越写越精炼,越写越快,

三月份所有考试去小区,四月考位锁定,五月换题,只能安排在五月~

不知道接下来还会发生什么事情~

加油!!!

猜你喜欢

转载自www.cnblogs.com/OFSHK/p/12309673.html