CodeForces 160D-Edges in MST kruskal + tarjan

Source: Internet
Author: User

Question:

Returns an undirected graph .. judge every edge in this graph... any is the edge of all the smallest spanning trees of the graph... at least one .. this edge must be at least the edge of a minimum spanning tree .. none .. this edge does not exist in any of the minimum spanning trees of the graph ..

Question:

Think about how to determine if an edge is the edge of a minimum spanning tree... review the kruskal algorithm .. sort all edges in ascending order according to their length .. starting from the short side... simultaneous contraction of links... use and query set maintenance... the principle of this question is similar... only when edge nodes are added, the same length and the previous two vertices are not in a set at the same time. add together .. connect together .. scale together .... you can think .. if there are several currently available minimum edges with the same length, they can be added to the existing graph... because these edges are currently the smallest .. according to the traditional kruskal .. these edges can be selected into the minimum spanning tree... so these sides should at least be
Least one... and point contraction... and there is a longer side behind... the points at both ends are already in a set... it must not be the edge of the smallest spanning tree...

The second question .. how to determine if an edge is owned by all the smallest spanning trees... here we use the tarjan algorithm to find the bridge of undirected graphs... when performing kruskal .. after each wheel is connected with an edge of the same length (before the contraction ).. run tarjan once for the existing undirected graph .. find the bridge .. is any .... tarjan's idea of finding a bridge without a directed graph is also very simple .. start dfs from any point .. form a search tree. dfn indicates the sequential number of vertices to be traversed .. low indicates the position at which the subnode can return at the top of the subnode... if you cannot return to this point or above .. the corresponding edge must be a bridge... that is, for an edge (s, e ).. if low (e) <dfn (s )... then this edge is a bridge .. it is important to note that this question has a duplicate .. an error will occur if he cannot return to his father .. so I want to go back without following the original path...

Finally, return to the nature of the problem... if an edge is at least one .. it indicates that when performing kruskal .. at least one side with the same length is connected to the same set with the two ends... if an edge is any .. it indicates that when performing kruskal .. an edge with the same length only has one or both ends connected to the two sets... the first example illustrates this meaning well...

The basic idea of this question goes a lot around .. to put it bluntly .. it's no different from kruskal .. as you can imagine .. if the length of each edge is different .. at least one is the bare kruskal... if multiple identical edges exist .. the reason is that two edges with the same length are connected to the same set .. if kruskal is used directly, only one of the edges is found... but according to the question, both sides are at least one .. after processing a group of edges with the same length .. what is left behind is a collection of points... the current point set status is the same as that of the bare kruskal... the following side is processed on this basis ..

Program:

# Include <iostream> # include <stdio. h> # include <string. h >#include <cmath> # include <queue> # include <stack> # include <set> # include <algorithm> # define ll long # define oo 1000000007 # define pi acos (-1.0) # define MAXN 100005 using namespace std; struct node {int x, y, l, id, next; bool operator <(node a) const {return l <. l ;}} line [2 * MAXN], Dline [MAXN]; int n, m, _ next [MAXN], father [MAXN], ans [MAXN], dfn [MAXN], low [MAXN], _ DfsIndex; void addline (int x, int y, int l, int id, int m) {line [m]. next = _ next [x], _ next [x] = m; line [m]. x = x, line [m]. y = y, line [m]. l = l, line [m]. id = id;} int getfather (int x) {if (father [x] = x) return x; return father [x] = getfather (father [x]);} void tarjan (int x, int f) {int k, y; dfn [x] = low [x] = ++ _ DfsIndex; for (k = _ next [x]; k; k = line [k]. next) if (line [k]. id! = F) // the path marked by the id .. do not go back to the original path {y = line [k]. y; if (! Dfn [y]) {tarjan (y, line [k]. id); low [x] = min (low [x], low [y]); if (low [y]> dfn [x]) ans [line [k]. id] = 2; // It is a bridge. yes any} else low [x] = min (low [x], dfn [y]);} return;} void Kruskal () {int I, x, s, e, lineNum = 0; sort (Dline + 1, Dline + 1 + m); // edge sort by length memset (dfn, 0, sizeof (dfn); memset (ans, 0, sizeof (ans); memset (_ next, 0, sizeof (_ next); for (I = 1; I <= n; I ++) father [I] = I; _ DfsIndex = 0; x = 1; while (x <= m) {s = x; int fx, fy; for (e = s; e <= M; e ++) if (Dline [e]. l! = Dline [s]. l) break; // locate a group of identical .. s is the header .. e is the end of e --; for (I = s; I <= e; I ++) {fx = getfather (Dline [I]. x), fy = getfather (Dline [I]. y); if (fx = fy) continue; ans [Dline [I]. id] = 1; // it must be in a minimum spanning tree that contains addline (fx, fy, Dline [I]. l, Dline [I]. id, ++ LineNum); addline (fy, fx, Dline [I]. l, Dline [I]. id, ++ LineNum); // Edge Addition} for (I = s; I <= e; I ++) {fx = getfather (Dline [I]. x), fy = getfather (Dline [I]. y); if (fx = fy) continue; tarjan (fx, 0); father [fx] = fy; // Contraction point} LineNum = 0; for (I = s; I <= e; I ++) {fx = getfather (Dline [I]. x), fy = getfather (Dline [I]. y); dfn [fx] = dfn [fy] = _ next [fx] = _ next [fy] = 0; // deedge} x = e + 1 ;}} int main () {int I; while (~ Scanf ("% d", & n, & m) {for (I = 1; I <= m; I ++) {int x, y, l; scanf ("% d", & x, & y, & l); Dline [I]. x = x, Dline [I]. y = y, l, Dline [I]. l = l, Dline [I]. id = I;} Kruskal (); for (I = 1; I <= m; I ++) if (! Ans [I]) printf ("none \ n"); else if (ans [I] = 1) printf ("at least one \ n "); else printf ("any \ n");} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.