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)
最近在恶补并查集的题,自己的实力实在是太过于弱,这个代码看了两天都还没有对它有完全的理解,惭愧。
思路:这道题首先是判断什么时候进行判断这个集合是否跟之前的数据矛盾:就是这个集合的前端点的根节点与后端点的根节点相同时(因为此时,前端点和后端点都处在一个大集合中可以根据之前的数据来推推测当前的数据是否错误),然后第二个点就是如何对这个问题进行处理,:记录每一个子节点到根节点的距离,更详细的看代码;
代码:
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 2e5 + 10;
int pre[maxn];//用来记录每一个点的父节点,也就是记录一个集合后端点的的前节点是谁;
int sum[maxn];//记录每一个点到自己父节点的距离
int ans = 0;
int n, m;
int find(int x)
{
if (pre[x] == x)
return x;
else
{
int t = pre[x];
pre[x] = find(pre[x]);
sum[x] += sum[t];//更新点到父节点的距离,因为父节点的变化,那么在压缩查找父节点时,进行更新路程,这个方法是并查集很常用的套路,就是在路径
return pre[x]; //压缩过程中,进行一些其他的操作。
}
}
void Un(int x, int y,int s)
{
int fx = find(x), fy = find(y);
if (fx == fy)
{
if (sum[x] + s != sum[y])
ans++;
return;
}
else
{
pre[fy] = fx;
sum[fy] = sum[x] + s - sum[y];
}
}
void ini()
{
for (int i = 0; i <= n; i++)
pre[i] = i;
memset(sum, 0, sizeof(sum));
}
int main()
{
while (scanf("%d %d", &n, &m)!=EOF)
{
ini();
ans = 0;
for (int i = 0; i < m; i++)
{
int x, y, s;
scanf("%d %d %d", &x, &y, &s);
Un(x, y + 1, s);//b+1,为了使【1,3】和【4,6】这样的区间合并,有利于判断
}
printf("%d\n", ans);
}
return 0;
}