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 */ 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) { for (Map.Entry> edge : redGraph.entrySet()) { int from = edge.getKey(); for(Integer to : edge.getValue()) { if( dfs(blueGraph, to, from, new HashSet<>())) { return true; } } } return false; } 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); } }