How Many Answers Are Wrong HDU - 3038 并查集,加权(路径)
TT and FF are ... friends. Uh... very very good friends -________-b
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).
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.
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.
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.
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.
What's more, if FF finds an answer to be wrong, he will ignore it when judging next answers.
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)
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).
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.
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.
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.
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.
What's more, if FF finds an answer to be wrong, he will ignore it when judging next answers.
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)
InputLine 1: Two integers, N and M (1 <= N <= 200000, 1 <= M <= 40000). Means TT wrote N integers and FF asked her M questions.
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.
You can assume that any sum of subsequence is fit in 32-bit integer.
OutputA single line with a integer denotes how many answers are wrong.Sample Input
10 5 1 10 100 7 10 28 1 3 32 4 6 41 6 6 1
Sample Output
1
题意:有大小为n的数列,m次数据,每次数据给出数列中从第a个到第b个的和为c,问这些数据中有多少数据是
错误的。
思路:将每个数想象成一条线段,线段的左边为这个数,右边为下一个数,因此数组要开n+1大小,用并查集给这些一个祖先,开一个数组存放这些数到祖先的距离。具体见代码
代码:
1 #include <cstdio> 2 #include <fstream> 3 #include <algorithm> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <string> 9 #include <cstring> 10 #include <map> 11 #include <stack> 12 #include <set> 13 #include <sstream> 14 #include <iostream> 15 #define mod 998244353 16 #define eps 1e-6 17 #define ll long long 18 #define INF 0x3f3f3f3f 19 using namespace std; 20 21 //fa保存下标对应的祖先 22 //num保存下标到祖先节点的距离 23 int fa[200005],num[200005]; 24 //初始化 25 void init(int n) 26 { 27 //因为有n个线段,那么有n+1个点 28 for(int i=0;i<=n+1;i++) 29 { 30 //初始情况下每个点的祖先为自己 31 fa[i]=i; 32 //初始情况下每个点到祖先的距离为0 33 num[i]=0; 34 } 35 } 36 //找到祖先,并进行压缩路径的优化 37 int find(int s) 38 { 39 //如果祖先为自己则返回自己 40 if(fa[s]==s) 41 { 42 return s; 43 } 44 //递归寻找祖先 45 int temp=find(fa[s]); 46 //将自己到祖先的距离加上祖先到祖先的祖先的距离 47 num[s]+=num[fa[s]]; 48 //压缩路径优化 49 fa[s]=temp; 50 //返回祖先 51 return fa[s]; 52 } 53 int main() 54 { 55 int n,m; 56 while(scanf("%d %d",&n,&m)!=EOF) 57 { 58 //初始化数据 59 init(n); 60 int a,b,c; 61 int ans=0; 62 for(int i=0;i<m;i++) 63 { 64 scanf("%d %d %d",&a,&b,&c); 65 //加一是因为从a到b是从a线段的起点到b线段的终点 66 b++; 67 //找到a和b的最远祖先 68 int x=find(a); 69 int y=find(b); 70 //如果两个祖先不相等,合并两个点到各自祖先的距离 71 if(x!=y) 72 { 73 //将b的祖先上再加一个祖先,即为b的祖先的祖先为a的祖先 74 fa[y]=x; 75 //因为将b的祖先加到了a的祖先的上,所以b到a的祖先的距离为 76 //b的a的距离加上a到a祖先的距离,或b到b祖先的距离加上b祖先到a祖先的距离 77 //因此b祖先到a祖先的距离等于b的a的距离加上a到a祖先的距离减去b到b祖先的距离 78 num[y]=num[a]+c-num[b]; 79 } 80 else 81 { 82 //b到b祖先的距离减去a到a祖先的距离即为a到b的距离 83 //判断c是否为a到b的距离 84 //不是则表示这句话的c是错的 85 if(c!=num[b]-num[a]) 86 { 87 ans++; 88 } 89 } 90 } 91 printf("%d\n",ans); 92 } 93 }