We will hold AtCoder Beginner Contest 395.
- Contest URL: https://atcoder.jp/contests/abc395
- Start Time: http://www.timeanddate.com/worldclock/fixedtime.html?iso=20250301T2100&p1=248
- Duration: 100 minutes
- Writer: hirayuu_cf, MMNMM, ynymxiaolongbao
- Tester: Nyaan, kyopro_friends
- Rated range: ~ 1999
- The point values: 100-200-300-350-425-500-625
We are looking forward to your participation!








I am looking forward to it.
The E also became easier and easier.
Maybe you are becoming stronger and stronger.
hope i can become green in this
E<<D,what even D?
It seems like ABC really likes these kinds of multiple pointer questions. You just need like 3 arrays maintaining the following mappings:
Maintaining these is quite simple but you must be careful.
Can you elaborate further?
3 Pointers version
I have other solution, but this is the 3 pointers one
I think the hash map one is more intuitive but little tricky
try not to use hashmap when 0~n is going to be filled with values
do you have the solution for that? I would be thankful
here
also do they not provide editorial? official one?
idk, i'm also new at atcoder
So glad that I was able to solve D by myself after the contest. Too me two wrong attempts and around two hours :)
I used three hash maps:
originalNest -> newNestnewNest -> originalNestpigeon -> originalNestHere's my submission
Somebody has met problems like D before, so this trick(or idea) of 3 pointer array is commen for them. Don't worry that you can't solve D fastly. It is just an idea you didn't meet before :)
Can you suggest some problems related to with these pointer indirection concepts ?
Sorry I forgot where I learned about this idea. I think this round's D is a good enough problem to let you understand this idea.
SpeedCoder 4 times in a row...
Is D meant to be solved via DSU?
No simple hashmaps
Can you tell me your method? I was using hashmaps too but couldnt handle for multiple swaps
Can anyone please help me, my code for E is ac in 69 tests cases and fails in one test case. My code
Me too. It's crazy。
1d dijkstra got me the same result as well. I barely had the time to implement 2d dijkstra and it got me AC
as wasif2397 said, use 2d djikstra, 1d is not correct,
arriving to a node with inverted/non-inverted has different effect.
Can you link me the code of the 2d djikstra solution if possible
here
thanks, but what does dist[i][0] and dist[i][1] store in your case?
whether it's currently inverted or not
D and E were good problems.
The one who passed 2 problems in 48 seconds might use AI. How can he type so fast!???
Can anyone help me with D. Below is my submission https://atcoder.jp/contests/abc395/submissions/63289001
You can't iterate in the whole nest list and modify the pigeon nest_id. It'll lead to TLE, you need to think about how do you manage the swapping of nests. Hashmaps will certainly help.
you suggest I use 2 hashmaps? one for nest to bird mapping and one for bird to nest mapping
Yes, I can post my solution, if needed. But the main idea is using 2 hashmaps. In fact I used 3 hashmaps, but probably 2 should be enough.
I used one hashmap for mapping notional_nest_id and one for mapping real_nest_id. And the a map for mapping pigeon_id to notional_nest_id.
And you'd have to think on where to use which. But this should give you an idea on how to move forward.
we need to print notional_nest_id and real_nest_id is the index i assume?
In fact it's opposite, all the changes would be done as per real_nest_id, but the map between pigeon_id and nest_id is actually the notional_nest_id.
yup after 2 hours of focus, understood the soln
You just need to maintain the physical location of the pigeon, the number corresponding to the nest at location i, and the location of the nest corresponding to the number i
Very good problems! They are not too hard to solve but require a little creative thinking. But I can't solve problem G at all...
Can anyone help me on how to solve the E.
My logic: First traverse (BFS) the reverse graph and find the cost for each node from root(1) to each node. And then traverse (BFS) the normal graph and check at every node, the current cost + cost to reach nth node in reverse graph from this node.
In fact, you can flip the graph more than two times. But it seems like your program only considerd two flips. Hack : 12 13 1 2 1 12 3 2 3 1 4 4 5 5 6 6 7 7 2 3 8 8 9 9 10 10 11 11 12
I hope my code can help you :)
finally an ABC with a good D which isn't a speedcoder problem.
First time saw problem D, yeah it's kinda hard.
But after solving it, I feel like it can qualify as speedcoder problem.
how i do a good upsolving ? i am beginner , so i am confused
Just try to a moment till you can say to yourself that yeah I give up now time to look at the editorials :)
I dont get it how, or why, G works.
What am I doing? I do a Floyd-Warshal on the whole graph to get the actual minimized cost of each edge. Then, on each query, I create the MST of the subgraph consting of the vertices 0..k, plus s and t.
But this does not give the right result. Can somebody explain?
Here is my notworking submission
After floyd-warshal, the path with the minimum distance between 2 different pairs of vertices could include the same edge of the orignal graph.
So, when you just add all the distances up, you may count the same edge multiple times.
Expected output:
3Your output:
4I see, thanks!
How to do F ? Can someone explain ?
Please update the editorial, I want to see how to solve G :)
I'll explain my solution to the problem. The idea is to generalize the solution to the original problem of finding the Minimum Steiner Tree.
Minimum Steiner Tree
Beforehand we compute the shortest path between every pair of nodes using Floyd-Warhsall, so we have access to the shortest path between every pair of nodes in $$$O(1)$$$.
Then we can solve this using dynamic programming, representing each state as follows:
$$$dp[mask][u]$$$ represents the minimum cost of a subtree that spans all nodes in $$$mask$$$ and includes $$$u$$$ as an arbitrary additional node.
Each mask represents a subset of the special nodes we want to have in the final tree, and $$$u$$$ is any node in the graph (possibly in $$$mask$$$). The solution to the problem is the minimum over all nodes $$$u$$$ of $$$dp[FULL][u]$$$ where $$$FULL$$$ is the mask representing the set with all special nodes in it.
We can compute each state in order of $$$mask$$$ (i.e when we are solving a particular mask, we should have solved all previous masks before). When computing the problem for a fixed mask: - if the mask is empty the cost is 0, since the tree consist of only one node ($$$u$$$) - if the mask has one element the cost is $$$dist(s, u)$$$, where $$$s$$$ is the node in the mask, and $$$u$$$ the other node (possibly the same). In this case the tree is just the shortest path connecting both nodes. - The case with at least two elements is the more interesting one.
If the mask has at least two elements, then in any optimal tree to this subproblem, exists a node $$$v$$$ such that removing the edges incident to $$$v$$$ will split the tree into multiple components where no component has all special nodes.
The intuition behind this argument is that, either $$$u$$$ itself is on the optimal tree, which means it is in the path between two special nodes, or is a leaf in the tree, and by moving in the direction of any special node we will find the node $$$v$$$ with the desired properties.
Given that we know this node $$$v$$$ exists, we try each node in the graph as if it were the node $$$v$$$, and then try every proper non empty $$$submask$$$, ending at $$$v$$$
This way we find the minimum cost tree with with nodes in $mask$ and a node $$$u$$$ in the path between two special nodes. Now we need to compute the case where the node $$$u$$$ is a leaf outside of the path. We can compute it using a variation of shortest path, using multiple sources and multiple destinations. It is equivalent to compute dijkstra in a graph with a virtual node that can reach any node $$$u$$$ with cost $$$dp[mask][u]$$$ and then can move using the regular edges of the graph.
The overall cost of computing the first part is $$$O(3^k * n)$$$ and the cost for the second part is $$$O(2^k \cdot n^2)$$$.
Minimum Steiner Tree 2
We now extend these ideas to solve the given problem. The previous dynamic programming is already quite rich, it returns the minimum tree with all special nodes plus an extra arbitrary node. Now we are interested in the case of having two extra arbitrary nodes, which indicates we want to compute the following dynamic programming:
$$$dp[mask][u][v]$$$ represents the minimum cost of a subtree that spans all nodes in $$$mask$$$ and include $$$u$$$ and $$$v$$$ as arbitraries additional nodes.
Where $$$u$$$ and $$$v$$$ could be the same and could be in $$$mask$$$. Again we fill the dynamic programming in a similar way. - If the mask is empty then the minimum cost is the cost of the shortest path between $$$u$$$ and $$$v$$$. - If the mask has one node $$$s$$$, then we should find the Steiner node, this is a node that minimizes the sum of the distances to $$$u$$$, $$$v$$$ and $$$s$$$. This can be done in $$$O(n^3 \cdot k)$$$ by trying all candidates. - Otherwise we are again in the interesting case.
Now we need a similar observation than the one we need before. We will initially compute correctly $$$dp[mask][u][v]$$$ for pair of nodes $$$u$$$ and $$$v$$$ such that they are part of an optimal tree, and then we can extend that later to every pair of nodes by moving through edges using dijkstra with the virtual node as we did before.
By removing incident edges to the nodes $$$u$$$ and $$$v$$$ we divide the tree in components, such that some components are a subtree of node $$$u$$$ , some are a subtree of node $$$v$$$ and potentially one of them is in the path between $$$u$$$ and $$$v$$$. More importantly there should be at least one special node in the subtree of $$$u$$$ and at least another special node in the subtree of $$$v$$$. Only in this case $$$u$$$ and $$$v$$$ are part of the optimal tree for the given $$$mask$$$. Given this observation we can iterate through all disjoint submask A, B, C, such that A and C are the special nodes in the subtrees of $$$u$$$ and $$$v$$$ respectively, and $$$B$$$ are the special nodes in the path between $$$u$$$ and $$$v$$$. Notice A, B, C should be a partition of $$$mask$$$, and $$$A$$$ and $$$C$$$ shouldn't be the whole mask. We can iterate through all these masks in $$$O(4^k)$$$, for an overall complexity of $$$O(4^k \cdot n^2)$$$
Using dijkstra to extend the found values to every possible pair has cost $$$O(2^k \cdot n^3 \cdot \log n)$$$