You are not logged in. Please login at www.codechef.com to post your questions!

×

Editorial for NINENINE - PELT2019

Problem setter: panik

Problem Code: NINENINE

Difficulty: Medium-Hard

Prerequisites: Finding Bridges in a graph in linear time, Combinatorics, Inclusion Exlusion Principle..

Explanation: First of all, you need to calculate the bridges in the graph in linear time, since the graph must remain connected at every step i.e during deletion and insertion. To find this you can read about it in these links:
1.) G4G
2.) youtube video (I personally found this link better than G4G)

After you have obtained the set of Bridges, you now need to precalculate some things which would later prove to be helpful.

1.)save all the edges of the Graph in an unordered Set. This can be done in nearly Linear time.

2.) For each vertex find the count of vertices connected to it having a particular degree for each kind of available degree. Store this information in a vector of maps which contains this information for each node. This can be done in O(M).

3.) Store the count of edges between vertex of degree x and vertex of degree y for all possible edges available in the graph. Store this information in a map which stores the count of edges between a particular degree x to a particular degree y. This can be done in O(M)

4.) Make a frequency array and find the count of vertices of each particular degree.

Now comes the time to finally start calculating all the possible graphs.

Start Traversing all the edges in Graph.
If the edge is available in the set of bridge edges, then skip this edge
If not,
Temporarily reduce the degree of both the vertices of the edge and reflect this change only in the frequency array. So the count of original degree frequencies reduces by 1 and the new degree frequency increase by 1.
This change would be reverted later.

If the degree of both the vertices is same p= (freq[degree]X(freq[degree]-1))/2 -1
Else p= freq[degree1]*freq[degree2] - 1

Now we need to remove some cases from p to remove the cases where multiple edges occurred in some possible graphs i.e edge already exist in the graph. We also need to add some cases in p because of change in the degree of some vertex due to which count of edges between two particular degrees would change, so some extra cases would be subtracted from p when subtracting those edges which already exist in the graph. This can be done by some observation in the graph

Refer to the solution for better clarity.

Author’s solution: click here

Testers Solution : click here

Bonus question: What if I remove the statement that the graph must be connected at each step and replace it with that the final graph should be connected which means that the graph can disconnect during deletion but later during insertion has to become connected again.
Personally, I was not able to find a solution to this question. Lets discuss this in the comment section. I would seriously like to know the solution to this problem.

asked 11 Jan, 18:35

panik's gravatar image

5★panik
1166
accept rate: 7%

edited 11 Jan, 18:47


At first, I missed the statement that graph should be connected at each step and thought of the following solution: Make bridge tree of the graph and calculate the answer corresponding to non-bridges as earlier. For bridges (let u-v), find the components (nodes in the bridge tree) in which u and v lie. Now we need to calculate the number of nodes with a given degree in the subtree of a node of bridge tree. This can be done using euler tour of the tree which is used for subtree queries. Since degree can be upto M, we can store the nodes for each degree and use binary search to get the count of nodes that belong to the subtree. Here, we do not need to worry about formation of multiple edges since it is a bridge.

link

answered 12 Jan, 18:14

hitman623's gravatar image

6★hitman623
111
accept rate: 0%

this would solve the bonus question right?

(12 Jan, 18:55) panik5★

Yeah, I guess so.

(12 Jan, 19:36) hitman6236★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,236
×864
×65
×10
×6
×2

question asked: 11 Jan, 18:35

question was seen: 123 times

last updated: 12 Jan, 19:36