Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pal;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.StringTokenizer;
- import static pal.Main.edgeCount;
- import static pal.Main.nodeCount;
- class Graph {
- int id;
- String cert = "";
- ArrayList<Node> nodes = new ArrayList();
- Set<Node> cycle = new HashSet();
- Node root;
- public void getCycle() {
- for (Node node : nodes) {
- for (Node n : node.con) {
- n.predecessors++;
- }
- }
- for (Node node : nodes) {
- if (node.predecessors > 1) {
- cycle.add(node);
- }
- }
- for (Node node : cycle) {
- for (Node n : node.con) {
- if (cycle.contains(n)) node.next = n;
- }
- }
- for (Node node : cycle) {
- Node n = node.next;
- n.prev = node;
- }
- }
- public void getAncestors() {
- for (Node n : root.con) {
- n.getAncestor(root);
- }
- }
- public void getCertificate() {
- boolean hasLeafs = true;
- while (hasLeafs) {
- Set<Node> leafs = new HashSet();
- for (Node n : nodes) {
- if (n.con.isEmpty()) leafs.add(n);
- }
- if (leafs.isEmpty()) {
- hasLeafs = false;
- continue;
- }
- for (Node n : leafs) {
- n.getCertificate();
- n.ancestor.labels.add(n.cert);
- n.ancestor.con.remove(n);
- nodes.remove(n);
- }
- }
- // rozuzluj smyčku
- Map<Node, String> loopCerts = new HashMap();
- for (Node n : cycle) {
- // n.getCertificate();
- String newLoopCert ="2";
- // newLoopCert += n.prev.cert;
- // newLoopCert += "4";
- // newLoopCert += n.cert;
- // newLoopCert += "5";
- // newLoopCert += n.next.cert;
- ArrayList<String> list = new ArrayList();
- //list.add(n.cert);
- //list.add(n.prev.cert);
- n.next.getCertificate();
- list.add(n.next.cert);
- Collections.sort(list);
- for (String str : list) {
- newLoopCert += str;
- }
- newLoopCert += "3";
- loopCerts.put(n, newLoopCert);
- }
- for (Node n : cycle) {
- n.labels.add(loopCerts.get(n));
- //n.cert = loopCerts.get(n);
- //n.con.remove(n.next);
- }
- for (Node n : cycle) {
- n.con.remove(n.next);
- }
- // znova hasLeafs jen s podmínkou na root
- while (nodes.size() > 1) {
- Set<Node> leafs = new HashSet();
- for (Node n : nodes) {
- if (n.con.isEmpty()) leafs.add(n);
- }
- if (leafs.isEmpty()) {
- hasLeafs = false;
- continue;
- }
- for (Node n : leafs) {
- n.getCertificate();
- n.ancestor.labels.add(n.cert);
- n.ancestor.con.remove(n);
- nodes.remove(n);
- }
- }
- root.getCertificate();
- cert = root.cert;
- }
- }
- class Node {
- int id;
- int predecessors = 0;
- ArrayList<Node> con = new ArrayList();
- String cert = "01";
- Node next, prev;
- Node ancestor = null;
- ArrayList<String> labels = new ArrayList();
- public void getAncestor(Node anc) {
- this.ancestor = anc;
- for (Node n : con) {
- if (n != next) {
- n.getAncestor(this);
- }
- }
- }
- public void getCertificate() {
- if (labels.isEmpty()) return;
- String oldCert = cert.substring(1, cert.length()-1);
- labels.add(oldCert);
- Collections.sort(labels);
- String newCert = "0";
- for (String str : labels) {
- newCert += str;
- }
- newCert += 1;
- cert = newCert;
- labels.clear();
- }
- }
- public class Main {
- public static ArrayList<Graph> graphs = new ArrayList();
- public static int graphCount = 0;
- public static int nodeCount = 0;
- public static int edgeCount = 0;
- private static void parseData() throws IOException {
- // BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- BufferedReader br = new BufferedReader(new FileReader("datapub/pub12.in"));
- StringTokenizer tokenizer;
- tokenizer = new StringTokenizer(br.readLine());
- graphCount = Integer.parseInt(tokenizer.nextToken());
- nodeCount = Integer.parseInt(tokenizer.nextToken());
- edgeCount = Integer.parseInt(tokenizer.nextToken());
- for (int g = 0; g < graphCount; g++) {
- Graph newGraph = new Graph();
- newGraph.id = g;
- newGraph.nodes.ensureCapacity(nodeCount);
- Set<Node> possibleRoots = new HashSet();
- for (int i = 0; i < nodeCount; i++) {
- Node newNode = new Node();
- newNode.id = i+1;
- newGraph.nodes.add(newNode);
- possibleRoots.add(newNode);
- }
- for (int i = 0; i < edgeCount; i++) {
- tokenizer = new StringTokenizer(br.readLine());
- int from = Integer.parseInt(tokenizer.nextToken());
- int to = Integer.parseInt(tokenizer.nextToken());
- Node nodeFrom = newGraph.nodes.get(from-1);
- Node nodeTo = newGraph.nodes.get(to-1);
- nodeFrom.con.add(nodeTo);
- possibleRoots.remove(nodeTo);
- }
- newGraph.root = possibleRoots.iterator().next();
- newGraph.getCycle();
- newGraph.getAncestors();
- graphs.add(newGraph);
- }
- }
- public static void main(String[] args) throws IOException {
- parseData();
- for (Graph g : graphs) {
- g.getCertificate();
- }
- Map<String, Integer> certMapping = new HashMap();
- for (Graph g : graphs) {
- certMapping.putIfAbsent(g.cert, 0);
- // System.out.println(g.cert);
- }
- for (Graph g : graphs) {
- int count = certMapping.get(g.cert);
- count++;
- certMapping.put(g.cert, count);
- }
- ArrayList<Integer> list = new ArrayList(certMapping.values());
- Collections.sort(list);
- for (Integer i : list) {
- System.out.print(i + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement