AcWing 240. food chain

#include <the iostream>
 the using  namespace STD;
 const  int N = 50010 ;
 int n-, m;
 int P [N], D [N];   // P is baba, d is the distance 
int Find ( int X) {
     IF (P [x] = x) {! // if x is not the root 
        int T = Find (P [x]); // points to save it as the root node becomes the finished after the assignment 
        d [x] + = d [ p [ X]]; // updated to the distance between the distance between the root == + parent parent node to the root node of the distance between 
        P [X] = T; // then assigned to the root 
    }
     return P [X];     
} 
int main () { 
    Scanf (" % D% D " , & n-, & m);
     for ( int I = . 1 ; I <= n-; I ++) P [I] = I; // initialize themselves is a collection 
    int RES = 0 ; / / number lies in 
    the while (m - ) {
         int T, X, Y; // T represents the type of inquiry 
        Scanf ( " % D% D% D " , T &, & X, & Y);
         IF (X> || Y n-> n-) RES ++; // out of range, lies 
        the else {
             int PX = find (X), Py = find (Y); // find the root 
            IF (T ==1 ) {   // If the same 
                IF (PX == Py && (D [X] - D [Y])% 3 ) // If a set of 3% and ranged from 
                    RES ++; // lies 
                else  IF (! px = py) { // if the root node are different, then a set is not 
                    P [px] = py; // put into the root px py of 
                    d [px] = d [y ] - d [x] ; // because the same class, then it is equal to 3%, the difference between the distance directly 
                } 
            } the else { // represents eat food if x y, then D [x] -d [Y] =. 1 
                IF (PX == && Py (D [X] - D [Y] - 1 )% . 3 ) // If a set of, but not 1
                    ++ RES; // lies 
                the else  IF (PX = Py!) { // if not a set of 
                    P [PX] = Py; // merge 
                    D [PX] = D [Y] + . 1 - D [X]; 
                } 
            } 
        } 
    } 
    the printf ( " % D \ n- " , RES);
     return  0 ; 
}

 

 

Guess you like

Origin www.cnblogs.com/QingyuYYYYY/p/11816425.html