FIILMTR what is wrong with my logic

Problem Statement : [FILLMTR][1]

My Solution :

import java.util.*;
import java.lang.*;
import java.io.*;
 
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{	public static void main (String[] args) throws java.lang.Exception{
	    Scanner scan = new Scanner(System.in);
        int t = scan.nextInt();
        
        while(t-- != 0){
            int n = scan.nextInt();
            int q = scan.nextInt();
            int[] arr = new int[n];
            Arrays.fill(arr, -2);
            boolean flag = true;
            
            for(int i = 0; i < q; i++){
                int x = scan.nextInt() - 1;
                int y = scan.nextInt() - 1;
                int val = scan.nextInt();
                if(!flag)
                    continue;
                    
                if(arr[x] == -2 && arr[y] == -2){
                    if(val == 0){
                        arr[x] = 0;
                        arr[y] = 0;
                    }
                    else{
                        arr[x] = 1;
                        arr[y] = 0;
                    }
                }
                
                else if(arr[x] == -2){
                    if(val == arr[y]){
                        arr[x] = 0;
                    }
                    else{
                        arr[x] = 1;
                    }
                }
                
                else if(arr[y] == -2){
                    if(val == arr[x]){
                        arr[y] = 0;
                    }
                    else{
                        arr[y] = 1;
                    }
                }
                
                else{
                    if(val == 1)
                        flag = arr[x] != arr[y];
                    
                    else
                        flag = arr[x] == arr[y];
                }
            }
            
            if(flag)
                System.out.println("yes");
            else
                System.out.println("no");
            
        }
	}
}  

I saw the solution using the graph coloring approach but I am not able to understand what is wrong with my logic and the testcases which would give WA for this.
[1]: https://www.codechef.com/problems/FILLMTR