Poj 1523 SPF (tarjan cut point)

Source: Internet
Author: User

The question is to give a connected undirected graph, find all the cut points of the graph, and output the cut points and the edge to be removed, it will become the basic question of several connected component ideas to use tarjan to find the cut point, we need to understand the principle of the tarjan algorithm. Code

/** =================================================== =================== * This is a solution for ACM/ICPC problem ** @ source: poj-1523 SPF * @ description: Graph connectivity-tarjan cut point * @ author: shuangde * @ blog: blog.csdn.net/shuangde800 * @ email: zengshuangde@gmail.com * Copyright (C) 2013/09/04 All rights reserved. * ===================================================== ===================*/# include <iostream> # include <cstdio> # inc Lude <algorithm> # include <vector> # include <cmath> # include <cstring> using namespace std; typedef long int64; const int INF = 0x3f3f3f; const int MAXN = 1010; int n; bool OK; int cnt [MAXN]; namespace Adj {int size, head [MAXN]; struct Node {int v, next;} E [MAXN * 2]; inline void initAdj () {size = 0; memset (head,-1, sizeof (head);} inline void addEdge (int u, int v) {E [size]. v = v; E [size]. next = Head [u]; head [u] = size ++ ;}} using namespace Adj; namespace Tarjan {bool vis [MAXN]; int dfn [MAXN], low [MAXN], idx; inline void initTarjan () {idx = 1; memset (vis, 0, sizeof (vis); memset (dfn, 0, sizeof (dfn); memset (low, 0, sizeof (low);} void tarjan (int u) {vis [u] = true; dfn [u] = low [u] = idx ++; for (int e = head [u]; e! =-1; e = E [e]. next) {int v = E [e]. v; if (vis [v]) {low [u] = min (low [u], dfn [v]);} else {tarjan (v ); low [u] = min (low [u], low [v]); if (u = 1) {++ cnt [u]; if (cnt [u] = 2) OK = true;} else if (low [v]> = dfn [u]) {OK = true; ++ cnt [u] ;}}}} using namespace Tarjan; int main () {int cas = 1; int u, v; while (~ Scanf ("% d", & u) {scanf ("% d", & v); initAdj (); n = 0; n = max (n, max (u, v); addEdge (u, v); addEdge (v, u); while (~ Scanf ("% d", & u) {scanf ("% d", & v); n = max (n, max (u, v )); addEdge (u, v); addEdge (v, u) ;}if (cas! = 1) puts (""); printf ("Network # % d \ n", cas ++); memset (cnt, 0, sizeof (cnt )); OK = false; initTarjan (); tarjan (1); if (! OK) puts ("No SPF nodes"); else {if (cnt [1]> 1) printf ("SPF node % d leaves % d subnets \ n", 1, cnt [1]); for (int I = 2; I <= n; ++ I) {if (cnt [I]) printf ("SPF node % d leaves % d subnets \ n", I, cnt [I] + 1) ;}} return 0 ;}

 


Related Article

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.