Facebook
From das, 5 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 274
  1. import java.util.*;
  2.  
  3. import static com.sun.tools.sjavac.Util.union;
  4.  
  5. public class Graph {
  6.     private List<Integer>[] nb;
  7.     private int n;
  8.     Graph (int n) {
  9.         this.n=n;
  10.         nb = new ArrayList[n];
  11.         for (int i=0; i<n; i++)
  12.             nb[i] = new ArrayList<>();
  13.     }
  14.     void addEdge (int a, int b) {
  15.         nb[a].add(b);
  16.         nb[b].add(a);
  17.     }
  18.     Set<Set<Integer>> sets;
  19.     Set<Integer>[] ns;
  20.     Set<Set<Integer>> complimentComponent () {
  21.         sets = new HashSet<>();
  22.         ns = new HashSet[n];
  23.         for (int i=0; i<=n; i++) {
  24.             Set<Integer> s = HashSet<Integer>.Singleton(i);
  25.             sets.add(s);
  26.             ns[i]=s;
  27.         }
  28.         for (int i=0; i<n; i++) process(i);
  29.         return sets;
  30.     }
  31.     void process (int a) {
  32.         Map<Set<Integer>, Integer> c = new HashMap<>();
  33.         for (Set <Integer> s : sets) {
  34.             c.put(s, 0);
  35.         }
  36.         for (int b : nb[a]) {
  37.             c.put(ns[b], c.get(ns[b])+1);
  38.         }
  39.         for (Set<Integer> s : sets) {
  40.             if (s != ns[a] && c.get(s) < s.size()) { // porównuje wskażniki
  41.                 union (ns[a], s);
  42.             }
  43.         }
  44.     }
  45.     void union (Set<Integer> s, Set<Integer> t) {
  46.         if (s.size() < t.size()) {
  47.             union (t, s); return;
  48.         }
  49.         s.addAll(t);
  50.         for (int a : t) {
  51.             ns[a] = s;
  52.         }
  53.         sets.remove(t);
  54.     }
  55. }
  56.  
  57.  
  58.  
  59. public class Main {
  60.     public static void main (String[] argv) {
  61.  
  62.     }
  63. }
  64.