POJ 1523 SPF (cut point)

Source: Internet
Author: User

Approximate test instructions:  

a connected undirected graph is given to find out which points are cut points, and for each cut point, the number of connected components after the point is removed. Output "No SPF nodes" if there is no cut point.

Ideas:

Seek cutting point with Tarjan can, and then ask to delete the number of connected components after cutting point, every time to find a cut point, and then from the point of cut Dfs, you can also directly in the Tarjan algorithm to record the number of child. So if the cutting point is the root, then the answer is child, not the root of the cut point answer is child+1

188k0msc++2121b#include<cstdio> #include <iostream> #include <cstring> #include <algorithm >using namespace Std;const int N = 1010;int head[n];struct edge{int v,next;}    es[n*n];struct node{int u; int cnt;} Ans[n];int n;int cnt;int dfn[n],low[n];int index,top=0;bool vis[n];bool book[n];void tarjan (int u,int rt,int PA) {Dfn[u    ]=low[u]=++index;    int child=0;        for (int i=head[u];~i;i=es[i].next) {int v=es[i].v;            if (dfn[v]==0) {Tarjan (v,rt,u);            Low[u]=min (Dfn[u],low[v]);        if (Low[v]>=dfn[u]) child++;    } if (V!=PA) low[u]=min (Dfn[u],low[v]);    } if (u==rt&&child>1) Ans[++top].u=u,ans[top].cnt=child; else if (u!=rt&&child) ans[++top].u=u,ans[top].cnt=child+1;}    void Ini () {memset (head,-1,sizeof (head));    cnt=0;    N=-1;    memset (dfn,0,sizeof (DFN));    memset (book,0,sizeof (book)); top=index=0;} BOOL CMP (const node &a,const node&b) {return a.u<b.u;} Int Main () {int u,v;    int cas=0;        while (scanf ("%d%d", &u,&v) = = 2) {printf ("Network #%d\n", ++cas);        INI ();        N=max (N,u);        N=max (V,n);        Es[++cnt].v=v;        Es[cnt].next=head[u];        head[u]=cnt;        Es[++cnt].v=u;        ES[CNT].NEXT=HEAD[V];        head[v]=cnt;            while (1) {scanf ("%d", &u);            if (u==0) break;            scanf ("%d", &v);            Es[++cnt].v=v;            Es[cnt].next=head[u];            head[u]=cnt;            Es[++cnt].v=u;            ES[CNT].NEXT=HEAD[V];            head[v]=cnt;        N=max (Max (u,v), n);        } for (int i=1;i<=n;i++) if (dfn[i]==0) Tarjan (i,i,-1);        Sort (ans+1,ans+1+top,cmp);        if (top==0) printf ("No SPF nodes\n");        for (int i=1;i<=top;i++) {printf ("SPF node%d leaves%d subnets\n", ans[i].u,ans[i].cnt);    } puts (""); } return 0;}



POJ 1523 SPF (cut point)

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.