[AGC033F]Adding Edges

Topic Link

The meaning of problems

Given the tree and $ T $ $ G $, their vertices numerals are n-minimum $ $ positive integers. If $ a, b, c $ (in any order) on the same strand tree of $ T $ and $ (a, b), (b, c) \ in E_G $, then the added edge in $ G $ in $ (a, c) $. Seeking the number of edges in the final graph.

answer

If only a sequential arrangement in the chain $ a, b, c $ new edge can be derived, there is $ (a, b) \ in G $ if and only if there exists $ a = v_0, v_1, \ ldots, v_k = b $ such that $ (v_0, v_1), (v_1, v_2), \ ldots, (v_ {k-1}, v_k) \ in E_ {G_0} $.

But the fact is not the case, we consider the repair side of the original title given by a repair. If sequential arrangement $ (a, b, c) $ satisfies $ (a, b), (a, c) \ in G $ on a chain, put $ (a, c) $ $ shortened to (A , b) $, in order to accommodate the above simple rules.

Consider added sequentially edges of $ G $. Add an edge, first of its own first condensing according to the other side very short, and then added to other existing shorter side.

Let's assume that to be added to the rear side reduced to a very short $ (u, v) $.

We consider the maintenance of $ n $ trees rooted tree $ T_1, T_2, \ ldots, T_n $, respectively, $ T $ to $ 1, 2, \ ldots, rooted tree when n $.

Within a $ T_u $ plus side when $ v $ subtrees marked all points $ x $ mark $ v $, represents the future when trying to add edge $ (u, x) $ shortened that step becomes $ (v, x) $, in turn, for $ v, u $ similar operations.

After edging, the need to shorten the other edges that should be connected with a point $ U $ in the $ T_u $ $ $ V subtree, or vice versa. To find these sides, we just need to find the mark.

If the already T_a $ $ a $ b $ $ c $ mark, You Yu playing $ D $ is now tagged, on one strand $ a, d, c, b $ sequential arrangement. Thus side $ (a, c) $ is shortened to $ (d, c) $, which was added in the next step edges. Difficult to find, if after addition of the next $ (a, b) $, shorten $ (d, b) $ will be further reduced to $ (c, b) $, and therefore no need to modify the markers in the subtree tree.

So practice is found deep in the sub-tree, if not marked on the point hit the mark, otherwise a new record and will not search down side. In the different sub-tree search will be a number of new edges, use a list to maintain, takes out a new edge to be added can be added.

Each point or shorten a search for a marker either side of a point, and a point total of only two $ N ^ 2 $, each edge is shortened up to $ N $ times for a total of up to plus side of the NM $ $ article.

Next, in order to know the number of edges in the final graph, we consider a fixed end and the other end $ U $ considered. Only needs to search deep in the $ T_u $, and the endpoint taken $ b = u $. When they find a time sides numerals (i.e. $ t_b $ a $ V $ on labeled $ V $), it means discovered the one side, will make $ b search its subtree = v $ then search deduced edge restore $ b $ when the sub-tree when you exit.

So the space-time algorithm complexity $ O (N (N + M)) $.

Achieve link

Guess you like

Origin www.cnblogs.com/nealchen/p/AGC033F.html