poj_1236 Strong Connected Branch

Source: Internet
Author: User
Tags in degrees

Main Topic

There are n schools with some one-way connections between these schools, and if school A is connected to school B (b does not necessarily connect to a), then school B is given a set of software. Now give the connection between schools, to find out at least a few schools to distribute software, so that all schools can access the software, and, at least, the need to add a few one-way edge of the school, in order to give any of these schools to send software, the rest of the school can be received.

Problem Analysis

In one diagram, any point within a strongly connected branch is "sent Software", then all points within the branch are available, so a strongly connected branch is first obtained, and the strongly connected branch is merged into a point.
After refactoring, if there is only one point, you only need to send it to any school. That is, the result is 1 (software is released to 1 schools at least) 0 (no new edges need to be added to make the entire graph connect).
If there are multiple points in the diagram after the refactoring, take into account the points in which the point is 0 in the point: the degree of 0 points can not be reached by other points, and a point in the degree of non-0 is reachable from a point of 0 points, then only to those points to 0 of the point distribution software, you can make all the points can be obtained software.
After refactoring, there is a point of 0 in the graph, in the figure, a point with a degree of 0 (set to m) cannot be reached from another point, so in order for all points to be connected, the M-entry is required to connect to the M-in-0 point, while the point with a degree of 0 (set to n) cannot reach other points, so that all points are connected, n paths are required to be connected from the N-out 0 points. So, at a minimum, you need to add the max (M, n) Edge so that all points in the graph are not 0 in degrees and out of degrees.
At the same time, in a undirected graph, if all the points of the graph can be connected to a piece, and the degree and the degree of each point is not 0, then the graph must be strongly connected. Thus, the result is Max (m,n)

Implementation (c + +)
#include <stdio.h> #include <string.h> #include <vector> #include <stack> #include <set># Define Max_node 105#define min (A, b) a < b? A:b#define Max (A, b) a > B? A:busing namespace std;vector<vector<int> > ggraph;//storage diagram structure stack<int> gstack;//for Tarjan algorithm bool gvisited[max_node];//determines whether each point has been accessed by bool ginstack[max_node];//in the Tarjan algorithm to determine whether the point is in the stack int gdfn[max_node];// The Tarjan algorithm marks the start time of each point at the beginning of the time that is accessed by the Int Glow[max_node];//tarjan algorithm, which marks the minimum start times for points that pass through the DFS process starting at each point int gclusterofnode[max_ node];//marks the strong connected branch where each point is located int gindex;//Mark Point is first accessed time int gclusterindex;//Strong connected branch number//tarjan algorithm strong connected branch void Tarjan (int u) {glow[ U] = gdfn[u] = ++gindex;gvisited[u] = True;ginstack[u] = True;gstack.push (u); for (int i = 0; i < ggraph[u].size (); i++) {int v = ggraph[u][i];if (!gvisited[v]) {Tarjan (v); Glow[u] = min (Glow[u], glow[v]);} else if (Ginstack[v]) {Glow[u] = min (Glow[u], gdfn[v]);}} if (glow[u] = = Gdfn[u]) {int v;do{v = Gstack.top (); Gstack.pop (); Ginstack[v] = false;gclusterofnode[v] = gclusterindex;//The strongly connected branch number to which the token belongs} while (V! = u); gclusterindex++;}} vector<set<int> > glinkto;//When a strong connected branch is shrunk to a point, the other points to which each point is connected are set so that the insertion is not duplicated. For statistical degrees vector<set<int> > glinkfrom;//after the strong connected branch is shrunk to a point, each point is connected by another point, and set makes the insertion non-repeating. Used for statistical penetration//The strong connected branch is shrunk into points, and then statistics of each strong connected branch of the penetration and the degree of void reconstructgraph (int nodes, int clusters) {glinkto.resize (clusters); Glinkfrom.resize (clusters); for (int u = 1, u <= nodes; u++) {for (int i = 0; i < ggraph[u].size (); i++) {int v = ggrap H[u][i];int UC = Gclusterofnode[u];int VC = gclusterofnode[v];if (UC! = VC) {//NOTE!!! Cannot connect itself to itself! Glinkto[uc].insert (VC); Glinkfrom[vc].insert (UC);}}}  int main () {int n, u, v;scanf ("%d", &n), ggraph.resize (n + 1), for (int i = 1; I <= n; i++) {while (scanf ("%d", &v) && v! = 0) {ggraph[i].push_back (v);}} Gindex = Gclusterindex = 0;memset (gvisited, False, sizeof (gvisited)), memset (Ginstack, False, sizeof (Ginstack)); for (int u = 1; U <= N; u++) {if (!gvisited[u]) {Tarjan (U);}} Reconstructgraph (n, gclusterindex); int zero_outdegree = 0, Zero_indegree =0;for (int i = 0; i < Gclusterindex; i++) {if (Glinkfrom[i].empty ()) {zero_indegree++;} if (Glinkto[i].empty ()) {zero_outdegree++;}} if (Gclusterindex = = 1) {//If there is only one strong connected branch, direct output 1 0printf ("1\n0\n");} else//minimum number of versions to send, equal to the number of points in the graph after the strong connected branch is indented to a point of 0,//Because, a direction-free graph, all the points in the degree of not 0, must be from a certain degree of 0 points can be reached. And the points that are 0 of these degrees cannot be reached, it is necessary to at least all of these into 0 points to the starting point//need to connect how many have to the edge to make the entire graph is called a strong connected graph//into the 0 point set to M, can not be any other points can be reached, therefore, to have M-bar to connect to this m-degree 0 points/ /The point of 0 is set to N, cannot reach other points, so to have n edges from the N out of 0 points connected out//can be proved that in a undirected graph, all points in the degree and the degree of not 0, then the graph is strongly connected//, at least the max (M, N) edge. printf ("%d\n%d\n", Zero_indegree, Max (Zero_outdegree, Zero_indegree)); return 0;}

poj_1236 Strong Connected Branch

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.