英文:
Getting StackOverflowError in DFS implementation for very large input
问题
以下是翻译好的部分:
在使用Java在HackerEarth上练习DFS问题时,其中一个测试用例在检查时导致NZEC错误,显示StackOverFlow错误。我检查了很多次,除了一个导致错误的测试用例外,所有的测试用例都通过了。这不仅仅是针对特定问题,而是在许多问题(DFS问题)中,总有一个测试用例在使用Java解决时总是导致NZEC错误。
我不知道是因为我的邻接表实现还是其他原因。下面是其中一个问题。
我的代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.*;
public class TestClass {
static long mod = 1000000007;
static String[] temp;
static int[] node;
static int[] vis;
public static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main (String[] args) throws IOException {
int testCases= Integer.parseInt(br.readLine().split(" ")[0]);
while (testCases > 0) {
temp = br.readLine().split(" ");
int N = Integer.parseInt(temp[0]);
int K = Integer.parseInt(temp[1]);
HashMap<Integer, ArrayList<Integer>> forEqual = new HashMap<>();
ArrayList<int[]> edgeList = new ArrayList<>();
for (int i = 0; i < K; i++) {
temp = br.readLine().split(" ");
int u = Integer.parseInt(temp[0]);
int v = Integer.parseInt(temp[2]);
if (temp[1].equals("=")) {
forEqual.computeIfAbsent(u, k -> new ArrayList<>()).add(v);
forEqual.computeIfAbsent(v, k -> new ArrayList<>()).add(u);
} else {
edgeList.add(new int[]{u, v});
}
}
boolean[] vis = new boolean[1000005];
node = new int[1000005];
int c = 0;
for (int i : forEqual.keySet()) {
if (!vis[i]) {
c++;
DFS(i, forEqual, c, vis);
}
}
boolean flag = true;
for(int[] i : edgeList){
if(node[i[0]] == node[i[1]]){
flag = false;
break;
}
}
System.out.println(flag ? "YES" : "NO");
testCases--;
}
}
private static void DFS(int i, HashMap<Integer, ArrayList<Integer>> forEqual, int c, boolean[] vis) {
vis[i] = true;
node[i] = c;
for (int v : forEqual.get(i)) {
if (!vis[v]) {
DFS(v, forEqual, c, vis);
}
}
}
}
英文:
While practising DFS problems on HackerEarth using Java, one of the test cases results in NZEC error when checking it out, it displays StackOverFlow error. I checked it many of the times but all of the test cases pass except one which results in an error, and this is not for that specific problem but for many of the problems(DFS problems) one test cases is always there out of 10 or 20 which always result in NZEC when solving with java.
I don't whether it is due to my Adjacency list implementation or something else. Here is the problem one of the problem.
My code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.*;
public class TestClass {
static long mod = 1000000007;
static String[] temp;
static int[] node;
static int[] vis;
public static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main (String[] args) throws IOException {
int testCases= Integer.parseInt(br.readLine().split(" ")[0]);
while (testCases > 0) {
temp = br.readLine().split(" ");
int N = Integer.parseInt(temp[0]);
int K = Integer.parseInt(temp[1]);
HashMap<Integer, ArrayList<Integer>> forEqual = new HashMap<>();
ArrayList<int[]> edgeList = new ArrayList<>();
for (int i = 0; i < K; i++) {
temp = br.readLine().split(" ");
int u = Integer.parseInt(temp[0]);
int v = Integer.parseInt(temp[2]);
if (temp[1].equals("=")) {
forEqual.computeIfAbsent(u, k -> new ArrayList<>()).add(v);
forEqual.computeIfAbsent(v, k -> new ArrayList<>()).add(u);
} else {
edgeList.add(new int[]{u, v});
}
}
boolean[] vis = new boolean[1000005];
node = new int[1000005];
int c = 0;
for (int i : forEqual.keySet()) {
if (!vis[i]) {
c++;
DFS(i, forEqual, c, vis);
}
}
boolean flag = true;
for(int[] i : edgeList){
if(node[i[0]] == node[i[1]]){
flag = false;
break;
}
}
System.out.println(flag ? "YES" : "NO");
testCases--;
}
}
private static void DFS(int i, HashMap<Integer, ArrayList<Integer>> forEqual, int c, boolean[] vis) {
vis[i] = true;
node[i] = c;
for (int v : forEqual.get(i)) {
if (!vis[v]) {
DFS(v, forEqual, c, vis);
}
}
}
}
答案1
得分: 2
没有人回答我的问题,这个问题不是来自实时竞赛,我花了一个星期的时间来找到问题的解决方案。
StackOverflow的问题是由HackerEarth IDE和类似的在线Java IDE上堆栈调用次数限制引起的,仅允许 100,000 次调用,为了避免这种情况,创建一个线程对象并手动传递堆栈大小作为参数,这将增加堆栈大小。
class Solution{
public static void main (String[] args) throws IOException {
new Thread(null, new TestClass(), "", 1 << 26).start();
}
public void run() {
// 无论你想做什么
}
}
英文:
No one answer to my question, this question is not from the live contest, it takes me a week to figure out the solution for my problem.
The problem of StackOverFlow is due to the limitation of the number of stack calls on HackerEarth IDE and similar online IDE for Java which is 100,000 calls are only allowed, to avoid such, create a thread object and pass stack size as a parameter manually, which will increase the stack size.
class Solution{
public static void main (String[] args) throws IOException {
new Thread(null, new TestClass(), "", 1 << 26).start();
}
public void run() {
// DO WHATEVER YOU WANT
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论