题意:给出n个数m个关系,其中A,B,S,表示从下标A到B的数的总和是S(n个数具体是什么不知道),问有多少个冲突关系。
将“下标A到B的数的总和是S”转换成,下标B比A-1的的数大S,这样题目就变成hdu 3047 解决方法同
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
#define asd puts("sdasdasdasdasd");
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 200050;
int r[N], fa[N];
int n, m;
void init()
{
for( int i = 1; i <= n; ++i )
{
fa[i] = i;
r[i] = 0;
}
}
int dfs( int x )
{
if ( x == fa[x] )
return x;
int ani = fa[x];
fa[x] = dfs( fa[x] );
r[x] += r[ani];
return fa[x];
}
bool Union( int a, int b, int c )
{
int x = dfs( a );
int y = dfs( b );
if( x == y )
{
if( r[a] + c == r[b] )
return 1;
else
return 0;
}
fa[y] = x;
r[y] = r[a] + c - r[b];
return 1;
}
int main()
{
while( ~scanf("%d%d", &n, &m) )
{
init();
int cnt = 0;
int a, b, x;
while ( m-- )
{
scanf("%d%d%d", &a, &b, &x );
if( !Union( a-1, b, x ) )
cnt++;
}
printf("%d\n", cnt);
}
return 0;
}