codeparrot/xlcost-text-to-code huggingface dataset:
Eulerian Path in undirected graph | Efficient C ++ program to find out Eulerian path ; Function to find out the path It takes the adjacency matrix representation of the graph as input ; Find out number of edges each vertex has ; Find out how many vertex has odd number edges ; If number of vertex with odd number of edges is greater than two return " No ▁ Solution " . ; If there is a path find the path Initialize empty stack and path take the starting current as discussed ; Loop will run until there is element in the stack or current edge has some neighbour . ; If current node has not any neighbour add it to path and pop stack set new current to the popped element ; If the current vertex has at least one neighbour add the current vertex to stack , remove the edge between them and set the current to its neighbour . ; print the path ; Driver Code ; Test case 1 ; Test case 2 ; Test case 3
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findpath ( int graph [ ] [ 5 ] , int n ) { vector < int > numofadj ; for ( int i = 0 ; i < n ; i ++ ) numofadj . push_back ( accumulate ( graph [ i ] , graph [ i ] + 5 , 0 ) ) ; int startpoint = 0 , numofodd = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( numofadj [ i ] % 2 == 1 ) { numofodd ++ ; startpoint = i ; } } if ( numofodd > 2 ) { cout << " No ▁ Solution " << endl ; return ; } stack < int > stack ; vector < int > path ; int cur = startpoint ; while ( ! stack . empty ( ) or accumulate ( graph [ cur ] , graph [ cur ] + 5 , 0 ) != 0 ) { if ( accumulate ( graph [ cur ] , graph [ cur ] + 5 , 0 ) == 0 ) { path . push_back ( cur ) ; cur = stack . top ( ) ; stack . pop ( ) ; } else { for ( int i = 0 ; i < n ; i ++ ) { if ( graph [ cur ] [ i ] == 1 ) { stack . push ( cur ) ; graph [ cur ] [ i ] = 0 ; graph [ i ] [ cur ] = 0 ; cur = i ; break ; } } } } for ( auto ele : path ) cout << ele << " ▁ - > ▁ " ; cout << cur << endl ; } int main ( ) { int graph1 [ ] [ 5 ] = { { 0 , 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 0 , 1 , 0 } , { 0 , 1 , 1 , 0 , 0 } , { 1 , 0 , 0 , 0 , 0 } } ; int n = sizeof ( graph1 ) / sizeof ( graph1 [ 0 ] ) ; findpath ( graph1 , n ) ; int graph2 [ ] [ 5 ] = { { 0 , 1 , 0 , 1 , 1 } , { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 , 1 } , { 1 , 1 , 1 , 0 , 0 } , { 1 , 0 , 1 , 0 , 0 } } ; n = sizeof ( graph1 ) / sizeof ( graph1 [ 0 ] ) ; findpath ( graph2 , n ) ; int graph3 [ ] [ 5 ] = { { 0 , 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 , 1 } , { 0 , 1 , 0 , 1 , 0 } , { 0 , 1 , 1 , 0 , 1 } , { 1 , 1 , 0 , 1 , 0 } } ; n = sizeof ( graph1 ) / sizeof ( graph1 [ 0 ] ) ; findpath ( graph3 , n ) ; }
Array element with minimum sum of absolute differences | C ++ implementation of the approach ; Function to return the minimized sum ; Sort the array ; Median of the array ; Calculate the minimized sum ; Return the required sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSum ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int x = arr [ n / 2 ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += abs ( arr [ i ] - x ) ; return sum ; } int main ( ) { int arr [ ] = { 1 , 3 , 9 , 3 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minSum ( arr , n ) ; return 0 ; }
Sort even and odd placed elements in increasing order | C ++ implementation of above approach ; function to print the odd and even indexed digits ; lists to store the odd and even positioned digits ; traverse through all the indexes in the integer ; if the digit is in odd_index position append it to odd_position list ; else append it to the even_position list ; print the elements in the list in sorted order ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void odd_even ( int arr [ ] , int n ) { vector < int > odd_indexes ; vector < int > even_indexes ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) odd_indexes . push_back ( arr [ i ] ) ; else even_indexes . push_back ( arr [ i ] ) ; } sort ( odd_indexes . begin ( ) , odd_indexes . end ( ) ) ; sort ( even_indexes . begin ( ) , even_indexes . end ( ) ) ; for ( int i = 0 ; i < odd_indexes . size ( ) ; i ++ ) cout << odd_indexes [ i ] << " ▁ " ; for ( int i = 0 ; i < even_indexes . size ( ) ; i ++ ) cout << even_indexes [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 3 , 2 , 7 , 6 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; odd_even ( arr , n ) ; }
Number of pairs whose sum is a power of 2 | C ++ implementation of above approach ; Function to return the count of valid pairs ; Storing occurrences of each element ; Sort the array in deceasing order ; Start taking largest element each time ; If element has already been paired ; Find the number which is greater than a [ i ] and power of two ; If there is a number which adds up with a [ i ] to form a power of two ; Edge case when a [ i ] and crr - a [ i ] is same and we have only one occurrence of a [ i ] then it cannot be paired ; Remove already paired elements ; Return the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int a [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ a [ i ] ] ++ ; sort ( a , a + n , greater < int > ( ) ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp [ a [ i ] ] < 1 ) continue ; int cur = 1 ; while ( cur <= a [ i ] ) cur <<= 1 ; if ( mp [ cur - a [ i ] ] ) { if ( cur - a [ i ] == a [ i ] and mp [ a [ i ] ] == 1 ) continue ; count ++ ; mp [ cur - a [ i ] ] -- ; mp [ a [ i ] ] -- ; } } return count ; } int main ( ) { int a [ ] = { 3 , 11 , 14 , 5 , 13 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << countPairs ( a , n ) ; return 0 ; }
In | Merge In Place in C ++ ; Both sorted sub - arrays must be adjacent in ' a ' ' an ' is the length of the first sorted section in ' a ' ' bn ' is the length of the second sorted section in ' a ' ; Return right now if we 're done ; Do insertion sort to merge if size of sub - arrays are small enough ; p -- ) Insert Sort A into B ; p ++ ) Insert Sort B into A ; Find the pivot points . Basically this is just finding the point in ' a ' where we can swap in the first part of ' b ' such that after the swap the last element in ' a ' will be less than or equal to the least element in ' b ' ; Swap first part of b with last part of a ; Now merge the two sub - array pairings ; } merge_array_inplace ; Merge Sort Implementation ; Sort first and second halves ; Now merge the two sorted sub - arrays together ; Function to print an array ; Driver program to test sort utility
#include <iostream> NEW_LINE using namespace std ; #define __INSERT_THRESH  5 NEW_LINE #define __swap ( x , y )  (t = *(x), *(x) = *(y), *(y) = t) NEW_LINE static void merge ( int * a , size_t an , size_t bn ) { int * b = a + an , * e = b + bn , * s , t ; if ( an == 0 || bn == 0 || ! ( * b < * ( b - 1 ) ) ) return ; if ( an < __INSERT_THRESH && an <= bn ) { for ( int * p = b , * v ; p > a ; for ( v = p , s = p - 1 ; v < e && * v < * s ; s = v , v ++ ) __swap ( s , v ) ; return ; } if ( bn < __INSERT_THRESH ) { for ( int * p = b , * v ; p < e ; for ( s = p , v = p - 1 ; s > a && * s < * v ; s = v , v -- ) __swap ( s , v ) ; return ; } int * pa = a , * pb = b ; for ( s = a ; s < b && pb < e ; s ++ ) if ( * pb < * pa ) pb ++ ; else pa ++ ; pa += b - s ; for ( int * la = pa , * fb = b ; la < b ; la ++ , fb ++ ) __swap ( la , fb ) ; merge ( a , pa - a , pb - b ) ; merge ( b , pb - b , e - pb ) ; #undef __swap #undef __INSERT_THRESH void merge_sort ( int * a , size_t n ) { size_t m = ( n + 1 ) / 2 ; if ( m > 1 ) merge_sort ( a , m ) ; if ( n - m > 1 ) merge_sort ( a + m , n - m ) ; merge ( a , m , n - m ) ; } void print_array ( int a [ ] , size_t n ) { if ( n > 0 ) { cout << " ▁ " << a [ 0 ] ; for ( size_t i = 1 ; i < n ; i ++ ) cout << " ▁ " << a [ i ] ; } cout << " STRNEWLINE " ; } int main ( ) { int a [ ] = { 3 , 16 , 5 , 14 , 8 , 10 , 7 , 15 , 1 , 13 , 4 , 9 , 12 , 11 , 6 , 2 } ; size_t n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; merge_sort ( a , n ) ; print_array ( a , n ) ; return 0 ; }
Maximizing the elements with a [ i + 1 ] > a [ i ] | C ++ implementation of the approach ; returns the number of positions where A ( i + 1 ) is greater than A ( i ) after rearrangement of the array ; Creating a HashMap containing char as a key and occurrences as a value ; Find the maximum frequency ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countMaxPos ( int arr [ ] , int n ) { unordered_map < int , int > map ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . count ( arr [ i ] ) ) map . insert ( { arr [ i ] , ( map . count ( arr [ i ] ) + 1 ) } ) ; else map . insert ( { arr [ i ] , 1 } ) ; } int max_freq = 0 ; for ( auto i : map ) { if ( max_freq < i . second ) { max_freq = i . second ; } } return n - max_freq ; } int main ( ) { int arr [ ] = { 20 , 30 , 10 , 50 , 40 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ( countMaxPos ( arr , n ) ) ; }
Permutation of an array that has smaller values from another array | C ++ program to find permutation of an array that has smaller values from another array ; Function to print required permutation ; Storing elements and indexes ; Filling the answer array ; pair element of A and B ; Fill the remaining elements of answer ; Output required permutation ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void anyPermutation ( int A [ ] , int B [ ] , int n ) { vector < pair < int , int > > Ap , Bp ; for ( int i = 0 ; i < n ; i ++ ) Ap . push_back ( make_pair ( A [ i ] , i ) ) ; for ( int i = 0 ; i < n ; i ++ ) Bp . push_back ( make_pair ( B [ i ] , i ) ) ; sort ( Ap . begin ( ) , Ap . end ( ) ) ; sort ( Bp . begin ( ) , Bp . end ( ) ) ; int i = 0 , j = 0 , ans [ n ] = { 0 } ; vector < int > remain ; while ( i < n && j < n ) { if ( Ap [ i ] . first > Bp [ j ] . first ) { ans [ Bp [ j ] . second ] = Ap [ i ] . first ; i ++ ; j ++ ; } else { remain . push_back ( i ) ; i ++ ; } } j = 0 ; for ( int i = 0 ; i < n ; ++ i ) if ( ans [ i ] == 0 ) { ans [ i ] = Ap [ remain [ j ] ] . first ; j ++ ; } for ( int i = 0 ; i < n ; ++ i ) cout << ans [ i ] << " ▁ " ; } int main ( ) { int A [ ] = { 12 , 24 , 8 , 32 } ; int B [ ] = { 13 , 25 , 32 , 11 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; anyPermutation ( A , B , n ) ; return 0 ; }
Rearrange all elements of array which are multiples of x in increasing order | C ++ implementation of the approach ; Function to sort all the multiples of x from the array in ascending order ; Insert all multiples of 5 to a vector ; Sort the vector ; update the array elements ; Driver code ; Print the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortMultiples ( int arr [ ] , int n , int x ) { vector < int > v ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % x == 0 ) v . push_back ( arr [ i ] ) ; sort ( v . begin ( ) , v . end ( ) ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % x == 0 ) arr [ i ] = v [ j ++ ] ; } } int main ( ) { int arr [ ] = { 125 , 3 , 15 , 6 , 100 , 5 } ; int x = 5 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortMultiples ( arr , n , x ) ; for ( int i = 0 ; i < n ; i ++ ) { cout << arr [ i ] << " ▁ " ; } return 0 ; }
Minimum increment in the sides required to get non | C ++ program to find Minimum increase in sides to get non - negative area of a triangle ; Function to return the minimum increase in side lengths of the triangle ; push the three sides to a array ; sort the array ; check if sum is greater than third side ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumIncrease ( int a , int b , int c ) { int arr [ ] = { a , b , c } ; sort ( arr , arr + 3 ) ; if ( arr [ 0 ] + arr [ 1 ] >= arr [ 2 ] ) return 0 ; else return arr [ 2 ] - ( arr [ 0 ] + arr [ 1 ] ) ; } int main ( ) { int a = 3 , b = 5 , c = 10 ; cout << minimumIncrease ( a , b , c ) ; return 0 ; }
Minimum sum of differences with an element in an array | C ++ program to find minimum sum of absolute differences with an array element . ; function to find min sum after operation ; Sort the array ; Pick middle value ; Sum of absolute differences . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int absSumDidd ( int a [ ] , int n ) { sort ( a , a + n ) ; int midValue = a [ ( int ) ( n / 2 ) ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + abs ( a [ i ] - midValue ) ; } return sum ; } int main ( ) { int arr [ ] = { 5 , 11 , 14 , 10 , 17 , 15 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << absSumDidd ( arr , n ) ; }
Printing frequency of each character just after its consecutive occurrences | CPP program to print run length encoding of a string ; Counting occurrences of s [ i ] ; Driver code
#include <iostream> NEW_LINE using namespace std ; void printRLE ( string s ) { for ( int i = 0 ; s [ i ] != ' \0' ; i ++ ) { int count = 1 ; while ( s [ i ] == s [ i + 1 ] ) { i ++ ; count ++ ; } cout << s [ i ] << count << " ▁ " ; } cout << endl ; } int main ( ) { printRLE ( " GeeeEEKKKss " ) ; printRLE ( " ccccOddEEE " ) ; return 0 ; }
Word Ladder ( Length of shortest chain to reach a target word ) | C ++ program to find length of the shortest chain transformation from source to target ; Returns length of shortest chain to reach ' target ' from ' start ' using minimum number of adjacent moves . D is dictionary ; If the target string is not present in the dictionary ; To store the current chain length and the length of the words ; Push the starting word into the queue ; While the queue is non - empty ; Increment the chain length ; Current size of the queue ; Since the queue is being updated while it is being traversed so only the elements which were already present in the queue before the start of this loop will be traversed for now ; Remove the first word from the queue ; For every character of the word ; Retain the original character at the current position ; Replace the current character with every possible lowercase alphabet ; If the new word is equal to the target word ; Remove the word from the set if it is found in it ; And push the newly generated word which will be a part of the chain ; Restore the original character at the current position ; Driver program ; make dictionary
#include <bits/stdc++.h> NEW_LINE using namespace std ; int shortestChainLen ( string start , string target , set < string > & D ) { if ( start == target ) return 0 ; if ( D . find ( target ) == D . end ( ) ) return 0 ; int level = 0 , wordlength = start . size ( ) ; queue < string > Q ; Q . push ( start ) ; while ( ! Q . empty ( ) ) { ++ level ; int sizeofQ = Q . size ( ) ; for ( int i = 0 ; i < sizeofQ ; ++ i ) { string word = Q . front ( ) ; Q . pop ( ) ; for ( int pos = 0 ; pos < wordlength ; ++ pos ) { char orig_char = word [ pos ] ; for ( char c = ' a ' ; c <= ' z ' ; ++ c ) { word [ pos ] = c ; if ( word == target ) return level + 1 ; if ( D . find ( word ) == D . end ( ) ) continue ; D . erase ( word ) ; Q . push ( word ) ; } word [ pos ] = orig_char ; } } } return 0 ; } int main ( ) { set < string > D ; D . insert ( " poon " ) ; D . insert ( " plee " ) ; D . insert ( " same " ) ; D . insert ( " poie " ) ; D . insert ( " plie " ) ; D . insert ( " poin " ) ; D . insert ( " plea " ) ; string start = " toon " ; string target = " plea " ; cout << " Length ▁ of ▁ shortest ▁ chain ▁ is : ▁ " << shortestChainLen ( start , target , D ) ; return 0 ; }
Reverse tree path | CPP program for the above approach ; Function to print inorder traversal of the tree ; Utility function to track root to leaf paths ; Check if root is null then return ; Store the node in path array ; Check if we find the node upto which oath needs to be reversed ; Current path array contains the path which needs to be reversed ; Swap the data of two nodes ; Check if the node is a leaf node then return ; Call utility function for left and right subtree recursively ; Function to reverse tree path ; Initialize a vector to store paths ; Driver Code ; 7 / \ 6 5 / \ / \ 4 3 2 1
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define nl  " NEW_LINE " class Node { public : int data ; Node * left ; Node * right ; Node ( int value ) { data = value ; } } ; void inorder ( Node * temp ) { if ( temp == NULL ) return ; inorder ( temp -> left ) ; cout << temp -> data << " ▁ " ; inorder ( temp -> right ) ; } void reverseTreePathUtil ( Node * root , vector < Node * > path , int pathLen , int key ) { if ( root == NULL ) return ; path [ pathLen ] = root ; pathLen ++ ; if ( root -> data == key ) { int i = 0 , j = pathLen - 1 ; while ( i < j ) { int temp = path [ i ] -> data ; path [ i ] -> data = path [ j ] -> data ; path [ j ] -> data = temp ; i ++ ; j -- ; } } if ( ! root -> left and ! root -> right ) return ; reverseTreePathUtil ( root -> left , path , pathLen , key ) ; reverseTreePathUtil ( root -> right , path , pathLen , key ) ; } void reverseTreePath ( Node * root , int key ) { if ( root == NULL ) return ; vector < Node * > path ( 50 , NULL ) ; reverseTreePathUtil ( root , path , 0 , key ) ; } int main ( ) { Node * root = new Node ( 7 ) ; root -> left = new Node ( 6 ) ; root -> right = new Node ( 5 ) ; root -> left -> left = new Node ( 4 ) ; root -> left -> right = new Node ( 3 ) ; root -> right -> left = new Node ( 2 ) ; root -> right -> right = new Node ( 1 ) ; int key = 4 ; reverseTreePath ( root , key ) ; inorder ( root ) ; return 0 ; }
Find if an array of strings can be chained to form a circle | Set 2 | C ++ code to check if cyclic order is possible among strings under given constrainsts ; Utility method for a depth first search among vertices ; Returns true if all vertices are strongly connected i . e . can be made as loop ; Initialize all vertices as not visited ; perform a dfs from s ; now loop through all characters ; I character is marked ( i . e . it was first or last character of some string ) then it should be visited in last dfs ( as for looping , graph should be strongly connected ) ; If we reach that means graph is connected ; return true if an order among strings is possible ; Create an empty graph ; Initialize all vertices as not marked ; Initialize indegree and outdegree of every vertex as 0. ; Process all strings one by one ; Find first and last characters ; Mark the characters ; increase indegree and outdegree count ; Add an edge in graph ; If for any character indegree is not equal to outdegree then ordering is not possible ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define M  26 NEW_LINE void dfs ( vector < int > g [ ] , int u , vector < bool > & visit ) { visit [ u ] = true ; for ( int i = 0 ; i < g [ u ] . size ( ) ; ++ i ) if ( ! visit [ g [ u ] [ i ] ] ) dfs ( g , g [ u ] [ i ] , visit ) ; } bool isConnected ( vector < int > g [ ] , vector < bool > & mark , int s ) { vector < bool > visit ( M , false ) ; dfs ( g , s , visit ) ; for ( int i = 0 ; i < M ; i ++ ) { if ( mark [ i ] && ! visit [ i ] ) return false ; } return true ; } bool possibleOrderAmongString ( string arr [ ] , int N ) { vector < int > g [ M ] ; vector < bool > mark ( M , false ) ; vector < int > in ( M , 0 ) , out ( M , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) { int f = arr [ i ] . front ( ) - ' a ' ; int l = arr [ i ] . back ( ) - ' a ' ; mark [ f ] = mark [ l ] = true ; in [ l ] ++ ; out [ f ] ++ ; g [ f ] . push_back ( l ) ; } for ( int i = 0 ; i < M ; i ++ ) if ( in [ i ] != out [ i ] ) return false ; return isConnected ( g , mark , arr [ 0 ] . front ( ) - ' a ' ) ; } int main ( ) { string arr [ ] = { " ab " , " bc " , " cd " , " de " , " ed " , " da " } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( possibleOrderAmongString ( arr , N ) == false ) cout << " Ordering ▁ not ▁ possible STRNEWLINE " ; else cout << " Ordering ▁ is ▁ possible STRNEWLINE " ; return 0 ; }
Dynamic Connectivity | Set 1 ( Incremental ) | C ++ implementation of incremental connectivity ; Finding the root of node i ; union of two nodes a and b ; union based on rank ; Returns true if two nodes have same root ; Performing an operation according to query type ; type 1 query means checking if node x and y are connected or not ; If roots of x and y is same then yes is the answer ; type 2 query refers union of x and y ; If x and y have different roots then union them ; Driver function ; No . of nodes ; The following two arrays are used to implement disjoint set data structure . arr [ ] holds the parent nodes while rank array holds the rank of subset ; initializing both array and rank ; number of queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; int root ( int arr [ ] , int i ) { while ( arr [ i ] != i ) { arr [ i ] = arr [ arr [ i ] ] ; i = arr [ i ] ; } return i ; } void weighted_union ( int arr [ ] , int rank [ ] , int a , int b ) { int root_a = root ( arr , a ) ; int root_b = root ( arr , b ) ; if ( rank [ root_a ] < rank [ root_b ] ) { arr [ root_a ] = arr [ root_b ] ; rank [ root_b ] += rank [ root_a ] ; } else { arr [ root_b ] = arr [ root_a ] ; rank [ root_a ] += rank [ root_b ] ; } } bool areSame ( int arr [ ] , int a , int b ) { return ( root ( arr , a ) == root ( arr , b ) ) ; } void query ( int type , int x , int y , int arr [ ] , int rank [ ] ) { if ( type == 1 ) { if ( areSame ( arr , x , y ) == true ) cout << " Yes " << endl ; else cout << " No " << endl ; } else if ( type == 2 ) { if ( areSame ( arr , x , y ) == false ) weighted_union ( arr , rank , x , y ) ; } } int main ( ) { int n = 7 ; int arr [ n ] , rank [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = i ; rank [ i ] = 1 ; } int q = 11 ; query ( 1 , 0 , 1 , arr , rank ) ; query ( 2 , 0 , 1 , arr , rank ) ; query ( 2 , 1 , 2 , arr , rank ) ; query ( 1 , 0 , 2 , arr , rank ) ; query ( 2 , 0 , 2 , arr , rank ) ; query ( 2 , 2 , 3 , arr , rank ) ; query ( 2 , 3 , 4 , arr , rank ) ; query ( 1 , 0 , 5 , arr , rank ) ; query ( 2 , 4 , 5 , arr , rank ) ; query ( 2 , 5 , 6 , arr , rank ) ; query ( 1 , 2 , 6 , arr , rank ) ; return 0 ; }