POJ-1523 SPF undirected graph

Source: Internet
Author: User

A simple undirected graph is used to calculate the cut point.

The Code is as follows:

# Include <cstdlib> # include <cstring> # include <cstdio> # include <algorithm> # define maxn 1005 using namespace STD; int head [maxn], idx, Lim, dfn [maxn], low [maxn], Ti; int subnet [maxn]; bool vis [maxn]; struct edge {int V, next;} e [maxn * maxn]; void addedge (int x, int v) {++ idx; E [idx]. V = V, E [idx]. next = head [X]; head [x] = idx;} void DFS (int u) {// The lower the low value, the better. For (INT I = head [u]; I! =-1; I = E [I]. Next) {If (! Vis [E [I]. v]) {vis [E [I]. v] = true; dfn [E [I]. v] = low [E [I]. v] = ++ Ti; DFS (E [I]. v); low [u] = min (low [u], low [E [I]. v]); If (low [E [I]. v] = dfn [u]) {// the child does not have any other way to reach the upper layer + + subnet [u];} else {LOW [u] = min (low [u], dfn [E [I]. v]) ;}} void solve () {bool flag = false; memset (VIS, false, sizeof (VIS); memset (subnet, 0, sizeof (subnet); subnet [1] =-1; vis [1] = true; // because Figure 1 is China Unicom, so we can find all the points dfn [1] = low [1] = ++ Ti; DFS (1); For (INT I = 1; I <= lim; ++ I) {If (subnet [I]> 0) {printf ("SPF node % d leaves % d subnets \ n", I, subnet [I] + 1); flag = 1 ;}} if (! Flag) {puts ("No SPF nodes") ;}} int main () {int A, B, CA = 0; bool indata = false, first = true; lim = Ti = 0, idx =-1; memset (Head, 0xff, sizeof (head); While (1) {scanf ("% d", & ); lim = max (Lim, a); If (! A) {If (! Indata) break; else {If (first) {First = false;} else {puts ("") ;}indata = false; printf ("Network # % d \ n ", ++ CA); solve (); Lim = Ti = 0, idx =-1; // an initialized memset (Head, 0xff, sizeof (head); continue ;}} indata = true; scanf ("% d", & B); // indicates that A and B have an undirected edge Lim = max (Lim, B ); // find the largest vertex number addedge (a, B); addedge (B, A); // bidirectional edge of an undirected graph} 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.