题目链接:https://vjudge.net/contest/353062#problem/F
题目大意:给你若干个区间和区间对应的值,求冲突的区间
带权并查集果题
#include<set> #include<map> #include<stack> #include<queue> #include<cmath> #include<cstdio> #include<cctype> #include<string> #include<vector> #include<climits> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define endl '\n' #define max(a, b) (a > b ? a : b) #define min(a, b) (a < b ? a : b) #define zero(a) memset(a, 0, sizeof(a)) #define INF(a) fill(a, a+maxn, INF); #define IOS ios::sync_with_stdio(false) #define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n") using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<double, int> P2; const double pi = acos(-1.0); const double eps = 1e-7; const ll MOD = 1000000007LL; const int INF = 0x3f3f3f3f; const int _NAN = -0x3f3f3f3f; const double EULC = 0.5772156649015328; const int NIL = -1; template<typename T> void read(T &x){ x = 0;char ch = getchar();ll f = 1; while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();} while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f; } const int maxn = 2e5+10; int p[maxn], dis[maxn]; int __find(int root) { if (root != p[root]) { int t = p[root]; p[root] = __find(p[root]); dis[root] += dis[t]; } return p[root]; } int main(void) { IOS; int n, m; while(cin >> n >> m) { int ans = 0; zero(dis); for (int i = 0; i<=n; ++i) p[i] = i; while(m--) { int l, r, d; cin >> l >> r >> d; --l; int r1 = __find(l); int r2 = __find(r); if (r1 == r2 && dis[l]-dis[r] != d) ++ans; else if (r1 != r2) { p[r1] = r2; dis[r1] = dis[r]-dis[l]+d; } } cout << ans << endl; } return 0; }