POJ1236 Network of schools "Tarjan" "Strong connectivity Components"

Source: Internet
Author: User
Tags in degrees

Topic Links:

http://poj.org/problem?id=1236


Main topic:

n computers can transfer files from the U,V computer to the V computer via a forward edge (a). If you run the first computer

A file, the file can be transmitted to the V computer via a forward side, giving you a connection to the N computers.

Then the problem is: 1, at least to the N computers in several computers put files, you can make n computers can receive files.

2, at least to this n computer composition of the diagram to add a few sides, so that only to a computer to put files, it can be n computers are

can receive the file.


Ideas:

The files in the diagram are transitive. Strongly connected features are soon found. Corresponds to a strongly connected component in the graph, as long as the

A point to run the file, then this strong connectivity component will be able to receive the file. Change this strongly connected component's indent to a DAG (directed

No-loop diagram). This is the basis for solving the first problem.

In a undirected graph, the edge becomes a file transfer relationship between the strongly connected components. Means this: As long as a strong connected component has into

Side, you can then receive the file from another component through this entry. However, a no-ring diagram implies that there must be no entry

Strong connected components with degrees (0 in degrees), which do not have a file source, so they are used as the location for serving files. So

The first question only needs to calculate the number of strongly connected components after the indentation is 0.

The second problem is to convert a undirected graph into a strongly connected component. The main characteristics of the strongly connected components are: each point

In and out of the degree is not 0, then the number of points to calculate the degree of Access 0 sumin and the number of points of 0 sumout, the problem

The point becomes: the minimum number of edges to be added between points with an entry level of 0 and a point with a 0 out. It is obvious that the answer is

Max (sumin,sumout).


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace    std;const int MAXN = 110;const int MAXM = 10100;struct edgenode{int to; int next;} Edges[maxm];int Head[maxn],vis[maxn],low[maxn];int Dfn[maxn],stack[maxn],indegree[maxn],outdegree[maxn];int Count    [Maxn],m,id;void addedges (int u,int v) {edges[id].to = v;    Edges[id].next = Head[u]; Head[u] = id++;}    int tarbfs (int pos,int lay,int &AMP;SCC) {Vis[pos] = 1;    Low[pos] = Dfn[pos] = lay;    STACK[++M] = pos; for (int i = head[pos]; i =-1; i = edges[i].next) {if (!vis[edges[i].to]) Tarbfs (edges[i].to,++lay,s        CC);    if (vis[edges[i].to] = = 1) low[pos] = min (low[pos],low[edges[i].to]);        } if (dfn[pos] = = Low[pos]) {++SCC;            do {count[scc]++;            LOW[STACK[M]] = SCC;        VIS[STACK[M]] = 2;    }while (stack[m--]! = POS); } return 0;} void Tarjan (int N) {int SCC, temp, lay;    SCC = temp = m = 0;    Lay = 1;    memset (vis,0,sizeof (VIS));    memset (low,0,sizeof (Low));    memset (dfn,0,sizeof (DFN));    for (int i = 1; I <= N; ++i) if (vis[i] = = 0) tarbfs (I,LAY,SCC); for (int i = 1, i <= N; ++i) {for (int j = head[i]; J! =-1; j = edges[j].next) if (low[i]! = low[ed                Ges[j].to]) {outdegree[low[i]]++;            indegree[low[edges[j].to]]++;    }} int sumin = 0, sumout = 0;        for (int i = 1; I <= SCC; ++i) {if (!indegree[i]) sumin++;    if (!outdegree[i]) sumout++;    } if (SCC = = 1) printf ("1\n0\n"); else printf ("%d\n%d\n", Sumin, Max (sumin,sumout));}    int main () {int N, V;        while (~SCANF ("%d", &n)) {memset (head,-1,sizeof (Head));        memset (outdegree,0,sizeof (Outdegree));        memset (indegree,0,sizeof (Indegree));        memset (count,0,sizeof (Count));        id = 0; for (int i = 1; I <= N        ++i) while (scanf ("%d", &v) && v) addedges (i,v);    Tarjan (N); } return 0;}


POJ1236 Network of schools "Tarjan" "Strong connectivity Components"

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.