public ListGraph transpose() { ListGraph g = new ListGraph(vs); for (Vertex v : g.vs) { v.edges = null; } for (Vertex v : vs) { Edge e = v.edges; while (e != null) { g.addEdge(e.dest.idx, v.idx, e.weight, false); e = e.next; } } g.setStart(start); // System.out.println(vs[0] + "," + g.vs[0]); return g; }
/* 1 8 14 1 2 2 3 2 5 2 6 3 4 3 7 4 3 4 8 5 1 5 6 6 7 7 6 7 8 8 8 3 */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) { int v = sc.nextInt(); int e = sc.nextInt(); ListGraph g = new ListGraph(v); for (int j = 0; j < e; j++) g.addEdge(sc.nextInt() - 1, sc.nextInt() - 1, 1); g.setStart(sc.nextInt() - 1); System.out.println(g); g.dfs(g.getStart()); g.printTree(); ListGraph tg = g.transpose(); tg.dfs(); tg.printTree(); System.out.println(tg); } sc.close(); }