Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.23 KB | None | 0 0
  1. package pal;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.util.ArrayList;
  8. import java.util.Collection;
  9. import java.util.Collections;
  10. import java.util.HashMap;
  11. import java.util.HashSet;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.Set;
  15. import java.util.StringTokenizer;
  16. import static pal.Main.edgeCount;
  17. import static pal.Main.nodeCount;
  18.  
  19. class Graph {
  20.     int id;
  21.     String cert = "";
  22.     ArrayList<Node> nodes = new ArrayList();
  23.     Set<Node> cycle = new HashSet();
  24.     Node root;
  25.    
  26.     public void getCycle() {                
  27.         for (Node node : nodes) {
  28.             for (Node n : node.con) {
  29.                 n.predecessors++;
  30.             }
  31.         }
  32.        
  33.         for (Node node : nodes) {
  34.             if (node.predecessors > 1) {
  35.                 cycle.add(node);
  36.             }
  37.         }
  38.        
  39.         for (Node node : cycle) {
  40.             for (Node n : node.con) {
  41.                 if (cycle.contains(n)) node.next = n;
  42.             }
  43.         }
  44.        
  45.         for (Node node : cycle) {
  46.             Node n = node.next;
  47.             n.prev = node;
  48.         }
  49.     }
  50.    
  51.     public void getAncestors() {
  52.         for (Node n : root.con) {
  53.             n.getAncestor(root);
  54.         }
  55.     }
  56.    
  57.     public void getCertificate() {
  58.         boolean hasLeafs = true;
  59.         while (hasLeafs) {
  60.             Set<Node> leafs = new HashSet();
  61.             for (Node n : nodes) {
  62.                 if (n.con.isEmpty()) leafs.add(n);
  63.             }
  64.            
  65.             if (leafs.isEmpty()) {
  66.                 hasLeafs = false;
  67.                 continue;
  68.             }
  69.            
  70.             for (Node n : leafs) {
  71.                 n.getCertificate();
  72.                
  73.                 n.ancestor.labels.add(n.cert);
  74.                 n.ancestor.con.remove(n);
  75.                 nodes.remove(n);
  76.             }
  77.         }
  78.        
  79.         // rozuzluj smyčku
  80.         Map<Node, String> loopCerts = new HashMap();
  81.        
  82.         for (Node n : cycle) {
  83. //            n.getCertificate();
  84.             String newLoopCert ="2";
  85. //            newLoopCert += n.prev.cert;
  86. //            newLoopCert += "4";
  87. //            newLoopCert += n.cert;
  88. //            newLoopCert += "5";
  89. //            newLoopCert += n.next.cert;
  90.            
  91.             ArrayList<String> list = new ArrayList();
  92.             //list.add(n.cert);
  93.             //list.add(n.prev.cert);
  94.             n.next.getCertificate();
  95.             list.add(n.next.cert);
  96.             Collections.sort(list);
  97.             for (String str : list) {
  98.                 newLoopCert += str;
  99.             }
  100.            
  101.             newLoopCert += "3";
  102.            
  103.             loopCerts.put(n, newLoopCert);
  104.         }
  105.        
  106.         for (Node n : cycle) {
  107.             n.labels.add(loopCerts.get(n));
  108.             //n.cert = loopCerts.get(n);
  109.             //n.con.remove(n.next);
  110.         }
  111.         for (Node n : cycle) {
  112.             n.con.remove(n.next);
  113.         }
  114.  
  115.        
  116.         // znova hasLeafs jen s podmínkou na root
  117.         while (nodes.size() > 1) {
  118.             Set<Node> leafs = new HashSet();
  119.             for (Node n : nodes) {
  120.                 if (n.con.isEmpty()) leafs.add(n);
  121.             }
  122.            
  123.             if (leafs.isEmpty()) {
  124.                 hasLeafs = false;
  125.                 continue;
  126.             }
  127.            
  128.             for (Node n : leafs) {
  129.                 n.getCertificate();
  130.                
  131.                 n.ancestor.labels.add(n.cert);
  132.                 n.ancestor.con.remove(n);
  133.                 nodes.remove(n);
  134.             }
  135.         }
  136.        
  137.         root.getCertificate();
  138.         cert = root.cert;
  139.     }
  140. }
  141.  
  142. class Node {
  143.     int id;
  144.     int predecessors = 0;
  145.     ArrayList<Node> con = new ArrayList();
  146.     String cert = "01";
  147.     Node next, prev;
  148.     Node ancestor = null;
  149.     ArrayList<String> labels = new ArrayList();
  150.    
  151.     public void getAncestor(Node anc) {
  152.         this.ancestor = anc;
  153.         for (Node n : con) {
  154.             if (n != next) {
  155.                 n.getAncestor(this);
  156.             }
  157.         }
  158.     }
  159.    
  160.     public void getCertificate() {
  161.         if (labels.isEmpty()) return;
  162.        
  163.         String oldCert = cert.substring(1, cert.length()-1);
  164.         labels.add(oldCert);
  165.         Collections.sort(labels);
  166.         String newCert = "0";
  167.         for (String str : labels) {
  168.             newCert += str;
  169.         }
  170.         newCert += 1;
  171.         cert = newCert;
  172.         labels.clear();
  173.     }
  174. }
  175.  
  176. public class Main {
  177.    
  178.     public static ArrayList<Graph> graphs = new ArrayList();
  179.     public static int graphCount = 0;
  180.     public static int nodeCount = 0;
  181.     public static int edgeCount = 0;
  182.    
  183.     private static void parseData() throws IOException {
  184. //        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        
  185.         BufferedReader br = new BufferedReader(new FileReader("datapub/pub12.in"));
  186.        
  187.         StringTokenizer tokenizer;
  188.         tokenizer = new StringTokenizer(br.readLine());
  189.        
  190.         graphCount = Integer.parseInt(tokenizer.nextToken());
  191.         nodeCount = Integer.parseInt(tokenizer.nextToken());
  192.         edgeCount = Integer.parseInt(tokenizer.nextToken());
  193.        
  194.         for (int g = 0; g < graphCount; g++) {
  195.             Graph newGraph = new Graph();
  196.             newGraph.id = g;
  197.             newGraph.nodes.ensureCapacity(nodeCount);
  198.            
  199.             Set<Node> possibleRoots = new HashSet();
  200.            
  201.             for (int i = 0; i < nodeCount; i++) {
  202.                 Node newNode = new Node();
  203.                 newNode.id = i+1;
  204.                 newGraph.nodes.add(newNode);
  205.                 possibleRoots.add(newNode);
  206.             }
  207.            
  208.             for (int i = 0; i < edgeCount; i++) {
  209.                 tokenizer = new StringTokenizer(br.readLine());
  210.                 int from = Integer.parseInt(tokenizer.nextToken());
  211.                 int to = Integer.parseInt(tokenizer.nextToken());
  212.                 Node nodeFrom = newGraph.nodes.get(from-1);
  213.                 Node nodeTo = newGraph.nodes.get(to-1);
  214.                 nodeFrom.con.add(nodeTo);
  215.                 possibleRoots.remove(nodeTo);
  216.             }
  217.            
  218.             newGraph.root = possibleRoots.iterator().next();
  219.             newGraph.getCycle();
  220.             newGraph.getAncestors();
  221.            
  222.             graphs.add(newGraph);
  223.         }
  224.     }
  225.  
  226.     public static void main(String[] args) throws IOException {
  227.         parseData();
  228.         for (Graph g : graphs) {
  229.             g.getCertificate();
  230.         }
  231.        
  232.         Map<String, Integer> certMapping = new HashMap();
  233.        
  234.         for (Graph g : graphs) {
  235.             certMapping.putIfAbsent(g.cert, 0);
  236. //            System.out.println(g.cert);
  237.         }
  238.        
  239.         for (Graph g : graphs) {
  240.             int count = certMapping.get(g.cert);
  241.             count++;
  242.             certMapping.put(g.cert, count);
  243.         }
  244.        
  245.         ArrayList<Integer> list = new ArrayList(certMapping.values());
  246.         Collections.sort(list);
  247.        
  248.         for (Integer i : list) {
  249.             System.out.print(i + " ");
  250.         }
  251.     }
  252.    
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement