OneClickAC's blog

By OneClickAC, history, 8 years ago, In English

Hello codeforces community :)

PROBLEM 1: C++ related

I am beginner who has recently started learning about graphs and for that I was trying to solve out this dfs problem (http://mirror.codeforces.com/problemset/problem/115/A) ..

I wrote a code in C++ which is working fine on my laptop but is giving a runtime error on codeforces .. Can anyone help me out why this is happening and how to sort it out and can someone also list out the possible reasons when and why this kind of error happens , as it will help me in future ... :)

Code Link -> http://pastebin.com/54hFqrMC

PROBLEM 2: Java related

Well my second problem is also related to same question but this time , I need help of people who code in java. Basically , When my solution was not working , I tried to write code for the problem in Java . This time , problem is not about runtime error but lack of knowledge that I am having about how to use iterators and collection frameworks in Java . I am attaching a picture of error I am getting on IntelliJ IDEA and also providing my code . Please do help me out with these 2 problems . :)

Code Link -> http://pastebin.com/42sekm53 Thanks in advance to the best competitive coding community in the world :) ..

  • Vote: I like it
  • +7
  • Vote: I do not like it

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I start — not sure that I'm right but:

C++:

for(auto h=store.begin();h!=store.end();h++)
{
    if(a[i].find(*h)==a[i].end() && (*h)!=i)
    {
        repeat.insert(*h);
        store.erase(*h);
    }
}

store.erase(*h) — you're deleting element -> h doesn't exist anymore -> I really don't know how will perform h++ operation in for-cycle (Someone tell me whether this is undefined behaviour).

JAVA:

while(j.hasNext())
{
    System.out.println(j == null);
    Integer q=j.next();
    if(a[q].size()!=0)
    {
        Iterator<Integer> p=a[q].iterator();
        while(p.hasNext())
        {
            a[i].add(p.next());
        }
    }
}

You're modifying same collection by two different iterators. hasNext() just check whether next atribute is null. This is not guarantee for next method call result equals to next atribute. When you modify same collection by different iterators only nextEntry() and remove() methods are guarantee correct result.

  • »
    »
    8 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Okay , first of all thanks for helping out balalaika .. I read your answer earlier and I have rectified my mistake as you said , and yes the code is working properly now , its still not accepted because its failing on some tests but I will sort that out or at least will try my best to , but yup the root cause is solved now :)

    Secondly , Can you please elaborate about your solution for the second problem (related to Java) . I am not able to understand it properly .

    • »
      »
      »
      8 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      JAVA:

      j is iterator for a[i]. In inner cycle you add to a[i]. How works hasNext-next methods? hasNext() returns value of next atribute of iterator. next() just returns value of next attribute and refresh it. But when you iterating over collection and modify this collection, this occurs exception.

      So you need don't modify collection while iterating over it (In fact in any language if you modify collection during iterating over it then you do something wrong except rare cases). Instead of this you can use standart for-cycle — bad but may works.

      P.S. Sorry for English

      • »
        »
        »
        »
        8 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        But how to get each element of a tree set in Java without iterating??

        I mean , Can we use for loop or any index to refer to each element of Tree Set..?? If yes , Can you tell me how (The Syntax) ..

        • »
          »
          »
          »
          »
          8 years ago, # ^ |
          Rev. 4   Vote: I like it 0 Vote: I do not like it

          In Treeset only iterating

          for (Object obj : treeSet) {
              //....
          }
          
          Iterator<Object> it = treeSet.iterator();
          while (it.hasNext()) {
              Object obj = it.next();
              //....
          }
          

          But you never add elements into any collection during iterating. The only thing you can do is call remove() method on iterator not collection element. And you need to be sure that this collecton is not modifying by any other process. And you can't do this in for-cycle — only in iterator-while-cycle. So you need other way to solve task.

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For Java, delete your package Codeforces; at the top, in addition to what balalaika said.