Go to diff to previous submission
import java.util.*; /** * * @author cteam039 */ public class Fn { int points, lines; while (scan.hasNextInt()) { iteration(scan); } } public static void iteration(Scanner scan) { int points, lines; points = scan.nextInt(); lines = scan.nextInt(); List<List<Graph>> graphs = new ArrayList<List<Fn.Graph>>(); if (points < 2 || lines < 1) { return; } for (int i = 0; i < lines; i++) { int a = scan.nextInt(); int b = scan.nextInt(); List<List<Graph>> sets = new ArrayList<List<Graph>>(); boolean added = false; for(List<Graph> graph : graphs){ added = insert(graph, new Graph(a), new Graph(b)); if(added){ sets.add(graph); } } if(!added){ List<Graph> gr= new ArrayList<Graph>(); graphs.add(gr); gr.add(new Graph(a)); gr.add(new Graph(b)); } else if(sets.size() > 1) { for(int j = 1; j < sets.size(); j++){ for(Graph g : sets.get(j)){ if(!sets.get(0).contains(g)){ sets.get(0).add(g); } } graphs.remove(sets.get(j)); } int counter = 0; for(Graph g: sets.get(0)){ if(g.incide == 1){ counter ++; } } if(counter > 3 ){ return; } } } for(List<Graph> list : graphs){ int counter = 0; for(Graph g: list){ if(g.incide == 1){ counter ++; } } if(counter > 3 ){ return; } } } public static boolean insert(List<Graph> graph, Graph a, Graph b){ if(graph.contains(a)){ if(graph.contains(b)){ return true; } else{ graph.get(graph.indexOf(a)).incide++; graph.add(b); return true; } } else{ if(graph.contains(b)){ graph.get(graph.indexOf(b)).incide++; graph.add(a); return true; } else{ return false; } } } private static class Graph { int id; int incide; public Graph(int id) { this.id = id; incide = 1; } @Override if(!(arg0 instanceof Graph)){ return false; } return ((Graph)arg0).id == id; } @Override public int hashCode() { int hash = 5; hash = 79 * hash + this.id; return hash; } } }
--- c5.s973.cteam039.fn.java.0.Fn.java +++ c5.s984.cteam039.fn.java.0.Fn.java @@ -57,4 +57,14 @@ } + int counter = 0; + for(Graph g: sets.get(0)){ + if(g.incide == 1){ + counter ++; + } + } + if(counter > 3 ){ + System.out.println("YES"); + return; + } } }