@@ -0,0 +1,2452 @@
+import java.util.*;%0A%0A// KATIO may be needed since about 500,000 edges are possible.%0A%0Apublic class Reactivity %7B%0A%0A  public List%3CVertex%3E ts(Graph g) %7B%0A    // TODO: Implement this.%0A    // Throw IllegalArgumentException if g can't be uniquely sorted.%0A    throw new IllegalArgumentException();%0A  %7D%0A%0A  class Graph %7B%0A%0A    private Map%3CVertex, Set%3CVertex%3E%3E list;%0A    private int edgeCount;%0A%0A    public Graph() %7B%0A      this.list = new HashMap%3C%3E();%0A    %7D%0A%0A    public Set%3CVertex%3E adjacentTo(Vertex v) %7B%0A      return list.get(v);%0A    %7D%0A%0A    public void addEdge(Vertex u, Vertex v) %7B%0A      if (list.get(u) == null) %7B%0A        list.put(v, new HashSet%3C%3E());%0A      %7D%0A      list.get(u).add(v);%0A      edgeCount++;%0A      u.increaseOutDegree();%0A      v.increaseInDegree();%0A    %7D%0A%0A    public void removeEdge(Vertex u, Vertex v) %7B%0A      if (list.get(u) == null) %7B%0A        return;%0A      %7D%0A      if (list.get(v) == null) %7B%0A        return;%0A      %7D%0A      list.get(u).remove(v);%0A      edgeCount--;%0A      u.decreaseOutDegree();%0A      v.decreaseInDegree();%0A    %7D%0A%0A    public int edgeCount() %7B%0A      return edgeCount;%0A    %7D%0A%0A  %7D%0A%0A  class Vertex %7B%0A%0A    private int label;%0A    private int inDegree;%0A    private int outDegree;%0A%0A    public Vertex(int label) %7B%0A      this.label = label;%0A      this.inDegree = 0;%0A      this.outDegree = 0;%0A    %7D%0A%0A    public int label() %7B%0A      return label;%0A    %7D%0A%0A    public void increaseInDegree() %7B%0A      inDegree++;%0A    %7D%0A%0A    public void decreaseInDegree() %7B%0A      inDegree--;%0A    %7D%0A%0A    public int inDegree() %7B%0A      return inDegree;%0A    %7D%0A%0A    public void increaseOutDegree() %7B%0A      outDegree++;%0A    %7D%0A%0A    public void decreaseOutDegree() %7B%0A      outDegree--;%0A    %7D%0A%0A    public int outDegree() %7B%0A      return outDegree;%0A    %7D%0A%0A  %7D%0A%0A  public void solve() %7B%0A    Scanner in = new Scanner(System.in);%0A    int metalCount = in.nextInt();%0A    int experimentCount = in.nextInt();%0A    Vertex%5B%5D metals = new Vertex%5BmetalCount%5D;%0A    Graph g = new Graph();%0A    for (int i = 0; i %3C metalCount; i++) %7B%0A      metals%5Bi%5D = new Vertex(i);%0A    %7D%0A    for (int i = 0; i %3C experimentCount; i++) %7B%0A      int l1 = in.nextInt();%0A      int l2 = in.nextInt();%0A      g.addEdge(metals%5Bl1%5D, metals%5Bl2%5D);%0A    %7D%0A    try %7B%0A      for (Vertex v : ts(g)) %7B%0A        System.out.println(v.label());%0A      %7D%0A    %7D catch (IllegalArgumentException e) %7B%0A      System.out.println(%22back to the lab%22);%0A    %7D%0A  %7D%0A%0A  public static void main(String%5B%5D args) %7B%0A    new Reactivity().solve();%0A%0A  %7D%0A%0A%7D%0A%0A%0A

Add Reactivity.java