### SecretSuperstar's blog

By SecretSuperstar, history, 14 months ago, ,

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://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 ... :)

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 :) ..

•
• +7
•

 » 14 months ago, # |   0 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 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.
•  » » 14 months ago, # ^ |   0 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 .
•  » » » 14 months ago, # ^ |   0 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
•  » » » » 14 months ago, # ^ | ← Rev. 2 →   0 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) ..
•  » » » » » 14 months ago, # ^ | ← Rev. 4 →   0 In Treeset only iterating for (Object obj : treeSet) { //.... } Iterator
•  » » » » » » 14 months ago, # ^ |   0 Okay :) Thanks for help balalaika :)
 » 14 months ago, # |   0 For Java, delete your package Codeforces; at the top, in addition to what balalaika said.