Go to diff to previous submission
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package test; import java.util.HashSet; import java.util.LinkedList; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; /** * * @author cteam043 */ public class Fn { static class Node{ LinkedList<Integer> children; int state; int number; int comp; public Node() { children = new LinkedList<Integer>(); } } static class Edge{ int from, to; public Edge(int from, int to) { this.from = from; this.to = to; } @Override if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Edge other = (Edge) obj; if (this.from == other.to && this.to == other.from) { return true; } if (this.from != other.from || this.to != other.from) { return false; } if (this.to != other.to || this.from != other.to) { return false; } return true; } @Override public int hashCode() { int hash = 3; hash = 89 * hash + this.from + this.to; return hash; } } static void BFS(Node start, Set<Integer> from){ LinkedList<Integer> q = new LinkedList<Integer>(); q.add(start.number); while(!q.isEmpty()){ int tmp = q.getFirst(); q.pollFirst(); if(from.contains(tmp)) from.remove(tmp); else continue; for(int n : uzly[tmp].children){ q.add(n); } } } static boolean Kostra(Node start){ HashSet<Edge> ns = new HashSet<Edge>(); LinkedList<Integer> q = new LinkedList<Integer>(); HashSet<Integer> set = new HashSet<Integer>(); q.add(start.number); while(!q.isEmpty()){ q.pollFirst(); set.add(f); if(uzly[integer].state == 0){ uzly[integer].state = 1; q.push(integer); ns.add(new Edge(f, integer)); } } } for(Edge e : ns){ stupne[e.from]++; stupne[e.to]++; } int res = 0; if(stupne[i] == 1) res++; } if(res >= 4) return true; else return false; } static Node [] uzly = new Node[10005]; static{ for(int i = 0; i < uzly.length; i++){ uzly[i] = new Node(); uzly[i].number = i; } } static int [] stupne = new int [10005]; //static int [][] inci = new int[10005][10005]; boolean bEnd; int points, lines, n1, n2; TreeSet<Integer> set = new TreeSet<Integer>(); LinkedList<Integer> comps = new LinkedList<Integer>(); while(in.hasNextInt()){ bEnd = false; points = in.nextInt(); lines = in.nextInt(); set.clear(); comps.clear(); for(int p = 0; p < stupne.length; p++){ stupne[p] = 0; uzly[p].children.clear(); uzly[p].state = 0; uzly[p].comp = 0; } // for(int k = 0; k < stupne.length; k++) // for(int l = 0; l < stupne.length; l++) // inci[k][l] = 0; for(int i = 0; i < lines; i++){ n1 = in.nextInt(); n2 = in.nextInt(); uzly[n1].children.add(n2); uzly[n2].children.add(n1); set.add(n1); set.add(n2); } int i = 1; while(set.size() != 0){ comps.add(set.first()); //System.out.println("Comp " + i + " " +set.first()); BFS(uzly[set.first()], set); } if(Kostra(uzly[g])){ bEnd = true; break; } } if(!bEnd) } } }
--- c5.s693.cteam043.fn.java.0.Fn.java +++ c5.s1120.cteam043.fn.java.0.Fn.java @@ -4,7 +4,11 @@ */ -package basic; +package test; +import java.util.HashSet; +import java.util.LinkedList; import java.util.Scanner; +import java.util.Set; +import java.util.TreeSet; /** @@ -14,30 +18,173 @@ public class Fn { + static class Node{ + LinkedList<Integer> children; + int state; + int number; + int comp; + + public Node() { + children = new LinkedList<Integer>(); + } + + } + + static class Edge{ + int from, to; + + public Edge(int from, int to) { + this.from = from; + this.to = to; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Edge other = (Edge) obj; + if (this.from == other.to && this.to == other.from) { + return true; + } + if (this.from != other.from || this.to != other.from) { + return false; + } + if (this.to != other.to || this.from != other.to) { + return false; + } + return true; + } + + @Override + public int hashCode() { + int hash = 3; + hash = 89 * hash + this.from + this.to; + return hash; + } + + } + + static void BFS(Node start, Set<Integer> from){ + LinkedList<Integer> q = new LinkedList<Integer>(); + + q.add(start.number); + while(!q.isEmpty()){ + int tmp = q.getFirst(); + q.pollFirst(); + if(from.contains(tmp)) + from.remove(tmp); + else + continue; + + for(int n : uzly[tmp].children){ + q.add(n); + } + } + } + + static boolean Kostra(Node start){ + HashSet<Edge> ns = new HashSet<Edge>(); + LinkedList<Integer> q = new LinkedList<Integer>(); + HashSet<Integer> set = new HashSet<Integer>(); + + q.add(start.number); + while(!q.isEmpty()){ + Integer f = q.getFirst(); + q.pollFirst(); + set.add(f); + for (Integer integer : uzly[f].children) { + if(uzly[integer].state == 0){ + uzly[integer].state = 1; + q.push(integer); + ns.add(new Edge(f, integer)); + } + } + } + + for(Edge e : ns){ + stupne[e.from]++; + stupne[e.to]++; + } + int res = 0; + for(Integer i : set){ + if(stupne[i] == 1) + res++; + } + if(res >= 4) + return true; + else + return false; + + } + + static Node [] uzly = new Node[10005]; + static{ + for(int i = 0; i < uzly.length; i++){ + uzly[i] = new Node(); + uzly[i].number = i; + } + } + static int [] stupne = new int [10005]; + //static int [][] inci = new int[10005][10005]; + public static void main(String[] args) { + boolean bEnd; Scanner in = new Scanner(System.in); int points, lines, n1, n2; - int [] nodes = new int [10005]; + + TreeSet<Integer> set = new TreeSet<Integer>(); + LinkedList<Integer> comps = new LinkedList<Integer>(); + + while(in.hasNextInt()){ + bEnd = false; points = in.nextInt(); lines = in.nextInt(); + set.clear(); + comps.clear(); - for(int p = 0; p < nodes.length; p++) - nodes[p] = 0; + for(int p = 0; p < stupne.length; p++){ + stupne[p] = 0; + + uzly[p].children.clear(); + uzly[p].state = 0; + uzly[p].comp = 0; + } + +// for(int k = 0; k < stupne.length; k++) +// for(int l = 0; l < stupne.length; l++) +// inci[k][l] = 0; for(int i = 0; i < lines; i++){ n1 = in.nextInt(); n2 = in.nextInt(); - nodes[n1]++; - nodes[n2]++; + uzly[n1].children.add(n2); + uzly[n2].children.add(n1); + set.add(n1); + set.add(n2); } - for(int p = 0; p < nodes.length; p++){ - if(nodes[p] >= 4){ + int i = 1; + while(set.size() != 0){ + comps.add(set.first()); + //System.out.println("Comp " + i + " " +set.first()); + BFS(uzly[set.first()], set); + + } + + for(Integer g : comps){ + if(Kostra(uzly[g])){ System.out.println("YES"); - return; + bEnd = true; + break; } } - System.out.println("NO"); + if(!bEnd) + System.out.println("NO"); + } }