Title Link: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038
Test instructions: n means an array of length n, followed by the input of M-line shapes such as x, Y, D, and for D (including x, and y) from the X, the elements to the Y-elements, and a few errors in the M-line input (the first input is correct);
Idea: Obviously with the right and check set, we can use the concept of distance and the concept of a better understanding, D for X to Y and that is x to Y distance;
You can use Rank[x] to represent the distance from the X to its Father node, merging the correct distance relationship into and concentrating (the wrong course cannot be merged into it);
Note that here X, Y is the closed interval, we do not direct processing, we must first turn it into an open interval, [x, Y] equivalent to (x-1, y] or [x, y+1);
Also have to pay attention to have multiple sets of input (pit father) ...
Code:
1#include <iostream>2#include <stdio.h>3 #defineMAXN 2000104 using namespacestd;5 6 intPRE[MAXN], RAN[MAXN];7 8 intFindintx) {9 if(x!=Pre[x]) {Ten intfx=find (Pre[x]); OneRAN[X]=RAN[X]+RAN[PRE[X]];//* * * * Update the value of rank during the path compression, as for this updated formula it's easy to see the plot line graph . Apre[x]=FX; - } - returnPre[x]; the } - - intJion (intXintYintd) { - intfx=find (x); + intfy=find (y); - if(FX==FY) {//* * * After the path compression fx,fy is the Father node of x, Y, if their father node is the same, we can determine whether and check the relationship between the set of contradictions, this we can also draw a line map to see it + if(ran[x]-ran[y]!=d) { A return 1; at } -}Else{//* * * Update the rank value after merging, and draw the line diagram as before is OK - if(fx<FY) { -pre[fx]=fy; -ran[fx]=ran[y]+d-Ran[x]; -}Else{ inpre[fy]=FX; -ran[fy]=ran[x]-ran[y]-D; to } + } - return 0; the } * $ intMainvoid){Panax Notoginseng intN, M, d, X, Y, ans=0; - while(~ scanf ("%d%d", &n, &m)) { theans=0; + for(intI=0; i<=n; i++){ Apre[i]=i; theran[i]=0; + } - while(m--){ $scanf"%d%d%d", &x, &y, &d); $ if(Jion (x, y+1, d)) {//Note that the problem gives a closed interval, we will turn it into a half open interval to better deal with -ans++; - } the } -printf"%d\n", ans);Wuyi } the return 0; -}
hdu3038 (with right and check set)