### fcspartakm's blog

By fcspartakm, history, 4 years ago, ,

• +85

 » 4 years ago, # |   +24 Can anyone solve problem D when statement require replace land by water instead of replace water by land ??
•  » » 4 years ago, # ^ |   -30 Read the statement: "You task is to fill up with the earth the minimum number of water cells" water should be filled up with the earth. It's just a translation from Russian and word order can be a bit unusual for native English speakers. At least, you always have some examples, and can guess, what you are requered. And, of course, you are allowed to clarify all the "dark" places, just asking the jury))
•  » » » 4 years ago, # ^ |   +4 I think you misunderstood the statement, he is assuming that the situation he mentioned was the question instead.I have no idea how to solve it with a decent time complexity though.
•  » » » 4 years ago, # ^ |   0 I think it’s just a matter of curiosity: is it possible to solve a similar problem where you have to replace land by water instead?
•  » » 4 years ago, # ^ |   0 if N,M<=20, it will be easy to solve.
•  » » » 3 months ago, # ^ |   0 how ??
•  » » 3 months ago, # ^ |   0 and we have been told that, we can change land to water and water to land !! any !! then how can we solve?
 » 4 years ago, # |   -177 Hi, i got accepted in main tests to all my submissions then all of it was skipped and i became out of competition , but i didn't cheat ; can anyone please explain in which cases that happensthanks in advance
•  » » 4 years ago, # ^ |   +199 I can! 21148857 and 21151896 Also you have really bad karma: I hope you can find the strength to apologize not to me, but to community.
•  » » » 4 years ago, # ^ |   +33 Codeforces police arrived
•  » » » 4 years ago, # ^ |   -62 apologies for my rating_greed
•  » » » 4 years ago, # ^ |   +43 why aren't both of the accounts disqualified/banned? it should decrease cheating by a great extent
•  » » » 4 years ago, # ^ |   +1 the great cheater detector (:
•  » » » 4 years ago, # ^ |   +34 My friends' template is same as my template, and it is quite long(20 lines). Can this be a problem in future?
•  » » 4 years ago, # ^ |   +23 shame on you
•  » » 4 years ago, # ^ |   -9 Shame on you !
•  » » 4 years ago, # ^ |   +4 Thief's mind is always like a police... "I didn't cheat" :P
•  » » 4 years ago, # ^ | ← Rev. 2 →   -16 Check the replies above my comment !I wonder why users with a lower rating always get downvoted !I think the letter s being lower case is not the problem here ! maybe it's the exclamation mark ?
•  » » » 4 years ago, # ^ |   +3 Maybe he is 3 hour late for the karma train.
•  » » » 4 years ago, # ^ | ← Rev. 2 →   0 It happens all the time on Codeforces. When one said something interesting and got upvoted, another one would comment a very similar statement with a hope to increase his contribution point. It is not because of lower rating that make people click downvote button. It's because of "following trends" in comments. I was also the one who downvote those repeated comments.
 » 4 years ago, # | ← Rev. 2 →   0 Exact code as in tutorial for D CODE
 » 4 years ago, # |   +8 Holy molly the solution for div2E is very elegant.
•  » » 4 years ago, # ^ |   0 I solved it without one observation and it was less elegant
•  » » 4 years ago, # ^ |   +9 Why is molly holy?
•  » » » 4 years ago, # ^ |   +3 Because it's molly silly! PS: It was rhyming. So...
 » 4 years ago, # |   0 Why in problem D contraints for n, m was set so low? I thought bruteforce would work fine, but it failed me :(
 » 4 years ago, # | ← Rev. 2 →   0 Why do people refer to the problems like “div2E” when div2 is the only div in this contest?
•  » » 4 years ago, # ^ |   +26 It helps in using Ctrl+F to find the comments related to that question.
 » 4 years ago, # |   0 Can anyone explain what's wrong with this solution — Link. Thanks
•  » » 4 years ago, # ^ |   +1 I had the same error getting WA in test 8 with answer 433 instead of 422 in 21177336 and now i have fixed it 21181912
 » 4 years ago, # | ← Rev. 2 →   0 For me ,it's hard to follow PROBLEM C, Can someone explain ? Thanks .
•  » » 4 years ago, # ^ |   0 Hi, I described my solution to problem C (Div2C) as Comments in my Accepted Code
 » 4 years ago, # | ← Rev. 3 →   -8 Thanks so much. Sorry for causing problems.Please ignore me.
•  » » 4 years ago, # ^ |   +11 5 5 1 3 1 4 1 5 2 3 2 4 1 2 2 2Participant's output NoJury's answer Yes 5 1 3 2 4 2 1 3Your output is No
 » 4 years ago, # | ← Rev. 2 →   0 I met a very strange problem, it cost me a lot of time and badly affect my standing. I still cannot figure out what the problem is. It's problem B. I ran it correctly locally but it could not even pass pretest 1. And When I debug it on code forces, it became very weird. #include #include #include using namespace std; #define pb push_back #define mp make_pair #define ll long long #define ull unsigned ll #define db double #define INF 0x3f3f3f3f #define MOD 1000000007 #define PII pair vector inp; vector outp; int main() { int len; string str; cin >> len >> str; str += "_"; bool flag = true; char buf[512]; int idx = -1; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (c == '(') { if (idx >= 0) { outp.pb(string(buf)); memset(buf, 0, sizeof(buf)); idx = -1; } flag = false; } else if (c == ')') { if (idx >= 0) { inp.pb(string(buf)); memset(buf, 0, sizeof(buf)); idx = -1; } flag = true; } else if (c == '_') { if (idx >= 0) { if (flag) outp.pb(string(buf)); else inp.pb(string(buf)); memset(buf, 0, sizeof(buf)); } idx = -1; } else { buf[++idx] = c; } } int a = 0; for (int i = 0; i < outp.size(); i++) { a = max(a, (int)outp[i].size()); //cout << a << endl; } cout << a << " " << inp.size() << endl; } This is is my code for problem B. You can see the line I commented. If I uncomments it, it outputs correct number for pretest1. However, when I comment it, it outputs 13 or 7 sometimes randomly. Is this a bug? Again, when my code runs locally, it outputs correct answer. Hope someone could help me. What's wrong? It drives me crazy!
•  » » 4 years ago, # ^ |   +6 You never initialize buf, so when you do string(buf) for the first time, you get garbage. In fact, to be more pedantic, the string(buf) call has what’s called undefined behaviour, so literally anything can happen from there on. Fixing this gets your solution accepted.
•  » » » 4 years ago, # ^ |   0 Thank you very much for the help!
 » 4 years ago, # | ← Rev. 2 →   0 There's also a similar but easier solution.That is using Kruskal to build a MST.The weight between two vertices is 3 if they are s and t,and the weight is 1 if one of them is s,and the weight is 2 if one of them is t,and the weight is 0 if they are not s and t.Then you can use Kruskal as usual with the limit of ds and dt.But when I finished coding this problem,the contest had been over. :(PS:This solution is a wrong one.But I cannot delete this comment.:(
•  » » 4 years ago, # ^ |   +1 It's wrong actually imo, i had similar to this during contest, got WA on 68. You got very lucky with this thing.Look this test5 5 1 3 1 4 1 5 2 3 2 4 1 2 2 2If you were to mark 1 if equal to t, and 2 if equal to s, your solution would fail on this testcase.
•  » » 4 years ago, # ^ | ← Rev. 2 →   +4 Your solution, as described, fails this test: 5 5 1 3 1 4 1 5 2 3 2 4 1 2 2 2 (should be Yes) (this is system test #68)Or as you implemented it (with a weight of 1 for t and 2 for s), this equivalent test: 5 5 1 3 1 4 2 3 2 4 2 5 1 2 2 2 (should be Yes; your code gives No)
•  » » » 4 years ago, # ^ |   +1 Can't he do that for both cases? I mean once t=1 and s=2 and vice-versa?
•  » » » » 4 years ago, # ^ | ← Rev. 2 →   +6 Like, try to solve with t=1 and s=2, and if it fails, try to solve with t=2 and s=1? Okay, here’s a new test: 7 8 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 7 1 2 3 3 This solution fails to find an answer and outputs “No”: with t=1 and s=2, it starts by adding edges 2 3, 2 4, 2 5 and fails to connect node 7 to the tree, and with t=2 and s=1, it starts by adding edges 1 3, 1 4, 1 5 and fails to connect node 6 to the tree.Meanwhile, the correct answer is “Yes”: for example, 1 6, 2 7, 1 3, 2 3, 1 4, 2 5.I should note that this solution can get lucky: the order in which Kruskal adds edges of equal weight is unspecified, so it can just happen to add the right edges first. The problem is that the opposite can happen equally well.
•  » » » » » 4 years ago, # ^ |   0 Yes,you are right.My solution is wrong.Thank you very much.
•  » » » » » 4 years ago, # ^ |   0 I first added the bridges to the MST and then applied kruskal....it passed all tests,dont know if its actually correct.
•  » » » » » » 4 years ago, # ^ |   0 Care to explain?
 » 4 years ago, # |   0 Wanted to know why any sequence of alphabets is a word if it is at the end . The question specifies that it should begin and end at "(" ,")" or "_". So a word athe end like "_c_abc" should give an answer 1 0 as abc is at the end and there is no special character at the end of abc . P.S I am a beginner so forgive me if my doubt seems too stupid
•  » » 4 years ago, # ^ |   0 In the task description they say if the beginning/end is a special character OR doesn't exist. So it's like there's a _ added before and after the string you input. :)
•  » » » 4 years ago, # ^ |   0 ok thanks :)
 » 4 years ago, # |   +1 can someone explain the solution for problem D. i am not able to understand the editorial
•  » » 4 years ago, # ^ |   0 Let's find all lakes with thier sizes, sort it by size and delete first sz — k lakes, where sz — number of lakes. That's all.
•  » » 4 years ago, # ^ |   0 i am not even getting statement of problem D , after getting pretests passed and WA in system testing.
•  » » » 4 years ago, # ^ |   +1 Why are you using BFS? DFS is better and shorter, I think
•  » » » » 4 years ago, # ^ |   0 but why it is giving WA..
•  » » » » » 4 years ago, # ^ |   0 Because you must use std::multiset to save sizes of lakes
•  » » » » » » 4 years ago, # ^ |   0 but i am using set > and second value of all pairs will be different so i think that it should not affect the answer.
•  » » » » » » » 4 years ago, # ^ |   0 Yes, you are right.
•  » » » » » » » » 4 years ago, # ^ |   0
•  » » » » » » » » » 4 years ago, # ^ |   +1 You can denote all STL containers as global variables. Maybe it will help.
•  » » » » » » » » » 4 years ago, # ^ |   +1 You can add the same cell to the BFS queue more than once, and you’re incrementing ss every time. You should update a when adding to the queue, not when removing from the queue; or at least check that a is still 0 when removing from the queue.
•  » » » » » » » » » 4 years ago, # ^ |   +1 After changing it takes MLE at 26 test because long long http://codeforces.com/contest/723/submission/21171332
•  » » » » » » » » » 4 years ago, # ^ |   +3 finally got it, thanks a lot
•  » » » » 4 years ago, # ^ | ← Rev. 3 →   +7 I disagree. BFS and DFS take the same amount of code, and, being used to BFS myself, I actually had to double-check that BFS wasn’t shorter! BFS is also faster and ensures there’s no need to worry about stack overflow.Finally, I personally find it easier to write. (Personally. You can find DFS easier to write, and that’s fine too.) For example, I normally declare my variables locally within functions, and with DFS, I need to move them out to the global scope, whereas BFS works as is. (Unless it’s moved to its own function, but this is rarely done during contests. And while modularization is great in practical programming, during contests I tend to find it easier when the search code is right where it happens rather than in a distant place that I need to jump to when writing or reading my code.)
•  » » » » » 4 years ago, # ^ | ← Rev. 2 →   +2 Firstly, thanks for taking the time to write a useful and well considered comment. I disagree with what you have said, but I am not looking for a fight! I just want to give a gentle rebuttal in defense of DFS.The "BFS is faster than DFS" sentiment doesn't seem right to me. They both examine every edge once, and every vertex comes on/off the stack/queue once. The only difference is their memory patterns. BFS puts more data in the queue when the graph has a large frontier. The DFS stack is larger when the graph is very long with few branches. For most randomly generated graphs, it is the case that BFS is slower because random graphs have large frontiers (this is related to research on small-world networks and other random graph properties). In addition, when working on a grid, BFS usually must allocate more queue space because the implicit graph has a large frontier. Finally, a stack typically has better cache locality than a queue, since it only ever accesses one end of the data structure. My own ad-hoc experiments show that, until your input is so large that is breaks L3 cache, DSU is actually faster, despite the inverse-Ackerman factor.The "easier to write" part also doesn't seem right to me. You can code a DFS simply by replacing queue with stack. No need for any recursive function. This means you can use a DFS any time you could use a BFS, as long as you aren't using any of the special properties of either.One final note on stack-overflow. In competitive programming it is common to set the stack size to be effectively unbounded. You will get MLE rather than stack-overflow on Codeforces and similar websites, I believe. In Python/Java it is trivial to set the stack-size/recursion-limit to whatever you want, I think.Don't get me wrong, I don't think DFS >> BFS. Certainly you should keep using a BFS if you prefer it; I am not trying to convert you to the one-true-way. I am just worried about people using BFS even when they prefer DFS.
•  » » » » » » 4 years ago, # ^ | ← Rev. 3 →   +3 Thanks for your well-thought-out response!I should note that I don’t want to pressure anyone to stop using DFS and switch to BFS either. By all means, use whichever you’re most comfortable with. Perhaps this thread will just serve to shed a little more light on the various benefits and drawbacks of each approach.You make a good point about nonrecursive DFS. Indeed, if your DFS doesn’t need to do any extra work after returning from a node’s child, you can implement it just like a BFS but with a stack instead of a queue. (Or if it does, you can’t easily use BFS proper either, so recursive DFS is your only choice.) I’ll admit I don’t have a developed opinion of this sort of DFS. I’ve considered this myself, and I do agree that sometimes the BFS frontier should be much larger than the DFS stack, and then it seems beneficial to prefer DFS. Maybe I should switch to this as my default search in contests.When I said earlier that BFS was faster, I was only comparing it to recursive DFS. And the only reason such DFS should be slow is that it involves a lot of function calls, and function calls are unfortunately slow. The call itself isn’t fast, and then there is stack frame management! On this note, recursive DFS also uses more memory than a nonrecursive DFS because it needs to preserve all the local variables on the stack.This is also where the stack overflow danger crops up: the more locals you have within the recursive DFS function, and possibly also the longer the function code is, the larger the stack frame. Perhaps this is not as much a problem nowadays, but I’ve seen DFS overflow the stack. The automatic stack extension in Linux that gives the impression of an infinite stack isn’t extremely reliable either, or at least wasn’t some years ago; and Windows doesn’t have such a thing at all AFAIK. Indeed, Codeforces specifies a fixed 64 MB or 256 MB stack where applicable. Of course, in many cases in competitive programming, there is a relatively easy workaround: don’t use locals in recursive DFS! Put your data in global arrays instead.As a quick test, I took my solution to this contest’s problem F that used BFS and resubmitted it with recursive DFS. This test is by no means conclusive, as it gives just one data point and I don’t have an awful lot of trust in Codeforces’ timings, but I got 405 ms using BFS and 623 ms using DFS.Just for completeness, one final note regarding function calls: calls that are inlined are fast! But recursive calls, other than tail-recursive calls, cannot be inlined. Although I know MSVC and CLR are actually able to unroll recursion somewhat (albeit MSVC requires a pragma for this), which should help, and GCC applies a different optimization, but I forget how useful it was in my tests. (Not DFS tests, just recursion tests.)Oh, what did you mean when you mentioned DSU? I know what that is, but why did it come up and what were you comparing it to?
•  » » » » » » » 4 years ago, # ^ |   +5 Yes, you're absolutely right that recursion is costly! Often it is nice, simple code, but it hurts performance, and you make a good point. Often in Haskell recursion can be very cheap, however, but I've never been able to use Haskell well in a competition.Regarding DSU: if you are using a BFS/DFS to find connected components, then simply running union on each edge via DSU is what I am talking about.
•  » » » » » » » » 4 years ago, # ^ |   0 (Regarding DSU:) huh, that’s interesting.
•  » » » » » 4 years ago, # ^ |   0 I think it's all about the nature of the scenario. If you are a competitive programmer, then chances are you need DFS to handle tree-dp (or retrieving information from children immediately). I think many people prefer dfs over bfs since more problems require dfs in specific than bfs. (Or at least on CF, I've been practicing a lot on CF and this is the case)
•  » » 4 years ago, # ^ |   +1 First of all, to decrease the number of lakes, you need to destroy an entire lake at a time. (If you try to destroy only a part of a lake and keep the rest of the lake, you’ll still have a lake! Or maybe even multiple lakes. Nothing good can come out of this.)So you just need to greedily find and destroy the smallest lake until there are only k lakes remaining.You can find all lakes first, then pick out the smallest ones (e. g. by sorting all lakes by their size) and destroy them.To find a lake, you start from a water cell and perform flood fill via a depth-first search or a breadth-first search (as the first two examples on Wikipedia show). To find all lakes, you can go through all cells on the grid and launch flood fill from each water cell that you’ve never visited before.Make sure to ignore “lakes” that touch the edge of the grid, as they are no lakes! Instead, per the problem statement, they’re part of the ocean.
•  » » » 4 years ago, # ^ |   0 i am doing the same thing.
 » 4 years ago, # |   0 I'm not getting the logic used for Div2C . Could anyone please explain it in a simpler way?
•  » » 4 years ago, # ^ |   0 Hi, I described my solution to problem Div2C as Comments in my Accepted Code
 » 4 years ago, # | ← Rev. 3 →   +16 For problem E, I did the exact same thing as mentioned in editorial except that instead of adding vertices between odd-odd nodes, I created a pseudo-node(say 0), and added edges between this node and every odd node in the graph. Then I simply found the euler tour, and remove edges with 0 as one end-point. Here is the AC code with comments.
•  » » 4 years ago, # ^ |   +3 Can you give some place to read about euler tour?Your code to find euler tour is quite short and simple.
•  » » » 4 years ago, # ^ | ← Rev. 2 →   0 I can't find the place from where I read about it. But it goes something like: If there's a Euler tour, then set your current vertex to any vertex with odd degree( otherwise any vertex will work.) Then do the following:1. If current vertex has no neighbors, add it to the answer.2. Otherwise push it to queue, and set the current vertex to one of its neighbor. Mark this edge so that you do not use it again in future.3. Continue doing this(step 1 and 2) until the queue is empty and current vertex has no neighbours.4. Your answer would contain the reverse euler tour you followed.Since you add a vertex to the answer only after going to all of its neighbours, the simple recursive solution(or dfs) could work. Otherwise you can even easily implement the above steps using a queue.
•  » » » » 4 years ago, # ^ | ← Rev. 2 →   0 Hey some basic doubts, The complexity of the algorithm is O(V + E) right?And I have a little problem in proving it.
 » 4 years ago, # |   +1 Hey guys,i recently got weirdest output for problem D which i cant explain..I passed all tests except 8th where my output brings out some really weird symbols..Can someone check it out ? Cheers LINK
•  » » 4 years ago, # ^ |   0 foo has only 260 elements, but there may be up to nm / 2 = 1250 lakes initially (imagine a chequerboard pattern), and the test you’re failing on evidently comes close. You’re writing past the end of the foo array and corrupting your memory.
•  » » » 4 years ago, # ^ |   0 Thanks a bunch man! :)
 » 4 years ago, # |   0 What will be the solution to problem D if we were also allowed to change land cells to water cells?
 » 4 years ago, # |   0 Help, I don't undestand the problem C. Can someone explain it?
•  » » 4 years ago, # ^ |   0 Hi, I described my solution to problem Div2C as Comments in my Accepted Code
 » 4 years ago, # |   0 Guys, is there any problem similar to "C — Polycarp at the Radio"?
 » 4 years ago, # |   0 I found Problem C very difficult to understand during the contest. I kept on messing around with bands, groups, songs and playlists.
 » 4 years ago, # |   +19 For problem E, I see 'flows' in the tags. How can it be solved using flows?
•  » » 4 years ago, # ^ | ← Rev. 2 →   0 This is a similar problem I solved using simple flows concept(Sum of incoming = Sum of outgoing).
 » 4 years ago, # |   +6 Solution to div2E is just elegant! Could someone please provide some problems that have similar topic to this one?
•  » » 4 years ago, # ^ |   +4
•  » » 4 years ago, # ^ |   0 could you please explain me in div2E how to find euler cycles. and also i'd be grateful if you could give some source to read it.
 » 4 years ago, # |   0 In problem F. Problem says: 'There are no loops and no multiple edges in the graph'. In the first case, 1 2 2 3 3 1 Isn't a loop?
•  » » 4 years ago, # ^ |   +8 I was confused at first, but this is the correct definition from online:"A loop in a graph is an edge of a node connecting to itself"So the set you mentioned is a cycle, instead of a loop. (Actually all graphs with undirected edges have cycles... Or else they are classified as tree)
•  » » » 4 years ago, # ^ |   0 [wiki](https://en.wikipedia.org/wiki/Loop_(graph_theory)) Yeah, thanks so much.
 » 4 years ago, # |   +16 How to solve E using flows ?
 » 4 years ago, # | ← Rev. 2 →   0 can someone tell me where i m wrong in my codehttp://codeforces.com/contest/723/submission/21200797
•  » » 4 years ago, # ^ |   0 You've done something wrong when counting the size of the lake during bfs.Imagine that we have (1,1) and (2,2) in queue, both of them will push (1,2) and (2,1 in queue since it is not yet "visited" when issafe was queried, and gets counted.
•  » » » 4 years ago, # ^ |   0 ya got it. thanx alot for identifing my mistake
 » 4 years ago, # | ← Rev. 3 →   +3 For problem E , its written in the editorial Let's choose how to connect them — with vertex s, with vertex t.... How can we connect the two spanning trees with s or t because s doesn't have any edge to other spanning tree and same goes for t ? can anyone please explain this ? Edit — Problem F
•  » » 4 years ago, # ^ |   0 First, let's notice that problem is Div2F, not E. I guess it was meant, that at the end the graph is restored(!) by getting back s, t, and all the deleted edges if any. After step 2, vertices s and t will belong to the two spanning trees. Assume s and t in the same component; otherwise we are done. These two spanning trees should be linked to the remaining components as well as to each other either directly or indirectly.
•  » » » 4 years ago, # ^ |   0 In step 2, we are only adding edges from s to all components, which have no edges to t and edges from t to all components, which have no edges to s,hence there is no way they can be in same component after step 2.Then we can only connect these two components either directly taking edge between s and t or by connecting s and t both to one of the remaining components which are not in these two spanning tree.There is no way seem possible to me by which we can connect these two components with vertex s only or with vertex t only.
•  » » » » 4 years ago, # ^ |   0 You are right. I was thinking of writing about this in a separate post tomorrow, but you were the first to bring it up! Consider a connected graph on 4 vertices: a,b,s,t. Edges: as, at, bs, bt. Using edges as, bs implies that either at or bt must be used. Thus the citation "or with both of them (only if we have in the graph an edge {s, t})" is not (!) valid meaning that the example above does not have an edge between s and t, yet both s and t must be used. I wonder whether this situation is covered in the system tests?
•  » » » » » 4 years ago, # ^ | ← Rev. 2 →   +4 I think the very last part of the solution for F in the editorial is just poorly written. Personally, I don’t understand what it means, even after reading your comments. I actually posted a comment about this almost immediately after the editorial went up, but you haven’t seen because it’s in Russian, and for some reason, it stands there unanswered and with 0 rating to this moment.Note that it’s literally impossible to restore only edges adjacent to s and none adjacent to t, or vice versa, and get a connected graph, unless the graph also contains a (s, t) edge. You always need either (s, t) or a component that is simultaneously connected to both s and t. You don’t need any special test case for this—this applies to every single test case!Here’s how I’d explain my solution instead:So we have two trees and a (possibly empty) bunch of components each of which we know how to connect to s and how to connect to t. Pick any one of these components and connect it to both s and t; then connect each of the rest of the components to whichever side you like. The easiest way would be to greedily connect the remaining components to s until its degree reaches d s and then connect the rest to t (or vice versa). If there are no such components at all and we only have two trees, then there must be an edge (s, t), because we know the original graph was connected; simply use that edge.
•  » » » » » » 4 years ago, # ^ |   0 **** ~~~~~ Привет[](http://)** [](http://)ф![ ](http://)- — — — — ______ ~~~~~ **** mikl181**********
»
4 years ago, # |
0

I have done a different approach to problem F: 1- first we remove t from the graph 2- then assign all edges coming from s the weight 1000. 3- then run prim's algorithm to calculate the MST. this will guarantee that all cycles are removed except cycles containing t, and the priority for the removed edges is given to the edges comming from s. 4- repeat steps 1, 2, 3 but in another version where s is removed instead of t. 5- now lets call first tree sTree and second tree tTree. 6- now combine sTree and tTree by adding any node from tTree which doesnt appear in sTree to sTree and its corresponding edges.

# note that any node which didnt appear in sTree is either t or a node that cant arrive from s without passing by t.

now we have a graph with no cycles except the ones that contain both s and t in the same cycle. 7- lets count the paths by simple DFS from each edges coming from s and if the DFS arrives to t then increament number of paths and record a pair of edges (the edge coming from s and the one arriving at t).

# note that paths cant overlap (two path can't have node or segment in common) because if this happens then there will be another cycle near to s OR near to t, but we already removed all cycles except ones containing both s and t (contradiction).

9- now to form the final tree, from x paths we got w should cut any x — 1 path and leave only one to make sure there is no cycles. FINALLY: using ds and dt and the pairs we recorded, u can determine which paths should be removes from s and which should be removed from t.

 » 4 years ago, # | ← Rev. 2 →   0 I think I have an alternative (greedy) solution for F. Let's call S the resulting spanning tree. The "add" operation assumes that it doesn't create cycles in S. First, put in S all bridges adjacent to s or to t. Then, put in S all edges not adjacent to s nor to t. Finally, greedily add all edges adjacent to s or to t. The only tricky case to handle is the (s, t) edge, which has to be processed at the end. Then there is a solution iff at the end of the algorithm, S contains all vertices from G. In overall it looks correct to me, though it's too late now for me to prove it.Submission
 » 4 years ago, # |   0 For Problem 723C, I passed first 2 cases, but for 3rd test case I am getting runtime error and I have no idea why. infact I tried it on ideone, and if the elements in original array exceed 1000, I start getting runtime error. below is my code, please help me why is it so. #include int main(){ long long int n,i; long long int j; long long int m; scanf("%lld %lld",&n,&m); long long int a[2001]; long long int c[2001]={ 0 }; for(i=0;i=res)||a[i]>m)){ if(a[i]<=m) c[a[i]]-=1; a[i]=ele; changes+=1; c[ele]+=1; if(c[a[ele]]>=res){ min=n+5; for(j=1;j<=m;j++){ if (c[j]
 » 17 months ago, # |   0 can somebody tell me why this solution does not work? https://codeforces.com/contest/723/submission/51326586
 » 5 months ago, # | ← Rev. 2 →   0 Problem DCan someone please point out why my solution is giving wrong answer on test case 7 problem D?? Thanks.