Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么这段代码在hackerrank上不起作用?_Java_Data Structures_Graph_Depth First Search_Connected Components - Fatal编程技术网

Java 为什么这段代码在hackerrank上不起作用?

Java 为什么这段代码在hackerrank上不起作用?,java,data-structures,graph,depth-first-search,connected-components,Java,Data Structures,Graph,Depth First Search,Connected Components,我正在解决Hackerrank上的一个问题,你可以在- 使用以下代码输出不正确 我已经在代码中实现了所有必需的数据结构,并尝试创建连接组件大小的数组 import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws Exception{ BufferedReader bfr = new BufferedReader(new Input

我正在解决Hackerrank上的一个问题,你可以在-

使用以下代码输出不正确

我已经在代码中实现了所有必需的数据结构,并尝试创建连接组件大小的数组

import java.io.*;
import java.util.*;



public class Solution {
 public static void main(String[] args) throws Exception{

  BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));       
    String[] temp = bfr.readLine().split(" ");
    int N = Integer.parseInt(temp[0]);
    int I = Integer.parseInt(temp[1]);
    Solution sol=new Solution();
    Graph g = sol.new Graph(N);


    for(int i = 0; i < I; i++){
        temp = bfr.readLine().split(" ");
        int a = Integer.parseInt(temp[0]);
        int b = Integer.parseInt(temp[1]);
        g.addEdge(a,b);
      // Store a and b in an appropriate data structure of your choice
    }
   CC ccg=sol.new CC(g);
   int len=ccg.getComp();

    long combinations = 0;
    for(int k=0;k<len;k++){
        if(k==0){
            combinations+=ccg.getNum(k);
        }else{
            combinations*=ccg.getNum(k);
        }
    }
    // Compute the final answer - the number of combinations

    System.out.println(combinations);
   }

   class Graph{
       final int s;
       Bag[] adj;
       public Graph(int si){
           s=si;
           adj=new Bag[s];
           for(int i=0;i<si;i++){
               adj[i]=new Bag();
           }
       }
       public void addEdge(int i,int j){
           adj[i].add(j);
           adj[j].add(i);
       }

       public int graphSize(){
           return s;
       }

       public Iterable<Integer> adj(int v){
           return adj[v];
       }
   }
   class Bag implements Iterable<Integer>{

    Node first;
       int size=0;
       final class Node{
           int i;
           Node next;
       }
       public void add(int x){
           Node old=first;
           first=new Node();
           first.i=x;
           first.next=old;
           size++;
       }
       public int getSize(){
           return size;
       }
       public Iterator<Integer> iterator() {
            // TODO Auto-generated method stub
            return new ListIterator();
        }

        public class ListIterator implements Iterator<Integer>{
            private Node current=first;
            public boolean hasNext(){
                return current!=null;
            }
            public void remove(){}
            public Integer next(){
                int i=current.i;
                current=current.next;
                return i;
            }
        } 

   }

   class CC{
       private boolean[] marked;
       private int[] id;
       private int[] comp;
       private int count=0;

       public CC(Graph g){
           int i=g.graphSize();
           marked=new boolean[i];
           id=new int[i];
           comp=new int[i];
           for(int j=0;j<i;j++){
               comp[j]=0;
           }

           for(int v=0;v<i;v++){
               if(!marked[v]){
                   dfs(g,v);
                   count++;
               }
               comp[count]=comp[count]+1;
           }

       }
       public int getComp(){
           return count;
       }
       public int getNum(int i){
           return comp[i];
       }
       private void dfs(Graph g,int v){
           marked[v]=true;
           id[v]=count;
           for(int w:g.adj[v]){
               if(!marked[w]){
                   dfs(g,w);
               }
           }
       }
   }




  }
import java.io.*;
导入java.util.*;
公共类解决方案{
公共静态void main(字符串[]args)引发异常{
BufferedReader bfr=新的BufferedReader(新的InputStreamReader(System.in));
字符串[]temp=bfr.readLine().split(“”);
int N=Integer.parseInt(temp[0]);
inti=Integer.parseInt(temp[1]);
溶液溶胶=新溶液();
图g=sol.new图(N);
for(int i=0;i对于(intk=0;k,我已经对您的程序进行了两次测试运行

在所有运行中,您的程序都打印了0。虽然在某些情况下0是正确的输出,但在我的所有情况下它都不是正确的输出。因此,这可能是Hackerrank拒绝您的程序的原因之一。输入示例:

3 1
0 2
我的意思是描述两个国家,宇航员0和2来自一个国家,宇航员1来自其他国家。 预期输出:
2
。程序的实际输出:
0

(我编辑了这一段。)似乎如果所有对的
A
都等于
B
,您将获得
数组索引OutofBoundsException
。例如:

1 1
0 0
我在黑客银行规则中没有看到禁止
A==B
,所以我想你应该考虑一下


正如我在评论中所说,我不是在深入研究您的程序来理解它为什么会像我所描述的那样运行;我只是在观察并向您报告。我将把调试留给您自己。

需要我们检查的代码太多了,如果没有任何解释,很难理解您认为它是如何工作的。我不知道我不认为我会愿意这样做。是的,我明白,谢谢你的努力:)非常感谢:)