Pat a problem-1013. Battle over Cities (25)-Number of branches for Unicom

Source: Internet
Author: User

The topic is to find the number of branches
Delete a point, the remaining number of branches of the Unicom is CNT, then need to establish cnt-1 edge to the CNT branch of the number of branches to find out
How to find the number of branches of Unicom?
Can be used and check the set, but the complexity of the search set is O (m*logn*k)
I'm using Dfs,dfs's complexity as long as O ((m+n) *k)
K here means that because there are k points to query, each requires a delete after the branch number of Unicom.
The topic is not given the range of M, so if M is large, the DFS time will be small.

For 1~n points, each time you start Dfs from an unmarked point, mark the points that have been visited in that Dfs.
U is unmarked, indicating that the point was not visited before DFS, which means that the point does not belong to the same branch as the previous point, which is equivalent to the new branch.
So just count the number of DFS in 1~n, the number of unicom branches
The deleted points are initially labeled and will not be accessed.

#include <iostream>#include<cstdio>#include<algorithm>#include<string>#include<vector>#include<cstring>using namespacestd;Const intmaxn= ++5;intn,m,k;intCHECK[MAXN];//mark which points are check first, prevent duplicate points from repeating the workintANS[MAXN];//the number of connected branches remaining after deleting node IintVIS[MAXN];//tags for dfs timestructedge{intto ; intNext;} EDGE[MAXN*MAXN];intHEAD[MAXN];inttot;voidinit () {memset (head,-1,sizeof(head)); Tot=0;}voidAddintUintv) {edge[tot].to=v; Edge[tot].next=Head[u]; Head[u]=tot++;}voidDfsintu) {Vis[u]=1; if(head[u]==-1)        return;  for(intk=head[u];k!=-1; k=Edge[k].next) {        intv=edge[k].to; if(!Vis[v])        {DFS (v); }    }}/*for the point to check, the number of branches to be deleted is stored in the ANS array*/voidsolve () {memset (ans,0,sizeof(ans));  for(intI=1; i<=n;i++){        //If I was to be questioned        if(Check[i]) {memset (Vis,0,sizeof(VIS)); Vis[i]=1;  for(intj=1; j<=n;j++){                //each time there is a node that has not been visited, it is a new branch of Unicom .                if(!vis[j] && j!=i)                    {DFS (j); Ans[i]++; }            }        }    }}intMain () {intu,v;    Init (); scanf (" %d%d%d",&n,&m,&k);  for(intI=0; i<m;i++) {scanf ("%d%d",&u,&v);        Add (U,V);    Add (V,u); } memset (check,0,sizeof(check)); Vector<int>query;  for(intI=0; i<k;i++) {scanf ("%d",&T); Check[u]=1;    Query.push_back (U);    } solve ();  for(intI=0; I<query.size (); i++) {u=Query[i]; printf ("%d\n", ans[u]-1); }    return 0;}
View Code

Pat a problem-1013. Battle over Cities (25)-Number of branches for Unicom

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.