Algorithm: POJ 1523 SPF (Tarjan cut point)

Source: Internet
Author: User
Tags bool cas int size min printf

Meaning

Give a connected undirected graph, find all the cut points of this graph, and output each cut point and the connected edge is removed, will become several connected components

Ideas

Use Tarjan to find the basic problem of cutting point, the principle of Tarjan algorithm to really understand, this problem on the water.

Code

/**===================================================== * This are 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@gma
il.com * Copyright (C) 2013/09/04 20:15 All rights reserved. *======================================================*/#include <iostream> #include <cstdio> #
    
Include <algorithm> #include <vector> #include <cmath> #include <cstring> using namespace std;
typedef long long Int64;
    
const int INF = 0X3F3F3F3F;
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++;
    
    
The 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++;//http://www.bianceng.cn 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) && u) {scanf ("%d", &v); Initadj (); n
= 0;
n = Max (n, Max (U, v));
Addedge (U, v);
    
Addedge (V, u);
    
while (~SCANF ("%d", &u) && 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; }

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.