fork download
  1. import std.stdio;
  2. import std.array;
  3. import std.algorithm;
  4.  
  5. string[] input = [
  6. "a1=a2", "b1=b2", "b3=b2", "c1=c2", "e1=e2", "a3=a4", "c3=c4", "e1=e3",
  7. "a2=a4", "c3=c1", "b3=a4", "c2=d1", "a4=a5", "d2=c1", "b4=b3", "d3=c3"
  8. ];
  9.  
  10. void main()
  11. {
  12. ulong[string] g;
  13. string[] id;
  14.  
  15. foreach (ref s; input) {
  16. auto h = s.split('=').map!(t => t in g ? g[t] : (id ~= t) ? g[t] = g.length : 0);
  17.  
  18. ulong i = h.minElement;
  19. ulong j = h.maxElement;
  20. foreach (ref x; g) if (x == j) x = i;
  21. }
  22.  
  23. foreach (i; g.values.sort.uniq) writeln(id.filter!(x => g[x] == i));
  24. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
["a1", "a2", "b1", "b2", "b3", "a3", "a4", "a5", "b4"]
["c1", "c2", "c3", "c4", "d1", "d2", "d3"]
["e1", "e2", "e3"]