Вобщем безнадега какая-то Edited by author 25.04.2007 01:26 100 5 5 6 odd 7 8 odd 1 6 even 1 4 odd 7 8 even 3 3 1 1 odd 3 3 odd 1 3 odd 5 4 1 2 even 4 5 even 1 5 odd 3 3 even 10 5 1 2 even 3 4 odd 5 6 even 1 6 even 7 10 odd 20 8 1 9 odd 10 17 odd 18 26 odd 4 5 odd 27 40 even 21 40 odd 1 20 odd 10 20 odd 200 8 1 9 odd 10 17 odd 18 26 odd 4 5 odd 27 40 even 21 40 odd 1 20 odd 10 20 odd -1 Output: 4 3 3 3 2 6 this sample is Wrong!! the right output is: 4 3 3 3 6 6 It used me 3 hours time to debug it..! Final..I find ,,this sample is wrong!! Could you please tell me how to get the datas? Edited by author 23.10.2008 15:28 Edited by author 23.10.2008 15:28 The original output for the test sequence (as posted by pperm) is correct. The last two tests are identical except for the lenght of the 0/1 sequence, which the third question in the 5th test violates, thus giving 2 as the correct answer. I don't know if this is an issue tested by The Judge though ^^ There is nobody wrong ... Because the problem in "18 26 odd" at the fifth test is that 26>20(the length of the sequence) ... But the description of the problem doesn't say anything about this situation , and also there no data like this ... So I doubt that we can use the length of the sequence to do what -_-!!! this sample is Wrong!! the right output is: 4 3 3 3 6 6 It used me 3 hours time to debug it..! Final..I find ,,this sample is wrong!! you are Wrong!! the right output is: 4 3 3 3 2 6 correct answer for the test 3 3 1 1 odd 3 3 odd 1 3 odd is 0, because interval with zero length contains 0 (even) ones :) correct answer for the test 3 3 1 1 odd 3 3 odd 1 3 odd is 0, because interval with zero length contains 0 (even) ones :) This is not correct. The range from 1 to 1 includes one digit at position 1. 100 5 5 6 odd 7 8 odd 1 6 even 1 4 odd 7 8 even for the above input I don't understand why the output should be 4. if 5 6 is odd and 1 6 is even, 1 4 is even, isn't it? No, 1 4 must be odd, because 1 4 + 5 6 = 1 6 or in other words odd + odd = even :) thnx its helpfull.i got ac. all tests passed, still getting wrong answer... it's super old but still I hope to get something i have read all the procedure. but i could not get which file shoul i have to submit for solution plz help me. i will be very thankful to you. Yes, it is just something strange. My program passes all the mentioned tests perfectly. I took in the accounts every remarks mentioned in this forum and still I get WA1. I'm in the same boat. all tests listed here are passing. but still got the WA1. anyone knows how can i get hold of the tests OJ is using? or at least get the feedbacks on the specific failing test case? Altoids, maybe we should try this test (answer -> 4): 12 6 1 2 even 1 1 even 3 4 odd 5 6 even 1 6 even 7 10 odd -1 How it can be solved with disjoint sets? Anybody help me please. This is my mail: quvondiq.otajonov.tuit@gmail.com. I'll wait for answer! Thanks for advance!!! ??????? I know it's an old thread, but it's how I solved it. So spoilers and all. if l->r is even, then s[r] and s[l - 1] have the same parity (where s[x] is the partial sum of bits from 1 to x) if l->r is odd, then they have opposite parities We add l-1 and r to a dsu. So a dsu is just a tree of all dependencies, no matter if odd or even (see how I differentiate between the two below). I used dsu with an added property that is only available for dsu roots: sons[root][0] is the "representative" of all elements that have the same parity of root, sons[root][1] is the "representative" of all elements that have opposite parity of root. Anything else besides the root->representative edges will only have 0s, codifying that entire subtrees have the same parity. So first off when you do find, I haven't really bothered with path compression. And instead of just saying what is the dsu root for a node, you also say which side it came from (0 if it came from sons[root][0] and 1 for sons[root][1]) - so (0, root) means it has the same parity as root and (1, root) means it has opposite parities. So now when you do union, you first have a small case, if the two nodes are already in a tree, you have to verify that the sides they're on match the parity of s[r] - s[l - 1] otherwise you stop at the current test. Now, if they're not yet united, you have 4 cases that are basically just 2: if l-1 and r are on the same side of their respective dsus with s[r]-s[l-1] even, then this is one case if l-1 and r are on opposite sides of their respective dsus with s[r]-s[l-1] odd, then this is one case(identical with the previous one) And of course you have the opposite cases as well, which form another case you have to treat separately. So now you just have to move the left and right sides from one tree to the other so that they all match up correctly. You can do this however you like, just make sure that the property we've added to the dsu (sons is a unique property of the root of a dsu) is kept. Here's how I did it (let's treat just the case where l->r is even and l-1 and r are on the 0 sides of their respective dsus), and let's say l - 1 has root T1 and r root T2, we want to join the tree T2 into tree T1. T1 T2 0/ \1 0/ \1 a b c d | | | | A B C D (l - 1 comes from A, r comes from C) a, b, c, d are representatives of A, B, C, D. And this is how the finalized tree will Look:
T1 0/ \1 T2 d 0|\0 |\ a c D b | | | A C B As you can see, since T2 is no longer a root, sons[T2] becomes redundant since it's gonna have 0's on the edges which is what keeps the property. For the opposite case, you do something similar, just that T2 will be on the 1 side of T1 and you play around with the representatives. Of course there are some cases where some roots don't yet have both/any sons, but it's not too ugly to deal with them. Also for implementation, I normalized values but I think you could just use unordered_map/map. Let a[1], ... , a[n] be the sequence written by the friend and pref[i] be the xor of the prefix a[1...i]. Then a question (l, r, t) (which means the parity of the number of ones on the segment from l to r is t) can be represented as (pref[r] xor pref[l - 1] = t). Let's build a graph with two vertices for each prefix (thus, there will be 2 * (n + 1) vertices). Using compression, we actually need only at most 2 * m vertices (where m is the number of questions). Let the vertices corresponding to the prefix i be f0(i) and f1(i). Now, let's add all edges f0(i) ~ f1(i). For a question (pref[r] xor pref[l - 1] = t) if t = 1, we add two edges f0(l - 1) ~ f0(r) and f1(l - 1) ~ f1(r), else if t = 0, we add edges f0(l - 1) ~ f1(r) and f1(l - 1) ~ f0(r). To check whether a sequence of questions from 1 to x is achievable we just need to check whether our current graph is bipartite. This can be done by simply doing dfs after each question. Nice! Can you please elaborate in a few words? I actually didn't get the significance of having two vertices for each prefix and how it's solving the problem. Is there any way I can see the output of my program? The message "Runtime error" is very uninformative, besides, on my computer, my program performs a variety of tests without any errors. //main logic #include <stdio.h> #include <map> #include <iostream> using namespace std; map<int, bool> exist; map<int, bool> odd; map<int, int> prev; bool add(int a, int b, bool c){//b>=a; if (!exist[b]){ exist[b] = true; odd[b] = c; prev[b] = a; return true; }; int i = prev[b]; if(i==a) return (odd[b]==c); if(i<a) return add(i,a-1, (c!=odd[b])); return add(a,i-1, (c!=odd[b])); }; Thank you very much, at least I solved it! Nice problem! thank you! i study very from you Thanks! nice idea ;) I've got AC thank you very much!!!) Edited by author 01.11.2011 17:53 100 5 5 6 odd 7 8 odd 1 6 even 1 4 odd 7 8 even why result is 3 and not 4? because 1 1 = 0 but 0 0 not 1 the input format is verrrry misleading, i have lost 2 hours of my live because of it. #include <stdio.h> #include <map> #include <iostream> #include <string> using namespace std; map<int, bool> exist; map<int, bool> odd; map<int, int> previous; bool add(int a, int b, bool c){//b>=a; if (!exist[b]){ exist[b] = true; odd[b] = c; previous[b] = a; return true; }; int i = previous[b]; if(i==a) return (odd[b]==c); if(i<a) return add(i,a-1, (c!=odd[b])); return add(a,i-1, (c!=odd[b])); }; int main(int argc, char * argv[]) { while (1) { exist.clear(); odd.clear(); previous.clear(); int numbers = 0; cin >> numbers; if (numbers==-1) break; int questions = 0; cin >> questions; bool flag = false; for (int i = 0; i<questions; i++) { int a = 0; cin >> a; int b = 0; cin >> b; string tmp; cin >> tmp; bool parity = false; if (tmp=="even") { parity = true; } if ((add(a,b,parity)==false)&&(flag==false)) { cout << i; flag=true; } } if (flag==false) { cout << questions; } } return 0; } What wrong? I got wa at 1 test. Then run it in the debug mode and you'll figure it out. change bool parity = false; if (tmp=="even") { parity = true; } to bool parity = true; if (tmp=="even") { parity = false; } and then break after flag=true; Edited by author 21.03.2012 22:25 Edited by author 21.03.2012 22:25{ 100 5 5 6 odd 7 8 odd 1 6 even 1 4 odd 7 8 even why result is 3 and not 4? } I think the answer is 4, because [1,4] is odd and [5,6] is odd and [1,4],[5,6] are continuous then the answer isn't 3, but it is 4. Edited by author 02.05.2012 15:37 Edited by author 02.05.2012 15:40 Edited by author 02.05.2012 15:40 Good programing skills!! Thanks~ :0 this approach is out of my mind, great work!! Извините, что по-русски, надеюсь, администраторы знают этот язык. В условии сказано, что длина последовательности задается в первой строке каждого теста. В примерах на форуме длина общая для всех, в каждом тесте задается только количество вопросов. Моя AC-программа действует по форуму: читает длину один раз, а потом только количество вопросов для каждого теста. Хорошо бы привести текст задачи в соответствие с реальностью. Совсем хорошо было бы дать в примере два теста, чтобы сделать структуру входных данных действительно ясной. 100000000 10 1 5 even 6 10 even 11 18 even 19 25 even 1 8 even 16 25 even 16 40 even 9 40 odd 1000 5000 odd 1000 6000 odd -1 answer: 7 why is it 7? could you explain? 10 6 1 2 even 1 1 even 3 4 odd 5 6 even 1 6 even 7 10 odd i think the answer of this data should be 1,but my program answer is 4 but it return an accepted! why?i think 4 is correct. "1 2 even" -> first 2 digits are 00 or 11 "1 1 even" -> first digit is 0. So if first 2 digits are 00, everything is ok Online tests surely contain several tests in one file, so: 1. Be sure clean your dictionaries before every test. 2. Be sure read ALL lines of current test, even you got correct answer before all input was used. This two issues were the reason of my WA1. After fixing them I got "Accepted". Use the following test to validate mentioned above points: 100 5 5 6 odd 7 8 odd 1 6 even 1 4 odd 7 8 even 3 3 1 1 odd 3 3 odd 1 3 odd 5 4 1 2 even 4 5 even 1 5 odd 3 3 even 10 5 1 2 even 3 4 odd 5 6 even 1 6 even 7 10 odd 20 8 1 9 odd 10 17 odd 18 26 odd 4 5 odd 27 40 even 21 40 odd 1 20 odd 10 20 odd 200 8 1 9 odd 10 17 odd 18 26 odd 4 5 odd 27 40 even 21 40 odd 1 20 odd 10 20 odd -1 Answer: 4 3 3 3 2 6 Why 6th answer is 2 rather than 6? Please Add it in tests! 20 10 1 1 even 4 4 even 2 2 odd 5 5 odd 1 3 even 3 5 odd 8 8 even 9 9 even 10 10 even 8 10 even -1 Correct answer is: 5 My AC prog give 10, but correct answer is 5 It looks as first input number (sequence length) isn't needed for solution. Why always Memory limit exceeded??? It is so wired. package com.island.timus.ahundrend; import java.util.Scanner; public class T1003_Parity { public static void main(String[] args) { Scanner in = new Scanner(System.in); int length = in.nextInt(); while (length != -1) { int[][] friendsAndEnemies = new int[2][length + 1]; for (int j = 0; j < length + 1; j++) { friendsAndEnemies[0][j] = j; friendsAndEnemies[1][j] = -1; } int conditions = in.nextInt(); int counter = 1; for (int i = counter; i <= conditions; i++, counter++) { Term.start = in.nextInt(); Term.end = in.nextInt(); Term.parity = in.nextLine(); if (Term.start > length || Term.end > length) { break; } if (Term.parity.trim().equals("even")) { join(friendsAndEnemies[0], Term.start - 1, Term.end); } else { int eneymyAncestor1 = findAncestor(friendsAndEnemies[0], Term.start - 1); int eneymyAncestor2 = findAncestor(friendsAndEnemies[0], Term.end); if (friendsAndEnemies[1][Term.start - 1] == -1 && friendsAndEnemies[1][Term.end] == -1) { friendsAndEnemies[1][Term.start - 1] = eneymyAncestor2; friendsAndEnemies[1][Term.end] = eneymyAncestor1; } else if (friendsAndEnemies[1][Term.start - 1] != -1 && friendsAndEnemies[1][Term.end] == -1) { join(friendsAndEnemies[0], friendsAndEnemies[1][Term.start - 1], Term.end); friendsAndEnemies[1][Term.end] = eneymyAncestor1; } else if (friendsAndEnemies[1][Term.start - 1] == -1 && friendsAndEnemies[1][Term.end] != -1) { join(friendsAndEnemies[0], friendsAndEnemies[1][Term.end], Term.start - 1); friendsAndEnemies[1][Term.start - 1] = eneymyAncestor2; } else { join(friendsAndEnemies[0], friendsAndEnemies[1][Term.end], Term.start - 1); join(friendsAndEnemies[0], friendsAndEnemies[1][Term.start - 1], Term.end); } } int pause = check(friendsAndEnemies); if (pause == friendsAndEnemies[0].length) { continue; } else { break; } } System.out.println(counter - 1); for (int i = counter + 1; i <= conditions; i++) { int x = in.nextInt(); int y = in.nextInt(); String z = in.nextLine(); } length = in.nextInt(); } } private static int findAncestor(int[] friends, int value) { int r = value; while (r != friends[r]) { r = friends[r]; } return r; } private static void join(int[] pre, int x, int y) { int ancestorX = findAncestor(pre, x); int ancestorY = findAncestor(pre, y); int r = y; if (ancestorX != ancestorY) { while (r != pre[r]) { int temp = pre[r]; pre[r] = ancestorX; r = temp; } pre[ancestorY] = ancestorX; } } private static int check(int[][] friendsAndEnemies) { int i = 1; for (; i < friendsAndEnemies[0].length; i++) { int friendAncestor = findAncestor(friendsAndEnemies[0], friendsAndEnemies[0][i]); int enemyAncestor = -1; if (friendsAndEnemies[1][i] != -1) { enemyAncestor = findAncestor(friendsAndEnemies[0], friendsAndEnemies[1][i]); } if (friendAncestor == enemyAncestor) { break; } else { continue; } } return i; } } class Term { public static int start; public static int end; public static String parity; } Deleting this post... had to hack another solution. Still don't see what was wrong with the original approach... must have been some kind of parsing issue with the input. Edited by author 08.02.2015 09:36 Please, who solved this problem with disjoint sets send me your solution to the e-mail which is in my profile. I wrote the code and it gives right answers on many tests, but not passes here. I need to compare where an error is. Thanks to all, no need anymore. I AC. For those who has not overcame the problem, i repeat (based on all of the advices i've read here) breafly where you may be wrong 1) if b>length, the friend lies 2) number of questions can be wrong. I read line by line and analyze if there is only one "word" or three. So, to determine where the next test begins i do NOT do like for(i=0; i<answers; ++i) but like while(scanf("%d%d%s",&a,&b,&oddeven) == 3) But so you all will receive 2 words from next lines, except for last ones. icanwin, of course i would. But if to be more precisely i do like this: gets(buf); if(sscanf(buf, "%d%d%s", &a, &b, oddeven) == 1) { if a == -1 then the end of input else a new test begins } can you please share your solutions with me could you please share your solutions with us? I don't understand why it show me a Runtime error (access violation) when it s compiling and it works perfectly in my computer ??? Can you share your code please? There can be many reasons for this behavior. Try to define a bigger array! In some way,I got test1. In the about 570th line,there is a mark"'".I don't know what is it for.I think it is by mistake. It is same with some large test points. Edited by author 05.04.2009 22:14 I have nothing to say about the Text 1... I Wa this point for ... about 1 hour please tell me if the max length is 109. which type i could use? I think you're meaning 10^9, in this case you can use int. The input is of multiple testcases, and each testcase starts with N, the length of the sequence, and K, the number of pairs of intervals. You only know when you stop after got N == -1. Naive ideas will get TLE even for testcase #1, and you will see your code runs out of 2s limit when feeding a testcase with N=10^9 and K=5000, when all pairs are good and you should output 5000. |
|