我正在将C ++代码翻译成Java。
控制台中什么也不显示,它很快终止了。我的代码有什么问题?
结果应该是:
周期数1:3 4 5 6
周期2:11 12 13
我不确定该怎么写:cycles [mark [i]]。push_back(i);
我编写的Java代码:
import java.util.ArrayList;
public class Graf {
static ArrayList <Integer> graph = new java.util.ArrayList<Integer>();
static ArrayList <Integer> cycles = new java.util.ArrayList<Integer>();
public static void dfs_cycle(int u, int p, int color[], int mark[], int par[], int cyclenumber) {
// already (completely) visited vertex.
if (color[u] == 2) {
return;
}
// seen vertex, but was not completely visited -> cycle detected.
// backtrack based on parents to find the complete cycle.
if (color[u] == 1) {
cyclenumber++;
int cur = p;
mark[cur] = cyclenumber;
// backtrack the vertex which are
// in the current cycle thats found
while (cur != u) {
cur = par[cur];
mark[cur] = cyclenumber;
}
return;
}
par[u] = p;
// partially visited.
color[u] = 1;
// simple dfs on graph
for (int v=0; v < graph.size(); v++) {
// if it has not been visited previously
if (v == par[u]) {
continue;
}
dfs_cycle(v, u, color, mark, par, cyclenumber);
}
// completely visited.
color[u] = 2;
}
// add the edges to the graph
static void addEdge(int u, int v)
{
graph.add(v);
graph.add(u);
}
// Function to print the cycles
static void printCycles(int edges, int mark[], int cyclenumber) {
// push the edges that into the cycle adjacency list
for (int i = 1; i <= edges; i++) {
if (mark[i] != 0)
cycles.add(i);
}
// print all the vertex with same cycle
for (int i = 1; i <= cyclenumber; i++) {
// Print the i-th cycle
System.out.println("Cycle Number" + i + ": ");
for (int x = 0; x < cycles.size(); x++) {
System.out.println(cycles.get(x) + " ");
}
System.out.println();
}
}
public static void main(String [] args) {
addEdge(1, 2);
//and so on...
// arrays required to color the graph, store the parent of node
int color[] = new int [100000];
int par[] = new int [100000];
// mark with unique numbers
int mark[] = new int [100000];
// store the numbers of cycle
int cyclenumber = 0;
int edges = 13;
// call DFS to mark the cycles
dfs_cycle(1, 0, color, mark, par, cyclenumber);
// function to print the cycles
printCycles(edges, mark, cyclenumber);
}
}
这是C ++中的原始代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
vector<int> graph[N];
vector<int> cycles[N];
void dfs_cycle(int u, int p, int color[],
int mark[], int par[], int& cyclenumber) {
if (color[u] == 2) {
return;
}
if (color[u] == 1) {
cyclenumber++;
int cur = p;
mark[cur] = cyclenumber;
while (cur != u) {
cur = par[cur];
mark[cur] = cyclenumber;
}
return;
}
par[u] = p;
color[u] = 1;
for (int v : graph[u]) {
if (v == par[u]) {
continue;
}
dfs_cycle(v, u, color, mark, par, cyclenumber);
}
color[u] = 2;
}
void addEdge(int u, int v) {
graph[u].push_back(v);
graph[v].push_back(u);
}
void printCycles(int edges, int mark[], int& cyclenumber) {
for (int i = 1; i <= edges; i++) {
if (mark[i] != 0)
cycles[mark[i]].push_back(i);
}
for (int i = 1; i <= cyclenumber; i++) {
cout << "Cycle Number " << i << ": ";
for (int x : cycles[i])
cout << x << " ";
cout << endl;
}
}
int main()
{
addEdge(1, 2);
//and so on...
int color[N];
int par[N];
int mark[N];
int cyclenumber = 0;
int edges = 13;
dfs_cycle(1, 0, color, mark, par, cyclenumber);
printCycles(edges, mark, cyclenumber);
}