import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
@SuppressWarnings("unused")
public class GraphReverse {
private int vertexNum;
private int arcNum;
private boolean visited[];
private List<ArrayList<Integer>> graphListS;
private List<ArrayList<Integer>> graphListF;
public GraphReverse(int vertexNum,int arcNum,List<ArrayList<Integer>> graphListS,List<ArrayList<Integer>> graphListF){
this.vertexNum=vertexNum;
this.graphListS=graphListS;
visited=new boolean[vertexNum];
this.graphListF=graphListF;
}
public static void main(String[] args) {
String path="f:/tinyDG.txt";
try{
Scanner scanner=new Scanner(new File(path));
List<ArrayList<Integer>> graphListS=new ArrayList<ArrayList<Integer>>();//存储有向图的邻接表
List<ArrayList<Integer>> graphListF=new ArrayList<ArrayList<Integer>>();//存储有向图的反向图的邻接表
int vertexNum=scanner.nextInt();
int arcNum=scanner.nextInt();
for(int i=0;i<vertexNum;i++){
graphListS.add(new ArrayList<Integer>());
graphListF.add(new ArrayList<Integer>());
}
//有向图的存储
for(int i=0;i<arcNum;i++){
int u=scanner.nextInt();
int v=scanner.nextInt();
if(!graphListS.get(u).contains(v)){
graphListS.get(u).add(v);
}
//有向图的反向图存储
if(!graphListF.get(v).contains(u)){
graphListF.get(v).add(u);
}
}
//有向图的广度优先遍历
GraphReverse g1=new GraphReverse(vertexNum, arcNum, graphListS,null);
System.out.println("有向图的邻接表的结果是:");
g1.bfs(vertexNum/2+1,graphListS);
System.out.println(" ");
//有向图的反向图广度优先的遍历
GraphReverse g2=new GraphReverse(vertexNum, arcNum, null,graphListF);
System.out.println("有向图的反向图的邻接表结果是:");
g2.bfs(1,graphListF);
}catch (Exception e) {
e.printStackTrace();
}
}
private void bfs(int v,List<ArrayList<Integer>> graphList){
Queue<Integer> que = new LinkedList<Integer>();
que.offer(v);
while (!que.isEmpty()) {
v = que.poll();
System.out.print(v+":");
visited[v] = true;
for (int i = 0; i <graphList.get(v).size(); i++) {
int k=graphList.get(v).get(i);
System.out.print(k+" ");
if (!visited[k]){
//que.add(k);
que.offer(k);
visited[k] = true;
}
}
System.out.println();
}
}
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
@SuppressWarnings("unused")
public class GraphReverse {
private int vertexNum;
private int arcNum;
private boolean visited[];
private List<ArrayList<Integer>> graphListS;
private List<ArrayList<Integer>> graphListF;
public GraphReverse(int vertexNum,int arcNum,List<ArrayList<Integer>> graphListS,List<ArrayList<Integer>> graphListF){
this.vertexNum=vertexNum;
this.graphListS=graphListS;
visited=new boolean[vertexNum];
this.graphListF=graphListF;
}
public static void main(String[] args) {
String path="f:/tinyDG.txt";
try{
Scanner scanner=new Scanner(new File(path));
List<ArrayList<Integer>> graphListS=new ArrayList<ArrayList<Integer>>();//存储有向图的邻接表
List<ArrayList<Integer>> graphListF=new ArrayList<ArrayList<Integer>>();//存储有向图的反向图的邻接表
int vertexNum=scanner.nextInt();
int arcNum=scanner.nextInt();
for(int i=0;i<vertexNum;i++){
graphListS.add(new ArrayList<Integer>());
graphListF.add(new ArrayList<Integer>());
}
//有向图的存储
for(int i=0;i<arcNum;i++){
int u=scanner.nextInt();
int v=scanner.nextInt();
if(!graphListS.get(u).contains(v)){
graphListS.get(u).add(v);
}
//有向图的反向图存储
if(!graphListF.get(v).contains(u)){
graphListF.get(v).add(u);
}
}
//有向图的广度优先遍历
GraphReverse g1=new GraphReverse(vertexNum, arcNum, graphListS,null);
System.out.println("有向图的邻接表的结果是:");
g1.bfs(vertexNum/2+1,graphListS);
System.out.println(" ");
//有向图的反向图广度优先的遍历
GraphReverse g2=new GraphReverse(vertexNum, arcNum, null,graphListF);
System.out.println("有向图的反向图的邻接表结果是:");
g2.bfs(1,graphListF);
}catch (Exception e) {
e.printStackTrace();
}
}
private void bfs(int v,List<ArrayList<Integer>> graphList){
Queue<Integer> que = new LinkedList<Integer>();
que.offer(v);
while (!que.isEmpty()) {
v = que.poll();
System.out.print(v+":");
visited[v] = true;
for (int i = 0; i <graphList.get(v).size(); i++) {
int k=graphList.get(v).get(i);
System.out.print(k+" ");
if (!visited[k]){
//que.add(k);
que.offer(k);
visited[k] = true;
}
}
System.out.println();
}
}
}