連通セットのリスト-Java
N個の頂点とE個のエッジを持つ無方向図を指定し、DFSとBFSでそれぞれの連通セットをリストします.頂点を0からN−1まで番号付けするとする.探索を行う場合,常に番号が最小の頂点から出発し,番号が増加する順に隣接点にアクセスすると仮定する.
入力形式:1行目に2個の整数N(0)を入力
出力フォーマット:「{v 1 v 2...v k}」のフォーマットに従って、各行に連通セットを出力します.DFSの結果を出力してから、BFSの結果を出力します.
入力サンプル:8 6 0 7 0 1 2 0 4 1 4 3 5出力サンプル:{0 1 4 2 7}{3 5}{6}{0 1 2 7 4}{3 5}{6}
注:书く时に穴を踏んで、半分の隣接する行列によって书きたいと思って、结果テスト2は死んで生きていけません...后でやっとTATを発见します
コード:
入力形式:1行目に2個の整数N(0)を入力
出力フォーマット:「{v 1 v 2...v k}」のフォーマットに従って、各行に連通セットを出力します.DFSの結果を出力してから、BFSの結果を出力します.
入力サンプル:8 6 0 7 0 1 2 0 4 1 4 3 5出力サンプル:{0 1 4 2 7}{3 5}{6}{0 1 2 7 4}{3 5}{6}
注:书く时に穴を踏んで、半分の隣接する行列によって书きたいと思って、结果テスト2は死んで生きていけません...后でやっとTATを発见します
コード:
import java.io.BufferedReader;
import java.io.FileDescriptor;
import java.io.FileReader;
public class Main {
public static void main(String[] args){
Main self = new Main();
BufferedReader br = new BufferedReader(new FileReader(FileDescriptor.in));
try {
Graph graph = self.createGraph(br);
self.DFS(graph);
graph.resetVisited();
self.BFS(graph);
} catch (Exception e) {
e.printStackTrace();
}
}
//
public Graph createGraph(BufferedReader br) throws Exception{
String[] s = br.readLine().trim().split(" ");
int n = Integer.parseInt(s[0]);
int e = Integer.parseInt(s[1]);
Graph graph = new Graph(n,e);
for (int i = 0; i < graph.getE(); i++) {
String[] s1 = br.readLine().trim().split(" ");
int temp1 = Integer.parseInt(s1[0]);
int temp2 = Integer.parseInt(s1[1]);
graph.getGraph()[temp1][temp2] = 1;
graph.getGraph()[temp2][temp1] = 1;
}
return graph;
}
//DFS
public void DFS(Graph graph){
for (int i = 0; i < graph.getN(); i++) {
if (!graph.getVisited()[i]){
System.out.print("{ ");
DFS(graph,graph.getNodes()[i]);
System.out.println("}");
}
}
}
// node
private void DFS(Graph graph, int node){
graph.getVisited()[node] = true;
System.out.print(node + " ");
for (int i = 0; i < graph.getN(); i++) {
if (graph.getGraph()[node][i] == 1 && !graph.getVisited()[i]){
DFS(graph, i);
}
}
}
//BFS
public void BFS(Graph graph){
for (int i = 0; i < graph.getN(); i++) {
if (!graph.getVisited()[i]){
System.out.print("{ ");
BFS(graph,graph.getNodes()[i]);
System.out.println("}");
}
}
}
// node
private void BFS(Graph graph, int node) {
Que que = new Que(graph.getN());
que.addQue(node);
graph.getVisited()[node] = true;
System.out.print(node + " ");
while (que.getLen() > 0){
int temp = que.leaveQue();
for (int i = 0; i < graph.getN(); i++) {
if (graph.getGraph()[temp][i] == 1 && !graph.getVisited()[i]){
System.out.print(i + " ");
graph.getVisited()[i] = true;
que.addQue(i);
}
}
}
}
}
//
class Graph{
private int n;
private int e;
private int[] nodes;
private int[][] graph;
private boolean[] visited;
public Graph(int n, int e) {
this.n = n;
this.e = e;
this.graph = new int[this.n][this.n];
this.visited = new boolean[this.n];
init();
}
private void init(){
this.nodes = new int[this.n];
for (int i = 0; i < this.n; i++) {
this.nodes[i] = i;
}
}
public void resetVisited(){
for (int i = 0; i < this.visited.length; i++) {
this.visited[i] = false;
}
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public int getE() {
return e;
}
public void setE(int e) {
this.e = e;
}
public int[][] getGraph() {
return graph;
}
public void setGraph(int[][] graph) {
this.graph = graph;
}
public boolean[] getVisited() {
return visited;
}
public void setVisited(boolean[] visited) {
this.visited = visited;
}
public int[] getNodes() {
return nodes;
}
public void setNodes(int[] nodes) {
this.nodes = nodes;
}
}
class Que{
private int[] que;
private int top;
private int rear;
public Que(int len) {
this.que = new int[len];
this.top = -1;
this.rear = -1;
}
public void addQue(int node){
que[++top] = node;
}
public int leaveQue(){
return que[++rear];
}
public int getLen(){
return top-rear;
}
}