UVALive-5135 Mining Your Own Business (dual-connected components of undirected graphs)

Source: Internet
Author: User

Title Analysis: In an undirected graph, some points are painted black, so that when you delete any of the points in the graph, each connected component has at least one black point. At least a few black spots can be applied, and there are several options in case of minimum coating.

Problem Analysis: Obviously, must not be cut point. For each connected component, if there are 1 cut points, it must be applied to any point in the component except the cut point, if there are multiple (2 or more) cut points, then this component does not need coloring. If the whole picture is not cut point, then any choice of two points coloring, the reason to paint two, is to prevent the deletion of the electricity is black spot.

The code is as follows:

# include<iostream># include<cstdio># include<stack># include<map># include<vector>#    include<cstring># include<algorithm>using namespace Std;const int maxn=100005;struct Edge{int u,v,nxt; Edge (int _u=0,int _v=0,int _nxt=0): U (_u), V (_v), NXT (_NXT) {}}; Edge E[maxn];int HEAD[MAXN],PRE[MAXN],ISCUT[MAXN],LOW[MAXN],BCCNO[MAXN],CNT,BCC_CNT,DFS_CNT;MAP&LT;INT,INT&GT;MP    ; stack<edge>s;vector<int>bcc[maxn];void Add (int u,int v) {e[cnt].v=v;    E[cnt].nxt=head[u]; head[u]=cnt++;}    void Read (int m,int &n) {mp.clear ();    int A, B;    cnt=n=0;    memset (head,-1,sizeof (head));        while (m--) {scanf ("%d%d", &a,&b);        if (mp[a]==0) mp[a]=++n;        if (mp[b]==0) mp[b]=++n;        Add (mp[a]-1,mp[b]-1);    Add (mp[b]-1,mp[a]-1);    }}void dfs (int u,int fa) {int child=0;    low[u]=pre[u]=++dfs_cnt;        for (int i=head[u];i!=-1;i=e[i].nxt) {int v=e[i].v; if (!pre[v]) {+ +Child            S.push (Edge (u,v));            DFS (V,U);            Low[u]=min (Low[v],low[u]);                if (Low[v]>=pre[u]) {iscut[u]=1;                Bcc[++bcc_cnt].clear ();                    while (1) {Edge x=s.top ();                    S.pop ();                        if (bccno[x.u]!=bcc_cnt) {bcc[bcc_cnt].push_back (X.U);                    bccno[x.u]=bcc_cnt;                        } if (bccno[x.v]!=bcc_cnt) {bcc[bcc_cnt].push_back (X.V);                    bccno[x.v]=bcc_cnt;                } if (x.u==u&&x.v==v) break;            }}}else if (PRE[V]&LT;PRE[U]&AMP;&AMP;V!=FA) {S.push (Edge (u,v));        Low[u]=min (Low[u],pre[v]); }} if (Fa<0&&child==1) iscut[u]=0;}    void findbcc (int n) {bcc_cnt=dfs_cnt=0;    memset (pre,0,sizeof (pre)); memset (low,0,sIzeof (low));    memset (iscut,0,sizeof (iscut));    memset (bccno,0,sizeof (BCCNO)); for (int i=0;i<n;++i) if (!pre[i]) DFS (i,-1);}    void solve (int k) {Long long ans1=0,ans2=1;        for (int i=1;i<=bcc_cnt;++i) {int ccnt=0;        for (int j=0;j<bcc[i].size (); ++j) if (Iscut[bcc[i][j]]) ++ccnt;    if (ccnt==1) ++ans1,ans2*= (Long Long) (Bcc[i].size ()-ccnt);    } if (bcc_cnt==1) ans1=2,ans2=bcc[1].size () * (Bcc[1].size ()-1)/2; printf ("Case%d:%lld%lld\n", k,ans1,ans2);}    int main () {int n,m,cas=0;        while (scanf ("%d", &m) &&m) {read (m,n);        FINDBCC (n);    Solve (++cas); } return 0;}

  

UVALive-5135 Mining Your Own Business (dual-connected components of undirected graphs)

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.