import java.util.*; /* 3 3 0 1 1 0 2 1 1 2 0 NO 4 4 0 1 1 1 2 1 2 3 0 3 0 0 YES 3 3 0 1 0 0 2 0 1 2 1 NO 4 4 0 1 0 1 2 0 2 3 1 3 0 1 YES 9 11 1 2 0 2 3 1 3 4 1 4 5 1 4 9 1 1 6 1 6 7 1 7 2 0 7 8 1 8 5 0 8 9 0 YES */ public class Nightmare { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); // vertexes int m = in.nextInt(); // edges Map> redGraph = new HashMap<>(); Map> blueGraph = new HashMap<>(); for (int i = 0; i < m; i++) { int from = in.nextInt(); int to = in.nextInt(); int color = in.nextInt(); if(color == 0) { addEdge(redGraph, from, to); addEdge(redGraph, to, from); } else { addEdge(blueGraph,from, to); addEdge(blueGraph,to, from); } } boolean found = search(redGraph, blueGraph) || search(blueGraph, redGraph); System.out.println(found?"NO":"YES"); } private static boolean search(Map> redGraph, Map> blueGraph) { int[] componentOfVertex = new int[300001]; // map vertex -> component number Set vertices = redGraph.keySet(); int nextComponent = 1; for (int vertex : vertices) { if(componentOfVertex[vertex]==0) { identifyComponent(componentOfVertex, redGraph, nextComponent, vertex); nextComponent++; } } for (Map.Entry> edge : blueGraph.entrySet()) { int from = edge.getKey(); for(int to : edge.getValue()) { // from < to -- take only from->to, not to->from if(componentOfVertex[from]!=0 && componentOfVertex[from] == componentOfVertex[to]) { return true; } } } return false; } private static void identifyComponent(int[] componentOfVertex, Map> graph, int nextComponent, int vertex) { componentOfVertex[vertex] = nextComponent; for (int next : graph.get(vertex)) { if(componentOfVertex[next]==0) { identifyComponent(componentOfVertex, graph, nextComponent, next); } } } // private static boolean dfs(Map> blueGraph, int from, int to, Set visited) { // if(to==from) { // return true; // } // if(blueGraph.containsKey(from)) { // visited.add(from); // for (int current : blueGraph.get(from)) { // if (!visited.contains(current)) { // if (dfs(blueGraph, current, to, visited)) { // return true; // } // } // } // visited.remove(from); // } // return false; // } private static void addEdge(Map> graph, int from, int to) { graph.putIfAbsent(from, new ArrayList<>()); graph.get(from).add(to); } }